Starting from:

$25

ECE357-Problem Set 1 Solved

Problem 1 -- What is kernel mode / what is user mode?

Explain for each of the following items whether they can be accomplished entirely within user mode, or require a system call? If the latter, identify what system call or calls would be used.

a)  getting input from the keyboard

b)  allocating memory via malloc (trick question)

c)  calling a function

d)  getting the current time of day

e)  computing the value of pi

Problem 2 -- What happens if...

For each of the following, tell me "what happens if". Explain your answer as appropriate.

a)  n=write(-1,"XYZ",3); /* what does n get set to? */

b)  fd=open("/ooopsy",O_WRONLY|O_CREAT|O_TRUNC,0666);

c)  char buf[3]; for(;;) printf("%d",read(fd,buf,3)); /* fd is open for reading, the associated file is 9 bytes long */

d)  fd=open("file",O_RDONLY); n=write(fd,"ABC",3); /* what does n get set to, and what does errno get set to? */Problem 3 -- Use of system calls in a simple concatentation program

The objective of this assignment is to write a simple C program which is invoked from the command line in a UNIX environment, to utilize the UNIX system calls for file I/O, and to properly handle and report error conditions.

The program is described below as a "man page", similar to that which describes standard UNIX system commands. The square brackets [ ] are not to be typed literally, but indicate optional arguments to the command.

kitty - concatenate and copy files USAGE:

kitty [-o outfile] infile1 [...infile2....] kitty [-o outfile]

DESCRIPTION:

This program opens each of the named input files in order, and concatenates the entire contents of each file, in order, to the output. If an outfile is specified, kitty opens that file (once) for writing, creating it if it did not already exist, and overwriting the contents if it did. If no outfile is specified, the output is written to standard output, which is assumed to already be open.

During the concatenation, kitty will use a read/write buffer size of 4096 bytes.

Any of the infiles can be the special name - (a single hyphen). kitty will then concatenate standard input to the output, reading until end-of-file, but will not attempt to re-open or to close standard input. The hyphen can be specified multiple times in the argument list, each of which will cause standard input to be read again at that point. If no infiles are specified, kitty reads from standard input until eof.

At the end of concatenating any file (including standard input), kitty will print a message to standard error giving the number of bytes transferred (for that file), and the number of read and write system calls made. If the file was observed to be a "binary" file, an additional warning message will be printed. The name of the file will also appear in this message. In the case of standard input, the name will appear as

<standard input

EXIT STATUS:

program returns 0 if no errors (opening, reading, writing or closing) were encountered. Otherwise, it terminates immediately upon the error, giving a proper error report, and returns -1.

EXAMPLES:

kitty file1 - file 2

(read from file1 until EOF, then standard input until EOF, then file 2, output to standard output) kitty -o output - - file3

(read from standard input until EOF, then read again from standard input until EOF, then read file3 until EOF, all output to file "output")

More products