Sie sind auf Seite 1von 29

Operating

Systems
Courtesy Dr Mansoor
What is a process?
ƒ Process – a program in execution; process
execution must progress in sequential
fashion.
ƒ A process consists of:
ƒ Code (text) section
ƒ Data section
ƒ Stack
ƒ Heap
ƒ Environment
ƒ CPU state (program counter, etc.)
ƒ Process control block (PCB)
CPU and I/O Bound
Processes
Processes can be:
ƒ I/O-bound process – spends more time doing
I/O than computations, many short CPU
bursts.
I/O Burst CPU Burst I/O Burst CPU Burst

ƒ CPU-bound process – spends more time


doing computations; few very long CPU
bursts.
CPU Burst I/O CPU Burst I/O
Process States
As a process executes, it changes state
ƒ new: The process is being created.
ƒ ready: The process is waiting to be
assigned to a processor.
ƒ running: Instructions are being executed.
ƒ waiting: The process is waiting for some
event to occur.
ƒ terminated: The process has finished
execution.
Process States
Process Control Block
(PCB)
Process information and attributes
ƒ Process state
ƒ Program counter
ƒ CPU registers
ƒ CPU scheduling information
ƒ Memory-management information
ƒ Accounting information
ƒ I/O status information
ƒ Per process file table
ƒ Process ID (PID)
ƒ Parent PID, etc.
Process Control Block
(PCB)
CPU Switch From
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 I/O devices.
ƒ Process migration between the various
queues.
Queues in the OS
Queues in a Computer
System
Schedulers
ƒ A process migrates between various
scheduling queues throughout its life
cycle. OS must select processes
from these queues in some
predefined fashion. This selection is
done by an appropriate scheduler.
ƒ Scheduling is a matter of managing
queues to minimize queuing delay
and to optimize performance in a
queuing environment.
Long Term Scheduler
ƒ Long-term scheduler (or job scheduler) –
selects processes from the job pool to be
brought into the ready queue.
ƒ Long-term scheduler is invoked very
infrequently (seconds, minutes) ⇒ (may be
slow).
ƒ The long-term scheduler controls the degree
of multiprogramming.
ƒ More processes, smaller percentage of time
each process is executed
Short Term Scheduler
ƒ Short-term scheduler (or CPU scheduler) –
selects which process should be executed
next and allocates it the CPU through the
dispatcher.
ƒ Short-term scheduler is invoked very frequently
(milliseconds) ⇒ (must be fast).
ƒ Invoked when following events occur
ƒ CPU slice of the current process finishes
ƒ Current process needs to wait for an event
ƒ Clock interrupt
ƒ I/O interrupt
ƒ System call
ƒ Signal
Medium Term
Scheduler
ƒ Also known as swapper
ƒ Selects an in-memory process and swaps
it out to the disk temporarily
ƒ Swapping decision is based on several
factors
ƒ Arrival of a higher priority process but no
memory available
ƒ Poor mix of jobs
ƒ Memory request of a process cannot be met
Addition of Medium
Term Scheduling
Context Switch
ƒ When CPU switches to another process, the
system must save the state (context) of the
‘current’ (old) process and load the saved state
for the new process.
ƒ Context-switch time is overhead; the system
does no useful work while switching.
ƒ Scheduler has nothing to do with a process
switch. It is mainly the job of Dispatcher. The
time it takes for a dispatcher to stop one
process and start another is known as dispatch
latency.
Process Creation
ƒ Parent process create children processes,
which, in turn create other processes,
forming a tree of processes.
ƒ Once the OS decides to create a process
it proceeds as follows:
ƒ Assigns a unique PID to the new process.
ƒ Allocate space.
ƒ Initialize the PCB for that process.
ƒ Set appropriate linkages.
ƒ Create or expand other data structures.
Process Creation …
ƒ Resource sharing
ƒ Parent and children share all resources.
ƒ Children share a subset of parent’s resources.
ƒ Parent and child share no resources.
ƒ Execution
ƒ Parent and children execute concurrently.
ƒ Parent waits until children terminate.
ƒ Address Space
ƒ Child duplicate of parent.
ƒ Child has a program loaded onto it.
Process Creation …
ƒ UNIX examples
ƒ fork system call creates a new process
ƒ exec system call used after a fork to replace the
process’ memory image with a new executable.
ƒ Reasons for Process Creation
ƒ In batch environment a process is created in response
to the submission of a job.
ƒ In interactive environment a process is created when a
new user attempts to log on.
ƒ OS can create a process to perform a function on
behalf of a user program. (e.g. printing)
ƒ Spawning.
Processes Tree on a
UNIX System
Process Termination
ƒ A process terminates when it finishes
executing its last statement and requests
the operating system to terminate it using
the exit system call.
ƒ Output data from child to parent (via wait).
ƒ Process resources are de allocated by the
operating system, to be recycled later.
Process Termination …
ƒ Parent may terminate execution of
children processes (abort).
ƒ Child has exceeded allocated resources
(main memory, execution time, etc.).
ƒ Parent needs to create another child but has
reached its maximum children limit
ƒ Task performed by the child is no longer
required.
ƒ Parent exits.
ƒ Operating system does not allow child to continue
if its parent terminates.
ƒ Cascaded termination
Process Termination …
ƒ A process may terminate due to following
reasons:
ƒ Normal completion.
ƒ Mathematical error.
ƒ I/O failure.
ƒ Bounds violation.
ƒ Protection error.
ƒ Cascading termination.
ƒ Operator intervention.
Process Management
in UNIX/Linux
Important process-related UNIX/Linux
system calls
ƒ fork
ƒ wait
ƒ exec
ƒ exit
fork()
ƒ When the fork system call is executed, a new
process is created which consists of a copy of
the address space of the parent.
ƒ An exact copy of the parent program is created.
ƒ This mechanism allows the parent process to
communicate easily with the child process.

SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
fork() ...
ƒ The return code for fork is zero for the
child process and the process identifier of
child is returned to the parent process.
ƒ On success, both processes continue
execution at the instruction after the fork
call.
ƒ On failure, -1 is returned to the parent
process and errno is set appropriately to
indicate the reason of failure; no child is
created
fork()—Sample Code
main()
{
int pid;
Parent Process pid = 1234

...
pid = fork();
if (pid == 0) {
/* Code for child */ Child Process pid = 0
...
}
else {
/* Code for parent */
...
}
... Kernel Space
}
Using fork() system call
int main()
{
int cpid;
cpid = fork();
if (cpid == -1)
{
printf (“Fork failed\n”);
exit (1)’
}
If (cpid == 0)
printf (“\n Hello I am child \n”);
else
printf (“\n Hello I am child \n”);
}

Das könnte Ihnen auch gefallen