使用fork創建N個進程

  • 創建clients個進程
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>

int clients = 3;

int main(int argc, char *argv[])
{
  int i;
  pid_t pid;
  if (argc != 2)
  {
    fprintf(stderr, "usage: %s clients\n", argv[0]);
    return 0;
  }
  clients = atoi(argv[1]);
  for(i=0;i<clients;i++)
  {
           pid=fork();
           if(pid <= (pid_t) 0)
           {
                   /* child process or error*/
               sleep(1); /* make childs faster */
                   break;
           }
  }
  printf("hello,world\n");
  exit(0);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章