Session 9: Process Synchronization - Theory Continue



Process Synchronization-Theory Continue


girls diary v/s boys diary
GIRLS DIARY: He was quiet 2day. I askd if its my fault. He said Nothing.. I
said I LUV U, But He smiled sadly. I cried all nite 
BOYS DIARY: Shit LIVERPOOL lost d matCh ;)”

Q?What is Round Robin scheduling 
/>During this the processes are dispatched
in a FIFO manner but are
given a limited amount of CPU time
called a time-slice or a
quantum.
/>If a process does not complete before its CPU-time
expires, the CPU is preempted
and given to the next process
waiting in a queue. The preempted process is then placed at the back
of the ready list. 
/>Round Robin scheduling is preemptive(goes
at the end of time-slice) therefore it is effective
in time-sharing environment in which the system needs to
guarantee reasonable response times
for interative users.
/>the only interesting issue with round robin scheme is the length of the
quantum. Setting the quantum too
short causes too many context switches and lower the CPU
efficiency. On the other hand, setting the quantum
too long may cause poor
response time. 

Q?Difference between kernel stack and user stack 
/>The kernel mode stack has privileges
to access the underlying hardware, while the user mode stack
does not. When a system call
is issued, say to open a file, the processor switches to the kernel
mode stack because file system information can
only be accessed in privileged mode. The reason
for isolating user mode processes from direct access to
hardware is to protect kernel data
structure from becoming inconsistent. 














Ex: copy.c : /*file copy to show the difference of user and kernel stacks
*/

/* copy.c */

#include <stdio.h>
#include <fcntl.h>

#define BUFDIM 1024

char buffer[ BUFDIM ];

int
main( int argc, char ** argv ){
int oldfd, newfd; /* file descriptors */

if ( argc != 3 ) {
fprintf(stderr,"usage: %s oldfile newfile\n", argv[0] );
exit( -1 ); // return -1;
}
/* open source file in read-only mode */
oldfd = open( argv[1], O_RDONLY );
if ( oldfd == -1 ){
fprintf(stderr,"%s: cannot open file %s\n", argv[0], argv[1] );
exit(-1);
}
/* create dest. file in read-write mode */
newfd = creat( argv[2], 0666 );
if ( newfd == -1) {
fprintf(stderr,
"%s: cannot create file %s\n",
argv[0], argv[2]);
exit( -1 );
}
copy( oldfd, newfd );
exit( 0 );
}

copy(int oldfd, int newfd ){
int count;
while ((count = read( oldfd, buffer, BUFDIM)) > 0 )
write( newfd, buffer, count );
}


Q?what is event flag
/>It is a process synchronization
primitive in OS. 
/>The process is synchronized by means
of signals.(using await and cause )
/>event signals in unix: by using LOCK and UNLOCK 
/>In LOCK : sleep();
/>while In UNLOCK : wakeup(); 

Q?What is semaphore variable ?
/>A semaphore is somewhat like an integer variable, but is special in
that its operations (increment and decrement) are guaranteed to be
atomic. you cannot be halfway through incrementing the semaphore. We
can increment and decrement the semaphore from multiple threads
without interference. By convention, when a sempahore
is zero it is “locked” or “in use”. Otherwise,
positive values indicate that the
semaphore is available. A semaphore will never
have a negative value. 
/>a semaphore can be defined by this structure

typedef struct semaphore_tag{
char lock;
int cnt;
process_t *head;
}semaphore_t;

/>we can define two kernel operations calling semaphore
disable(S) : suspends the issuing process CP, and appends it to the semaphore list S; unlock(S.lock)
enable(S):moves the first process blocked in the semaphore S list to the
ready list.
 
Q?Are there any methods for semaphore 
/>Sempahore has important two primitives 
WAIT(S):
SIGNAL(S):

INIT(S,k) { S.cnt = k } k >= 0 // initialize the counter

WAIT(S){
lock(S.lock);
S.cnt --; //decrement counter
if(S.cnt <0)
disable(S); // disable semaphore if the counter is < 0
unlock(S.lock);
}

SIGNAL(S){
lock(S.lock);
S.cnt++;
if(S.cnt<=0)
enable(S); // enble semaphore if the counter is <= 0
unlock(S.lock);
}

Q?How do we synchronize by means of semaphores 
/1>first we need to declare a
sempahore variable of type Semaphore 
/2>Second we use the method Wait()
when a thread must wait on the semaphore 
/3>third, we use the method Signal()
to release a waiting threads. 










Q?how
cobegin-coend block is implemented using semaphores  
/> 

















 
Q?How mutual exclusion is done with sempahores 
/>










Q?What are time-dependent errors  
 />














Q?How are deadlock and Starvation occurred
/>










Q?How do we solve Producer-Consumer problem using semaphore 
/> 
//access functions to the common buffer 
Message buffer [MAX];
int in, out;

init(){
in=0;
out=0;
}
enter(Message m){
buffer[in]= m;
in = (in+1)%MAX;
}
remove(Message m){
m = buffer[out];
out = (out+1)%MAX;
}
//producer-consumer solution for single producers or consumers
INIT(full)= 0; INIT(empty)= MAX;
Producer(){
Message m;
while(TRUE){
produce m;
wait(empty);
enter(m);
signal(full);
}
}
Consumer(){
Message m;
while(TRUE){
wait(full);
m = remove();
signal(empty);
consume m;
}
}
//producer-consumer solution for multiple producer / consumers
INIT(full)= 0; INIT(empty)= MAX;
INIT(ME_p)= 1; INIT(ME_c)= 1;
Producer(){
Message m;
while(TRUE){
produce m;
wait(empty);
wait(ME_p);
enter(m);
signal(ME_p);
signal(full);
}
}
Consumer(){
Message m;
while(TRUE){
wait(full);
wait(ME_c);
m = remove();
signal(ME_c);
signal(empty);
consume m;
}
}



YouTube: 


Comments

Popular Posts