πŸŽ“ BookMCQ
← Back to 18. The Linux System

πŸ“ Processes and threads in The Linux System (11 MCQs)

Linux Tasks and Lightweight Execution Contexts

Linux represents schedulable execution entities using task structures, commonly associated with the kernel's task_struct. A process generally has its own virtual address space and resource context, while threads within a process share selected resources such as memory and open file tables. Linux implements both using kernel-schedulable tasks created through clone-related mechanisms.

Why Applications Need Multiple Execution Paths

A single-threaded process may block while performing I/O or may not use multiple processor cores effectively. Threads allow parts of an application to execute concurrently while sharing data and resources. Separate processes provide stronger address-space isolation, but communication between them generally requires explicit interprocess communication mechanisms.

Task Creation and Shared Resource Configuration

Linux provides clone-related interfaces that determine which resources a new task shares with its creator. Thread libraries commonly use these mechanisms to create threads sharing an address space and other process resources. The scheduler treats runnable tasks as execution entities and selects them for available CPU time, subject to scheduling policy.

Example of a Multithreaded Web Application

A web server creates four worker threads within one process. Each thread handles a client connection while sharing the process's address space and configuration data. If one thread blocks on I/O, others may continue executing. Shared memory reduces communication overhead but requires synchronisation when modifying common data.

Concurrency, Isolation and Shared Data Risks

Threads support responsive applications and parallel execution on multicore processors. Shared address spaces make data exchange efficient but introduce race conditions and synchronisation requirements. Separate processes offer stronger isolation and independent failure boundaries, at the cost of additional memory and communication overhead.

Misconceptions About Linux Processes and Threads

'Linux threads are invisible to the kernel scheduler' is false because kernel tasks are scheduled. 'All threads have separate address spaces' ignores shared-memory thread groups. 'Using more threads always improves speed' overlooks lock contention, CPU limits, and scheduling overhead.

Exam Focus: Process, Thread and Shared Resource Behaviour

Compare processes and threads by address space, resource sharing, isolation, and communication. Explain how clone-related flags affect resource sharing. Questions may ask what happens when a thread modifies shared memory or compare the benefits of multicore parallelism with synchronisation and context-switching costs.

Key Terms of Linux Processes and Threads

  • task_struct - Kernel structure describing a task.
  • Thread - Execution path sharing process resources.
  • Process - Program in an execution environment.
  • clone() - Creates tasks with selected shared resources.
  • Thread group - Related tasks forming a process.
2
Easy
5
Medium
4
Hard

πŸ“ All Processes and threads in The Linux System MCQs

Q1. A program calls fork() and then, in the child process, calls exec() to run /usr/bin/ls. Which sequence correctly describes what happens?

A.fork creates a child; the child runs ls while the parent continues the original program. βœ…
B.fork creates a child; the parent runs ls while the child continues the original program.
C.exec replaces both the parent and child processes with ls.
D.fork creates a child, but exec fails because only the parent can call exec.
πŸ’‘ Difficulty: easy | βœ… Correct: A

πŸ“– Explanation: When fork() returns, the operating system creates an exact copy of the calling process. The child receives a return value of zero and can immediately invoke exec() to replace its memory image with the new program (/usr/bin/ls). The parent receives the child's PID and continues executing the original code. Therefore option A describes the correct behavior. Options B, C, and D misstate which process runs exec or claim exec cannot be used in the child, which is false.

Q2. What system call is used to create a new process that is an exact copy of the calling process?

A.exec
B.wait
C.fork βœ…
D.clone
πŸ’‘ Difficulty: easy | βœ… Correct: C

πŸ“– Explanation: The fork() system call creates a new process that is a duplicate of the calling process, inheriting its code, data, and open file descriptors. This child process receives a return value of zero, while the parent receives the child's process identifier. Because fork is specifically designed for process creation, option C is correct. Exec, wait, and clone serve different purposes, so the other choices are incorrect.

Q3. Why does UNIX separate process creation (fork) from program execution (exec) instead of providing a single call that does both?

A.To simplify kernel design
B.To allow the parent to modify the child's environment before exec βœ…
C.To enable multithreading
D.To reduce memory usage
πŸ’‘ Difficulty: medium | βœ… Correct: B

πŸ“– Explanation: Separating fork and exec lets the parent process intervene after the child is created but before a new program is loaded. This window enables the parent to adjust environment variables, file descriptors, or resource limits specific to the child. Option B captures this benefit. Options A, C, and D describe unrelated motivations and do not reflect the primary design rationale.

Q4. A parent process wants its child to have a modified PATH variable before running /usr/bin/python. Which sequence of calls should the parent perform?

A.fork, modify child's environment, exec in child βœ…
B.exec then fork
C.fork then exec in parent
D.modify parent environment then fork
πŸ’‘ Difficulty: medium | βœ… Correct: A

