You are on page 1of 5

6/11/2014

Wisdom from Texas Instruments

“Unless you change the process, why would


you expect the results to change”

Process Management

Process Process in Memory

• A program in execution
– We should know about processes by now.
• A process is an instance of a program running
• How does the OS correctly run multiple
processes concurrently?
– What kind of information to be kept?
– What does the OS have to do in order to run
processes correctly.

Process State Diagram of 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
– ready: The process is waiting to be assigned to a
processor
– terminated: The process has finished execution

1
6/11/2014

Process Control Block (PCB) Process Control Block (PCB)

Information associated with each process


• Process state
• Program counter
• CPU registers
• CPU scheduling information
• Memory-management information
• Accounting information
• I/O status information

Multiprogramming Time sharing

• Early computers ran one process at a time • Method of operation in which multiple users
• A multiprogramming operating system is one with different programs interact nearly
that allows end-users to run more than one simultaneously with the central processing
program at a time. unit of a large-scale digital computer.
• Load several independent processes into
memory and switch the CPU from one job to
another when the first becomes blocked while
waiting for servicing by another device.

Process Scheduling
Pros and Cons

• Pros • Maximize CPU use, quickly switch processes onto


– Provide advantage of quick response. CPU for time sharing
– Avoids duplication of software.
• Process scheduler selects among available processes
– Reduces CPU idle time.
for next execution on CPU
• Cons • Maintains scheduling queues of processes
– Problem of reliability.
– Job queue – set of all processes in the system
– Question of security and integrity of user programs and
– Ready queue – set of all processes residing in main
data.
memory, ready and waiting to execute
– Problem of data communication.
– Device queues – set of processes waiting for an I/O device
– Processes migrate among the various queues

2
6/11/2014

Schedulers Process Creation

• Long-term scheduler (or job scheduler) – • Parent process create children processes, which, in turn
create other processes, forming a tree of processes
selects which processes should be brought
into the ready queue • Generally, process identified and managed via a process
identifier (pid)
• Short-term scheduler (or CPU scheduler) –
• Resource sharing
selects which process should be executed next
– Parent and children share all resources
and allocates CPU – Children share subset of parent’s resources
– Sometimes the only scheduler in a system – Parent and child share no resources

• Execution
– Parent and children execute concurrently
– Parent waits until children terminate

Process Creation How To Create New Processes?

• Address space • Underlying mechanism


– Child duplicate of parent – A process runs fork to create a child process
– Child has a program loaded into it – Parent and children execute concurrently
– Child process is a duplicate of the parent process
• UNIX examples parent
– fork system call creates new process
fork()
– exec system call used after a fork to replace the
process’ memory space with a new program
child

16

Process Creation Process Creation

 After a fork, both parent and child keep running, and each can
fork off other processes.
 A process tree results. The root of the tree is a special process
created by the OS during startup.
 A process can choose to wait for
children to terminate. For
example, if C issued a wait()
system call, it would block until
G finished.

3
6/11/2014

Bootstrapping
Fork System Call

• When a computer is switched on or reset, there must • Current process split into 2 processes: parent, child
be an initial program that gets the system running
fork()
• This is the bootstrap program  Returns -1 if unsuccessful
– Initialize CPU registers, device controllers, memory
– Load the OS into memory  Returns 0 in the child Stack Stack
– Start the OS running
 Returns the child’s
• OS starts the first process (such as “init”) identifier in the parent ret = 0

• OS waits for some event to occur Data Data


– Hardware interrupts or software interrupts (traps) Text ret = xxx Text

Fork System Call State Transition on wait and exit Calls

• The child process inherits from parent


– identical copy of memory
– CPU registers
– all files that have been opened by the parent

• Execution proceeds concurrently with the


instruction following the fork system call

• The execution context (PCB) for the child process is a


copy of the parent’s context at the time of the call

Other useful system calls: getpid, getppid Fork Example 1

int main()
{
• getpid returns the identifier of the calling pid_t pid;
process. Example call (pid is an integer): int x = 1;

pid = getpid(); pid = fork();


if (pid != 0) {
• getppid returns the identifier of the parent. printf(“parent: x = %d\n”, --x);
exit(0);
pid = getpid(); } else {
printf(“child: x = %d\n”, ++x);
exit(0);
}
}

4
6/11/2014

Fork Example 3
Fork Example 2

• Key Points
– Both parent and child can continue forking void fork3() Bye
{ L2 Bye
printf("L0\n"); Bye
void fork2() Bye
{ fork(); L1 L2 Bye
L1 Bye
printf("L0\n"); printf("L1\n"); Bye
fork(); Bye L2 Bye
L0 L1 Bye
fork();
printf("L1\n"); Bye
fork(); printf("L2\n");
L0 L1 L2 Bye
printf("Bye\n"); fork();
} printf("Bye\n");
}

Fork Example 4 Fork Example 5

void fork4() void fork5()


{ {
printf("L0\n"); printf("L0\n");
if (fork() != 0) { if (fork() == 0) {
printf("L1\n"); printf("L1\n");
if (fork() != 0) { if (fork() == 0) {
printf("L2\n"); printf("L2\n");
fork(); fork();
} }
} }
printf("Bye\n"); printf("Bye\n");
} }

Summary

• Fork
– Creates a duplicate of the calling process
– The result is two processes: parent and child
– Both continue executing from the same point on
• Exit
– Orderly program termination
– Unblocks waiting parent
• Wait
– Used by parent
– Waits for child to finish execution

You might also like