Comp111 - Quiz 1 Answers

Oct 7, 2010 - closed book



Please fill in the blanks. Each blank is worth 5 points. Blanks with the same number have the same answer.

  1. The difference between a file pointer and a file descriptor is that a file pointer has a ____buffer____ that can be emptied via the fflush subroutine. This is not needed for the file pointer ____stderr/standard error____, which is intended to send error output to the terminal and thus flushes automatically.
  2. In the code:
    if (!(pid1=fork())) { 
        FILE *read = fdopen(fd[0],"r"); close(fd[1]); 
        fgets(buf,1024,read); fclose(read); 
        printf("got '%s'\n",buf); 
    } else { 
        FILE *write = fdopen(fd[1],"w"); close(fd[0]); 
        fprintf(write, "hi there!\n"); 
        fflush(write); fclose(write); 
        pid2=wait3(&status, 0, &usage); 
        printf("exit code for %d is %d\n", pid2, status); 
    The top clause is executed by the ____child____ while the bottom clause is executed by the ____parent____ process. For this to work, one must have called ____pipe____(fd) first. At the end of both clauses, pid1 and pid2 will be ____equal/the same____.
  3. Consider the code:
    void handler(int foo) { 
        fprintf(stderr, "got %d\n", foo); 
    signal(SIGINT, handler); 
    registers a signal handler so the process will call ____handler/handler(SIGINT)____ when the user types the character ____control-C____.
  4. One of the great controversies of Operating Systems concerns how to ____schedule/manage_____ runnable processes. ____Round-robin scheduling____ is suitable for interactive processing, while ____batch/job scheduling____ maximizes throughput.
  5. A process contains four main memory "segments". The ____text____ segment contains a read-only version of your program. The ____stack____ segment contains variables created by subroutine invocation.
  • A process changes from runnable to ____blocked____ when it is waiting for some outside event. The ____sleep/nanosleep____ system call causes it to wait for a specified number of seconds before continuing.
  • The difference between a system call and a library function is that the system call communicates with the ____kernel/operating system____, while a library function calls system calls but does not call the ____kernel/operating system____ directly.
  • The difference between a program and a process is that a ____program____ is a file describing what to do, while a ____process____ is the result of loading that file into memory.
  • CPU usage statistics are measured based upon which processes are ____running____ at specific times. A process that is waiting for something does not consume ____CPU/user/system____ time.
  • Extra credit. The reason that I usually write
    if ((pid==fork()) { 
    rather than
    pid = fork(); 
    if (pid!=0) { 
    is that the former is a bit ____faster____.