0
点赞
收藏
分享

微信扫一扫

fork

#include <unistd.h>

#include <sys/wait.h>

#include <iostream>

#include <signal.h>

#include <list>

using namespace std;


void handler(int){

   pid_t pid = wait(NULL);

   // cout << pid << " exit" << endl;

   printf("%d exit\n",pid);

}

int main(){

   signal(SIGCHLD,handler);

   // cout << getpid() << endl;

   // list<pid_t> pids;

   pid_t pid = -1;

   for(int i=0;i<10;++i){

      if(pid==0){

         sleep(5);

         // cout << "pid:" << getpid() << endl;

  printf("pid:%d\n",getpid());

      }else{

         pid = fork();

  /*if(pid!=0){

      pids.push_front(pid);

  }*/

      }

   

   }

   sleep(50);

   // pause();

   /*

   if(pid!=0){

for(auto pid:pids){

        cout << "before wait:" << pid << endl;

        waitpid(pid,NULL,0);

        cout << "end wait:" << pid << endl;

}

    pause();

   }*/

}

举报

相关推荐

0 条评论