semaphore - c if (semctl (semid, 0, SETVAL, 1) == -1) {...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
// semaphore.cpp / #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <stdlib.h> #include <iostream.h> #include <stdio.h> # const int DUMMY = 0; const int COUNT = 4; c void main( void ) { int semid; i if ( (semid = semget(1234, 1, IPC_CREAT | IPC_EXCL | 0666) ) == -1) { perror("semaphore creation failed"); exit(1); } else cout << "\nSemaphore created, semid = " << semid << endl;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: c if (semctl (semid, 0, SETVAL, 1) == -1) { perror (&quot;Can't initialize semaphore&quot;); exit(2); } else cout &lt;&lt; &quot;Semaphore initialized, to value &quot; &lt;&lt; semctl (semid, 0, GETVAL, 0) &lt;&lt; endl; if (semctl (semid, DUMMY, IPC_RMID, DUMMY) == -1) { perror (&quot;Semaphore not removed&quot;); exit(3); } else cout &lt;&lt; &quot;Semaphore removed&quot; &lt;&lt; endl; }...
View Full Document

Ask a homework question - tutors are online