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++;
}
...and here's my creation of the semaphore set:
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";
}
}
you can see, all o the semaphores are initialized to 1. now that you know this, how do i make it so that the first semaphore is signalled? also, how do i wait for a semaphore?