进程好难啊,当初OS课仅仅是学了一些调度算法之类的东西,对wait之类的命令理解还不够,看来要尽快补上了。
进程学习中提到了fork()叉子的概念,可以从主进程中拓展出一个子进程(pid==0)和一个父进程(pid>0)。
如果换成vfork(),则不会产生这种效果
#include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(){ pid_t pid; char *message; pid = fork(); int n; if(pid==0){ message = "This is the child"; n = 5; printf("%d\n",pid); } else if (pid>100 && pid<1000){ message = "Pid is 100~1000"; n = 7; printf("%d\n",pid); } else{ message = "This is the parent"; n = 3; printf("%d\n",pid); } while(n--){ puts(message); sleep(1); } return 0; }
还应该去学一些进程间通信的知识。。加油。