πŸ“– Explanation: The correct approach is to fork first, giving the child a copy of the parent's address space. The parent can then change the PATH in the child's memory (often via setenv or similar) before the child invokes exec() to load /usr/bin/python. This ensures the new program sees the altered environment. The other options either reverse the order or attempt to change the environment after exec, which would have no effect.

Q5. If a process calls exec() without having called fork() first, what is the effect on the system?

A.A new process is created
B.The existing process is replaced by the new program βœ…
C.Both parent and child are terminated
D.A new thread is spawned
πŸ’‘ Difficulty: medium | βœ… Correct: B

πŸ“– Explanation: Calling exec() replaces the current process image with a new program while retaining the same PID. No additional process is created; the original code is overwritten. Therefore option B is correct. Option A is false because fork is required to create a new process. Option C incorrectly suggests termination, and option D confuses exec with thread creation, which it does not perform.

Q6. How does Linux's threading model differ from the traditional UNIX process model regarding resource sharing?

A.Threads share address space, file descriptors, and signal handlers, while processes do not βœ…
B.Threads have separate address spaces
C.Linux does not support threads
D.Linux threads are implemented as separate processes with no sharing
πŸ’‘ Difficulty: medium | βœ… Correct: A

πŸ“– Explanation: Linux implements threads as lightweight tasks that share the same virtual address space, open file descriptors, and signal handling structures, allowing efficient communication and reduced overhead. Traditional UNIX processes have distinct address spaces and resources, requiring explicit inter‑process communication. Option A accurately describes this distinction. The other options either invert the sharing relationship or claim non‑existence of threads, which are incorrect.

Q7. Consider the code: pid = fork(); if (pid == 0) execve(\/bin/grep\, args, env); else execve(\/usr/bin/wc\, args2, env); Which program runs in the parent and which in the child?

A.Parent runs grep, child runs wc
B.Parent runs wc, child runs grep βœ…
C.Both run grep
D.Both run wc
πŸ’‘ Difficulty: hard | βœ… Correct: B

πŸ“– Explanation: After fork(), the child process receives a return value of zero, so it executes the first execve call and runs /bin/grep. The parent receives the child's PID (non‑zero) and therefore executes the second execve call, running /usr/bin/wc. Option B reflects this behavior. Options A, C, and D misunderstand the fork return values or assume both processes execute the same program, which is not the case.

Q8. After fork, both parent and child need to close an inherited file descriptor before exec to avoid a descriptor leak. Which ordering ensures no leak?

A.Parent closes, then child closes, then both exec βœ…
B.Child closes, then parent closes, then both exec
C.Both close after exec
D.Close before fork
πŸ’‘ Difficulty: hard | βœ… Correct: A

πŸ“– Explanation: Each process must close the descriptor in its own address space before executing a new program; otherwise the descriptor remains open in the new program. By having both parent and child perform the close operation prior to their respective exec calls, the descriptor is not leaked. Option A follows this correct ordering. Options B and C close descriptors at the wrong time, and option D is impossible because the descriptor does not exist before fork.

Q9. During exec, what happens to the previous program's memory mappings, and why is this important for security?

A.They are preserved, allowing code reuse
B.They are unmapped and replaced, preventing old code from lingering βœ…
C.They are swapped to disk
D.They are merged with the new program
πŸ’‘ Difficulty: hard | βœ… Correct: B

πŸ“– Explanation: exec() discards the existing memory mappings of the old program, freeing pages and loading the new executable's segments. This removal eliminates any residual code or data that could be exploited, ensuring that only the intended program resides in memory. Option B correctly describes this behavior. The other options suggest retention, swapping, or merging of memory, which does not occur and would weaken security.

Q10. How does the use of fork() followed by exec() simplify handling of environment variables compared to a single combined system call?

A.It doesn't simplify
B.It allows the parent to adjust the environment in the child before exec βœ…
C.It forces the environment to be inherited unchanged
D.It requires explicit passing of the environment each time
πŸ’‘ Difficulty: hard | βœ… Correct: B

πŸ“– Explanation: By forking first, the child receives a copy of the parent's environment, which the parent can modify (e.g., change PATH) before the child calls exec(). This selective alteration is easier than a single call that would need to specify the entire environment upfront. Option B captures this advantage. Options A and C deny the benefit, and D misrepresents the need for explicit environment passing, which is unnecessary after fork.

Q11. A server process forks a child to handle a client request, then the child calls exec() to run a different binary that processes the request. If the child fails to exec, what should the parent do to avoid zombie processes?

A.Ignore the failure
B.Call waitpid for the child βœ…
C.Call fork again
D.Terminate the parent
πŸ’‘ Difficulty: medium | βœ… Correct: B

πŸ“– Explanation: When the child cannot replace its image with the new program, it remains as a regular process that will eventually exit. The parent must reap the terminated child using waitpid (or a similar wait call) to remove its entry from the process table and prevent a zombie. Option B describes the correct cleanup step. Ignoring the failure or forking again does not address the zombie issue, and terminating the parent is unnecessary.

πŸ”— Related Topics (MCQs)