os实验1 ——进程控制 - webdancer's Blog
os实验1 ——进程控制
#include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> #include<signal.h> #include<stdlib.h> typedef void (*sighandler_t) (int); void sincon() { } int main() { pid_t p0; int sta0,sta1; signal(SIGINT,(sighandler_t)sincon); while(1) { if((p0=fork())<0) { perror("error!fork first child"); exit(0); } else if(p0==0) { pause(); sta0=execlp("/bin/ls","ls",NULL); exit(0); } else { pid_t p1; if((p1=fork())<0) { perror("error!fork second child"); exit(0); } else if(p1==0) { printf("exe the second child\n"); sta1=execlp("/bin/ps","ps",NULL); exit(0); } else { if(waitpid(p1,&sta1,0)!=p1) perror("error!"); if((kill(p0,SIGINT))>=0) printf("wake up first child\n"); sleep(3); } } } return 0; }