* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Contents
1. Introduction
2. Computer-System Structures 3. Operating-System Structures 4. Processes
5. Threads
6. CPU Scheduling
7. Process Synchronization 8. Deadlocks
9. Memory Management 10. Virtual Memory 11. File Systems
Chapter 4 Processes
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Processes
Objective:
Process Concept & Definitions
Process Classification:
Operating system processes executing system code
User processes executing system code
User processes executing user code
Processes
Example: Special Processes in Unix
PID 0 – Swapper (i.e., the scheduler)
Kernel process
No program on disks correspond to this process
PID 1 – init responsible for bringing up a Unix system after the kernel has been
bootstrapped. (/etc/rc* & init or /sbin/rc* & init)
User process with superuser privileges
PID 2 - pagedaemon responsible for paging
Kernel process
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Processes
Process
A Basic Unit of Work from the Viewpoint of OS
Types:
Sequential processes: an activity resulted from the execution of a program by a processor
Multi-thread processes
An Active Entity
Program Code – A Passive Entity
Stack and Data Segments
The Current Activity
PC, Registers , Contents in the Stack and Data Segments
Processes
Process State
new
ready
waiting
terminated
running admitted
interrupt
scheduled
exit
I/O or event wait
I/O or event completion
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Processes
Process Control Block (PCB)
Process State
Program Counter
CPU Registers
CPU Scheduling Information
Memory Management Information
Accounting Information
I/O Status Information
Processes
PCB: The repository for any information that may vary from process to process
pointer process state
pc register 0
1 2 PCB[]
NPROC-1
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Processes
Process Control Block (PCB) – An Unix Example
proc[i]
Everything the system must know when the process is swapped out.
pid, priority, state, timer counters, etc.
.u
Things the system should know when process is running
signal disposition, statistics accounting, files[], etc.
Processes
Example: 4.3BSD
text structure
proc[i]
entry
page
table Code Segment
Data Segment
PC heap
user stack argv, argc,…
sp .u
per-process kernel stack
p_textp x_caddr
p_p0br
u_proc p_addr
Red
Zone
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Processes
Example: 4.4BSD
proc[i]
entry
process grp
…
file descrptors
VM space region lists
page
table Code Segment
Data Segment heap user stack argv, argc,…
.u
per-process kernel stack
p_p0br
u_proc p_addr
Process Scheduling
The goal of multiprogramming
Maximize CPU/resource utilization!
The goal of time sharing
Allow each user to interact with his/her program!
PCB1 PCB2
head tail head
tail head
tail
PCB3 ready
queue
disk
unit 0
tape
unit 1
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Process Scheduling – A Queueing Diagram
ready queue dispatch
CPU
I/O I/O queue I/O request
time slice expired
fork a child
wait for an interrupt interrupt occurs
child executes child terminate
Process Scheduling – Schedulers
Long-Term (/Job) Scheduler
Goal: Select a good mix of I/O-bound and CPU-bound process
Remarks:
1. Control the degree of multiprogramming
2. Can take more time in selecting process because of a longer interval between executions
3. May not exist physically CPU
Memory
Job pool
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Process Scheduling – Schedulers
Short-Term (/CPU) Acheduler
Goal:Efficiently allocate the CPU to one of the ready processes
according to some criteria.
Mid-Term Scheduler
Swap processes in and out memory to control the degree of multiprogramming
Process Scheduling – Context Switches
Context Switch ~ Pure Overheads
Save the state of the old process and load the state of the newly scheduled process.
The context of a process is usually reflected in PCB and others, e.g., .u in Unix.
Issues:
The cost depends on hardware support
e.g. processes with multiple register sets or computers with advanced memory management.
Threads, i.e., light-weight process (LWP), are
introduced to break this bottleneck!
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Operations on Processes
Process Creation & Termination
Restrictions on resource usage
Concurrent execution
root
pagedaemon swapper init
user1 user2 user3
Operations on Processes
Process Duplication
A copy of parent address space + context is made for child, except the returned value from fork():
Child returns with a value 0
Parent returns with process id of child
No shared data structures between parent and child communicate via shared files, pipes, etc.
Use execve() to load a new program
fork() vs vfork() (Unix)
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Operations on Processes
Example:
…
if ( pid = fork() ) == 0) { /* child process */
execlp(“/bin/ls”, “ls”, NULL);
} else if (pid < 0) {
fprintf(stderr, “Fork Failed”);
exit(-1);
} else {
/* parent process */
wait(NULL);
}
Operations on Processes
Termination of Child Processes
Reasons:
Resource usages, needs, etc.
Cascading Termination
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Cooperating Processes
Cooperating processes can affect or be affected by the other processes
Independent Processes
Reasons:
Information Sharing
Computation Speedup
Modularity
Convenience
Cooperating Processes
A Consumer-Producer Example:
Bounded buffer or unbounded buffer
Supported by inter-process
communication (IPC) or by hand coding
z 0
1 2
n-1 n-2
in out
buffer[0…n-1]
Initially,
in=out=0;
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Cooperating Processes
Producer:
while (1) {
/* produce an item nextp */
while (((in+1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[ in ] = nextp;
in = (in+1) % BUFFER_SIZE;
}
Cooperating Processes
Consumer:
while (1) {
while (in = out)
; /* do nothing */
nextc = buffer[ out ];
out = (out+1) % BUFFER_SIZE ; /* consume the item in nextc */
}
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Interprocess Communication
Why Inter-Process Communication (IPC)?
Exchanging of Data and Control Information!
Why Process Synchronization?
Protect critical sections!
Ensure the order of executions!
Interprocess Communication
IPC
Shared Memory
Message Passing
Logical Implementation of Message Passing
Fixed/variable msg size, direct/indirect communication,
automatic/explicit buffering, send by
copy or reference, etc.
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Interprocess Communication
Classification of Communication by Naming
Processes must have a way to refer to each other!
Types
Direct Communication
Indirect Communication
Interprocess Communication –
Direct Communication
Process must explicit name the
recipient or sender of a communication
Send(P, msg), Receive(Q, msg)
Properties of a Link:
a. Communication links are established automatically.
b. Two processes per a link
c. One link per a pair of processes
d. Bidirectional or unidirectional
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Interprocess Communication –
Direct Communication
Issue in Addressing:
Symmetric or asymmetric addressing receive(id, msg)
Difficulty:
Process naming vs modularity
Interprocess Communication – Indirect Communication
Two processes can communicate only if the process share a mailbox (or ports)
Properties:
1. A link is established between a pair of processes only if they share a mailbox.
2. n processes per link for n >= 1.
3. n links can exist for a pair of processes for n >=1 .
4. Bidirectional or unidirectional A A
send(A, msg)=> =>receive(A, msg)
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Interprocess Communication – Indirect Communication
Issues:
a. Who is the recipient of a msg?
b. Owners vs Users
Process Æ owner as the sole recipient?
OS Æ Let the creator be the owner?
Privileges can be passed?
Garbage collection is needed?
P1
mgs ?
P2 P3
Interprocess Communication – Synchronization
Blocking or Nonblocking
(Synchronous versus Asynchronous)
Blocking send
Nonblocking send
Blocking receive
Nonblocking receive
Rendezvous – blocking send &
receive
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Interprocess Communication – Buffering
The Capacity of a Link = the # of messages could be held in the link.
Zero capacity(no buffering)
Msg transfer must be synchronized – rendezvous!
Bounded capacity
Sender can continue execution without waiting till the link is full
Unbounded capacity
Sender is never delayed!
The last two items are for asynchronous
communication and may need acknowledgement
Interprocess Communication –
Buffering
Special cases:
a. Msgs may be lost if the receiver can not catch up with msg sending Æ synchronization
b. Senders are blocked until the receivers have received msgs and replied by reply msgs
Æ A Remote Procedure Call (RPC)
framework
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Interprocess Communication – Exception Conditions
Process termination
a. Sender TerminationÆ Notify or terminate the receiver!
b. Receiver Termination
a. No capacity Æ server is blocked b. BufferingÆ msgs are accumulated
Interprocess Communication – Exception Conditions
Ways to Recover Lost Messages (due to hardware or network failure):
OS detects & resends msgs
Sender detects & resends msgs
OS detects & notify sender to handle it.
Issues:
a. Detecting methods, such as timeout!
b. Distinguish multiple copies if retransmitting is possible
Scrambled Messages:
Usually OS adds checksums, such as CRC, inside
messages & resend them as necessary!
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Example - Mach
Mach – A message-based OS from the Carnegie Mellon University
When a task is created, two special mailboxes, called ports, are also created.
The kernel mailbox is used by the kernel to communication with the tasks
The Notify mailbox is used by the kernel sends notification of event occurrences.
Example - Mach
Three system calls for message transfer:
msg_send:
Options when mailbox is full:
a. Wait indefinitely b. Return immediately c. Wait at most for n ms d. Temporarily cache a mag.
a. A cached message per mailbox
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Example - Mach
msg_receive
To receive from a mailbox or a set of mailboxs. Only one task can own &
have a receiving privilege of it
* options when mailbox is empty:
a. Wait indefinitely b. Return immediately c. Wait at most for n ms
msg_rpc
Remote Procedure Calls
Example - Mach
port_allocate
create a mailbox (owner)
port_status ~ .e.g, # of msgs in a link
All messages have the same priority and are served in a FIFO fashion.
Message Size
A fixed-length head + a variable-length data + two mailbox names
Message copying: message copying Æ remapping of addressing space
System calls are carried out by messages.
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Example – Windows 2000
Local Procedure Call (LPC) – Message Passing on the Same Processor
1. The client opens a handle to a subsystem’s connection port object.
2. The client sends a connection request.
3. The server creates two private
communication ports, and returns the handle to one of them to the client.
4. The client and server use the corresponding port handle to send messages or callbacks and to listen for replies.
Example – Windows 2000
Three Types of Message Passing Techniques
Small messages
Large messages – section object
To avoid memory copy
For sending and receiving
A callback mechanism
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Communication in Client- Server Systems
Socket
An endpoint for communication identified by an IP address concatenated with a port number
A client-server architecture
Socket
146.86.5.2:1652
Socket
146.86.5.2:1652
Socket
161.25.19.8:80
Socket
161.25.19.8:80
Web server Host X
Communication in Client- Server Systems
Three types of sockets in Java
Connection-oriented (TCP) – Socket class
Connectionless (UDP) – DatagramSocket class
MulticastSocket class
sock = new ServerSocket(5155);
…
client = sock.accept();
pout = new PrintWriter(client.getOutputStream(), true);
…
pout.close();
client.close();
sock = new Socket(“127.0.0.1”,5155);
…
in = sock.getInputStream();
bin = new BufferReader(new InputStreamReader(in));
…
sock.close();
Server Client
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Communication in Client- Server Systems
Remote Procedure Call (RPC)
A way to abstract the procedure-call mechanism for use between systems with network connection.
Needs:
Ports to listen from the RPC daemon site and to return results, identifiers of functions to call, parameters to pack, etc.
Stubs at the client site
One for each RPC
Locate the proper port and marshall parameters.
Communication in Client- Server Systems
Needs (continued)
Stubs at the server site
Receive the message
Invoke the procedure and return the results.
Issues for RPC
Data representation
External Data Representation (XDR)
Parameter marshalling
Semantics of a call
History of all messages processed
Binding of the client and server port
Matchmaker – a rendezvous mechanism
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.
Communication in Client- Server Systems
Client Messages Server
Call kernel to send RPC msg to Procedure X
Kernel sends msg to matchmaker
Kernel places port P in usr RPC msg
Kernel sends RPC
Kernel receives reply and passes to user
Port: matchaker Re: addr. to X
Port: kernel Re: port P to X
Port: P
<contents>
Port: kernel
<output>
matchmaker receives msg
matchmaker replies to client with port P
daemon listen to port P and receives msg
daemon processes request and sends output
Communication in Client- Server Systems
An Example for RPC
A Distributed File System
A set of RPC daemons and clients
DFS port on a server on which a fiel operation is to take place
Disk operations: read, write, delete,
status, etc
* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2002.