Professional Documents
Culture Documents
Operating Systems
Agenda
[SGG7] Chapter 3 and 4
• Process
+ Process Concept
Processes and Threads + Process Scheduling
+ Operation on Processes
+ Cooperating Processes
Introduce the notion of process and thread + Interprocess Communication
Describe the various features of processes • Threads
Describe interprocess communication
Copyright Notice: The lecture notes are mainly based on Silberschatz’s, Galvin’s and Gagne’s book (“Operating System
Concepts”, 7th ed., Wiley, 2005). No part of the lecture notes may be reproduced in any form, due to the copyrights
reserved by Addison-Wesley. These lecture notes should only be used for internal teaching purposes at the Linköping
University.
• A process includes:
+ program counter
+ stack
+ data section
+ register contents
+ program code (text section)
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.3 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.4 Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.5 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.6 Silberschatz, Galvin and Gagne ©2005
1
Process Control Block (PCB) Process Control Block PCB (Cont.)
• Program counter
• I/O status information
+ Address of the next instruction
+ List of I/O devices allocated to process, open files etc.
• CPU registers
+ Accumulators, index registers, stack pointers, general-purpose PCB is sometimes denoted as task control block.
registers
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.7 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.8 Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.9 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.10 Silberschatz, Galvin and Gagne ©2005
Process Scheduling Queues Ready Queue And Various I/O Device Queues
• Job queue
set of all processes in the system
• Ready queue
set of all processes residing in main memory,
ready and waiting to execute
• Device queues
set of processes waiting for an I/O device
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.11 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.12 Silberschatz, Galvin and Gagne ©2005
2
Representation of Process Scheduling Schedulers
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.13 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.14 Silberschatz, Galvin and Gagne ©2005
Best performance:
Balance between I/O-bound and CPU-bound processes
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.15 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.16 Silberschatz, Galvin and Gagne ©2005
• When CPU switches to another process, the system must • Parent process creates children processes, which, in turn create
+ save the state of the old process and other processes, forming a tree of processes (system call).
+ load the saved state for the new process.
• Resource sharing
• Context-switch time is overhead Goal: Preventing system overloading
+ the system does no useful work while switching + Parent and children share all resources.
+ Children share subset of parent’s resources.
• Time dependent on hardware support (from 1µs to 1ms). + Parent and child share no resources.
+ E.g., consider an architecture having multiple sets of registers.
• Execution
+ Parent and children execute concurrently.
+ Parent waits until children terminate.
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.17 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.18 Silberschatz, Galvin and Gagne ©2005
3
Process Creation (Cont.) Process Creation (Cont.)
• Address space
+ Child duplicate of parent
+ Child has a program loaded into it
• UNIX examples
+ pid - process identifier
+ fork - system call creates new process,
holding a copy of the memory space of the parent process
+ exec - system call used after a fork
to replace the process’ memory space with a new program
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.19 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.20 Silberschatz, Galvin and Gagne ©2005
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed!\n");
exit(-1);
} else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
} else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete\n");
exit(0);
}
return 0; Solaris OS
}
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.21 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.22 Silberschatz, Galvin and Gagne ©2005
• Process executes last statement (exit) and • Independent process cannot affect or be affected by the
asks the operating system to execution of another process (no data sharing).
+ Output data from child to parent (parent issues a wait).
+ Process’ resources are deallocated • Cooperating process can affect or be affected by the execution of
(released) by operating system. another process
• Parent may terminate execution of children processes (abort). • Advantages of process cooperation
+ Child has exceeded allocated resources + Information sharing
+ Task assigned to child is no longer required ̶ Also enables fault-tolerance, e.g., N-version redundancy
+ Parent is exiting. + Computation speed-up
̶ Operating system does not allow child to continue if its + Modularity
parent terminates. + Convenience
̶ Note, this depends on the type of coupling that is supported.
̶ All children terminated - cascading termination
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.23 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.24 Silberschatz, Galvin and Gagne ©2005
4
Producer-Consumer Problem Interprocess Communication (IPC)
• Paradigm for cooperating processes, producer process produces • Mechanism for processes to communicate
information that is consumed by a consumer process. and to synchronize their actions.
+ unbounded-buffer places no practical limit on the size of the • Message system – processes communicate with each other
buffer. without resorting to shared variables.
+ bounded-buffer assumes that there is a fixed buffer size. • IPC facility provides two operations:
+ send(message) – message size fixed or variable
• Variables in, out are initialized to 0 + receive(message)
+ n is the number of slots in buffer • If P and Q wish to communicate, they need to:
+ in points to the next free position in the buffer + establish a communication link between them
+ out points to the first full position in the buffer + exchange messages via send/receive
+ Buffer is empty when in = out • Implementation of communication link
+ Buffer is full when in + 1 mod n = out + physical (e.g., shared memory, hardware bus)
̶ Buffer is a circular array + logical (e.g., logical properties)
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.25 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.27 Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.28 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.29 Silberschatz, Galvin and Gagne ©2005
• Processes must name each other explicitly: • Messages are directed and received from mailboxes
+ send (P, message) – send a message to process P (also referred to as ports)
+ receive(Q, message) – receive a message from process Q + Each mailbox has a unique id
• Properties of communication link + Processes can communicate only if they share a mailbox
+ Links are established automatically
+ A link is associated with exactly one pair of communicating • Properties of communication link
processes + Link established only if processes share a common mailbox
+ Between each pair there exists exactly one link + A link may be associated with many processes
+ The link may be unidirectional, but is usually bi-directional + Each pair of processes may share several communication links
+ Link may be unidirectional or bi-directional (usually)
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.30 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.31 Silberschatz, Galvin and Gagne ©2005
5
Indirect Communication (Cont.) Indirect Communication
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.32 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.33 Silberschatz, Galvin and Gagne ©2005
Synchronization Buffering
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.34 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.35 Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.36 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.37 Silberschatz, Galvin and Gagne ©2005
6
Remote Procedure Calls Marshalling Parameters
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.38 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.39 Silberschatz, Galvin and Gagne ©2005
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.40 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.41 Silberschatz, Galvin and Gagne ©2005
7
User/Kernel Threads Multithreading Models
• User threads
+ Thread management done by user-level threads library
(POSIX Pthreads, Win32 threads, Java threads)
Many-to-one
Many-to-many
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.44 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.45 Silberschatz, Galvin and Gagne ©2005
• Terminating a thread before it has finished • Signals are used in UNIX systems to notify a process that a
particular event has occurred
• Two general approaches:
+ Asynchronous cancellation terminates the target thread • A signal handler is used to process signals
immediately 1. Signal is generated by particular event
+ Deferred cancellation allows the target thread to periodically 2. Signal is delivered to a process
check if it should be cancelled 3. Signal is handled
• Options:
+ Deliver the signal to the thread to which the signal applies
+ Deliver the signal to every thread in the process
+ Deliver the signal to certain threads in the process
+ Assign a specific thread to receive all signals for the process
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.46 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.47 Silberschatz, Galvin and Gagne ©2005
• Exercises:
+ 3.1 to 3.6
+ 3.7-3.11 (implementation)
̶ Project: UNIX shell
+ 4.1 to 4.8
+ 4.8-4.12 (implementation)
̶ Project: Matrix multiplication using threads
TDDI12, A. Bednarski, IDA, Linköpings universitet 4.48 Silberschatz, Galvin and Gagne ©2005 TDDI12, A. Bednarski, IDA, Linköpings universitet 4.49 Silberschatz, Galvin and Gagne ©2005