
作业社区
探索学习新天地,共享知识资源!
Linkus 的学生作业:
/* 创建两个子进程,子进程A 与 子进程 B * * A 进程延时 2s 后退出, B 进程延时 5s 后退出, * * 父进程分别等待两个子进程退出 */ #include #include #include #include #include int main(void) { pid_t pidA,pidB,pidP; int status; pidA = fork(); if(pidA == 0) { printf("Process A init.\n"); sleep(2); printf("Process A exit.\n"); exit(0); } else if(pidA < 0) { perror("fork"); exit(1); } pidB = fork(); if(pidB == 0) { printf("Process B init.\n"); sleep(5); printf("Process B exit.\n"); exit(0); } else if(pidB < 0) { perror("fork"); exit(1); } #if 1 pidP = wait(&status); pidP == pidA ? printf("P: process A exit.\n") : pidP == pidB ? printf("P: process B exit.\n") : printf("pidP = %d\n",pidP); #else waitpid(pidA, &status, 0); printf("P: process A exit.\n"); #endif waitpid(pidB, &status, 0); printf("P: process B exit.\n"); return 0; }




