每天几个编程小笔记
wait()
小栗子🌰
1.无 wait()
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<unistd.h>
int main(){
pid_t pid;
pid = fork();
if(pid == 0){
printf("I am child. My pid is %d\n", getpid());
}else{
printf("I am parent. My pid is %d\n", getppid());
return 0;
}
2.有wait(NULL)
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<unistd.h>
int main(){
pid_t pid;
pid = fork();
if(pid == 0){
printf("I am child. My pid is %d\n", getpid());
}else{
wait(NULL);
printf("I am parent. My pid is %d\n", getppid());
return 0;
}``
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200723193635786.png)
如果有了wait则孩子优先, 若无,则parent优先。 尊老爱幼。