I need to create 5 processes (not threads) and synchronize between them with semaphors. An algorithm of synchronization will be something like “Round Robin”. Question is how to create 5 processes? Can I do it in that way:
pID = fork();
if(pID < 0) {
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if(pID == 0) {
pID = fork();
if(pID < 0) {
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pID == 0) {
pID = fork();
if (pID < 0) {
fprintf(stderr, "Fork Failed");
exit(-1);
} else if (pID == 0) {
pID = fork();
if (pID < 0) {
fprintf(stderr, "Fork Failed");
exit(-1);
} else if (pID == 0) {
/* Process 5 */
printf("process5 is running... id: %d\n", pID);
} else {
/* Process 4 */
printf("process4 is running... id: %d\n", pID);
}
} else {
/* Process 3 */
printf("process3 is running... id: %d\n", pID);
}
}
else {
/* Process 2 */
printf("process2 is running... id: %d\n",pID);
}
}
else {
/* Process 1 */
printf("process1 is running... id: %d\n",pID);
return (EXIT_SUCCESS);
}
Yes, but a loop would be easier for others to read and understand.
then somewhere else
and put the guts of your program in some_function.