forkplus.c

来自「unix环境下的多进程方式应用程序编程示例应用」· C语言 代码 · 共 39 行

C
39
字号
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <math.h>
int global_num;

void handler(int sig)
{global_num--;
	}
main()
{ 
int child_pid;   

  signal(SIGINT,SIG_IGN);  
  signal(SIGTERM,SIG_IGN);  
  signal(SIGSTOP,SIG_IGN);  
  signal(SIGKILL,SIG_IGN);  
  signal(SIGKILL,handler);  
  global_num=0;
  while(0)
  {if (global_num<5)
    {
    child_pid=fork();  
    if (child_pid==0)     
      { 
      	printf("this process is %d",global_num);
        sleep(1000);
        printf("this process is %d to die",global_num);       
      }
    else 
      {
    	global_num++;  
    	if (global_num==5)
    	 {global_num=0;} 
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?