-1
我需要創建5個進程(不是線程)並使用信號同步它們。同步算法將會像「循環」一樣。問題是如何創建5個進程?我可以這樣做:我如何創建多個進程
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);
}
是的,你可以... – Saphrosit 2012-01-04 22:26:19