Week 10 Tutorial Questions

Objectives

Code Review

This week the code review is for create_borts_file.c. The reviewees should give a brief description of their code, and the class should ask questions, comment on the quality of the code, and suggest improvements. Each review should take about 10 minutes.

Code reviews take place in the the second hour of tutorials for the week. Please let your tutor know if you want to volunteer to do one

Tutorial Questions

  1. Write a C program, print_diary.c, which prints the contents of the file $HOME/.diary to stdout

    The lecture example get_env.c shows how to get the value of an environment variable.

    snprintf is a convenient function for constructing the pathname of the diary file.

  2. The Unix/Linux shell is a text-oriented program that runs other programs. It behaves more-or-less as follows:

    print a prompt
    while (read another command line) {
       break the command line into an array of words (args[])
       // args[0] is the name of the command, a[1],... are the command-line args
       if (args[0] starts with '.' or '/')
          check whether args[0] is executable
       else
          search the command PATH for an executable file called args[0]
       if (no executable called args[0])
          print "Command not found"
       else
          execute the command
       print a prompt
    }
    
    1. How can you find what directories are in the PATH?

    2. Describe the "search the command PATH" process in more detail, including the kinds of system calls that would be needed to determine whether there was an executable file in one of the path directories?

  3. What does the following system call do:

    •   pid_t fork(void);

    Indicate what the possible return values are, and describe the conditions under which they might occur.

  4. What are the possible outputs from the execution of this code?

    int main(void)
    {
       printf("Hello\n");
       if (fork() != 0)
          printf("Gan bei\n");
       else
          printf("Prost\n");
       printf("Goodbye\n");
    }
    

    Assume that all of the relevant #includes have been done.

  5. What are the possible outputs from:

    int main(void)
    {
        int x = 1;
        pid_t pid = fork(); 
        if (pid < 0) { 
            fprintf(stderr, "Fork failed\n"); 
            exit(1); 
        } else if (pid > 0) { 
            x++;  
            printf("x = %d\n", x); 
        } else { 
            x--;  
            printf("x = %d\n", x); 
        }
    }
    

    You can asssume that all of the appropriate #include's have been done.

  6. What are the possible outputs from:

    int main(void)
    {
       if (fork() != 0) {    
         wait(NULL);
         printf("Parent done\n");
       }
       else if (fork() != 0) {      
          wait(NULL);
          printf("Child done\n");
       }
       else {
          printf("Grand-child done\n");
       }
       return 0;
    }
    
  7. The function execve() aims to replace the current process with a process executing the specified program. Ideally, the execve() function never returns. However, it has a return type of int.

    1. Under what circumstances would does execve() return a value?

    2. What should the calling program do if execve() does return?

    3. Write two small programs to work out whether execve() causes a new process ID to be created, or whether it inherits the caller's process ID.

  8. Write a C program, using posix_spawn called now.c, which prints the following information:

    1. The current date.
    2. The current time.
    3. The current user.
    4. The current hostname.
    5. The current working directory.
    dcc now.c -o now
    ./now
    29-02-2022
    03:59:60
    cs1521
    zappa.orchestra.cse.unsw.EDU.AU
    /home/cs1521/lab08
    
  9. Consider the following edited output from the ps command running on one of the CSE servers:

      PID    VSZ   RSS TTY      STAT START   TIME COMMAND
        1   3316  1848 ?        Ss   Jul08   1:36 init
      321   6580  3256 pts/52   Ss+  Aug26   0:00 -bash
      334  41668 11384 pts/44   Sl+  Aug02   0:00 vim timing_result.txt
      835   6584  3252 pts/124  Ss+  Aug27   0:00 -bash
      857  41120 10740 pts/7    Sl+  Aug22   0:00 vi echon.pl
      924   6524  3188 pts/184  Ss   15:52   0:00 -bash
      938   3664    96 pts/184  S    15:52   0:00 /usr/local/bin/checkmail
     1199   6400  3004 pts/142  Ss   Oct05   0:00 -bash
     1381  41504 11436 pts/142  Sl+  Oct05   0:00 vim PageTable.h
     2558   3664    96 pts/120  S    13:47   0:00 /usr/local/bin/checkmail
     2912  41512 11260 pts/46   Sl+  Aug02   0:00 vim IntList.c
     3483  14880  5168 pts/149  S+   Sep20   0:00 gnuplot Window.plot
     3693  41208 11240 pts/120  Tl   13:50   0:00 vim trace4
     3742   6580  3320 pts/116  Ss+  Sep07   0:00 -bash
     5531   6092  2068 pts/158  R+   16:04   0:00 ps au
     5532   4624   684 pts/158  S+   16:04   0:00 cut -c10-15,26-
     5538   3664    92 pts/137  S    15:05   0:00 /usr/local/bin/checkmail
     6620   5696  3028 pts/89   S+   Aug13   0:00 nano PingClient.java
     7132  41516 11196 pts/132  Sl+  Sep08   0:00 vim board1.s
    12256 335316 10436 ?        Sl   Aug14  15:01 java PingServer 3331
    12272   4260  2816 ?        Ss   Aug02  10:34 tmux 
    12323  10276  4564 ?        S    Sep09   0:02 /usr/lib/i386-linux-gnu/gconf/gconfd-2
    12461   4260  2808 ?        Ss   Sep02   5:42 tmux
    13051  43448 13320 pts/110  Sl+  Sep05   0:02 vim frequency.pl
    13200  47772 21928 ?        Ssl  15:19   0:02 gvim browser.cgi
    13203  41756 11560 pts/26   Sl+  Aug12   0:02 vim DLList.h
    13936  11872  6856 ?        S    Sep19   0:06 /usr/lib/gvfs/gvfs-gdu-volume-monitor
    30383   7624  3828 pts/77   S+   Aug23 336:28 top
    
    1. Where might you look to find out the answers to the following questions?

    2. What does each of the columns represent?

    3. What do the first characters in the STAT column mean?

    4. Which process has consumed the most CPU time?

    5. When was this machine last re-booted?

Assignment 2

Please discuss the assignment in the second hour of tutorials for the week

What needs to be done for subset 0. Discuss/demonstrate the following

What needs to be done for subset 1. Discuss/demonstrate the following