Skip to main content
Logo image

Dive Into Systems: Exercises

Section 13.2 Processes

Checkpoint 13.2.1. How Many Processes?

    The user takes the following actions: runs a web browser program, runs the Unix shell program in one terminal, runs a spreadsheet program, runs another Unix shell program in a different terminal.
    What is the minimum number of processes created by the OS based on the user actions listed above?
  • 1
  • Incorrect.
  • 2
  • Incorrect.
  • 3
  • Incorrect.
  • 4
  • Correct!
  • 5
  • Incorrect.

Checkpoint 13.2.2. The Process Abstraction.

    Which of the following statements are true? Select all.
  • Programmers do not need to write special code to run their programs on operating systems that support multiprogramming.
  • Correct!
  • The OS performs a context switch when the currently-executing process terminates.
  • Correct!
  • Operating systems prevent process isolation.
  • Incorrect.
  • Unless written to do so, a process waiting for data to be read in from disk will stay scheduled on the CPU while they wait until the data are ready.
  • Incorrect.
  • Under a timesharing system, when a process gets scheduled on the CPU, it gets to continue executing until it terminates.
  • Incorrect.

Checkpoint 13.2.3. Context Switching.

    Which of the following does the operating system perform as part of a context switch? Select all that apply.
  • Saves the program counter (PC) register value of the process currently on the CPU.
  • Correct!
  • Switches to user mode at the end of handling the context switch.
  • Correct!
  • Restores the memory state of new process.
  • Correct!
  • Makes a system call that indicates that a context switch is occurring.
  • Incorrect.

Checkpoint 13.2.4. Process States.

Checkpoint 13.2.5. Process States.

    Which of the following statements that are true?
  • A process transitions to the Blocked state when it is waiting for I/O to complete.
  • Correct!
  • A process transitions to the Exited state when it finishes execution or is explicitly killed.
  • Correct!
  • The operating system’s scheduler is responsible for deciding when a process in the Ready state moves to the Running state.
  • Correct!
  • A process can transition directly from the Blocked state to the Running state.
  • Incorrect.
  • A process in the Ready state is guaranteed to transition to the Running state immediately.
  • Incorrect. The process must first wait for the OS to schedule it on the CPU.

Checkpoint 13.2.6. Process State Transitions.

    Select all the valid state transitions that a process could transition through.
  • Ready -> Running -> Exited
  • Correct!
  • Ready -> Running -> Blocked -> Ready -> Running -> Exited
  • Correct!
  • Ready -> Running -> Ready -> Running -> Exited
  • Correct!
  • Ready -> Blocked -> Ready -> Running -> Exited
  • Incorrect.
  • Ready -> Running -> Blocked -> Running -> Exited
  • Incorrect.

Checkpoint 13.2.7. Fork Return Value(s).

    Fill in the blank with the value that results in the child process printing “I am the child” and the parent process printing “I am the parent”.
    pid = fork();
    
    if ( pid == _____ ) {
        printf(“I am the child\n”);
    } else {
        printf(“I am the parent\n”);
    }
    
  • 0
  • Correct!
  • -1
  • Incorrect.
  • 1
  • Incorrect.
  • getpid()
  • Incorrect.

Checkpoint 13.2.8. Concurrent Processes.

    Given the following code, which of the following are a valid ordering of possible outputs?
    int main(void) {
        printf("ant");
    
        if (fork() == 0) {
            printf("bird");
        } else {
            printf("cat");
        }
    
        printf("dog");
        return 0;
    }
    
  • “ant”, “bird”, “cat”, “dog”, “dog”
  • Correct!
  • “ant”, “cat”, “bird”, “dog”, “dog”
  • Correct!
  • “ant”, “bird”, “dog”, “cat”, “dog”
  • Correct!
  • “ant”, “cat”, “dog”, “bird”, “dog”
  • Correct!
  • “ant”, “bird”, “dog”, “dog”, “cat”
  • Incorrect.

Checkpoint 13.2.9. Concurrent Processes And Variables.

    Consider the following code. Select all possible program outputs:
  • c(12) p(10)
  • Correct!
  • p(10) c(12)
  • Correct!
  • c(10) p(11)
  • Incorrect.
  • c(12) p(11)
  • Incorrect.
  • p(10) c(11)
  • Incorrect.
  • p(12) c(11)
  • Incorrect.
Hint.
Variable values just before the fork is cloned and live separate lives. A write in one process does not affect the variable in the other process.

Checkpoint 13.2.10. Writing Code With Fork.

Write a C program that uses fork() in the following ways:
  1. Creates a child process.
  2. The child process prints numbers from 1 to 5, one per line.
  3. The parent process prints numbers from 6 to 10, one per line.
When executed, the output may appear in any order depending on the process scheduling.
Solution.

Checkpoint 13.2.11. wait().

    True or False: A child process waits for a parent process to exit by calling wait()
  • False
  • Correct! A child process cannot use wait() to wait for its parent process to complete, as wait() is designed specifically for a parent process to wait for its child processes. The process hierarchy in operating systems enforces this behavior.
  • True
  • Incorrect.

