10 likes | 86 Views
Process P. Process C. for (i=1; i <=2000; i++) { wait(consumed); n++; signal(produced);}. for (i=1; i <=2000; i++) { wait(produced); printf(“n = %d<br>”, n); signal(consumed);}.
E N D
Process P Process C for (i=1; i <=2000; i++) { wait(consumed); n++; signal(produced);} for (i=1; i <=2000; i++) { wait(produced); printf(“n = %d\n”, n); signal(consumed);} How wait( ) and signal( ) calls work Process X consumed = screate(0); produced = screate(1); resume(create(.., P,..)); resume(create(..,C,..)); struct sentry { char sstate; short semcnt; short sqhead; short sqtail; }; screate(count) 1. sem = newsem( ); gets an unused entry. 2. semaph[sem].semcnt = count; newsem( ) if (semaph[sem].sstate == SFREE) { semaph[sem]sstate = SUSED; return(sem); extern struct sentry semaph[] Scenario: Process P executes before Process C Process P Process C wait(consumed) signal(consumed) disable(ps); interrupts are disabled sptr = &semaph[sem]; if (--sptr->semcnt < 0 ) { (pptr = &proctab[currpid])->pstate = PRWAIT; pptr->psem = sem; enquue(currpid, sptr->sqtail); resched( ); } restore(ps); interrupts are enabled return; disable(ps); interrupts are disabled sptr = &semaph[sem]; if (sptr->semcnt++ < 0 ) { ready(getfirst (sptr->sqhead)); resched( ); } restore(ps); interrupts are enabled return;