fILEs, Directories

  1. Files 1 : How do I read from and write to a file? Data is being read/written by a program (e.g. a C program), not a person. List relevant system calls and briefly explain the use protocol.
  2. Files 2 : How do I read and write a large quantity of data in a C program in a way that does not stall my user interface? Data is being read by a program (e.g. a C program), not a person. List relevant system calls and briefly explain the use protocol.
  3. Files 3 : How do I create a temporary file? Temporary file is to created by a program (e.g. a C program), not a person. List relevant system calls as well as directories (where does an editor or a mail program keep its temporary files?) and briefly explain the use protocol.
  4. Files 4 : Why is a hard link indistinguishable from the original file itself? What happens if you rm a hard link? Why is it not possible to have a hard link to a file in a different file system?
  5. Files 5 : Using vi can you read a file encrypted with crypt? What about decoding a file encrypted with vi -x?
  6. Files 6 Peter wants to join the contents of two files "file1" and "file2" together, so he typed
    cat file1 file2 > file1
    What would happen as a result? How could you join these files together?
  7. Files 7 Peter then wanted to print out a numbered listing of file "file1". He typed
    cat -n file1 > lpr
    but no printout appeared. Why? What happened here?
  8. Files 8 How would you remove all files whose names start with #?
  9. Files 9 How would you change all instances of multiple consecutive spaces to single spaces using a Unix editor, e.g. vi or sed ? Try to use as few commands as possible. Substitution may be done in a single line for simplicity.

    Hint: a sample replacement command for vi/sed to replace all occurrence of substring "usa" by "United States" is
    s/usa/USA/g

    This exercise is about regular expressions, which are supported by many Unix commands, e.g. shell, perl, grep, awk, vi, sed, .... The regular expression allow one to specify patterns with special operators for repitition, choice, etc. It allows one to explore some useful features which not covered in the textbook.

  10. Files 10 Indicate the trueness of the following statements by marking them "True" or "False". Briefly justify your answers.
    1. Hard links to a file can go across two file systems (i.e. two file servers with no shared i-nodelist or disk partition).
    2. Typing name of an executable on shell command line will select the named file in the current directory before looking elsewhere.
    3. Deleting a link will delete the datafiles pointed to.
  11. Files 11 How many inodes and how many directory entries are created by each command in the following command sequence. Assume that myfile.txt exists before his command sequence.
    ln hardlink1 myfile.txt
    ln hardlink2 hardlink1
    ln -s symbolicLink1 myfile.txt
    ln -s symbolicLink2 symbolicLink1
  12. Files 12 (a) Determine the size of disk space that can be addressed by a 32-bit operating systems. Assume that each byte on disk is addressable and the address-pointer is 32-bit long. Provide brief explanation of your calculations and identify your assumptions. Use values for other parameters (e.g. inode size, file information size within inode, etc.) from Exercise 3.3 (pp. 87) in the textbook. Should the values of these parameters change when we go to 64 bit operating system?
  13. Files 12 (b) Repeat the calculation for question for a 64-bit operating system, where address-pointers are 64-bit long.
  14. Files 12 (c) Consider the data-block addressing scheme used by Unix with at most 3-level indirection. Does it need modification for 64-bit operating systems? Briefly justify your answer by computing the largest file size possible with 1 Kbyte block-size and 64-bit disk-block-pointers.
  15. Files + Process 1 Review the program segment given below for this question. Consider an invocation of this program with one command line argument valued 10 . Hints: argv[0] contains the name of the program.
    1. Draw a process diagram and per process "file descriptor tables" for each process for the above invocation. Clearly show the parent-child relationships and shared file-descriptors. Diagram should use a notation similar to that in Figures 3.17-20 in the textbook, where processes are circles, pipes are rectangles, arrows show I/O channels.
    2. Determine the output from the invocation. Clearly identify the contributing process for each output line.
    3. Redraw the per process "file descriptor tables" a modified program where the "close(fd[0])" and "close(fd[1])" systems calls are not used in the program. Does this modification affect the output? Identify your assumptions.
    4. What will happen if the dup2() calls flipped its parameters i.e. dup2(STDOUT_FILENO, fd[1]) was used in place of dup2(fd[1], STDOUT_FILENO) and dup2(STDIN_FILENO, fd[0]) was used in place of dup2(fd[0], STDIN_FILENO).
    /* Invoke with a positive integer command line argument */
    /* HTML web browser may not print include file properly. */
    /* Include stdio.h, unistd.h, stdlib.h enclosing each in angular brackets */
    #include 
    #include 
    #include 
    #define NL "\n"
    void foo(int i, int p) { if ( div(i,p).rem > 0) printf("%d %s", i, NL); }
    
     int main(int argc, char *argv[]) {
     int  p, max, n, cpid, status, fd[2];
    
     if (argc >= 2) { max = atoi(argv[1]); }
     else { 
    	printf("Usage: %s positiveInteger %s", argv[0], NL); 
    	exit(0); 
     }
    
     if (argv[2] == NULL) { p = 2; } 
     else { scanf("%d", &p) ; }
    
     if (p <= max) {
       printf("%d %s", p, NL);
       pipe(fd);
       cpid = fork();
    
       if (cpid) { dup2(fd[1], STDOUT_FILENO); close(fd[1]); }
       else{
            dup2(fd[0], STDIN_FILENO); 
    	close(fd[0]);
            execl(argv[0], argv[0], argv[1], "child", NULL);
       }
       if (p == 2) { n = 2; while(n < max)  foo(n++,p) ;  }
       else { 
    	do { 	scanf("%d", &n); 
    		foo(n,p); 
    	} while (n <= max ); 
       }
    
       printf("%d %s", max+1, NL); /* termination flag */
       while ( wait(&status) != cpid );
     }
     exit(0);
    }
    
    
  16. Files + Process 2 Review the program segment given below for this question. Note that NL string represents newline and flushes buffers for screen (stdout). Note the presence of fork() call in each case. Assume "printf" to be atomic, i.e. synchronization issues are not relevant.
    /* HTML web browser may not print include file properly. */
    /* Include stdio.h enclosed in angular brackets */
    #include 
    void main (int argc, char* argv[])
    {
     int flag; char NL[] = "\n" ;
     if (argc != 2) { printf ("Usage %s case(0/1/2)", argv[0]); exit(0); }
     else { flag = atoi(argv[1]); }
     switch (flag) {
      case 0: { printf("Hello %s", NL) ; fork(); printf(" Bye "); break;}
      case 1: { printf("Hello"); fork(); printf("Bye  %s", NL); break;}
      case 2: { printf("Hello %s", NL); fork (); printf(" Bye %s", NL); break;}
     }
    }
    

    A. What is the output of this program if 1st argument on command-line is 0? Briefly explain.
    B. What is the output of this program if 1st argument on command-line is 1? Briefly explain.
    C. What is the output of this program if 1st argument on command-line is 2? Briefly explain.