Semid semmutex mutual exclusion semaphore msgqid

Info icon This preview shows pages 2–4. Sign up to view the full content.

View Full Document Right Arrow Icon
SEM_ID semMutex; /* mutual exclusion semaphore */ MSG_Q_ID mqPrnRequest; /* request message queue */ MSG_Q_ID mqPrnRelease; /* release message queue */ /* Create a binary semaphore that is initially full (available) */ SEM_ID semMutex; /* Initialize */ semMutex = semBCreate(SEM_Q_PRIORITY, SEM_FULL); /* create mutex semaphore */ /*** message queues ***/ mqPrnRequest = msgQCreate(MAX_MSGS, MAX_MSG_LEN, MAG_Q_PRIORITY); mqPrnRelease = msgQCreate(MAX_MSGS, MAX_MSG_LEN, MAG_Q_PRIORITY); /* spawn printer manager task */ taskSpawn("prnManager",100,0,20000,prntask,0,0,0,0,0,0,0,0,0,0); . . .
Image of page 2

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
/*** printer request procedure ***/ int prnRequest(int tid) { int pid; char msgbuf[MAX_MSG_LEN]; semTake(semMutex, WAIT_FOREVER); /* critical region, protected by semMutex semaphore */ /* send tid */ sprintf(msgbuf,"1 %d 0",tid); /* code = 1 (request) */ msgQSend(mqPrnRequest,msgbuf,MAX_MSG_LEN,WAIT_FOREVER); /* and wait for pid */ msgQReceive(mqPrnRelease,msgbuf,MAX_MSG_LEN,WAIT_FOREVER); sscanf(msgbuf, "%d", &pid); /* printer ID */ semGive(semMutex); return(pid); } /*** printer release procedure ***/ void prnRelease(int tid, int pid) { char msgbuf[MAX_MSG_LEN]; semTake(semMutex, WAIT_FOREVER); /* critical region, protected by semMutex semaphore */ /* send operation code and tid */ sprintf(msgbuf,"0 %d %d", tid, pid); msgQSend(mqPrnRequest,msgbuf,MAX_MSG_LEN,WAIT_FOREVER); /* and wait for acknowledgment */ msgQReceive(mqPrnRelese,msgbuf,MAX_MSG_LEN,WAIT_FOREVER); semGive(semMutex); return(pid); }
Image of page 3
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern