#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int cnt = 0;
pid = fork();
if(pid>0) {
wait(NULL);
while(1) {
printf(“this is father print,pid = %d\n”,getpid());
sleep(1);
printf(“cnt = %d\n”,cnt);
}
}
else if(pid == 0) {
while(1) {
printf(“this is child print,pid = %d\n”,getpid());
sleep(1);
cnt++;
if(cnt == 3) {
exit(0);
}
}
}
return 0;
}
2.
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int cnt = 0;
int status = 10;
pid = fork();
if(pid>0) {
wait(&status);
printf(“child quit,child status = %d\n”,WEXITSTATUS(status));
while(1) {
printf(“this is father print,pid = %d\n”,getpid());
sleep(1);
printf(“cnt = %d\n”,cnt);
}
}
else if(pid == 0) {
while(1) {
printf(“this is child print,pid = %d\n”,getpid());
sleep(1);
cnt++;
if(cnt == 3) {
exit(0);
}
}
}
return 0;
}
3.
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int cnt = 0;
int status = 10;
pid = fork();
if(pid>0) {
printf(“this is father print,pid = %d\n”,getpid());
}
else if(pid == 0) {
while(1) {
printf(“this is child print,pid = %d,my father pid = %d\n”,getpid(),getppid());
sleep(1);
cnt++;
if(cnt == 5) {
exit(0);
}
}
}
return 0;
}
4.
父进程等待子进程退出
父进程等待子进程退出
父进程等待子进程退出
父进程等待子进程退出
父进程等待子进程退出

相关文章:

  • 2019-07-08
  • 2022-01-11
  • 2021-11-29
  • 2021-12-05
  • 2021-10-21
  • 2021-11-27
  • 2021-07-13
  • 2021-11-11
猜你喜欢
  • 2019-02-15
  • 2021-09-25
  • 2020-07-15
  • 2021-04-15
  • 2021-12-06
  • 2019-09-13
  • 2021-11-23
相关资源
相似解决方案