Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Silberschatz, Galvin and Gagne ©2006
Operating System Principles
Chapter 3:
Processes - Concept
Mi-Jung Choi
mjchoi@kangwon.ac.kr
Dept. of Computer and Science
Ch3. Process - Concept
Chapter 3: Processes
Process Concept
Process Scheduling
Operations on Processes
Cooperating Processes
Interprocess Communication
Communication in Client-Server Systems
CSIE301 Operating System
-2-
Fall 2009
Ch3. Process - Concept
Process Concept
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
Textbook uses the terms job and process almost
interchangeably
Process – a program in execution; process execution
must progress in sequential fashion
A process includes:
program counter
stack
data section
CSIE301 Operating System
-3-
Fall 2009
Ch3. Process - Concept
Process in Memory
Current activity of a process is represented by
the value of program counter
The contents of the processor’s registers
Stack
contains temporary data
Function parameters, return address, local
variables
Data section
contains global variables
Heap
is a memory that is dynamically allocated
during process run time
Program is a passive entity.
Process is an active entity.
Two processes may be associated with the
same program.
CSIE301 Operating System
-4-
Fall 2009
Ch3. Process - Concept
Process State
As a process executes, it changes state
new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor (CPU)
terminated: The process has finished execution
The state of a process is defined in part by the current activity of that
process.
CSIE301 Operating System
-5-
Fall 2009
Ch3. Process - Concept
Diagram of Process State
It is important to realize that only one process can be running on any
processor at any instant.
Many processes may be ready and waiting states.
CSIE301 Operating System
-6-
Fall 2009
Ch3. Process - Concept
Process Control Block (PCB)
Each process is represented in the operating system by a
process control block (PCB).
Information associated with each process
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
CSIE301 Operating System
-7-
Fall 2009
Ch3. Process - Concept
Process Control Block (PCB)
Process state
New, ready, running, waiting, terminated
Program counter
The address of the next instruction to be executed
for this process
CPU registers
Accumulators, index registers, stack pointers, and
general-purpose registers, etc.
CPU scheduling information
Process priority, pointers to scheduling queue, etc.
Memory-management information
Value of base and limit registers, the page tables
or segment tables, etc.
Accounting information
The amount of CPU and real time used, time limits,
account number, job and process number
I/O status information
CSIE301 Operating System
List of I/O devices allocated, open files, etc.
-8-
Fall 2009
Ch3. Process - Concept
CPU Switch From Process to Process
CSIE301 Operating System
-9-
Fall 2009
Ch3. Process - Concept
Process Scheduling
The objective of multiprogramming is to have some
process running at all times, to maximize CPU utilization.
The objective of time sharing is to switch the CPU among
processes so frequently that users can interact with each
program while it is running.
To meet these objectives, the process scheduler selects
an available process for program execution on the CPU
For a single-processor system, there will never be more
than one running process. The rest will have to wait until
the CPU is free and can be rescheduled.
CSIE301 Operating System
- 10 -
Fall 2009
Ch3. Process - Concept
Process Scheduling Queues
Processes migrate among the various queues for
process scheduling.
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
Each device has it’s own device queue.
CSIE301 Operating System
- 11 -
Fall 2009
Ch3. Process - Concept
Ready Queue And Various I/O Device Queues
Queues are generally
implemented as
linked lists.
Queue header
contains pointers to
the first and last
PCBs in the list
Each PCB includes a
pointer to the next
PCB.
CSIE301 Operating System
- 12 -
Fall 2009
Ch3. Process - Concept
Representation of Process Scheduling
Two types of queues: one ready queue, a set of device queues
Two types of resources: CPU, I/O
Arrow indicates the flow of processes in the system
CSIE301 Operating System
- 13 -
Fall 2009
Ch3. Process - Concept
Process Scheduling
A new process is initially put in the ready queue and waits
there until it is selected for execution. (dispatched)
Once the process is allocated the CPU and is executing
until one of the following events occur:
The process issues an I/O request
The process creates a new child process
The process waits for an interrupt (timer event)
The time slice assigned is expired.
CSIE301 Operating System
- 14 -
Fall 2009
Ch3. Process - Concept
Schedulers
is a component of OS the role of which is to select a
process for execution among a set of processes in
various queues
In a batch and multiprogramming system
More processes are submitted than can be executed immediately.
These processes are spooled to a disk, where they are kept for
later execution
Long-term scheduler (or job scheduler) – selects which
processes should be brought from disk into the ready queue
Short-term scheduler (or CPU scheduler) – selects which
process should be executed next from the ready queue and
allocates CPU
CSIE301 Operating System
- 15 -
Fall 2009
Ch3. Process - Concept
Schedulers
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
The number of processes in memory
The degree of multiprogramming must be stable
The rate of process creation == the rate of process termination
The long term scheduler may need to be invoked only when a
process leaves the system.
CSIE301 Operating System
- 16 -
Fall 2009
Ch3. Process - Concept
Schedulers
It is important that the long-term scheduler make a careful
selection.
Processes can be described as either:
I/O-bound process – spends more time doing I/O than
computations, many short CPU bursts (ex, find)
CPU-bound process – spends more time doing computations; few
very long CPU bursts (ex. Sort)
It is important that the long-term scheduler selects a good
process mix of I/O-bound and CPU-bound processes.
If all processes are I/O bound, the ready queue will almost always
be empty
If all processes are CPU bound, the I/O waiting queue will almost
always be empty
CSIE301 Operating System
- 17 -
Fall 2009
Ch3. Process - Concept
Schedulers
In time-sharing system such as UNIX and Windows XP
No long-term scheduler is used.
simply put every new process in memory for the short-term
scheduler.
The stability of these systems depends on a physical limitation or
on the self-adjusting nature of human users.
In time-sharing system, a medium-term scheduler is used.
It sometimes can be advantageous to remove processes from
memory and thus reduce the degree of multiprogramming.
Later, the process can be reintroduced into memory, and its
execution can be continued.
We call it swapping
CSIE301 Operating System
- 18 -
Fall 2009
Ch3. Process - Concept
Addition of Medium Term Scheduling
CSIE301 Operating System
- 19 -
Fall 2009
Ch3. Process - Concept
Context Switch
is a task of switching CPU to another process
which requires performing a state save of the current process and
a state restore of a different process
When CPU switches to another process, the system must
save the state of the old process and load the saved state
for the new process
When an interrupt occurs, the OS saves the context of current
running process in its PCB.
Context-switch time is overhead; the system does no
useful work while switching
Time dependent on memory speed, number of registers, etc.
Typical speeds are a few millisecond.
CSIE301 Operating System
- 20 -
Fall 2009
Ch3. Process - Concept
Operations of Processes
The Processes in most system can execute concurrently, they may be created and
deleted dynamically.
Process Creation
Process Termination
CSIE301 Operating System
- 21 -
Fall 2009
Ch3. Process - Concept
Process Creation
Parent process create children processes, which, in turn
create other processes, forming a tree of processes
via create-process system call
Three possibilities in terms of resource sharing
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources, child receives resources from
OS directly.
Two possibilities in terms of execution
Parent and children execute concurrently
Parent waits until children terminate
CSIE301 Operating System
- 22 -
Fall 2009
Ch3. Process - Concept
A tree of processes on a typical Solaris
A process is identified by a
unique process identifier
(PID)
An integer number
At top of the tree
Sched process
pid =0
Process scheduling
Pageout process (2)
Memory management
Fsflush process (3)
CSIE301 Operating System
- 23 -
File system management
Fall 2009
Ch3. Process - Concept
Process Creation
Two possibilities in terms of address space
Child is a duplicate of parent;
child has same program and data as the parent
Child has a program loaded into it
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
CSIE301 Operating System
- 24 -
Fall 2009
Ch3. Process - Concept
fork() & exec()
A new process is created by the fork() system call
The new process consists of a copy of the address space
of the original process
Both processes (parent & child) continue execution at the
instruction after the fork().
The return code for the fork() is zero for the child process,
whereas the (nonzero) pid of the child process is returned
to the parent
The exec() loads a binary file into memory, destroys the
memory image of the program, and starts its execution.
CSIE301 Operating System
- 25 -
Fall 2009
Ch3. Process - Concept
C Program Forking Separate Process
int main()
{
pid_t pid;
pid = fork();
if (pid < 0)
{
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0)
{
execlp("/bin/ls", "ls", NULL);
}
else
{
/* fork another process */
/* error occurred */
/* child process */
/* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
CSIE301 Operating System
- 26 -
Fall 2009
Ch3. Process - Concept
C Program Forking Separate Process
CSIE301 Operating System
- 27 -
Fall 2009
Ch3. Process - Concept
Process Creation
Windows example
CreateProcess() function is used, which is similar to fork
creates a new child process
requires loading a specified program into the address space of the
child process at process creation
expects no fewer than ten parameters.
CSIE301 Operating System
- 28 -
Fall 2009
Ch3. Process - Concept
C Program Forking Separate Process
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess(
NULL,
“c:\\Windows\\system32\mspaint.exe”,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi )
)
CSIE301 Operating System
- 29 -
// No module name (use command line)
// program name
// Process handle not inheritable
// Thread handle not inheritable
// Set handle inheritance to FALSE
// No creation flags
// Use parent's environment block
// Use parent's starting directory
// Pointer to STARTUPINFO structure
// Pointer to PROCESS_INFORMATION structure
Fall 2009
Ch3. Process - Concept
C Program Forking Separate Process
{
printf( "CreateProcess failed (%d)\n", GetLastError() );
return;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
CSIE301 Operating System
- 30 -
Fall 2009
Ch3. Process - Concept
Process Termination
Process executes last statement and asks the operating
system to delete it (exit)
Status value returned from child to parent (via wait)
Process’ resources are deallocated by operating system
Memory, open files, I/O buffers
Parent may terminate execution of children processes
(abort) with various reason
Child has exceeded allocated resources
Task assigned to child is no longer required
If parent is exiting
Some operating system do not allow child to continue if its
parent terminates
– All children terminated - cascading termination
CSIE301 Operating System
- 31 -
Fall 2009
Ch3. Process - Concept
User
Process 1
User
Process 2
Operating System
Hardware
Cooperating Processes
The Processes executing concurrently in the operating system may be either
independent processes or cooperating processes
CSIE301 Operating System
- 32 -
Fall 2009
Ch3. Process - Concept
Cooperating Processes
Independent process cannot affect or be affected by the
execution of another process
Cooperating process can affect or be affected by the
execution of another process
Advantages of process cooperation
Information sharing
Computation speed-up; parallelism may speed up the computation
Modularity; dividing the system function into separate process
Convenience; concurrently running environment.
CSIE301 Operating System
- 33 -
Fall 2009
Ch3. Process - Concept
User
Process 1
User
Process 2
Operating System
Hardware
InterProcess Communication
Cooperating processes require an InterProcess Communication (IPC) mechanism that
will allow them to exchange data and information
CSIE301 Operating System
- 34 -
Fall 2009
Ch3. Process - Concept
Interprocess Communication (IPC)
is a mechanism for processes to communicate and to synchronize their
actions
Two fundamental models
Shared memory
Message passing
In the shared-memory model, a region of memory that is shared by
cooperating process is established.
IPC is performed by reading and writing data to the shared region
In the message-passing model, IPC takes place by means of messages
exchanged between the cooperating processes.
IPC is performed by sending and receiving data
Many system implements both.
CSIE301 Operating System
- 35 -
Fall 2009
Ch3. Process - Concept
Communications Models
(a) message-passing mechanism
CSIE301 Operating System
(b) shared-memory mechanism
- 36 -
Fall 2009
Ch3. Process - Concept
Interprocess Communication (IPC)
Message-passing
is useful for exchanging smaller amounts of data, due to no
conflicts
is easier to implement than shared memory
Shared-memory
allows maximum speed and convenience of communication
It can be done at memory speeds
is faster than message passing
Message-passing systems are typically implemented using system
calls and thus require more time consuming task of kernel intervention
Shared-memory systems requires system calls for only establishing
shred-memory region.
CSIE301 Operating System
- 37 -
Fall 2009
Ch3. Process - Concept
Shared-Memory System
Creation of shared-memory
A process creates a shared-memory segment in the address space
of the process.
Other processes attach it to their address space
Termination of shared-memory
requires the agreement from all processes to remove it.
Exchange of information
By reading and writing data in the shared areas
The form of the data and the location are determined by these process,
not OS
Processes are responsible for synchronized access.
CSIE301 Operating System
- 38 -
Fall 2009
Ch3. Process - Concept
Producer-Consumer Problem
Paradigm for cooperating processes,
producer process produces information that is consumed by a
consumer process
Example
compiler produces assembly code, consumed by assembler
Assembler produces object modules, consumed by loader
Producer-consumer problem can be solved by sharedmemory
Producer and consumer processes runs concurrently
They communicate to each other via shared-memory (buffer)
Producer produces an item and store it to the buffer
Consumer consumes an item from the buffer
The producer and consumer must be synchronized, so that the
consumer consumes an item only when available.
CSIE301 Operating System
- 39 -
Fall 2009
Ch3. Process - Concept
Producer-Consumer Problem
Two types of buffer can be used.
unbounded-buffer places no practical limit on the size of the
buffer
bounded-buffer assumes that there is a fixed buffer size
In unbounded-buffer
The consumer may have to wait for new items
The producer can always produce new items
In bounded-buffer
The consumer must wait if the buffer is empty
The producer must wait if the buffer is full
CSIE301 Operating System
- 40 -
Fall 2009
Ch3. Process - Concept
Bounded-Buffer – Shared-Memory Solution
Shared data among producer and consumer
#define BUFFER_SIZE 10
typedef struct {
int
content;
} item;
item buffer[BUFFER_SIZE];
int in
= 0;
int out
= 0;
// initial state
// empty
Shared buffer is implemented as a circular array with two
logical pointer: in and out.
can store no more than BUFFER_SIZE-1 elements
Empty when in == out
Full
when (in+1)%BUFFER_SIZE == out
CSIE301 Operating System
- 41 -
Fall 2009
Ch3. Process - Concept
Bounded-Buffer – Insert() Method
item nextProduced;
while (true)
{
/* Produce an item in nextProduced */
while ( ( (in + 1) % BUFFER_SIZE ) == out );
/* do nothing -- no free buffers */
/* insert an item into the buffer */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE ;
}
CSIE301 Operating System
- 42 -
Fall 2009
Ch3. Process - Concept
Bounded Buffer – Remove() Method
item nextConsumed;
while (true)
{
while (in == out);
// do nothing -- nothing to consume
// remove an item from the buffer
nextConsumed = buffer[out];
out = (out + 1) % BUFFER SIZE;
return nextConsumed;
}
CSIE301 Operating System
- 43 -
Fall 2009
Ch3. Process - Concept
Message-Passing System
OS provides the means for cooperating processes to
communicate with each other via message-passing
processes may resides on different computers connected by a
network
processes communicate with each other without resorting to
shared variables
Two operations:
send(message) – message size fixed or variable
receive(message)
Those are system calls initiated by cooperating processes to
communicate each other.
CSIE301 Operating System
- 44 -
Fall 2009
Ch3. Process - Concept
Message-Passing System
If processes P and Q wish to communicate, they need to:
establish a communication link between them
exchange messages via send/receive operations
Implementation of communication link
It can be done in a variety of ways
Three logical Issues in the implementation
Direct or Indirect communication
Synchronous or asynchronous communication
Autonomic or Explicit buffering
CSIE301 Operating System
- 45 -
Fall 2009
Ch3. Process - Concept
Implementation Questions
How are links established?
Can a link be associated with more than two processes?
How many links can there be between every pair of
communicating processes?
What is the capacity of a link?
Is the size of a message that the link can accommodate
fixed or variable?
Is a link unidirectional or bi-directional?
CSIE301 Operating System
- 46 -
Fall 2009
Ch3. Process - Concept
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 communication link
Links are established automatically, processes need to know only
each other’s identity
A link is associated with exactly one pair of communicating
processes
Between each pair there exists exactly one link
The link may be unidirectional, but is usually bi-directional
CSIE301 Operating System
- 47 -
Fall 2009
Ch3. Process - Concept
Direct Communication
Two addressing mode
Symmetry in addressing
Both sender and receiver must name the other to communicate
Asymmetry in addressing
Only the sender names the recipient, the recipient is not
required to name the sender.
send (P, message) – send a message to process P
receive (id, message) – receive a message from any process, id is
set to the name of the sender with which communication has taken
place
Disadvantages in direct communication (symmetry & Asymmetry)
Limited modularity of the resulting process definition
Changing the id of a process may necessitate examining all other
process definitions.
CSIE301 Operating System
- 48 -
Fall 2009
Ch3. Process - Concept
Indirect Communication
Messages are directed and received from mailboxes (also
referred to as ports)
Mailbox is an object into which messages can be placed and from
which messages can be removed.
Each mailbox has a unique id
Processes can communicate only if they share a mailbox
Properties of indirect communication link
Link established only if processes share a common mailbox
A link may be associated with many processes
Each pair of processes may share several communication links
Link may be unidirectional or bi-directional
CSIE301 Operating System
- 49 -
Fall 2009
Ch3. Process - Concept
Indirect Communication
Operations
create a new mailbox
send and receive messages through mailbox
destroy a mailbox
The owner of the mailbox
User process
The owner only receives, others only send messages.
Operating system
A mailbox is independent and is not attached to any process.
Primitives are defined as:
send (A, message) – send a message to mailbox A
receive (A, message) – receive a message from mailbox A
CSIE301 Operating System
- 50 -
Fall 2009
Ch3. Process - Concept
Indirect Communication
Mailbox sharing example
P1, P2, and P3 share mailbox A
P1, sends; P2 and P3 execute a receive() from A
Who gets the message?
Answer depends on which one below you chose.
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. Sender is
notified who the receiver was.
CSIE301 Operating System
- 51 -
Fall 2009
Ch3. Process - Concept
Synchronization
Message passing may be either blocking or non-blocking
Blocking is considered synchronous
Blocking send has the sender blocked until the message is
received
Blocking receive has the receiver blocked until a message is
available
Non-blocking is considered asynchronous
Non-blocking send has the sender send the message and continue
Non-blocking receive has the receiver receive a valid message or
null
When both send() and receive() are blocking, we have a
rendezvous between sender and receiver
CSIE301 Operating System
- 52 -
Fall 2009
Ch3. Process - Concept
Synchronization
For the producer and consumer problem.
Blocking method is desirable
Use blocking send() and blocking receive() primitives.
The producer
merely invokes the blocking send() call and
waits until the message is delivered to either the receiver or the
mailbox.
When the consumer invokes receive(),
it blocks until a message is available
CSIE301 Operating System
- 53 -
Fall 2009
Ch3. Process - Concept
Buffering
is queue of messages attached to the communication link;
Messages resides in a temporary queue during communication
Three ways of queue implemented
Zero capacity - 0 messages in the queue
Maximum length of queue is 0
Sender must wait for receiver receives message (rendezvous)
Bounded capacity – finite length of n messages
Sender must wait if link full
Unbounded capacity – infinite length
Sender never waits
CSIE301 Operating System
- 54 -
Fall 2009
Ch3. Process - Concept
Client-Server Communication
Sockets
Remote Procedure Calls
Remote Method Invocation (Java)
CSIE301 Operating System
- 55 -
Fall 2009
Ch3. Process - Concept
Sockets
A socket is defined as an endpoint for communication
Concatenation of IP address, protocol, and port
The socket 161.25.19.8:TCP:1625 refers to protocol TCP,
port 1625 on host 161.25.19.8
Communication consists between a pair of sockets
CSIE301 Operating System
- 56 -
Fall 2009
Ch3. Process - Concept
Socket Communication
CSIE301 Operating System
- 57 -
Fall 2009
Ch3. Process - Concept
Remote Procedure Calls
Remote procedure call (RPC) abstracts procedure calls
between processes on networked systems.
Stubs – 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, performs the procedure on the
server, and return the results.
CSIE301 Operating System
- 58 -
Fall 2009
Ch3. Process - Concept
Execution of RPC
CSIE301 Operating System
- 59 -
Fall 2009
Ch3. Process - Concept
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.
CSIE301 Operating System
- 60 -
Fall 2009
Ch3. Process - Concept
Remote Procedure Call
Marshalling Parameters
CSIE301 Operating System
- 61 -
Fall 2009
Ch3. Process - Concept
Summary
A process is a program in execution.
As a process executes, the process may be in one of the 5 states:
new, ready, running, waiting, terminated
Each process is represented in the OS by its PCB
Ready queue contains all the processes that are ready to execute and
are waiting for the CPU, I/O queue exists for each I/O device.
Long-term scheduler selects processes from disk and put them into
ready queue.
Short-term scheduler selects a process from ready queue and assigne
CPU
OS provides a mechanism for parent process to create a child process
Cooperating processes require an IPC mechanism to communicate
with each other.
Two types of IPC: shared-memory and message-passing
Communication in client-server system may use sockets, RPC, RMI.
CSIE301 Operating System
- 62 -
Fall 2009
Silberschatz, Galvin and Gagne ©2006
Operating System Principles
End of Chapter 3
Mi-Jung Choi
mjchoi@kangwon.ac.kr
Dept. of Computer and Science