Process Creation and Termination: fork, exec, and wait Quiz Quiz

Assess your understanding of process creation, process termination, and process management using fork, exec, and wait system calls. This quiz covers fundamentals, real-world scenarios, and key behaviors important for operating systems and programming.

  1. Purpose of fork

    What is the primary purpose of the fork system call in operating systems?

    1. To create a new process by duplicating the calling process
    2. To pause a process until a resource is available
    3. To replace the memory image of a process with a new program
    4. To terminate the current process immediately

    Explanation: The fork system call is used to create a new process by duplicating the calling (parent) process, resulting in a child process with its own process ID. It does not terminate processes, which is done by exit, nor does it replace process memory or programs, which is handled by exec. Pausing a process is not the purpose of fork.

  2. Distinguishing parent and child after fork

    After a fork call, what does the returned value indicate to the parent and child process?

    1. 0 for the child, child’s PID for the parent
    2. Parent PID for child, 0 for parent
    3. -1 for the parent, 0 for the child
    4. 0 for the parent, child’s PID for the child

    Explanation: After fork, the return value is 0 in the child process and the child’s process ID in the parent. This distinction allows each process to determine its role. Returning -1 indicates fork failed, not process identity. Option B correctly describes this behavior while the other options swap values or reference incorrect identifiers.

  3. exec’s core function

    When a process calls an exec family function, what happens to its existing code and data?

    1. They are copied to the new process
    2. They remain in memory alongside the new program
    3. They are replaced by the new program’s code and data
    4. They are paused until the new program finishes

    Explanation: Using exec replaces the current process’s memory (code, data, and stack) with the new program’s image. The original memory and code are not kept or copied, nor are they paused or run alongside the new program. Options A and D are incorrect as the original content does not remain or pause; option C is inaccurate since exec does not create a new process.

  4. wait system call usage

    Why might a parent process use the wait system call after creating a child process?

    1. To increase the child’s execution speed
    2. To free up memory immediately
    3. To receive the child’s exit status and prevent a zombie process
    4. To duplicate the child’s resources

    Explanation: The wait call allows the parent to obtain the child process’s exit status and remove the child process entry, preventing zombies. It does not affect the execution speed or resource duplication, and does not directly free memory; memory is freed when the process exits and wait is called. Other options do not match wait’s purpose.

  5. fork return value on failure

    What value does fork return if it fails to create a new process?

    1. -1
    2. NULL
    3. 1
    4. 0

    Explanation: Fork returns -1 on failure, indicating an error in creating a new process. A return value of 0 signals a successful child process, while 1 and NULL are not standard fork return values. The others are realistic but incorrect for fork’s error reporting.

  6. Parent and child after fork

    Immediately after a successful fork, how many separate processes are running?

    1. 1
    2. 3
    3. 0
    4. 2

    Explanation: A successful fork results in two processes: the parent and the newly created child, both executing independently. One process would mean fork did not happen, while three is possible only if multiple forks occur. Zero indicates no processes, which is not the case after a successful fork.

  7. Zombie process definition

    What is a zombie process in the context of process termination?

    1. A terminated child process whose exit status hasn’t been collected
    2. A process stuck in an infinite loop
    3. A duplicate process running in parallel
    4. A system process that cannot be killed

    Explanation: A zombie process has terminated but continues to occupy a process table slot because its parent hasn’t retrieved its exit status, usually via wait. It is not an infinite loop, unkillable system process, or a duplicate. The other options describe different process issues but not zombies.

  8. exec and process IDs

    Does calling an exec family function change the process identifier (PID) of a process?

    1. Yes, a new PID is assigned
    2. The PID is reset to 0
    3. No, the PID stays the same
    4. The PID becomes the parent’s PID

    Explanation: Exec replaces the program image but does not change the process ID. The PID remains the same since only the code and data change, not the process identity. Assigning a new PID or resetting it is incorrect, and the PID does not become the parent’s PID.

  9. Resource sharing after fork

    After a fork, which of the following is shared between parent and child processes?

    1. Open file descriptors
    2. Address space
    3. Process ID
    4. Stack pointer

    Explanation: File descriptors open at the time of fork are inherited by the child, meaning both parent and child can access open files. The process IDs are unique, address spaces are independent, and stack pointers are different after the fork. Only file descriptors among these are shared.

  10. Sequence for creating a new program

    To run a different program in a child process after a fork, what is the recommended sequence of system calls?

    1. fork then exec
    2. exec then fork
    3. wait then exec
    4. exec then wait

    Explanation: First, fork creates a child process. Then, the child uses exec to run a different program. Reversing this order or combining with wait in the wrong sequence is incorrect; wait is usually used by the parent after the child starts. Fork followed by exec is the standard and recommended sequence.