![]() |
| | #1 |
| Registered User Join Date: Sep 2001
Posts: 20
| Semaphores How do I go about creating and unlocking a set of 5 System V semaphores? Once this is done, how do I wait for them, and signal when I'm finished using it? I'm trying to fork 5 children and execute their functions in a user-specified order. There are 5 children, so there should be a set of 5 semaphores. The user enters the order of execution (integer between 1 and 5, inclusive) using the keyboard. I fork the 5 children all at once, but I need to synchronize their execution with semaphores. How do I do this? Also, please post code examples, not links to other pages. I have tried many other pages but they confuse me more than they help. Thanks in advance.
__________________ liberate tutamet ex inferis |
| edk is offline | |
| | #2 |
| Registered User Join Date: Sep 2001
Posts: 20
| Here's the code segment for what i am doing: Code:
for (int i(0); i < theOrder.size(); i++) {
pid = fork();
if (pid < 0) {
cerr<<"ERROR: fork error\n";
exit(0);
}
else if (pid == 0) {
// wait for semaphore here (code not written yet
doChildStuff(i);
// signal semaphore here (code not written yet
exit(0);
}
childPid[children] = pid;
children++;
}
Code: sem_key = 0x2222;
if ((sem_id = semget(sem_key, theOrder.size(), 0666 | IPC_CREAT)) == -1) {
cerr<<"ERROR: semget error\n";
}
arg.val = 1;
for (int i(0); i < theOrder.size(); i++) {
if (semctl(sem_id, i, SETVAL, arg) == -1) {
cerr<<"semctl error\n";
}
}
__________________ liberate tutamet ex inferis |
| edk is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Semaphores, need advice on implementation. | Swerve | C++ Programming | 2 | 01-13-2009 01:54 AM |
| semaphores | Dr Spud | C Programming | 7 | 09-22-2007 12:45 PM |
| Semaphores Problems | mhelal | Linux Programming | 2 | 05-06-2007 10:36 PM |
| Semaphores | Jules | C Programming | 4 | 01-18-2004 01:58 PM |
| semaphores | theLukerBoy | C Programming | 0 | 11-05-2002 01:46 AM |