Session 7 : Process Synchronization
Process Synchronization
“A Boy Took A Book From His Shelf To Study... All The Other Books Fell
On Him & He Died ... ... Moral : Whoever picks up the book, think
that the book picked him up ... So Be Safe ... ;->
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>:)
”
Q?what is process synchronization
/>It is the techniques to coordinate execution among processes
/>during this one process may have to wait for another process
/>shared resource(critical
section) may require the exclusive access.
Q?whatare synchronization primitives
/>these are simple software mechanism
provided by a operating system to its user for the purpose of
supporting thread or process synchronization. Mutex,
event, conditional variables and semaphores are all synchronization
primitives.
/>monitor is generally
considered a high-level synchroization tool. It is a tool which
gurantees mutual exclusion for its methods using other
synchronization primitives.
/>critical section is not a
synchronization primitive. It's a part of execution
path that must be protected from concurrent execution in order
to maintain some invariants. We need to use some synchronization
primitives to protect critical
section.
Q?whatis mutex
/>mutex provides mutual
exclusion
/>either producer or consumer can have the key(mutex)
and proceed with their work. So the other not having the key have to
wait or viceversa
/>at one point of time, only one thread can work with the entire buffer
/>mutex is something like a locking
mechanism used to synchronize
theaccess to the resources.
Q?what is semaphore
/>it is a generalized mutex.
/>sempahore is a signaling mechanism.
(I'm done you can carry on)kind of signal.
Q?fork and join primitives
/>cobegin/coend are limited to properly nested graphs
/>fork/join can express arbitrary functional parallelism(any
process flow graph)
fork:creates new process
join:joins two process
this two operations are indivisible(one
cannot be divided from another)
var
P1,
P2
:
process;
procedure
E1;
begin
C;F;
end
begin
A;
P1
:=
fork E1;
B;
P2
:=
fork
E;
D;
join
P1;
join
P2;
G;
end
Q?UNIX process synchronization
/>
/*
UNIX-process-synchronization.c
Illustrates:
system
call fork - wait
and
process state waiting
*/
#include
<sys/types.h>
#include
<sys/wait.h>
int
num_arg; //
read-only global variable
main(int
argc,
char
**
argv)
{
pid_t
childpid;
num_arg
= argc;
printf("parent
- S1 - pid = %d\n",
getpid());
if
((childpid = fork()) == -1)
perror("can't
fork\n");
if
(childpid == 0) {
printf("child:
child pid = %d, parent pid = %d\n",
getpid(), getppid());
P34();
exit(0);
}
else
{
if(num_arg
> 1) sleep(15); //
precedence to child process
printf("parent
- S2 - pid = %d\n",
getpid());
printf("parent:
child pid= %d, parent pid = %d\n",
childpid, getpid());
printf("wait
- P34 - \n");
while
(wait((int
*)
0) != childpid); /*
wait for child */
printf("parent
- S5 - pid = %d\n",
getpid());
exit(0);
}
}
P34()
{
pid_t
pid4;
printf("fork
- P34 - \n");
if
((pid4 = fork())
== -1)
perror("can't
fork\n");
if
(pid4 == 0) {
printf("-
S4 - pid = %d, parent pid = %d\n",
getpid(),
getppid());
exit(0);
} else
{
if(num_arg
> 1) sleep(15); //
precedence to child process
printf("-
S3 - pid = %d, parent pid = %d\n",
getpid(),
getppid());
while
(wait((int
*)
0) != pid4) /*
wait for child */
;
printf("end
of - P34 - \n");
exit(0);
}
}
Result:
parent- S1 - pid = 4022
parent- S2 - pid = 4022
parent:child pid= 4023, parent pid = 4022 wait- P34 -
child:child pid = 4023, parent pid = 4022
fork- P34 -
-
S3 - pid = 4023, parent pid = 4022
-S4 - pid = 4024, parent pid = 4023
end of - P34 -
parent- S5 - pid = 4022
Process returned 0 (0x0) execution time : 0.004 s
Press ENTER to continue.
Example : Sequential Process
typedef
.... T;
T
this;
int
i;
{
i:=
0;
while(i<n){
get(this,reader);
update(this);
put(this,printer);
i++;
}
}
Example: Concurrent Process
if(n>=1){
get(last,reader);
if(n>=2){
update(last);
start
printing do
put(last,printer);
get(this,reader);
for(i=2;i<n;i++){
start
reading do
get (next,reader);
update(this);
complete
printing;
last
= this;
start
printing do
put (last,printer);
complete
reading;
this
= next;
}
complete
printing;
last
:=
this;
}
update(last);
put(last,printer);
}
Q?what is cobegin/coend
/>The Cobegin/coend construct allows concurrent execution of all
program block enclosed. At cobegin all program blocks begin
concurrent execution and at coend all of them must complete execution
before the next block may beign execution.
Here there is explicit use of signal and
wait.
Q?how can I correctly use cobegin-coend
/>Ex: sum = n1 + n2 + n3 + n4
cobegin
sum1= n1+n2
sum2= n3+n4
coend
sum= sum1 +sum2
Q?What is the incorrect use of cobegin-coend
/>
Ex:
x= 100;
cobegin
x+=10;
if(x>100)
print x;
else
print x-50;
coend
Q?what are the concurrency conditions for processes
/>Two processes Si and Sj can run in concurrence iff
Otherwise time dependent errors occur![]()
/>Now if the concurrency conditions are not fulfilled than the process
must run in Mutual Exclusion.
YouTube:
https://www.youtube.com/watch?v=qpd9wnmsqwE&index=7&list=PL2bE7itI_Ia5EalU1kCXtlNxJHAsUjZEB
Comments
Post a Comment