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

• What is process?

o A process is a program in execution; process execution must progress in


sequential fashion

o A process includes: program counter, stack, data section

• In multiprogramming OS, only one program could be active at any instant on the CPU.

• Principal events that cause process creation:

o system initialization (process creation by the init system process)

o User request to create a new process

o Initiation of a batch job

• Process termination. Conditions which terminate processes:

o Voluntary: normal exit, error exit

o Involuntary: fatal error, killed by another process

• Process group in UNUX: parent create a child process, child processes create their own
processes

• Windows has no concept of process hierarchy: all processes are created equal.

• Process states: new, running, waiting, ready & terminated.

o Running: the process is assigned to a CPU; process instructions are being


executed

o Waiting: the process is waiting for some event to occur

o Ready: the process is waiting to be assigned to a processor.

• A process state:

o can transit from new to ready, ready to running, running to terminated, running
to waiting, waiting to ready, running to ready.

o cannot transit from new to running, waiting to running, ready to waiting.

• PCB (process control block) :

o Can hold information such as: Process state, process id, Program counter, CPU
registers, CPU scheduling information, Memory-limits, list of open files, I/O
status information

o Cannot contain program data

• Process scheduling queues:


o Job queue (new processes), ready queue (processes with ready state), device
queues (processes waiting for an I/O device).

o Process migrates between the various queues

• Job scheduler (long-term scheduler)- select processes to be brought into ready queue.

o invoked very infrequently, may be slow

o controls the degree of multiprogramming (nb of processes in memory)

• CPU scheduler (short-term scheduler)- select a process for the execution on CPU.

o invoked very frequently, must be fast

• Medium-term scheduler- swaps processes in and out to free memory to other


processes.

• I/O-bound process- does more I/O than computations (use many short CPU bursts)

• CPU-bound process- does more computations than I/O (use few long CPU bursts)

• Switching CPU from one process to another is called context switching. It consist of
saving the state of the old process and loading the saved state for the new process.

o Context-switch time is overhead; the system does no useful work while


switching.

o Time dependent on hardware support (for example: takes less time if all
registers could be saved altogether rather than individually)

• At process creation:

o Child duplicate address space of parent

o Child may share all, or subset of, or none of parent's resources

o Parent and children execute concurrently

o Parent waits until children terminate

o In UNIX: fork() system call creates new process; exec() system call used after a
fork to replace the process’ memory space with a new program

o After the call pid=fork(), parent gets the value of pid, whereas child gets 0 for
pid.

• For process termination:

o Child executes last statement then exits (calls exit() system call); it could pass
output data (integer value) to parent through the exit system call. Parent gets
the output data via wait

o Parent may terminate execution of children processes (abort):


▪ Child has exceeded allocated resources.

▪ Task assigned to child is no longer required.

▪ Parent is exiting - Cascading termination may happen to terminate all


children

Exercise-1 what is the number of child process created by the following C program?
#include <stdio.h>
main (int argc, char *argv [])
{
int i, pid;
(1) for (i = 0, i < 2, i++) {
(2) pid = fork ();
}
}
Answer: 3 child processes.

Exercise-2 what is the output of parent & child for the following C program?

#include <stdio.h>
int R = 2;
main (int argc, char *argv [])
{
int pid = fork();
if (pid == 0) {
R = R * 5; (1)
printf ("R = %d\n", R); (*)
} else {
R = R * 2; (1)
wait (NULL);
printf ("R = %d\n", R); (**)
exit(0);
}
}

Answer:

Parent's output: R = 4

Child's output: R = 10
Exercise-3 what is the output of parent & child for the following C program?

#include <stdio.h>
int R = 3, pid;
main (int argc, char *argv [])
{
pid = fork(); if (pid < 0) exit (-1);
if (pid > 0) {
wait (&R);
R = R * 2; (1)
printf ("R = %d\n", R); (**)
exit(0);
} else {
R = R * 2; (1)
printf ("R = %d\n", R);
exit (R);
}
}

Answer:

Parent's output: R = 12

Child's output: R = 6

Exercise
• What happens when executing the
following C code, supposing no errors int main() {
in any fork call ?
int pid1, pid2;
• Draw the resulting process tree,
and illustrate which process is a pid1 = fork();
parent and which is a child. pid2 = fork();
}
• Answer: (see course slides)

You might also like