Professional Documents
Culture Documents
QA - 3 PT 1
QA - 3 PT 1
• In multiprogramming OS, only one program could be active at any instant on the CPU.
• 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.
• 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 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
• Job scheduler (long-term scheduler)- select processes to be brought into ready queue.
• CPU scheduler (short-term scheduler)- select a process for the execution on CPU.
• 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 Time dependent on hardware support (for example: takes less time if all
registers could be saved altogether rather than individually)
• At process creation:
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.
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
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)