Operating Systems Part III: Process Management (Process States and Transitions)

30
Operating Systems Part III: Process Management (Process States and Transitions)

Transcript of Operating Systems Part III: Process Management (Process States and Transitions)

Operating Systems

Part III: Process Management (Process States and Transitions)

Process Definition

Definition (informal): A process is a program in execution

Referred to as a job in batch systems and user program / task in time-shared systems

More broadly: all CPU activities (includes user programs, internal O/S activities, etc.)

Process States

Processes are executed sequentially Process States:

– New: process is being created– Running: instructions are being executed– Waiting: waiting for an event (i.e. I/O completion)– Ready: waiting to be assigned to a processor– Terminated: process has finished execution

Process States

new

waiting

running

ready

terminated

admitted

interrupt

exit

scheduler dispatch

I/O or event waitI/O or event completion

Process Control Block

Each process is represented by a process control block (PCB)

Information contained in PCB:– Process state– Program counter: address of next instruction– CPU registers: accumulators, index registers, stack

pointers -- must be saved when interrupt occurs

Process Control Block

Information in PCB (continued)– CPU scheduling information - process priority &

scheduling parameters– Memory-management information - page tables, etc.– Accounting information - amount of CPU USED– I/O status information - list of I/O devices allocated

to this process, list of open files, etc.

Process Scheduling

Processes are put in a job queue (consists of all processes)

Processes residing in main memory (ready and waiting) are kept in ready queue

Uni-processor (only one running process at a time); multiprocessor (# of processes running depend on # of processors)

Process Scheduling

Long-term scheduler (a.k.a. job scheduler) - selects processes from mass storage device and loads into main memory for execution

Short-term scheduler (a.k.a. CPU scheduler) - selects from ready to execute processes and allocates CPU to one of them

Process Scheduling

CPU-bound process - spends more time doing computation

I/O-bound process - spends more time doing I/O

Medium-term scheduler - removes some processes in main memory and then later reintroduced to continue execution --> also called swapping

Process Scheduling

Job Queue(New)

Secondary Memory

Main Memory

Ready Queue(Ready) CPU

(Running)

long - term / job scheduler short -

term / CPU scheduler

I/O Queue(Waiting)

Portion of Virtual Memory

medium - term scheduler / swapper

I/O

I/O complete

I/O request

interruptExit

(Terminated)

Process Scheduling

Context switch – CPU switching from one process to another– Purely overhead because system does no useful

work– Value of registers must be copied to PCB– Speed ranges vary from machine to machine (but

typically 1 to 1000 microseconds)

Operation in Processes

Process creation– Created via a create-process system call– Creating process is the parent process and the new

process is the child process– Forms a tree of processes

Operation in Processes

Process creation (continued)– Two possibilities (execution)

Parent continues to run concurrently w/ children Parent waits until some/all children have finished

– Two possibilities (address space) Child has duplicate of parent address space Child has program loaded into parent address space

Operation in Processes

Process creation (continued)– In Unix, new process is created using the fork

system call Parent continues execution Copy of address space is created

– Also in Unix, execve is similar to fork except Parent “dies” Memory space replaced by child

Operation in Processes

Process termination– Normally terminates w/ exit system call (Unix)– All resources are deallocated by O/S– Other circumstances

Parent terminates child (i.e. abort)– Child exceeded usage of resources– Child is no longer required– Parent is exiting, O/S does not allow child to exist without

parent Process killed by other processes (user’s own)

Cooperating Processes

Independent process - cannot affect or be affected by other processes (does not share any data)

Dependent process - if it can affect or be affected by other processes (shares data with other processes)

Cooperating Processes

Reasons for sharing data w/ other processes– Information sharing– Computation speedup - break complex task into

subtasks– Modularity - divide a system into separate

processes– Convenience - work on many tasks at the same time

(editing/printing/compiling)

Cooperating Processes

An illustration– Producer-consumer problem: producer produces

information for the consumer to consume (i.e. print program and printer device)

– Unbounded-buffer -> no limit on size of buffer but consumer has to wait for new items

– Bounded-buffer -> fixed buffer (consumer waits if buffer is empty, producer if full)

Threads

Allows sharing of resources among peers Also called a lightweight process (LWP) Has a program counter, register set, and stack

space Shares with peer threads its code section, data

section, O/S resources (collectively known as a task)

Threads

Traditional task (heavyweight process) has exactly one thread

Threads makes CPU switching among peers less expensive than among heavyweight processes (no memory mgt.)

Threads

Similar to processes since a thread– has states (ready, running, blocked, terminated)– can create child threads– shares CPU

Threads

Unlike a process, threads– are not independent of each other– can access every address in the task– have no protection from other threads since threads

are meant to assist and not be hostile to one another

Threads are gaining ground because they execute more efficiently

Interprocess Communication

Cooperating processes communicate through a shared-memory environment

Another means is through an interprocess-communication (IPC) facility– Achieved through a messaging system

Function of message system is to allow process communication without resorting to shared variables

Interprocess Communication

IPC provides at least two basic functions:– send(message)– receive(message)

A communication link must exist between two or more processes

Unidirectional link – can either send or receive but NOT BOTH and each link has at least one receiver process connected to it

Interprocess Communication

Processes refer to each other through names Name referencing is used in direct and indirect

communication Direct communication

– send(process_name,message)– receive(process_name,message)– The link is exactly between two processes and

usually bi-directional (sometimes unidirectional)

Interprocess Communication

Direct communication (continued)– Example:

Producer-consumer problem: when producer finishes, it sends to the consumer

– Symmetric – sender and receiver indicate process name

– Asymmetric – only the sender specifies the process name; receiver can receive message from any process

– Disadvantage: limited modularity (e.g. when process changes name)

Interprocess Communication

Indirect communication– Messages sent through a mailbox

send(mailbox_name,message) receive(mailbox_name,message)

– Link is established between processes ONLY through a shared mailbox

– Mailbox may be owned by either the process or the system– Mailboxes owned by the process are destroyed when process

terminates

Interprocess Communication

Buffering– Link capacity determines number of messages that

can reside in it temporarily– Zero capacity

No messages can wait in buffer Sender waits until recipient receives Must be synchronized to transfer a message (rendezvous)

Interprocess Communication

Buffering (continued)– Bounded capacity

With finite length n Sender waits if buffer is full

– Unbounded capacity Sender never delays

Communication in Client-Server Systems

Socket– An endpoint for communication– A link consists of two sockets

RPC (Remote Procedure Call)– Allows a local process to invoke a procedure on a

remote host as if accessing a local procedure

RMI (Remote Method Invocation)– The Java implementation of RPC