Title: Chapter 4 Processes
1Chapter 4 Processes
2Contents
- Process Concept
- Process Scheduling
- Operations on Processes
- Cooperating Processes
- Interprocess Communication
- Communication in Client-Server Systems
3a brief history
- early computer systems allowed only one program
to be executed at a time - modern computer systems allow multiple programs
to be loaded into memory and to be executed
concurrently - firmer control and more compartmentalization of
programs in execution
4why concurrent programs
- multiple user programs
- more productive
- more responsive (time sharing system, TSS)
- various system tasks outside the kernel
- for kernel safety, simplicity, ease of design,
etc.
54.1 Process Concept
- jobs in batch system
- user programs or tasks in a time-shared system
- system programs such as memory management,
network I/O, file systems, etc.
64.1.1 The Process
- a process is a program in execution
- a process contains
- text section, or code area
- program counter (PC)
- stack (user stack, kernel stack), contains method
parameters, return addresses, local variables - stack pointer (SP)
- data section
- registers
7process .vs. program
- compare program, file, source file, object file,
executable file - a program is a passive entity, such as a file on
disk - a process is an active entity, with a program
counter (PC) specifying the next instruction to
execute and a set of associated resources - many processes may be associated with the same
program - one process may be associated with multiple files
(executable, DLLs) - one process may spawns may processes
84.1.2 Process State
- as a process executes, it changes state
- each process may be in one of the following
states - new being created
- running instructions are being executed
- waiting waiting for some event to occur
- ready waiting to be assigned to a processor
- terminated has finished execution
- different OS may have different terminologies
and/or a little bit different meanings
9Diagram of process state
- new being created
- running instructions are being executed
- waiting waiting for some event to occur
- ready waiting to be assigned to a processor
- terminated has finished execution
104.1.3 Process Control Block
- each process is represented in the OS by a
process control block (PCB), also called a task
control block - the PCB contains many pieces of information
associated with the process - the PCB resides in the kernel space
11Contents of PCB
- Process state
- Program counter
- CPU registers
- CPU scheduling information
- Memory-management information
- Accounting information
- I/O status information
12CPU switches from process to process
processes switch overhead
scheduling dispatch
134.1.4 Threads
- conventional process contains a single thread of
execution - modern OS support processes that contain multiple
threads of execution in a single process - eg. a word processor
- one thread accepts user key strokes,
- another does format and pagination,
- and still another runs spell checker,
- and even another runs background printing
- and
144.2 Process Scheduling
- objective of multiprogramming
- have some process running at all times, so as to
maximize CPU utilization - objective of time-sharing
- switch CPU among processes so frequently that
users can interact with his program - process scheduling which process should be
running?
154.2.1 Scheduling 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 - generally stored as a linked list
- device queues set of processes waiting for a
particular I/O device, eg. a tape driver, a disk - each device has its own device queue
16Ready Queue and Various I/O Device Queues
17Representation of Process Scheduling
- process migrates among the various queues
184.2.2 Schedulers
- long-term scheduler (or job scheduler) selects
which processes should be loaded for execution - short-term scheduler (or CPU scheduler) selects
which process should be executed next and
allocates CPU
19Schedulers
- short-term scheduler is invoked very frequently
(milliseconds) ? (must be fast) - long-term scheduler is invoked very infrequently
(seconds, minutes) ? (may be slow) - The long-term scheduler controls the degree of
multiprogramming - processes can be described as either
- I/O-bound process spends more time doing I/O
than computations, many short CPU bursts - CPU-bound process spends more time doing
computations few very long CPU bursts - long-term scheduler must make a careful selection
of processes mix
20Medium-term scheduler
- swapping remove a process from memory (swap
out) to reduce its active contention for the CPU
and save memory, and reintroduce the process into
memory to continue its execution (swap in) at a
later time
214.2.3 Context Switch
- the context of a process is represented in the
PCB of the process - when CPU switches to another process, the system
must save the state of the old process into its
PCB and load the saved state for the new process - context-switch time is overhead, because the
system does no useful work while switching - context-switch time dependent on hardware support
- memory speed, number of registers, special
instructions support, multiple register sets,
memory management scheme, etc.
224.3 Operations on Processes
- creation
- scheduling dispatch
- termination
234.3.1 Process Creation
- parent process create children processes, which,
in turn create other processes, forming a tree of
processes
24parent and children processes
- Resource sharing
- CPU time, memory, files, I/O devices
- parent and children share all resources
- children share subset of parents resources
- parent should partition its resources among its
children - parent and child share no resources
- Execution
- parent and children execute concurrently
- parent waits until children terminate
- Address space
- child is a duplicate of the parent process (UNIX)
- child has a (new) program loaded into it (DEC
VMS) - Windows NT support both modes
25Unix examples
- UNIX examples
- fork system call creates new process
- exec system call used after a fork to replace the
process memory space with a new program - execl, execle, execlp
- execv, execve, execvp
26fork()???????(????)
27C program forking a process
- include ltstdio.hgt
- void main( )
- int pid
- pid fork( ) // fork another process
- if (pid lt 0) // parent process, an error
occurred - printf(fork failed\n)
- exit(-1)
- else if (pid gt 0) // parent process
- wait(NULL) // wait for the child to
complete - printf(child completed\n)
- exit(0)
- else // pid0, child process
- execlp(/bin/ls, ls, NULL)
-
284.3.2 Process Termination
- process executes last statement and asks the OS
to delete it (via exit, explicitly or
inexplicitly) - output data from child to parent (via wait)
- process resources are deallocated by operating
system (memory, files, I/O buffers, etc.) - a process (usually the parent) may terminate
execution of another (a child) process, for
possibly one of the reasons (abort) - child has exceeded resources previous allocated
- the task assigned to the child is no longer
required - parent is exiting, some OS do not allow child to
continue if its parent terminates, usually
referred to as cascading termination
29process termination example
- In UNIX system
- a process can terminate itself by calling exit( )
- a parent process may wait for childs termination
by using the wait( ) system call - the wait( ) returns the child process identifier,
so that the parent can tell which of its possibly
many children has terminated - if the parent terminates, all its children have
assigned as their new parent the init process,
and continue to run (rather than being terminated)
304.4 Cooperating Processes
- concurrent processes executing in the OS may be
either independent or cooperating processes - independent process cannot affect or be affected
by the execution of another process - a process that does not share any data with any
other processes - cooperating process can affect or be affected by
the execution of another process - a process that shares data with other processes
31Advantages of process cooperation
- Information sharing
- a shared file to be accessed by several users who
are interested in it - Computation speed-up
- break a particular task into subtasks and
executes them in parallel (multiple CPUs or I/O
channels) - Modularity
- construct the system in a modular fashion,
dividing the system into separate processes or
threads - Convenience
- an individual user may have many tasks to work at
one time, e.g. a user may be editing, printing,
compiling in parallel
32Producer-Consumer Problem
- Paradigm for cooperating processes a producer
process produces information that is consumed by
a consumer process - e.g. a print program produces characters that are
consumed by the printer driver, a compiler
produce assembly code which is consumed by an
assembler - a buffer of items that is filled by the producer
and emptied by the consumer - if the buffer is empty, the consumer must wait
until an item is produced - unbounded-buffer places no practical limit on the
size of the buffer - bounded-buffer assumes that there is a fixed
buffer size gt the producer must wait if the
buffer is full
33unbounded-buffer (unrealistic)
typical
in
out
occupied
in6 out2
free
0 1 2 3 4 5 6 7 8 9 10
out
empty
in
in6 out6
inout
0 1 2 3 4 5 6 7 8 9 10
consumer while (outin) // wait info
bufferout out out1 // consuming
info
producer // produce something
bufferin something in in1
34bounded-buffer shared-memory solution
- shared data
- define BUFFER_SIZE 10
- typedef struct
- . . .
- item
- item bufferBUFFER_SIZE
- int in 0
- int out 0
- the shared buffer is implemented as a circular
array, with two pointers in and out - buffer is empty when inout
- buffer is full when ((in1)BUFFER_SIZE)out
- solution is correct
- but can only use BUFFER_SIZE-1 elements
35bounded-buffer representation
typical
1
2
empty
inout
full
(in1)out
36bounded-buffer the producer
- item nextProduced // local variable
- while (1)
- / produce an item in nextProduced /
- while (((in 1) BUFFER_SIZE) out)
- / buffer is full, do nothing /
- bufferin nextProduced
- in (in 1) BUFFER_SIZE
-
37bounded-buffer the consumer
- item nextConsumed // local variable
- while (1)
- while (in out) // buffer is empty
- / do nothing /
- nextConsumed bufferout
- out (out 1) BUFFER_SIZE
- / consume the item in nextConsumed /
-
384.5 Interprocess Communication
- mechanism for processes to communicate and to
synchronize their actions - shared-memory environment, code written
explicitly by the application programmer - alternatively, the OS provide IPC facility for
cooperating processes to communicate with each
other - IPC provides a way to allow processes to
communicate and to synchronize their actions
without sharing the same address space - particular useful in a distributed environment
394.5.1 Message-Passing System
- message system processes communicate with each
other without resorting to shared
variables/data/memory - services are provided as ordinary user processes,
i.e. the services operate outside of the kernel - cf. microkernels, messages passed within the
kernel - two basic operations
- send(message), message length fixed or variable
- receive(message)
40communication link
- if P and Q wish to communicate, they need to
- establish a communication link between them
- exchange messages via send and receive
- implementation of communication link
- physical (e.g. shared memory, hardware bus,
network) - logical (logical properties)
- in Linux pipes, sockets, semaphore, shared
memory, message queue
414.5.2 Links implementation
- direct or indirect communication
- symmetric or asymmetric communication
- automatic or explicit buffering
- send by copy or send by reference
- fixed-size or variable-size message
424.5.2.1 Direct Communication
- processes must name each other explicitly
- send (P, message) send a message to process P
- receive(Q, message) receive a message from
process Q - properties of the communication link
- a link is established automatically
- a link is associated with exactly two
communicating processes - exactly one link exists between each pair
- the link may be unidirectional, but is usually
bi-directional
43symmetric .vs. asymmetric
- symmetric communication
- send (P, message)
- receive(Q, message)
- asymmetric communication
- send(P, message)
- receive(id, message), receive a message from any
process, where id is the name of the process with
which communication has taken place - disadvantage both schemes have the limited
modularity, changing the name of a process may
necessitate examining all other process
definitions
444.5.2.2 Indirect Communication
- messages are sent to and received from mailboxes,
or ports - each mailbox has a unique identification
- processes can communicate only if they share a
mailbox - primitives are defined as
- send(A, message) send a message to mailbox A
- receive(A, message) receive a message from
mailbox A
45Indirect Communication
- properties of the communication link
- a link is established between two processes only
if both share a common mailbox - a link may be associated with more than two
processes - each pair of processes may share several
communication links - link may be unidirectional or bi-directional
46Indirect Communication
- mailbox sharing
- P1 , P2 , and P3 share mailbox A
- P1 sends, while P2 and P3 receive
- Who gets the message?
- solutions
- allow a link to be associated with at most two
processes - allow only one process at a time to execute a
receive operation - allow the system to select arbitrarily the
receiver, and sender is notified who the receiver
was
47ownership of the mailbox
- owned by the process who creates it
- resides in the owners address space
- the owner can only receive message from it
- the user can only send message to it
- mailbox disappears while the owner terminates
- owned by the OS
- OS must provide a mechanism that allows a process
to - create a new mailbox
- send and receive messages through that mailbox
- delete a mailbox
- ownership may be passed or shared by using system
call
484.5.3 Synchronization
- blocking (synchronous) versusnonblocking
(asynchronous) - blocking send the sending process is blocked
until the message is received by the receiving
process or by the mailbox - nonblocking send the sending process sends the
message, and resumes operation - blocking receive the receiver blocks until a
message is available - nonblocking receive the receiver retrieves
either a valid message or a null, and resumes
operation - different combinations of send and receive are
possible
494.5.4 Buffering
- direct or indirect, messages reside in a
temporary queue - three possible queue implementations
- zero capacity the queue has maximum length 0
the sender must block until the recipient
receives the message - bounded capacity the queue has finite length n
the sender blocks when the queue is full - unbounded capacity the sender never blocks
50Case study Mach Win 2000
- 4.5.5 Mach
- 4.5.6 Windows 2000
514.6 client-server communication
- a process on one machine needs to access data on
another machine - three ways
- Sockets
- Remote Procedure Calls (RPC)
- Remote Method Invocation (Java)
524.6.1 Sockets
- a socket is defined as an endpoint for
communication - a socket is a concatenation of an IP address and
a port number - The socket 161.25.19.81625 refers to port 1625
on host 161.25.19.8 - communication consists between a pair of sockets
- connections are all unique
53socket connection
54socket communication
- two kind of services
- TCP, connection-oriented service
- UDP, connectionless service
- socket communication is considered as a low-level
form of communication - support only an unstructured byte stream
- common and efficient, anyway
554.6.2 Remote Procedure Calls
- remote procedure call (RPC) abstracts procedure
calls between processes on networked systems - stub client-side proxy for the actual procedure
on the server - the client-side stub locates the server and
marshalls the parameters - the server-side stub receives this message,
unpacks the marshalled parameters, and performs
the procedure on the server
56Considerations in RPC
- external data representation (XDR)
- big-endian versus small-endian
- semantics of RPC
- carried out exactly once (no lost, no duplicate)
- servers port binding
- cf. standard library call, binding takes place
during link, load, or execution time - predetermined, fixed port address
- determined while compiling and linking
- rendezvous mechanism, a matchmaker daemon
57Execution of RPC matchmaker
584.6.3 Remote Method Invocation
- Remote Method Invocation (RMI) is a Java
mechanism similar to RPCs - RMI allows a Java program on one machine to
invoke a method on a remote object
59Marshalling Parameters
604.7 Summary
- a process is a program in execution
- as a process executes, it changes state
- ready, running, waiting,
- a process is represented by its PCB
- process queues
- ready queue, I/O request queue,
- long-term (job) and short-term (CPU) scheduling
61Summary
- concurrent processes
- independent processes
- cooperating processes
- e.g. producer consumer
- processes communication
- shared memory
- message passing
62Homework
- paper
- 1, 4, 6
- oral
- 5
- lab
- inter-process communication mechanism on Linux