Session 5 : Process Types & Management Part I
Processes:Types-Management– I
Q?what is Algorithm:
/>a logical procedure in defined steps solves a problem
Q?what is program
/>formal expression of algorithm by means of programming language
/>programming language needs run-time support
Q?What is Process Concept
/>a sequence of operation performed by a program while executing
on a given set of input data.
/>a process includes : program counter – stack - data section
Q?What is sequential process
/>its state is not shared in any way by any other process.
/>it is a deterministic process
i.e. the result of execution depends only on the input
state.
/> its execution is reproducable,
given the set of input data ,it always produces the same
output independently from start execution time , the
execution speed and the number of
active processes on the same system.
/>its execution can be stopped and restarted
without causing ill effects.
Q?What is concurrent process
/>its state is shared along other processes
/>its execution is nondeterministics:
i.e. the result of the execution depends on relative execution
sequence and cannot be predicted in advance.
/>its execution is irreproducible
: i.e. the result of the execution will not always be the same for
the same input.
Q?what is precedence graphs
The precedenc graph is a directed acylic graph
whose nodes correspond to individual statements. It shows
the order in which the activities can execute.
precedence graph of sequential process
S0 → S1 → S2 → S3 → S4 …... → Sn+1
precedence graph of concurrent process
S0
|
.------------------------------------------------.
| | | |
S1 S2 S3 . . . Sn
| | | |
`------------------------------------------------'
|
Sn+1
Q?Process State and Process State Diagram
/>As process executes, it changes state.
The State of the process means the current
activity at which the process is running.
The process can exist in only one of the 5 possible states.
/1>New : the process is being created
/2>Ready : the process is waiting to be assigned to a processor.
Ready processes are processor allocated to them by the operating
system so that they can run.
/3>Running : the process is currently being executed
/4>Waiting : the process is waiting for some events to occur after
the completion of the I/O operations
/5>Terminated : the process has finished execution
interruption
|------------------------------|
NEW ----->READY RUNNING -->TERMINATED
^ |------------------------------| | | Scheduler dispatch | |-------------WAITING<----------------|i/o Or event wait
i/o or event completion
Q?What is process control block, PCB
/>PCB is the datastructure used by the OS to represent each process in OS. It
contains important piece of information associated with the process.
Pointer
|
It
points to another process control block. It is used for
maintianing scheduling list.
|
Process
State
|
May
be new,ready,running,waiting or terminated
|
Program
Counter
|
It
indicated the address of the
next instruction to be executed for this process.
|
CPU
Registers
|
This
include general purpose
register,stack register
and type of register totally dependent
upon computer architecture
|
Memory
management information
|
The
information include the value of base and limit
registers, the page
tables, or the segment
tables depending on the memory system used by the operating
system. This info is used for deallocating the memory when the
process terminates.
|
Accounting
information
|
This
info includes the amount of CPU
and real time used, time limits, job or process numbers, account
numbers etc
|
Q?What is context switching
/>CPU switch from one process to another process this is called
context switching.
/>when CPU switches to another process, the system must save
the state of the old process and load the saved state for the
new process.
/>the time devoted to context switching is overhead, i.e.
work not directly useful for any process
/>so the context switching time must be minimum.
It is dependent on HW support
Q?What are the types of Schedulers
/>Schedulers are used to do process
scheduling. We have 3 types of schedulers:
/1>Long-term scheduler : (job scheduler) this selects which
processes should be bought into the ready queue.
This is invoked very frequently so must be fast.(milliseconds)
/2>Short-term scheduler : (cpu scheduler) this selects which process
should be executed next and allocates CPU.This is invoked very
infrequently so may be slow (seconds, minutes)
/3>Medium-term scheduler :controls the degree of multiprogramming.
Q?How do we create processes
/>parent process creates childeren processes, which
in turn creates other processes, forming tree of processes
/>the parent and children can share all resources
or children can share subset of parent's resources
or the parent and child share no resources
/>for execution the parent and children execute concurrently
or the parent will wait until children terminate.
/>the address space taken by the child is either the
child is duplicate of parent or the child has a program loaded into it.
/>in unix : we use fork system call to create a new process and we use
execve system call after a fork to replace the process memory space
with a new program.
Example 1: of fork:
/*
fork0.
c
Illustrates
system call fork
*/
int
main()
{
printf("I'm
the parent, my PID is %d, my parent is process %d\n",
getpid(),
getppid());
if
(fork()
== 0){
//
branch is for the child
sleep
(5);
}
//
branch is for the parent
printf("This
sentence has been printed by process: %d my parent is process
%d\n",getpid(),
getppid());
//
who is process number 1 ??
exit(43);
//
return 0;
}
Result:
I'm
the parent, my PID is 7287, my parent is process 7286
This
sentence has been printed by process: 7287 my parent is process7286
Process
returned 0 (0x0) execution time : 0.002 s
Press
ENTER to continue.
This
sentence has been printed by process: 7288 my parent is process 1
Explaination:
//child
sleeps for 5 seconds and print out this
What is
happening here is that : the parent is terminating before the child
terminates. This leaves the child as an orphan and it gets adopted by
the root process with PID of 1.
Example 2: of Synchronous Alarm program:
/*
*
alarm.c
*
*
Simple synchronous alarm program. This is used as a
*
reference for progressive examples of asynchronous
* alarm
programs.
*/
#include
"errors.h"
int
main (int
argc, char
*argv[])
{
int
seconds;
char
line[128];
char
message[64];
while
(1) {
printf
("Alarm>
");
if
(fgets (line, sizeof (line), stdin) == NULL) exit (1);
if
(strlen (line) <= 1) continue;
/*
*
Parse input line into seconds (%d) and a message
*
(%64[^\n]), consisting of up to 64 characters
*
separated from the seconds by whitespace.
*/
if
(sscanf (line, "%d
%64[^\n]",
&seconds,
message) < 2) {
fprintf
(stderr, "Bad
command\n");
}
else
{
sleep
(seconds);
printf
("(%d)
%s\n",
seconds, message);
system("date");
}
}
}
Result:
Alarm>
23 Namaste Niranjan Khatri
(23)
Namaste Niranjan Khatri
Sat
Nov 22 11:03:13 CET 2014
Alarm>
Example 3: of Concurrent Alarm program:
/*
*
alarm_fork.c
*
* This
version of alarm.c uses fork to create a new process to
* wait
for each alarm to expire.
*/
#include
<sys/types.h>
//include
definition of the types : pid_t : used for process ids and process
//group ids
#include
<sys/wait.h>
// include
declaration for waiting
#include
"errors.h"
//
this file define the ErrorException class and the error function
int
main
(int
argc,
char
*argv[])
{
char
line[128];
int
seconds;
pid_t
pid;
char
message[64];
//
signal(SIGCHLD,SIG_IGN); // avoids zombie
children
while
(1)
{
printf
("Alarm>
");
if
(fgets (line, sizeof
(line), stdin)
== NULL)
exit
(1);
if
(strlen
(line) <= 1)
continue;
system
("date");
/*
*
Parse input line into seconds (%d) and a message
*
(%64[^\n]), consisting of up to 64 characters
*
separated from the seconds by whitespace.
*/
if
(sscanf
(line, "%d
%64[^\n]",
&seconds,
message) < 2) {
fprintf
(stderr,
"Bad
command\n");
}
else
{
pid
= fork
();
if
(pid == (pid_t)
-1)
errno_abort
("Fork");
if
(pid == (pid_t)
0) {
// If
we are in the child, wait and then print a message
sleep
(seconds);
system
("date");
printf
("(%d)
%s\n",
seconds, message);
exit
(0);
}
}
}
}
Result:
Alarm>
23 Namaste Niranjan Khatri
Sat
Nov 22 11:20:44 CET 2014
Alarm>
Sat Nov 22 11:21:07 CET 2014
(23)
Namaste Niranjan Khatri
waits and prints out
Example 4 : Generation of K children
//Generation
of K children process
//Erroneous
use of fork
//use
command "sh fork3 | sort -n -k 8 " to better see the result
#define
K 3
int
main()
{
int
i;
for(i=0;
i<K;
i++) {
fork();
printf("Body
of process %d and i = %d\n",
getpid(),
i);
}
exit(0);
}
Result
Body
of process 6431 and i = 0
Body
of process 6431 and i = 1
Body
of process 6431 and i = 2
Process
returned 0 (0x0) execution time : 0.002 s
Press
ENTER to continue.
Body
of process 6434 and i = 2
Body
of process 6433 and i = 1
Body
of process 6433 and i = 2
Body
of process 6432 and i = 0
Body
of process 6432 and i = 1
Body
of process 6432 and i = 2
Body
of process 6435 and i = 2
Body
of process 6437 and i = 2
Body
of process 6436 and i = 1
Body
of process 6436 and i = 2
Body
of process 6438 and i = 2
Note*:
depending on the situation the answer changes :
Explanation:
6431(0) → 6432 (0)
6432 (1)
6432 (2)
6431(1) → 6433 (1)→ 6435(2)
6433 (2)→ 6436(1)
6436(2)
6431(2) → 6434 (2)→ 6437(2)→ 6438(2)
Example 5 : Generation of K children
//Generation
of K children process
#define
K 3
int
main()
{
int
i;
for(i=0;
i<K;
i++) { //
for(i=0; i<K && fork(); i++)
printf("I'm
process %d i = %d %d\n",
getpid(),
i, getppid());
if(fork()
== 0)
break;
// here
we break if we find the child
}
printf("Body
of process %d with parent %d and i = %d\n",
getpid(),
getppid(),
i);
exit(0);
}
Result:
I'm
process 7368 i = 0 7367
I'm
process 7368 i = 1 7367
I'm
process 7368 i = 2 7367
Body
of process 7369 with parent 7368 and i = 0
Body
of process 7368 with parent 7367 and i = 3
Body
of process 7371 with parent 1 and i = 2
Body
of process 7370 with parent 1 and i = 1
Process
returned 0 (0x0) execution time : 0.002 s
Press
ENTER to continue.
Note*:
depending on the situation the answer changes :
Explanation:
1 → 7367→7368 → 7369
7368 7370
7368 7371
Example 6 : Demonstrate Separate Address Space
/*
fork1.c
Illustrates
the use of separate process address spaces
with
common inital
values */
int
x,y; //
global variables are not shared
int
main()
{
int
x,y;
x
= 4;
if
(fork()){ //
recall 0 in C is FALSE
y
= 5; //
parent != 0
x
= 15;
sleep(5);
}
else
y=
-5; //
child
printf("
x = %d y = %d process %d, PPID %d\n",
x,
y, getpid(),
getppid());
exit(0);
}
Result:
x = 4 y = -5 process 8136,
PPID 8135
x = 15 y = 5 process 8135,
PPID 8134
Process
returned 0 (0x0) execution time : 5.005 s
Press
ENTER to continue.
Explanation:
first child is printed then the parent
8136 → 8135
Comments
Post a Comment