Unix/Linux上C语言fork多个进程的代码示例

    下面一段代码算是一个完整的例子了, 纪念一下.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
   pid_t pid;
  
   /* Remove the defunc pid in process table */
   signal(SIGCLD, SIG_IGN);

   while(true)
   {
      switch(pid=fork())
      {
         case -1:
           printf("Fork failured!\n");
           break;
         case 0:
           printf("This is child thread!\n");
           _exit(0);
           break;
         default:
           printf("Child process %d created!\n", pid);
           sleep(10);
           break;
      }
   }
}

    以前总是没有写signal一行.

发表留言:

« Previous | Main | Next »

英语900句 | English 900

  • We have 50 provinces.
  • 我们有50个省.
  • My country is rich in natural resources.
  • 我国自然资源丰富.
  • That nation is famous for its tourism.
  • 那个国家以旅游业闻名.
  • The biggest festival in my country is the Spring Festival.
  • 我国最大的节日是春节.
  • Geographically, China is located in the Northern Hemisphere.
  • 从地理位置上说, 中国位于北半球.