Sistemas Operativos 3MIEEC

Problem Set #2: Files in Linux/Unix


Questions

  1. An operating system (OS) can be viewed both as a virtual machine and a resource manager. Explain each of these facets of an OS.
  2. One of the abstractions supported by an OS is the process. What is a process? Describe two OS services related to process management.
  3. About system calls.
    1. What is a system call?
    2. Why the implementation of a system call requires the execution of an instruction that is not used in ordinary functions?
    3. However, from the point of view of the programmer, a system call appears to be a function. Why?

Problems

IMP.: Your programs must use the file system calls, e.g. open(), read(), close(), not the C library functions for streams, e.g. fopen(), fread(), fclose()

Read either the OSTEP chapter on files and directories or the notes on the UNIX file-system API by Prof. Pimenta Monteiro (in Portuguese). You should also read, or at least browse, the man pages for the different system calls.

  1. Consider the copy of a file
    1. Write a program that copies to the standard output the contents of a file whose name is passed as its single command line argument, in a way similar to the cat utility when invoked with a single argument.
      Before exiting, your program should print the length of the file, i.e. the number of bytes it read from the file.

      Hint: You can use the macro STDOUT_FILENO, defined in unistd.h, as the stdandard output file descriptor.

      IMP.: To ensure that your program will have to invoke read() several times, use a buffer with only 64 bytes. You may test your program using the file with its source code.

    2. Write a second version of this program that rather than copying the file content to the standard output copies it to a new file, whose name is given in the second command line argument.

      Hint: Read the open() man page carefully, specially the different flags.

      IMP.: Your program should report an error, if the second argument is the name of an existing file.

  2. The split utility splits a file into multiple files (for more details read its man page: man split)

    Write the merge program which restores a file from all the pieces generated by split. Assume that all these pieces are in the directory where you invoke merge.

    merge shall be invoked as follows:

    merge <prefix>
    where:
    <prefix> is the prefix used as argument of split

    The name of the file created by merge shall be <prefix>.mrg

    Hint: Use the string handling functions of the C library (man 3 string).

  3. Consider the following declarations:

    typedef char name[30]; 
    typedef struct { 
    	name st;
    	int grades[3];
    } test_t;
    
    1. Write a program that creates a binary file to store structs of type test_t, i.e. the grades of a test with 3 questions. The members of each struct shall be read from the standard input. Your program should take as argument the name of the output file, which may already exist. In that case, you should append the information to that file.
    2. Write a program that reads a file created by the program you have developed in the previous question, and that prints in the standard output the members of each struct, one struct per line.
  4. Write the sample program that samples a file, whose name is passed as an argument. I.e. sample shall be invoked as follows:

    sampe <filename> <offset>
    where:
    <filename> is the name of the file to sample
    <offset> is the offset of the first sampled byte with respect to the beginning of the file

    The size of the sample should be one tenth of the length of the sampled file, unless the specified offset is such that it must be shorter. The sample shall be written to a file with name <filename>.smp.

    Hint: To find out the size of the sampled file you can use one of the stat(),fstat(),lstat() system calls. Check out their man page.

    Hint: To advance the current position of the file you can use the lseek system call. Check out its man page.

  5. Consider problem 1 above. Write a new version, which shall behave either as in 1.a or as in 1.b, depending on the number of arguments of the command line.

    IMP. Your solution must use the dup2() syscall. Check out its man page.