简体   繁体   English

如何发送信号

[英]how to send a sigterm signal

In my following program i have two processes( the father and the child) both do the same thing but we want to figure out how will finish his task first. 在下面的程序中,我有两个过程(父亲和孩子)都做相同的事情,但是我们想弄清楚如何先完成他的任务。 There is a randomized number of seconds both will sleep causing the challenge of who finishes first more random. 两者都会进入睡眠状态的随机秒数导致了谁先随机完成的挑战。 Both are sending signals to the other process, when five signals have been recieved the process will then send a SIGTERM to the other process signaling that it has finished first. 两者都向另一个进程发送信号,当已收到五个信号时,该进程将向另一个进程发送SIGTERM,表示它已首先完成。 that other process will print that the opponent process has won. 其他进程将打印出对手进程赢了。 My problem is with sending that sigterm signal to the other process, ive tried kill function, singal function and dont know where is my mistake and what to try next. 我的问题是将sigterm信号发送到其他进程,我尝试了kill函数,singal函数,却不知道我的错误在哪里以及下一步该怎么做。 So any help would e=be appreciated below is my code: 因此,下面的任何帮助将成为我的代码:

 //--------including--------------
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 #include <sys/types.h>
 #include <signal.h>
 #include <unistd.h>
//-------global----------------
int sig1_counter=0;
int sig2_counter=0;
//-------prototypes--------------
void catch_sigusr1(int sig_num) ;
void catch_sigusr2(int sig_num) ;
void do_son() ;
void do_dad() ;
//--------main------------------
int main (){
pid_t pid;
srand((unsigned)time(NULL));

signal(SIGUSR1, catch_sigusr1) ;
signal(SIGUSR2, catch_sigusr2) ;

pid = fork() ;

switch(pid) {
case -1 : perror("fork() failed") ;
          exit(EXIT_FAILURE) ;
case 0 : do_son() ;
          exit(EXIT_SUCCESS) ;
default: do_dad() ;
          exit(EXIT_SUCCESS) ;
 }


    return EXIT_SUCCESS;
 }
//-------functions-------------------
void do_son() {
   int i ;
 for (i=0;i<10;i++)
 {
    int sleep_time=rand()%4;
    sleep(sleep_time);
    int num=rand()%2;
    if (num==0)
        kill(getpid(), SIGUSR1) ;
    else
        kill(getpid(), SIGUSR2);
 }
 }
//---------------------------------
void do_dad() {
   int i ;

  for (i=0;i<10;i++)
  {
    int sleep_time=rand()%4;
    sleep(sleep_time);
    int num=rand()%2;
    if (num==0)
        kill(getpid(), SIGUSR1) ;
    else
        kill(getpid(), SIGUSR2);
   }
  }
//---------------------------------
 void catch_sigusr1(int sig_num) {
     signal(SIGUSR1, catch_sigusr1);
     printf(" process %d got signal SIGUSR1\n", getpid()) ;
   if (sig_num==SIGTERM)
  {
    printf("process %d win\n", getpid());
    exit(EXIT_SUCCESS);
  }
   sig1_counter++;
  if (sig1_counter==5)
  {
    printf(" process %d surrender\n", getpid()) ;
    kill(getpid(),SIGTERM);        // here we have a mistake
    //signal(SIGTERM,catch_sigusr2);  // !!!!!!!!!!!
    exit(EXIT_SUCCESS);
   }
 }
//---------------------------------
 void catch_sigusr2(int sig_num) {
   signal(SIGUSR2, catch_sigusr2) ;
   printf(" process %d got signal SIGUSR2\n", getpid()) ;
  if (sig_num==SIGTERM)
  {
    printf("process %d win\n", getpid());
    exit(EXIT_SUCCESS);
  }
   sig2_counter++;
  if (sig2_counter==5)
  {
    printf(" process %d surrender\n", getpid()) ;
    kill(getpid(),SIGTERM);
    //signal (SIGTERM,catch_sigusr1);
    exit(EXIT_SUCCESS);
  }
} 

Both your do_son() and do_dad() functions send signals to getpid() , which means they are signalling only themselves rather than each other. 您的do_son()do_dad()函数均向getpid()发送信号,这意味着它们仅在自身发出信号,而不是在相互发出信号。

In order to signal each other, you need: 为了互相发信号,您需要:

  • do_dad() to send the signal to pid , the return value from fork() being the child PID; do_dad()将信号发送到pidfork()的返回值是子PID; and
  • do_son() to send the signal to getppid() , the parent PID, noting the use of non-sexist terms in the system call :-) do_son()将信号发送到父PID的getppid() ,注意在系统调用中使用了不存在的术语:-)

In other words, something like this in your main function: 换句话说,在您的主要功能中是这样的:

switch (pid = fork()) {
    case -1:
        perror("fork() failed");
        break;
    case 0:
        do_both(getppid());
        break;
    default:
        do_both(pid);
        break;
 }

The reason there's no distinct parent and child function any more is that the only difference is which PID gets signalled. 不再有不同的父子功能的原因是,唯一的区别是PID发出了信号。 Because you're passing that in as a parameter, you can combine the two functions: 由于您将其作为参数传递,因此可以结合使用以下两个函数:

void do_both(pid_t other) {
    for (int i = 0; i < 10; i++) {
        int sleep_time = rand() % 4;
        sleep(sleep_time);
        if ((rand() % 2) == 0)
            kill(getpid(), SIGUSR1);
        else
            kill(getpid(), SIGUSR2);
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM