49
NETW 3005 Threads and Data Sharing

NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

Embed Size (px)

Citation preview

Page 1: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

NETW 3005

Threads and

Data Sharing

Page 2: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

Reading

• For this lecture, you should have read Chapter 4 (Sections 1-4).

NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing 2

Page 3: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 3

Page 4: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 4

Page 5: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 5

Page 6: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 6

Page 7: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

Communication between processes

NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing 7

root

child

daemon init

user1 user2 user3

system proc application

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.

Page 8: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 8

Page 9: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 9

Page 10: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 10

Page 11: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 11

Page 12: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

Some questions (2)

• Why is the parent process given the PID of the child process?

NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing 12

Page 13: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 13

Page 14: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 14

Page 15: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 15

Page 16: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 16

Zombie process: a process whose parent has terminated.

Page 17: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 17

Operating System

P2

P1

Page 18: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 18

Page 19: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 19

This is a taster for the issue of process synchronisation.

Page 20: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

Pipes (in UNIX)

• Pipes provide a simple method for sharing data.

• grep ‘party’ events.txt | lpr

NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing 20

Page 21: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 21

Page 22: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 22

Page 23: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 23

Page 24: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

Threads (3)

NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing 24

SPC R

SPC R

SPC R

code segment

data segment

OS management information

Page 25: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 25

Page 26: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 26

Page 27: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 27

Page 28: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 28

Page 29: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 29

Page 30: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 30

Page 31: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing 31

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.

Page 32: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 32

Page 33: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 33

Page 34: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 34

Page 35: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 35

Page 36: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 36

Page 37: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 37

Page 38: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 38

Page 39: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 39

Page 40: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 40

Page 41: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 41

Page 42: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 42

Page 43: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 43

Page 44: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 44

Page 45: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 45

Page 46: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 46

Page 47: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

Summary: ways of sharing data

• Creating a child process

• Shared memory

• Pipes

• Threads

• Message passing system

NETW3005 (Operating Systems) Lecture 03 - Threads and data-sharing 47

Page 48: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

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-sharing 48

Page 49: NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -

Next Lecture

SchedulingChapter 5 (Sections 1-3, 7)