Chapter 4

Chapter 4: Threads

What resources are used when a thread is created? How do they differ
from those used when a process is created? (with a graphical illustration )

Creating a new thread doesn’t need any new resources but it needs the resources (memory) to be shared between process and threads. Creating a new process is consider as a heavyweight as new process needs new address space for the creation to be done and for sharing memory the communication of the process will be very expensive.

Which of the following components of program state are shared across
threads in a multithreaded process?
a. Register values b. Heap memory
c. Global variables d. Stack memory

Only heap memory and global variables are the shared.

Describe the actions taken by a kernel to context-switch between kernel levels
Threads.
– Save data from CPU register of the current thread
– Restore registers of new thread
Provide three programming examples in which multithreading provides
Better performance than a single-threaded solution.
1. Running application in parallel
2. Web server
3. GUI

Assume that an operating system maps user-level threads to the kernel
using the many-to-many model and that the mapping is done through
LWPs. Furthermore, the system allows developers to create real-time
threads for use in real-time systems. Is it necessary to bind a real-time
thread to an LWP? Explain.

Yes it is necessary to bind a real thread

Consider the following code segment:

pid t pid;
pid = fork();
if (pid == 0) { /* child process */
fork();
thread create( . . .);
}
fork();

a. How many unique processes are created?
b. How many unique threads are created?
a. 6 processes b. 2 threads