Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

Semaphore (PowerPoint)

VIEWS: 3 PAGES: 1

									                     How wait( ) and signal( ) calls work


    Process X                         Process P                          Process C
consumed = screate(0);           for (i=1; i <=2000; i++) {          for (i=1; i <=2000; i++) {
produced = screate(1);           wait(consumed);                     wait(produced);
resume(create(.., P,..));        n++;                                printf(“n = %d\n”, n);
resume(create(..,C,..));         signal(produced);}                  signal(consumed);}


 struct sentry {                     screate(count)
   char sstate;                      1. sem = newsem( ); gets an unused entry.
 short semcnt;                       2. semaph[sem].semcnt = count;
 short sqhead;
  short sqtail;                      newsem( )
        };                           if (semaph[sem].sstate == SFREE) {
                                     semaph[sem]sstate = SUSED;
extern struct sentry semaph[]        return(sem);



 Scenario: Process P executes before Process C

    Process P                                                           Process C
  wait(consumed)                                                signal(consumed)
 disable(ps); interrupts are disabled                  disable(ps); interrupts are disabled
 sptr = &semaph[sem];                                  sptr = &semaph[sem];
 if (--sptr->semcnt < 0 ) {                            if (sptr->semcnt++ < 0 ) {
 (pptr = &proctab[currpid])->pstate = PRWAIT;          ready(getfirst (sptr->sqhead));
 pptr->psem = sem;                                     resched( ); }
 enquue(currpid, sptr->sqtail);                        restore(ps); interrupts are enabled
 resched( ); }                                         return;
 restore(ps); interrupts are enabled
 return;

								
To top