1 / 1

Efficient Process Synchronization Using Semaphores

Learn how wait() and signal() calls work with semaphores in process synchronization. Detailed code examples and scenarios explained.

damali
Download Presentation

Efficient Process Synchronization Using Semaphores

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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;

More Related