Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Operating System Concepts

Lecture -3
The Process

Process a program in execution;

A process includes:

Text section: contains the program code

program counter : the current activity

Stack : contains temporary data, such as function parameters, return address, local variables

data section: global variables

Heap: memory allocated dynamically during process run time

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 (such as an I/O completion or reception
of a signal)

Ready: The process is waiting to be assigned to a processor

Terminated: The process has finished execution

Fig: Diagram of a process state

Process Control Block (PCB)


Process state: The process state may be new, ready, running, waiting, halted and so on.
Program counter: The counter indicates the address of the next instruction to be executed for this
process.
CPU registers: The register vary in number and type, depending on the computer architecture.
CPU scheduling information: This information includes a process priority, pointers to scheduling
queues and any other scheduling parameters.
Memory-management information: It includes value of the base and limit registers, the page tables
or the segment tables, depending on the memory system used by the OS.
Accounting information: This information includes the amount of CPU and real time used, time
limits, account numbers, job or process numbers.
I/O status information: The information includes the list of I/O devices allocated to the process, a
list of open files, and so on.

CPU Switch from Process to Process

Fig: CPU switch from one process to process

Process 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
Device queues set of processes waiting for an I/O device
Processes migrate among the various queues

Representation of Process Scheduling

Fig: Queuing-diagram representation of process scheduling.

Schedulers
A process migrates among the various scheduling queues throughout its lifetime.
Scheduler selects processes from different queues.
Long-term scheduler (or job scheduler) selects which processes should be brought into
memory for execution.
Short-term scheduler (or CPU scheduler) selects which process should be executed
next and allocates CPU
- Sometime the only scheduler in the system
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


Medium-term scheduler can be added if degree of multiple programming needs to decrease
Remove process from memory, store on disk, bring back in from disk to continue
execution: swapping

Fig: Addition of medium-term scheduling to the queuing diagram

Context Switch
When CPU switches one process to another process, the system must save the state of the old process
and load the saved state for the new process via a context switch. When context switch occurs the
kernel saves the context of the old process in its PCB.
Context of a process represented in the PCB
Context-switch time is overhead; the system does no useful work while switching
The more complex the OS and the PCB -> longer the context switch
Time dependent on hardware support
Some hardware provides multiple sets of registers per CPU -> multiple contexts loaded at
once

Process Creation
A process may create several new processes, via a create-process system call, during execution.
The creating process is called parent process.
The new processes are called the children process.
Each process in turn creates other processes, forming a tree of processes.
Generally, process identified and managed via a unique process identifier (pid)
When a process creates a sub process, it also needs resources.
Resource sharing options

Parent and children share all resources


Children share subset of parents resources prevents any process from overloading the system.
Parent and child share no resources children obtain resources directly from system
Execution options
Parent and children execute concurrently
Parent waits until children terminate

Fig: Process creation using fork() system call


The address space of the new process may
Duplicate of the parent process.
The child process has a new 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

Process Termination
A process terminate, when it finishes executing its final statement
It asks the OS for deleting itself by exit() system call
At this point, the process may return the status value to the parent via wait() system call
All the resources used by the child process will be deallocated
Some process may terminate child process by TerminateProcess() system call
A process may terminate the execution of its child process for variety of reason
Child has exceeded allocated resources
Task assigned to child is no longer required
If parent is exiting
Some operating systems do not allow child to continue if its parent terminates
All children terminated - cascading termination

If no parent waiting, then terminated process is a zombie


If parent terminated, processes are orphans

Inter-process Communication
Processes within a system may be independent or cooperating
Cooperating process can affect or be affected by other processes, including sharing data
Reasons for cooperating processes:
Information sharing
Computation speedup
Modularity
Convenience
Two models of IPC
Shared memory
Message passing

Fig: Communications modes. a) Message Passing b) Shared Memory


Difference between Message passing and shared memory:
Message passing is useful for exchanging, smaller amounts of data, because no conflicts need be
avoided. Message passing is also easier to implement than is shared memory for inter computer
communication. Shared memory allows maximum speed and convenience of communication. Shared
memory is faster than message passing, as message passing systems are typically implemented using
system calls and require more time-consuming task of kernel intervention. In contrast, in shared
memory systems, system calls are required only to establish shared-memory regions.

You might also like