Download presentation
Presentation is loading. Please wait.
Published byPhillip Lewis Modified over 9 years ago
1
NETW 3005 Threads and Data Sharing
2
Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing2
3
Last Lecture Hierarchical structure in Operating Systems System calls and interrupts Representing processes in Operating Systems Overview of process scheduling NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing3
4
This lecture Cooperating processes. Communication between parent and child processes. Shared memory and pipes. Threads. Inter-process communication. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing4
5
Independent and cooperating processes Independent processes. The execution of one process cannot affect the execution of another. Cooperating processes. The execution of one process can affect the execution of another. Naturally, any processes which share data are cooperating processes. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing5
6
Advantages of process cooperation Information sharing. Two processes might want to access the same data. Computation speedup. To introduce some parallelism into program execution. Modularity. Having program functions performed by their own processes. Convenience. A user might want to print a file at the same time as editing it. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing6
7
Communication between processes NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing7 root child daemoninit user1user2user3 system procapplication child A process can create a child process to do a particular task. But to do so, it has to be able to communicate with its child.
8
Creating child process in UNIX (1) In UNIX, a child process is created with a system call called fork. fork creates a new process, consisting of a copy of the address space of the parent process. The child process then typically executes an execlp command, which loads a new program into its memory space (erasing the copy of the parent). NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing8
9
Creating child process in UNIX (2) System calls are functions, and functions can return values. fork is no exception. Both parent and child processes continue after the fork function call—but a different value is returned in the two cases: –the value for the child process is 0. –the value for the parent process is the PID of the child. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing9
10
Some questions (1) Why is the parent’s address space copied to the child process if execlp is just going to wipe it all out? NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing10
11
Some questions (1) Why is the parent’s address space copied to the child process if execlp is just going to wipe it all out? Because you want to be able to pass arbitrary values as the parameters of execlp. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing11
12
Some questions (2) Why is the parent process given the PID of the child process? NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing12
13
Some questions (2) Why is the parent process given the PID of the child process? So it knows which process is going to deliver the results of the computation. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing13
14
What next? As we have seen, the child process will go off and do its own thing, possibly executing a different program. The parent process often executes a wait system call, which moves it off the ready queue until the child process has terminated. When the child process has terminated, it may return data to the parent. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing14
15
Process termination When a process has executed its last statement, it executes an exit system call. At this point: –it may return data to its parent process (the one waiting for it to terminate); –all its resources (main memory, open files etc.) are de-allocated by the operating system. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing15
16
Process termination Processes can also die more violently – they can be terminated by other processes (e.g. in UNIX with kill). There is an important constraint on which processes you can kill — you can only kill your children. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing16 Zombie process: a process whose parent has terminated.
17
Shared memory: a simple kind of data sharing The map memory family of system calls allow two processes to share some region of main memory. This is achieved by overriding the operating system’s normal constraints on memory access for processes. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing17 Operating System P2 P1
18
The Producer-Consumer problem A producer process writes data to a buffer of fixed size. A consumer process reads data from the buffer. The two processes are being scheduled independently by the CPU. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing18
19
Obviously … The producer process must wait if the buffer gets full; The consumer process must wait if the buffer is empty. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing19 This is a taster for the issue of process synchronisation.
20
Pipes (in UNIX) Pipes provide a simple method for sharing data. grep ‘party’ events.txt | lpr NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing20
21
What’s happening here? We’re typing the command to the shell. When you hit ‘return’, the shell process normally forks a child process, which is told to execute the specified program. When you link two programs with a pipe, the shell process first sets up a buffer in memory, then forks two child processes, which write to / read from this buffer. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing21
22
Threads (1) Operating systems frequently support a special kind of process called a thread to allow more complex data-sharing. A standard process has a data section, code section, and a full PCB. A thread just has a program counter, a register set and a stack space. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing22
23
Threads (2) You can have several threads within a single process, using the same code section, data section, memory and I/O resources. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing23
24
Threads (3) NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing24 SPCR S R S R code segment data segment OS management information
25
Terminology The code segment, data segment, and O/S housekeeping information of a process is collectively known as a task. A task with just one thread is called a heavyweight process. A thread is also known as a lightweight process. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing25
26
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. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing26
27
Advantages of threads (1) Responsiveness. Multithreading an interactive application may allow a program to continue running, even if part of it is waiting. Switching speed. Switching between threads is faster than between (heavyweight) processes, because there’s less to change. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing27
28
Advantages of threads (2) Communication between processes. We don’t need special setups for shared memory when implementing communicating processes as threads in the same task. Redundancy avoidance. If you need several versions of one program reading the same data at the same time, it’s inefficient to have each version implemented as a heavyweight process. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing28
29
Problems with threads It’s a bit perilous running threads, because there are no OS-enforced constraints on how threads can interact. However, if the code has been designed by a single person, there’s no reason why it can’t be written correctly. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing29
30
Servers as threads (1) A web server is a process that provides data over the web to requesting clients. (Possibly a large number of them.) Imagine a queue of client requests arriving in an input buffer. If we implemented the web server as a single process, what would the problem be? NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing30
31
NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing31 The file server would be inequitable. The clients at the end of the queue wouldn’t get ANY service until the ones on the front had been fully serviced.
32
Servers as threads (2) It’s more efficient if the web server process forks a child process to deal with each separate request that arrives. Why? NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing32
33
Servers as threads (2) It’s more efficient if the web server process forks a child process to deal with each separate request that arrives. Why? Because this way each client gets some access to the CPU, and gets some data delivered. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing33
34
Servers as threads (3) One particular thread is a daemon – it does nothing but listen for new requests. Each time a new request is detected, a new thread is created to service this request. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing34
35
User-level threads Implemented in user-level libraries, without the need for system calls. Fastest kind of thread to switch between. But, the kernel doesn’t know about individual threads within a process. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing35
36
Why should that matter? I.Unfairness. One process might have 100 threads, another process just one. The kernel gives them equal time in the CPU. II.System calls. If a thread makes a system call, then the whole (heavy- weight) process is suspended until it’s completed; that includes all the other threads. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing36
37
Kernel-level threads Implemented via system calls. In this case, the kernel knows about individual threads. But switching between threads is slower. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing37
38
Multi-threading models (1) Many systems support both user-level and kernel-level threads. We then need some way to map user- level threads to kernel-level threads. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing38
39
Multi-threading models (2) In a many-to-one model, many user threads are mapped onto a single kernel thread. This suffers from the problem of blocking: the kernel doesn’t know about it when one thread blocks. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing39
40
Multi-threading models (3) In a one-to-one model, each user thread is mapped onto a single kernel thread. This doesn’t suffer from the above prob- lem, but there’s an overhead in creating the corresponding kernel threads. Most systems only support some maximum number of kernel threads. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing40
41
Multi-threading models (4) In a many-to-many model, a set of user threads is multiplexed onto a (smaller or equal) set of kernel threads. You need to have an extra mechanism that allocates each user thread to a kernel thread. A many-to-many scheme avoids many of the disadvantages of both above schemes. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing41
42
Java threads (1) As well as any threads defined in a Java program, there are a number 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). NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing42
43
Java threads (2) The JVM will implement threads in different ways in different versions, and in different operating systems. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing43
44
Message-passing systems (1) One final, general, way of communicating between processes is via a message- passing system. To pass messages between processes, a communication link must be established. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing44
45
Message-passing systems (2) This can be implemented in various ways, but it must specify certain logical characteristics: –Can a link be associated with more than two processes? –What is the capacity of the link? –Is the link unidirectional or bidirectional? The main instructions: send, receive. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing45
46
Direct and indirect communication Direct communication is achieved by naming the process that a message is sent to / received from, e.g. send(P001, message). BUT, process IDs are problematic – a process’ ID might be different next time. Solution: send to and receive from mail- boxes, e.g. send(mbox1, message). NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing46
47
Summary: ways of sharing data Creating a child process Shared memory Pipes Threads Message passing system NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing47
48
Reading For this lecture, you should (have) read Chapter 4 (Sections 1–4) of Silberschatz et al. For the tutorial, you MUST read 4.1 and 4.2. NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing48
49
Next Lecture Scheduling Chapter 5 (Sections 1-3, 7)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.