- 論壇徽章:
- 0
|
這個(gè)程序就是要父進(jìn)程創(chuàng)建兩個(gè)子進(jìn)程,其中一個(gè)執(zhí)行l(wèi)s,另一個(gè)執(zhí)行ps,但保證先執(zhí)行ps,再執(zhí)行l(wèi)s- #include <stdio.h>
- #include <wait.h>
- #include <unistd.h>
- #include <signal.h>
- #include <stdlib.h>
- #include <sys/types.h>
- int main(int argc, char *argv[])
- {
- int pid1, pid2;
- int status;
- char *ls_argv[] = {"/bin/ls", "-a", 0};
- char *ps_argv[] = {"/bin/ps", "-l", 0};
- pid1 = fork(); //create one child process
-
- if(pid1 < 0){
- printf("Create process fail!\n");
- exit(EXIT_FAILURE);
- }
- if(pid1 == 0){ //The first child process
- printf("I'm the first child! I'm waiting for another.\n");
- pause();
- printf("I'm going to run ls!\n");
- execv("/bin/ls", ls_argv);
- }
- else{ //parent process
- printf("I'm parent. I will create another process.\n");
- pid2 = fork();
- if(pid2 == 0){ //The second child process
- printf("I'm the second process! I will do ps!\n");
- execv("/bin/ps", ps_argv);
- }
- else{ //parent process
- waitpid(pid2, &status, 0);
- printf("The second process exit status = %d\n", status);
- printf("I will arouse my first child!\n");
- kill(pid1, SIGCONT);
- waitpid(pid1, &status, 0);
- printf("The first process exit status = %d\n", status);
- }
- }
- exit(0);
- }
復(fù)制代碼 邏輯上我覺(jué)得沒(méi)有問(wèn)題,但運(yùn)行時(shí)就卡在kill(pid1, SIGCONT)那里了,根本沒(méi)發(fā)送信號(hào),希望大家?guī)兔φ乙幌率悄睦锏膯?wèn)題,多謝! |
|