linux中wait和waitpid函数的作用 linux wait waitpid

/a.cpp
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
  pid_t childpid;
  int status;
  childpid = fork();
  if ( -1 == childpid )
  {
  perror( "fork()" );
  exit( EXIT_FAILURE );
  }
  else if ( 0 == childpid )
  {
  puts( "In child process" );
  sleep( 3 );//让子进程睡眠3秒,看看父进程的行为
  printf("tchild pid = %dn", getpid());
  printf("tchild ppid = %dn", getppid());
  exit(EXIT_SUCCESS);
  }
  else
  {
  waitpid( childpid, &status, 0);
  puts( "in parent" );
  printf( "tparent pid = %dn", getpid() );
  printf( "tparent ppid = %dn", getppid() );
  printf( "tchild process exited with status %d n", status);
  }
  exit(EXIT_SUCCESS);
}
  
执行结果为:./a.o
In child process
  child pid = 4469
  child ppid = 4468
in parent
  parent pid = 4468
  parent ppid = 4379
  child process exited with status 0
 
如果将上面“waitpid( childpid,&status, 0 );”行注释掉,程序执行效果如下:./a.o
In child process
in parent
  parent pid = 4481
  parent ppid = 4379
  child process exited with status 1331234400
[root@localhost src]# child pid = 4482
  child ppid = 1

子进程还没有退出,父进程已经退出了。

  

爱华网本文地址 » http://www.aihuau.com/a/25101013/154464.html

更多阅读

中医指压按摩在人体任脉和督脉的作用 任脉督脉走向图

指压按摩是指点穴位按摩,在我国中医医疗中占有很重要的地位。特别在民间流传许多特效的疗法。在这里主要介绍有关提高男性性能力的指压按摩,和提高女性性能力的指压按摩。并改善增长男性性时间和女性性阈值高潮的和谐。通过夫妻指压按

全画幅和半画幅的区别是什么? 中画幅和全画幅的区别

想购买单反的朋友肯定看到过全幅和半幅的讨论,那究竟全幅和半幅指的是什么呢?我们知道,目前的数码单反相机,从光学原理上来说和传统胶片单反还是一样的,只是将原来的感光部分的胶片换成了电子感光元件CMOS或者CCD。全幅和半幅指的就是电

声明:《linux中wait和waitpid函数的作用 linux wait waitpid》为网友温存分享!如侵犯到您的合法权益请联系我们删除