Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C SIGALRM未在指定时间后终止程序_C_Linux_Process_Signals - Fatal编程技术网

C SIGALRM未在指定时间后终止程序

C SIGALRM未在指定时间后终止程序,c,linux,process,signals,C,Linux,Process,Signals,我编写了一个C代码,其目的是将N作为输入,然后创建N个进程,并创建它们自己的子进程。每个进程从i+1进程向i+12进程发送信号(不管它是否存在)SIGUSR1信号。如果某个进程没有收到任何进程发出的任何信号,则退出该进程。其思想是使用SIGALRM在10秒后关闭所有进程,但程序不会终止。如果通过命令行将N作为输入,则程序可以运行,但如果通过scanf,则程序会以某种方式中断。短暂性脑缺血发作 static int signal_receive = 0; void sighandler(int

我编写了一个C代码,其目的是将N作为输入,然后创建N个进程,并创建它们自己的子进程。每个进程从i+1进程向i+12进程发送信号(不管它是否存在)SIGUSR1信号。如果某个进程没有收到任何进程发出的任何信号,则退出该进程。其思想是使用SIGALRM在10秒后关闭所有进程,但程序不会终止。如果通过命令行将N作为输入,则程序可以运行,但如果通过scanf,则程序会以某种方式中断。短暂性脑缺血发作

static int signal_receive = 0;

void sighandler(int signum){       //////////signal handler for SIGUSR1 signal
  signal_receive++;
}

int main(void){

  int n;
  printf("enter N - ");
  scanf("%d", &n);

  int id1 = 1, id2 = 1;    //////// ids for tracking the process
  for(int i = 0; i<n; ++i){
    if(id1>0 && id2>0){     /////// only the grandparent can enter
      id1 = fork();
      int z = getpid()%13;
      for(int j = 0; j<z; ++j){
        if(id2>0 && id1==0)          //////// only the parent can enter
          id2 = fork();
      }
    }
  }

  alarm(10);          ///////// alarm for 10 seconds after which process stops sending SIGUSR1 and exits

  while(1){                       /////// SIGUSR1 signal
    sleep(2);
    for(int i = 1; i<13; ++i)
      kill(getpid()+i, SIGUSR1);
      if(signal_receive==0){
         printf("signal exited %d\n", getpid());
      exit(0);
    }
  }

  for(int i = 0; i<n*13; ++i)
    wait(NULL);

}

static int signal\u receive=0;
void sighandler(int signum){///SIGUSR1信号的信号处理程序
信号接收++;
}
内部主(空){
int n;
printf(“输入N-”);
scanf(“%d”和“&n”);
int id1=1,id2=1;///ids用于跟踪流程
对于(inti=0;i0&&id2>0){///只有祖父母才能进入
id1=fork();
int z=getpid()%13;
对于(int j=0;j0&&id1==0)//只有父级才能输入
id2=fork();
}
}
}
警报(10);//报警10秒,之后进程停止发送SIGUSR1并退出
而(1){///SIGUSR1信号
睡眠(2);
对于(int i=1;i
您如何知道第i个子进程具有该pid?这是一个错误的假设。您必须将pid存储在表中,并使用它终止相应的进程。例如:

pid_t pids[...];
int n;

...
id = fork();
if (id!=0) pids[n++] = id; // store the new pid in the array
...


...
for (int i=0; i<n; i++)
   kill(pids[i],SIGUSR1); // get pids from array and kill them
...
pid_t pid[…];
int n;
...
id=fork();
如果(id!=0)pid[n++]=id;//将新pid存储在数组中
...
...

对于(int i=0;我要感谢你没有回答清楚这个问题。每个进程向i+1进程发送信号到i+12进程(不管它是否存在)SIGUSR1信号。如果一个进程没有收到任何进程发出的信号,你就退出该进程。我对代码做了一些编辑。请看一看。@johnny_bravo
getpid()+i
没有任何意义!当你创建一个新的pid时,它会返回给给给你孩子pid的调用者,但孩子的pid与父亲的pid无关,第i个孩子与调用者pid+i没有关系!不要这样做。就像我说的,首先将pid存储在某个地方。明确的问题:1.返回
pid\t
,而不是
>int
.2.您需要添加
#include
才能使用
fork()
3.您从未使用
signal()或更好的
sigaction()`4.设置信号处理程序。
静态int-signal\u-receive=0;
[需要是
volatile-sig\u-atomic\t
5.
getpid()+当调用
fork()
失败时,posted无法检查(和处理)错误。注意:用户进程只允许创建(非常有限)数量的子进程关于:
void signandler(int signum){/////SIGUSR1信号的信号处理程序[u receive++}
未处理参数。这将导致编译器发出有关“未使用参数”的消息。这不是代码中唯一的“未干净编译”问题。发布的代码未编译。它缺少所需的
\include
语句
pid_t pids[...];
int n;

...
id = fork();
if (id!=0) pids[n++] = id; // store the new pid in the array
...


...
for (int i=0; i<n; i++)
   kill(pids[i],SIGUSR1); // get pids from array and kill them
...