Transcript thread

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 and data-sharing
2
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
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
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
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
Communication between processes
A process can
root
create a child
process to do a daemon
init
particular task.
But to do so, it
user1 user2 user3
has to be able
to communicate system proc application
with its child.
child
NETW3005 (Operating Systems)
Lecture 03 - Threads and data-sharing
child
7
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
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
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
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
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
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
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
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
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.
Zombie process: a process
whose parent has terminated.
NETW3005 (Operating Systems)
Lecture 03 - Threads and data-sharing
16
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
P1
P2
Operating
System
17
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
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 synchronisation.
NETW3005 (Operating Systems)
Lecture 03 - Threads and data-sharing
19
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
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
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
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
Threads (3)
code
segment
PC
R
S
data
segment
PC
R
S
OS management
information
PC
R
S
NETW3005 (Operating Systems)
Lecture 03 - Threads and data-sharing
24
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
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
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
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
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
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
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.
NETW3005 (Operating Systems)
Lecture 03 - Threads and data-sharing
31
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
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
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
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
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 (heavyweight) process is suspended until it’s
completed; that includes all the other
threads.
NETW3005 (Operating Systems)
Lecture 03 - Threads and data-sharing
36
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
Multi-threading models (1)
• Many systems support both user-level
and kernel-level threads.
• We then need some way to map userlevel threads to kernel-level threads.
NETW3005 (Operating Systems)
Lecture 03 - Threads and data-sharing
38
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
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 problem, 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
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
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
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
Message-passing systems (1)
• One final, general, way of communicating
between processes is via a messagepassing system.
• To pass messages between processes, a
communication link must be established.
NETW3005 (Operating Systems)
Lecture 03 - Threads and data-sharing
44
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
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 mailboxes, e.g. send(mbox1, message).
NETW3005 (Operating Systems)
Lecture 03 - Threads and data-sharing
46
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
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
Next Lecture
Scheduling
Chapter 5 (Sections 1-3, 7)