NETW3005 Operating Systems Lecture 3: Threads and...
Transcript of NETW3005 Operating Systems Lecture 3: Threads and...
![Page 1: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/1.jpg)
NETW3005 Operating SystemsLecture 3: Threads and Data Sharing
Last lecture:
• Hierarchical Structure in Operating Systems
• System Calls and Interrupts
• Representing Processes in Operating Systems
• Overview of Process Scheduling
This lecture:
• Cooperating Processes
• Communication between Parent and Child Processes
• Shared Memory and Pipes
• Threads
• Inter-Process Communication
1
![Page 2: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/2.jpg)
Cooperating Processes
Some terminology:
• Cooperating processes: where the execution ofone process can affect the execution of another.
• Independent processes: where the execution ofone process cannot affect the execution of another.
Naturally, any processes which share data are cooperat-ing processes.
2
![Page 3: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/3.jpg)
Why Allow Processes to Cooperate?
What are some advantages of process cooperation?
• Information sharing. Two processes might want toaccess the same data. E.g. an ATM machine. . .
• Computation speedup. To introduce some paral-lelism into program execution.
• Modularity. Having particular program functionsperformed by their own processes.
• Convenience. E.g. a user might want to print a fileat the same time as editing it.
3
![Page 4: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/4.jpg)
Communication between Parent and Child Pro-cesses
Recall the process tree we saw in the last lecture:
root
daemon init
user1 user2 user3
system proc application
sys. proc sys proc
A process can create a child process to do a particulartask. But to do so, it has to be able to communicatewith its child.
4
![Page 5: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/5.jpg)
Creating Child Processes in UNIX
In UNIX, a child process is created with a system callcalled fork. fork creates a new process, consisting of acopy of the address space of the parent process.
• System calls are functions, and functions can returnvalues: fork is no exception.
• Both parent and child processes continue after thefork function call—but a different value for the func-tion is returned in the two cases:
– the value for the child process is 0.
– the value for the parent process is the PID of thechild.
(By consulting this value, each process knows whetherit’s the parent or the child.)
• The child process then typically executes an execlpcommand, which loads a new program into its mem-ory space (erasing the copy of the parent).
5
![Page 6: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/6.jpg)
Communication Between Parents & Children
Some questions:
• Why is the parent’s address space copied to the childprocess if execlp is just going to wipe it all out? A:because you want to be able to pass ar-bitrary values as the parameters of ex-eclp.
• Why is the parent process given the child process’PID? A: so it knows which process is go-ing to deliver the results of the compu-tation.
After a process has created a child process, it often exe-cutes a wait system call.
• wait moves a process off the ready queue, until thechild process has terminated.
• When the child process has terminated, it can returndata to the parent.
6
![Page 7: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/7.jpg)
Process termination
When a process has executed its last statement, it exe-cutes a special function: the exit system call.
At this point,
• it may return data to its parent process (the onewaiting for it to terminate);
• all its resources (main memory, open files etc) aredeallocated by the operating system.
Processes can also die more violently: they can be ter-minated by other processes (e.g. in UNIX with kill).
Of course there is an important constraint on which pro-cesses a process can kill:
• you can only kill your children.
Zombie process: a process whose parent has terminated.
7
![Page 8: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/8.jpg)
Shared Memory: A Simple Kind of Data Shar-ing
The map memory family of system calls: allow twoprocesses to share some region of main memory.
Operating System
P2
P1
map memory
• This is achieved by overriding the operating system’snormal constraints on memory access for processes.
8
![Page 9: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/9.jpg)
An Example of Cooperation:The Producer-Consumer Problem
• A producer process writes data to a buffer of fixedsize.
• A consumer process reads data from the buffer.
• The two processes are being scheduled independentlyby the CPU.
Obviously,
• the producer process must wait if the buffer gets full;
• the consumer process must wait if the buffer is empty.
This is a taster for the issue of process synchronisa-tion.
9
![Page 10: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/10.jpg)
Pipes (in UNIX)
grep ’party’ events.txt | lpr
What’s happening here?
• We’re typing the command to the shell.
• When you hit ‘return’, the shell process normallyforks a child process, which is told to execute thespecified program.
• When you link two programs with a pipe, the shellprocess first sets up a buffer in memory, then forkstwo child processes, which are to read / write to thisbuffer.
10
![Page 11: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/11.jpg)
Threads
Pipes provide a simple method for sharing data.
Operating systems frequently support a special kind ofprocess called a thread to allow more complex data-sharing.
• A standard process has a data section, code section,a program counter, a register set, a stack space, infoabout open files, allocated I/O devices etc.
• A thread just has a program counter, a register setand a stack space.
• You can have several threads within a single process,using the same code section, data section, memoryand I/O resources.
11
![Page 12: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/12.jpg)
Threads
O/S management info(open files, allocateddevices etc)
text segment
data segment
program counterregistersstack space
program counterregistersstack space
program counterregistersstack space
thread1
thread2
thread3
task
process
Some terminology:
• The code section, data section, and O/S housekeep-ing info of a process is collectively known as a task.
• A task with just one thread is called a heavyweightprocess.
• A thread is also known as a lightweight process.
12
![Page 13: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/13.jpg)
Operations on Threads
Threads and processes can do a similar range of things.
• A thread can wait while an I/O process completes(called blocking).
• A thread can be in different states: ready, blocked,running or terminated.
• A thread can create child threads.
13
![Page 14: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/14.jpg)
Advantages of Threads
What’s useful about threads?
• Responsiveness. Multithreading an interactive ap-plication may allow a program to continue running,even if part of it is waiting.
• Switching speed. Switching between threads is fasterthan between (heavyweight) processes, because there’sless to change.
• Communication between processes. We don’t needspecial setups for shared memory when implement-ing communicating processes as threads in the sametask.
• Redundancy avoidance. If you need several versionsof one program reading the same data at the sametime, it’s inefficient to have each version implementedas a heavyweight process.
Problems with threads?
It’s a bit perilous running threads, because
14
![Page 15: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/15.jpg)
there’s no O/S-enforced constraints on howthe two threads can interact.
But if the the code has been designed bya single person, there’s no reason why itcan’t be written right.
15
![Page 16: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/16.jpg)
Servers as Threads
A web server is a process that provides data over theweb to requesting clients. (Possibly a large number ofthem.)
• Imagine a queue of client requests arriving in an in-put buffer.
• If we implemented the web server as a single process,what would the problem be? the file serverwould be inequitable: the clients at theend of the queue wouldn’t get ANY ser-vice until the ones on the front had beenfully serviced.
It’s more efficient if the web server process forks a childprocess to deal with each separate request that arrives.
• Why? Because this way each client getsa bit of access to the CPU, and gets de-livered some data.
In this approach:
16
![Page 17: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/17.jpg)
• One thread is a daemon: it does nothing but listenfor new requests.
• Each time a new request is detected, a new thread iscreated to service this request.
17
![Page 18: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/18.jpg)
Two Kinds of Thread
User-level threads: implemented in user-level libraries,without the need for system calls.
• These are the fastest kind of thread to switch be-tween.
• But then the kernel doesn’t know about individualthreads within a process.
Why should that matter? (i) Unfairness: oneprocess might have 100 threads, anotherprocess just one. The kernel gives themequal time in the CPU.
(ii) System calls. If a thread makes asystem call, then the whole (heavyweight)process is suspended until it’s completed;that includes all the other threads.
Kernel-level threads: implemented via system calls.
• In this case, the kernel knows about individual threads.
18
![Page 19: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/19.jpg)
• But switching between threads is slower.
19
![Page 20: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/20.jpg)
Gobbledegook?
‘Because kernel thread management is done bythe operating system, kernel threads are gener-ally slower to create and manage than user threads.However, since the kernel is managing the threads,if a thread performs a blocking system call, thekernel can schedule another thread in the appli-cation for execution.’ (Silberschatz et al.)
20
![Page 21: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/21.jpg)
Multithreading models
Many systems support both user-level and kernel-levelthreads.
• In a many-to-one model, many user threads aremapped onto a single kernel thread.
– This suffers from the problem of block-ing: the kernel doesn’t know about itwhen one thread blocks.
• In a one-to-one model, each user thread is mappedonto a single kernel thread.
– This doesn’t suffer from the above prob-lem - but there’s an overhead in cre-ating the corresponding kernel threads.Most systems only support a fixed max-imum number of kernel threads.
• In a many-to-many model, a set of user threadsis multiplexed onto a (smaller or equal) set of kernelthreads.
21
![Page 22: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/22.jpg)
– You need to have an extra mechanism that allo-cates each user thread to a kernel thread.
– A many-to-many scheme avoids many of the dis-advantages of both above schemes.
22
![Page 23: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/23.jpg)
Java threads
Since Java runs on a virtual machine, it can provide sup-port at the language level for creation and managementof threads.
The simplest way to create a new thread is to extendthe class Thread:
public class Worker1 extends Thread {
public void run() {
System.out.println("I am a worker thread");
}
}
public class First {
public static void main(String args[]) {
Thread runner = new Worker1();
runner.start();
System.out.println("I am the main thread");
}
}
23
![Page 24: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/24.jpg)
Java threads
Another way to create a new thread is to define a classthat implements the Runnable interface.
public interface Runnable {
public abstract void run();
}
public class Worker2 implements Runnable {
public void run() {
System.out.println("I am a worker thread");
}
}
public class Second {
public static void main(String args[]) {
Thread thrd = new Thread(new Worker2());
thrd.start();
System.out.println("I am the main thread");
}
}
24
![Page 25: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/25.jpg)
More on Java threads
As well as threads defined in a Java program, there are anumber of extra threads running on behalf of the JVM.For instance:
• a thread to listen and react to the mouse/keyboard;
• a thread to do garbage collection;
• a thread to handle timer events (e.g. the sleep()
method).
The JVM will implement threads in different ways in dif-ferent versions, and in different operating systems. Forinstance, on the Linux lab machines:
• Java version 1.18 employs user-level threads.
• Java version 1.4 employs kernel-level threads.
25
![Page 26: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/26.jpg)
Message-Passing Systems
One final, general way of communicating between pro-cesses is via a message-passing system.
To pass messages between processes, a communica-tion link must be establised. This can be implementedin various ways, but it must have certain logical charac-teristics specified:
• Can a link be associated with more than two pro-cesses?
• What is the capacity of the link?
• Is the link unidirectional or bidirectional?
The main instructions: send, receive.
26
![Page 27: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/27.jpg)
Direct and Indirect Communication
Direct communication is achieved by naming the processa message is sent to / received from.E.g. send(P001, message).
BUT:
• Absolute process IDs are problematic: the ID of aprocess might be different next time.
Solution: send to and receive from mailboxes. E.g.send(mbox1, message).
27
![Page 28: NETW3005 Operating Systems Lecture 3: Threads and …netw3005.wdfiles.com/local--files/lectures/L3.pdf · · 2012-09-17NETW3005 Operating Systems Lecture 3: Threads and Data Sharing](https://reader031.fdocuments.us/reader031/viewer/2022022505/5abce4437f8b9af27d8e7fca/html5/thumbnails/28.jpg)
Summary: Ways of Sharing Data BetweenProcesses
• Creating a child process
• Shared memory
• Pipes
• Threads
• Message system
28