fork.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> void main(){ int pid; pid = fork(); printf("pid is %d\n", pid); exit(0); }
vfork.c
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> void main(){ int pid; pid = vfork(); printf("pid is %d\n", pid); exit(0); }
wait.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <wait.h> void main(){ int pid; pid = fork(); if(pid > 0){ wait(NULL); printf("pid is %d\n", pid); }else{ printf("pid is %d\n", pid); } exit(0); }
excel.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <wait.h> void main(){ int pid; pid = fork(); if(pid > 0){ wait(NULL); printf("pid is %d\n", pid); }else{ execl("/bin/ls", "ls", NULL); printf("pid is %d\n", pid); //不会被执行 } exit(0); }
时间: 2024-10-21 21:18:34