Threads: What Is Thread?
Threads: What Is Thread?
com
Threads
Reading: Section 2.2 of the Textbook (Tanenbaum)
What is Thread?
al
are a finer-grained unit of execution than processes
ep
itn
cs
1
Source: www.csitnepal.com Threads
• Traditional threads has its own address space and a
single thread of control.
• The term multithreading is used to describe the
situation of allowing the multiple threads in same
process.
• When multithreaded process is run on a single-CPU
system, the threads take turns running as in the
multiple processes.
• All threads share the same address space, global
variables, set of open file, child processes, alarms and
signals etc.
al
ep
itn
cs
2
Source: www.csitnepal.com Threads
( a) Three process each with one thread (b) one process with three threads.
• Figure (a) organization is used when three processes
are unrelated whereas (b) would be appropriate when
the three threads are actually part of the same job and
al
are actively and closely cooperating with each other.
ep
itn
cs
3
Source: www.csitnepal.com Threads
• Each thread maintain its own stack.
al
ep
itn
cs
4
Source: www.csitnepal.com Threads
al
ep
itn
cs
5
Source: www.csitnepal.com Threads
Needs to accept user input, display it on screen, spell
check, auto save and grammar check.
– Implicit: Write code that reads user input, displays/formats it
on screen, calls spell checked etc. while making sure that
interactive response does not suffer.
– Threads: Use threads to perform each task and communicate
using queues and shared data structures
– Processes: expensive to create and do not share data
structures and so explicitly passed.
Others: Spreadsheet, Server for www, browser etc.
Advantages:
– Responsiveness.
– Resource sharing.
– Economy.
al
Problems: designing complexity.
ep
itn
cs
6
Source: www.csitnepal.com Threads
In Unix system when fork create it copy all threads of parent to
the child.
What happens if the thread of parent blocked ? When
the line typed, do both thread get a copy of it?
Threads share the many data structure.
What happens if one thread closes a file while another is still reading from
it?
Need complex scheduling operations
al
ep
itn
cs
7
Source: www.csitnepal.com
al
2.
ep
itn
cs
8
Source: www.csitnepal.com
Home works
HW #3:
1. Q. 7, 8, 9 & 12 from the Textbook (Tanenbaum )
2. Discribe how multithreading improve performance over a
singled-threaded solution.
3.What are the two differences between the kernel level threads
and user level threads? Which one has a better performance?
4. List the differences between processes and threads.
5. What resources are used when a thread is created? How do
they differ from those used when a process is created?
al
ep
Reading: Section 2.3 of Textbook (Tanenbaum)
itn
cs
cs
itn
9
ep
al