Sunteți pe pagina 1din 3

A Five-State Process Model

UNIX Process Model


(Review)

n The not-running state in the two-state


model has now been split into a ready
state and a blocked state
Running currently being executed
Ready prepared to execute
Blocked waiting for some event to
occur (for an I/O operation to complete, or
a resource to become available, etc.)
New just been created
Exit just been terminated

n State transition diagram:


dispatch
admit release
new ready running exit
timeout

event
occurs event
wait

blocked
Figure from Operating Systems, 2nd edition, Stallings, Prentice Hall, 1995
Original diagram from The Design of the UNIX Operating System, M.
Bach, Prentice Hall, 1986
1 Fall 1998, Lecture 06 2 Fall 1998, Lecture 06

UNIX Process Model


Process Creation in UNIX
(cont.)

n Start in Created, go to either: n One process can create another process,


Ready to Run, in Memory
perhaps to do some work for it

or Ready to Run, Swapped (Out) if there The original process is called the parent
isnt room in memory for the new process The new process is called the child
Ready to Run, in Memory is basically The child is an (almost) identical copy of
same state as Preempted (dotted line) parent (same code, same data, etc.)
n Preempted means process was returning
The parent can either wait for the child to
to user mode, but the kernel switched to
another process instead complete, or continue executing in
parallel (concurrently) with the child
n When scheduled, go to either:
n In UNIX, a process creates a child
User Running (if in user mode) process using the system call fork( )
or Kernel Running (if in kernel mode) In child process, fork( ) returns 0
Go from U.R. to K.R. via system call In parent process, fork( ) returns process
id of new child
n Go to Asleep in Memory when waiting
for some event, to RtRiM when it occurs n Child often uses exec( ) to start another
completely different program
n Go to Sleep, Swapped if swapped out
3 Fall 1998, Lecture 06 4 Fall 1998, Lecture 06
Example of UNIX Process Creation Context Switching

#include <sys/types.h> n Stopping one process and starting


#include <stdio.h>
another is called a context switch
int a = 6; /* global (external) variable */
When the OS stops a process, it stores
int main(void) the hardware registers (PC, SP, etc.) and
{ any other state information in that
int b; /* local variable */ process PCB
pid_t pid; /* process id */
When OS is ready to execute a waiting
b = 88; process, it loads the hardware registers
printf("..before fork\n"); (PC, SP, etc.) with the values stored in
pid = fork(); the new process PCB, and restores any
if (pid == 0) { /* child */ other state information
a++; b++;
} else /* parent */ Performing a context switch is a relatively
wait(pid); expensive operation
n However, time-sharing systems may do
printf("..after fork, a = %d, b = %d\n", a, b); 1001000 context switches a second
exit(0);
} n Why so often?
n Why not more often?
aegis> fork
..before fork
..after fork, a = 7, b = 89
..after fork, a = 6, b = 88
5 Fall 1998, Lecture 06 6 Fall 1998, Lecture 06

Schedulers Schedulers (cont.)

n Long-term scheduler (job scheduler) n Short-term scheduler (CPU scheduler)


Selects job from spooled jobs, and loads Executes frequently, about one hundred
it into memory times per second (every 10ms)
Executes infrequently, maybe only when Runs whenever:
process leaves system n Process is created or terminated
Controls degree of multiprogramming n Process switches from running to blocked
n Goal: good mix of CPU-bound and I/O- n Interrupt occurs
bound processes Selects process from those that are ready
Doesnt really exist on most modern time- to execute, allocates CPU to that process
sharing systems Goals:
n Minimize response time (e.g., program
n Medium-term scheduler execution, character to screen)
On time-sharing systems, does some of n Minimize variance of average response
time predictability may be important
what long-term scheduler used to do
n Maximize throughput
May swap processes out of memory Minimize overhead (OS overhead, context
temporarily switching, etc.)
Efficient use of resources
May suspend and resume processes
n Fairness share CPU in an equitable
Goal: balance load for better throughput fashion
7 Fall 1998, Lecture 06 8 Fall 1998, Lecture 06
Ready Queue and
Various I/O Device Queues

From Operating System Concepts, Silberschatz & Galvin., Addison-Wesley, 1994

n OS organizes all waiting processes (their


PCBs, actually) into a number of queues
Queue for ready processes
Queue for processes waiting on each
device (e.g., mouse) or type of event
9
(e.g., message) Fall 1998, Lecture 06

S-ar putea să vă placă și