Process Concept Minsoo Ryu Real-Time Computing and Communications Lab. Hanyang University msryu@hanyang.ac.kr
Topics Covered Process Concept Definition, states, PCB Process Scheduling Scheduling queues, schedulers, context switch Operations on Processes Creation, process tree, termination Cooperating Processes Producer/consumer model Interprocess Communication 2 2
Process Concept
Process Concept Process: a program in execution A process includes Program code Current activity Program counter and processor s registers Stack (temporary data) Subroutine parameters, return address, temporary variables Data section Global variables 4 4
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 (I/O completion, signal reception). ready: The process is waiting to be assigned to a process. terminated: The process has finished execution. 5 5
Diagram of Process State 6 6
Process Control Block (PCB) PCB includes Information associated with each process Process state Program counter CPU registers CPU scheduling information Priority, pointers to scheduling queues, etc Memory-management information Values of base and limit registers, page tables, segment tables Accounting information CPU and real-time used, time limits, process numbers, etc I/O status information List of allocated I/O devices, list of open files 7 7
Process Control Block (PCB) 8 8
CPU Switch 9 9
u Area and proc Structure (Unix) proc structure (visible at all times) Process state Identification (PID, UID) Location of kernel address map for the u area Scheduling parameters. u area (visible only when the process is running) Extension of proc structure A pointer to the proc structure System call parameters File descriptors for all open files Internal I/O parameters. 10 10
Process Address Space (Unix) 11 11
Process Scheduling
Job queue Process Scheduling Queues Set of all processes in the system Ready queue Set of all processes that are ready and waiting to execute while residing in main memory Linked list A ready queue header contains pointers to the first and last PCBs Device queues Set of processes waiting for an I/O device Process migration between the various queues 13 13
Ready Queue And I/O Device Queues 14 14
Representation of Process Scheduling 15 15
Context Switch 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 Context-switch time is overhead The system does no useful work while switching Time dependent on hardware support. Some processors (DECSYSTEM-20) provide multiple sets of registers A context switch simply includes changing the pointer the current register set 16 16
Operations on Processes
Process Creation (1) Parent process creates children processes Forming a tree of processes Resource allocation Child process can obtain its resources from the OS Child process can share resources with its parent Two possibilities of parent process execution Parent continues to execute concurrently Parent waits until child the process terminates 18 18
Address space Process Creation (2) Child process has a copy of the address space of its parent Child process has a program loaded into its address space UNIX fork system call creates new process Returning zero for the new child Returning the non-zero process identifier to the parent exec system call used after a fork to replace the process memory space with a new program 19 19
Process Creation (3) int main() { int chpid; // Child PID int stat; // Used by parent wait pid_t thischpid; struct command_t command; // Create a process to execute the command if((chpid = fork()) == 0) { // This is the child execv(command.name, command.argv); } } thischpid = wait(&stat); exit(0); 20 20
Swapper and Init Processes Process 0 (swapper) The ancestor of all processes Scheduling (controlling the number of active processes) Process 1 (init ) The first user process when the system boots The ancestor of all user processes When a process terminates, it has any active child, they become orphans and are inherited by init Process 2 (pagedaemon) Maintains a pool of free memory 21 21
Process Tree on a UNIX System 22 22
Normal termination Process Termination Executes last statement and asks the operating system to decide it (exit) Process resources are deallocated by operating system Abnormal termination A process can terminate another process (SIGKILL) 23 23
Cooperating Processes
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 Modularity Convenience 25 25
Producer-Consumer Problem Paradigm for cooperating processes producer process produces information that is consumed by a consumer process unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size 26 26
Bounded Buffer Shared buffer #define BUFFER_SIZE 5 typedef struct {... } item; item buffer[buffer_size]; int in = 0; int out = 0; in: points to the next free position out: points to the first full position in == out: empty ((in+1)%buffer_size) == out: full 27 27
Example of Bounded Buffer Producer Consumer item nextproduced; item nextconsumed; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextproduced; in = (in + 1) % BUFFER_SIZE; } while (1) { while (in == out) ; /* do nothing */ nextconsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; } Why (in + 1) % BUFFER_SIZE? 28 28
Example of Bounded Buffer Producer Consumer item nextproduced; item nextconsumed; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextproduced; in = (in + 1) % BUFFER_SIZE; } while (1) { while (in == out) ; /* do nothing */ nextconsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; } The expression may say that the buffer is empty when in == 5 29 29
Interprocess Communication (IPC) Mechanism for processes to communicate and to synchronize their actions IPC is can be performed by shared memory or message passing IPC based on message passing Two types of operations send and receive Communication link logical implementation (e.g., logical properties) physical implementation (e.g., shared memory, hardware bus) 30 30
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 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 31 31
Indirect Communication Messages are directed and received from mailboxes Each mailbox has a unique id Processes can communicate only if they share a mailbox Properties of 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 32 32
Operations Indirect Communication create a new mailbox send and receive messages through mailbox destroy a mailbox Primitives are defined as: send(a, message) send a message to mailbox A receive(a, message) receive a message from mailbox A 33 33
Synchronization Message passing may be either blocking or nonblocking Blocking is considered synchronous Non-blocking is considered asynchronous send and receive primitives may be either blocking or non-blocking 34 34
Buffering Queue of messages attached to the link; implemented in one of three ways 1. Zero capacity 0 messages Sender must wait for receiver (rendezvous) 2. Bounded capacity finite length of n messages Sender must wait if link full 3. Unbounded capacity infinite length Sender never waits 35 35