Checkpoint 13.2.12. Processes.

    The ___ system call initializes the calling process to run a new program.
  • exec
  • Correct!
  • fork
  • Incorrect.
  • wait
  • Incorrect.
  • exit
  • Incorrect.

Checkpoint 13.2.13. exec.

    True or False: The exec system call always returns to the calling function.
  • False
  • Correct!
  • True
  • Incorrect. If exec is successful, it changes the calling process’s address space contents and execution context to run a new program. As a result, it will not return to the call from exec in the original program the process was running.

Checkpoint 13.2.14. exit.

    Which of the following will not trigger a process to invoke the exit system call?
  • Receiving a SIGCHLD signal from a child process.
  • Correct!
  • Returning from its main function.
  • Incorrect.
  • Receiving a SIGKILL signal.
  • Incorrect.

Checkpoint 13.2.15. exec and wait.

    Which of the following statements correctly describe the purpose and usage of the wait() and exec() system calls?
  • The exec() system call replaces the current process image with a new program, while wait() is used by a parent process to block until its child process terminates.
  • Correct!
  • The exec() system call is used to create a new child process, and wait() is used to terminate the parent process.
  • Incorrect.
  • The exec() system call suspends the current process until a child process finishes execution, and wait() is used to replace the current process with a new program.
  • Incorrect.
  • Both exec() and wait() are used to terminate processes in a coordinated manner.
  • Incorrect.

Checkpoint 13.2.16. fork and exec.

    Consider the following C code that executes a program called hello that prints out the string “hello world” to the terminal.
    int main(void) {
      pid_t pid;
      int status, ret;
      char* argv[2];
    
      argv[0] = "./hello";
      argv[1] = NULL;
     
      printf("Start\n");
    
      pid = fork();
    
        if (pid == 0) { // Child process                                                                        
            printf("Child: Before exec\n");
            ret = execvp(argv[0], argv);
            printf("Child: After exec\n");
            exit(ret);
        } else if (pid > 0) { // Parent process                                                                 
            wait(&status);
            printf("Parent: After wait\n");
        } else {
            printf("Fork failed\n");
        }
    
        return 0;
    }
    
    Which of the following outputs are possible from running this program:
  • Start
    Child: Before exec
    Hello World
    Parent: after wait
    
  • Correct!
  • Start
    Child: Before exec
    Child: After exec
    Parent: after wait
    
  • Correct!
  • Start
    Child: Before exec
    Hello World
    Child: After exec
    Parent: after wait
    
  • Incorrect.
  • Start
    Child: Before exec
    Parent: After wait
    Hello World
    
  • Incorrect.

Checkpoint 13.2.17. Shell Execution.

    The user types the name of a program to run in the foreground in a shell and then presses enter. Given the following set of actions of the shell and child processes:
    1) The shell process calls the fork function.
    2) The shell process calls the wait function.
    3) The child process calls an exec function.
    4) The child process invokes the exit system call.
    5) The shell process prints the command line prompt.
    Which of the following are valid orderings of these actions?
  • 1, 2, 3, 4, 5
  • Correct!
  • 1, 3, 2, 4, 5
  • Correct!
  • 1, 3, 4, 2, 5
  • Correct!
  • 1, 2, 5, 3, 4
  • Incorrect.
  • 3, 4, 1, 2, 5
  • Incorrect.

Checkpoint 13.2.18. fork, exec, and wait.

    Consider a parent process that creates a child process using fork(). The child process uses an exec() system call to execute a new program. Which of the following correctly describes potential behaviors and implications of this scenario?
  • If the parent process does not call wait(), the child process becomes a zombie process after termination.
  • Correct!
  • The exec() system call terminates the parent process and replaces it with the new program specified by the child.
  • Incorrect.
  • If the parent process calls wait(), it must do so before the child calls exec() or the child becomes a zombie after termination.
  • Incorrect.
  • If the child process’s call to exec() fails, the OS terminates the child process without notifying its parent process.
  • Incorrect.

Checkpoint 13.2.19. fork, exec, and wait.

    Given the following code, and assuming no errors occur while it is run:
    pid_t pid1, ret;
    int status;
    
    printf("A\n");
    
    pid1 = fork();
    
    printf("B\n");
    
    if(pid1 == 0) {
        printf("C\n");
        execvp("ls", NULL);
        printf("D\n");
    } else {
        printf("E\n");
        ret = wait(&status);
        printf("F\n");
    }
    printf("G\n");
    
    Choose ALL valid orderings of output that can be produced by running the code.
  • A B B C E F G
  • Correct!
  • A B E B C F G
  • Correct!
  • A B C B E F G
  • Correct!
  • A B C D E F G
  • Incorrect. Does a successful call to execvp return?
  • A B C B D E F G G
  • Incorrect. Does a successful call to execvp return?
  • A B B E F C G
  • Incorrect.
  • A B E B C D G F G
  • Incorrect. Does a successful call to execvp return?