Tutorial 04 Threads
Tutorial 04 Threads
Ex 1. What two advantages do threads have over multiple processes? What major
disadvantage do they have? Suggest one application that would benefit from the use of
threads, and one that would not.
Answer:
• Threads are very inexpensive to create and destroy, and they use very little resources
while they exist.
• They do use CPU time for instance, but they don’t have totally separate memory
spaces.
• Threads must “trust” each other to not damage shared data. For instance, one thread
could destroy data that all the other threads rely on, while the same could not happen
between processes unless they used a system feature to allow them to share data.
• Any program that may do more than one task at once could benefit from multitasking.
For instance, a program that reads input, processes it, and outputs it could have three
threads, one for each task.
• “Single-minded” processes would not benefit from multiple threads; for instance, a
program that displays the time of day.
Ex 2. What resources are used when a thread is created? How do they differ from those
used when a process is created?
Answer: A context must be created, including a register set storage location for storage
during context switching, and a local stack to record the procedure call arguments, return
values, and return addresses, and thread-local storage. A process creation results in
memory being allocated for program instructions and data, as well as thread-like storage.
Code may also be loaded into the allocated memory.
1
• And like process, if one thread is blocked, another thread can run.
Differences
• Unlike processes, threads are not independent of one another.
• Unlike processes, all threads can access every address in the task.
• Unlike processes, threads are design to assist one other. Note that processes might or
might not assist one another because processes may originate from different users.