Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello Guys I need urgent help. My macbook got destroyed and I lost all my file b

ID: 3884987 • Letter: H

Question

Hello Guys I need urgent help. My macbook got destroyed and I lost all my file because I tried to insall this Ubuntu OS for this assignment. I felt sooo dismayed right now. could any of you help and guide me with this program? I have one day before due date. Thanks all

-----------------------------------------------------------------------------------

This assignment will illustrate the advantages of using different system level calls that interface with the OS and can be used directly from your code.

The task for this assignment is to construct a number of simple programs on a Ubuntu 16.04 Linuxsytem that use system calls. We will for the most part use system calls that focus on file I/O and process management. Be sure and make yourself as familiar with the man command as possible:

% man 2 intro – read about system (kernel) call errors.

You want the 2 here, otherwise you get man 1 introduction

% man 2 <some system call> for example man 2 signal

% man 3 for example man 3 printf

% whatis read – index of man pages (not always working or available)

% gdb debugger for your code

% cc or gcc – the C compiler. Make sure you know what or not the compiler is ANSI-C compatible

% make – make your program with a makefile utility

1. myhead.c – Write a program that simply reads the first N lines(default 10 lines) of an ASCII file and displays it on the terminal (stdout). Note: there is already a program called ‘head’. The pgram should optimally take one argument, which changes the default number of lines. The syntax should be:

% myhead <-n> filename

for examples

% ./myhead myhead.c

% ./myhead -5 myhead.c

“-5” means only display the first 5 lines. “-” is called a “switch”, and is an argument to the program.

In general, do NOT use any section 3 I/O library calls (no “stdoi” calls), except for error handling. Please do use perror(3). System calls used should be confined to open (2), close(2), read(2) and write(2) or any other section 2 calls you might want. In order to write to stdout, you can use convention: write(1, buf, cc).

Check all system calls for errors, print an error message with perror(3) if a system call fails andn then call exit(1).

Note: as a test, we will expect that “% ./myhead -5 /dev/tty” will work as expected; i.e.: the program should be able to read input from stdin.

2. read1.c and fread1.c: This part of the assignment, you will write two similar programs that will both read a large file and measure how long it takes them to do so. Choose a large file for testing: For example select some large binary (executable) for a large application such as your LibreOffice (soffice) or emacs (emacs) if you can find it, or use linux kernel itself (/boot or /usr/src), or “/usr/share/dict/words” which is a dictionary of all words that come with your system. For the sake of testing purposes, try a small program first like “head”.

“read1.c” should use the read system call, open(2) and read(2), and should read 1 byte at a time. “fread1.c” should use fopen(3) and fread(3) to do the same thing – both part of the stdio pacakge – and should fread 1 byte at a time. Both programs should not write anything, simply read and toss the data.

Use /usr/bin/time library to measure the runs of your application. Use “% man time” to learn more about the command, try “/usr/bin/time -v sleep 2”

% /usr/bin/time -v ./read1 someLargeFileHere

% /usr/bin/time -v ./fread1 someLargeFileHere

Enclose in the comments for this program, a sample output run and a calculation as to how long it takes to read 1 byte and fread 1 byte. Explain whythere is a difference. Make sure you explain what is happening as that is worth the points here.

3. popen.c – write a program that will use a Linux pipe (2). The program should work as follows:

% popen arg1 arg2 arg3 … argN

for example

% popen ls -1

% popen ls

Popen should “execute” the 1st arguemnt as a program on the system and make sure all remaining arguments are passed to the executed program. All output from the child should be put into a pipe. The “parent” program (popen) should read from the pipe and write the output to stdout. The results should appear as if “ls -l” was executed in the normal fashion. What you are doing here is simulating the normal “redirection” function of a Linux shell. For example: “% ls > out.txt

This might not be intuitive at beginning, but once you get it, it should be a trivial. Check out the following code for example:

pipe(2) – open pipe

. create a child process with fork (2)

//check if child process

if (fork () == 0){

pack arguments arg1 (name of program) and other arguments up in “vector”

redirect pipe to stdout

close(1); //close stdout

dup (pipefd_1); // hook write side of pipe up

execute program with arguments

rc = execvp (arg1, vector);

if (rc<0)

error msg

exit(1)

while (1)

read(2) from pipefd_0 until eof (read side of pipe)

if eof

break

write any output to stdout(1)

//parent should wait on child

wait();

System calls used: pipe(2), exec(2), fork(2), close(2), exit(2), wait(2), dup(2)

4. write a very simple database handling program that uses lseek(2). lseek(2) allows random access to a file. You will actually need two programs, createdb and accessdb. Both share a fixed-length record that you will read/write from the database file. Assume the record can be done in C like this:

In order to write the record to an open file, you will need to do is:

errcode = write (fd, &r, sizeof(struct rec));

createdb should create a new file, and write 100 records to the file then close it. The contents of id should be 0..99. The contents of name are up to you but should not be the same as the id.

accessdb should access the file and randomly read 10 records using lseek. Use the rand(3) library call to determine the index for example: next_index = rand() % 100;

print out the id value and the name value.

System calls used: lseek(2), open(2), close(2), read(2), write(2). Use printf/perror as appropriate.

Explanation / Answer

N=10 f=open("file") for i in range(N): line=f.next().strip() print line f.close() from itertools import islice with open("datafile") as myfile: head = list(islice(myfile, N)) print head

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote