Linux inter-process communication signal (signal)

1. Concept:

  1) is an analog signal to interrupt mechanism in the software level, is an asynchronous communication

  2) signals can be direct interaction between the user-space processes and kernel processes, the kernel process can also use it to notify the user space process has occurred which system events.

  3) if the process is not currently in execution mode, the signal is saved by the kernel up until the process resumes execution before being passed to it; if a signal is set to block the process, then passes the signal is delayed until it It is passed to the process when the blocking is canceled.

2. The user process on the signal responds:

  1) ignore signals: the signal without any treatment, but there are two signals can not be ignored: the SIGKILL and SIGSTOP.

  2) capture signal: signal processing function is defined, when the signal occurs, executes the corresponding handler.

  3) the default action: Linux signals for each specified default action

 

3. Signal:

  SIGINT : Ctrl + C termination signal

  SIGQUIT: ctrl + \ termination signal

  SIGTSTP: ctrl + z halt signal

  SIGALRM: alarm signal is received after the end of this signal timing, the process ends

  SIGCHLD: child process status changes, the parent process receives a signal

  SIGKILL: Kill signal

4. related functions:
. 1) int the kill (pid_t PID, int SIG);
  Function: signal transmitting
  parameters: PID: specified process
  SIG: the signal to be transmitted
  Return Value: 0 success; failure -1
2) int The raise (int SIG) ;
  function: to send their signal process
  parameters: SIG: signal
  return value: 0 success; failure -1
. 3) unsigned int Alarm (seconds the unsigned int)
  function: set a timer in the process
  parameters: seconds the: regular time in sec
  returns: If you call before this alarm (), the process has been set alarm time, time remaining on the clock a time of return, and 0 otherwise.

Note: A process can only have one alarm time. If you already set the alarm time when calling the alarm, the alarm time before being replaced by a new value

4) int PAUSE (void);
  Function: the calling process is suspended until the signal is received.

. 5) void (* Signal (int Signum, void (* Handler) (int))) (int);
  or: typedef void (* sighandler_t) (int);   sighandler_t Signal (int Signum, sighandler_t Handler);   Function: Signal Processing function   parameters: signum: // can not be processed signal is SIGKILL and SIGSTOP     Handler: SIG_IGN: ignore the signal.     SIG_DFL: processing signals using the system default.     Function pointer from the signal processing defined by   the return value: Success: before setting a signal processing mode; Failure: SIG_ERR
  






 

Examples : kill function sends a signal to kill the pid point of the process, raise a signal is sent to kill himself  

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
    pid_t pid;
    pid = fork();
    if(pid < 0)
    {
        perror("fork fail ");
        exit(1);
    }
    else if(pid == 0)
    {
        printf("child \n");
    }
    else
    {
        kill(pid,SIGKILL);//(此处pid是子进程的进程号)杀死 pid 发送进程的信号,kill 给其他进程发送信号,指定进程号
        printf("child killed\n");
        sleep(1);
        printf("father \n");
        wait(NULL); //等待子进程结束,并回收子进程资源
        raise(SIGKILL); //杀死自己的信号,函数raise 给自己发送信号
    }
    return 0;
}

测试:子进程还未来得及运行就被杀死了

 

 例子2: 定时器的使用

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
    printf("%d\n",alarm(7)); //7 秒定时器,此时返回 0 ,前面没有使用定时器
    sleep(2);  //睡 2 秒
    while(1)
    {
        printf("%d\n",alarm(3)); //重新设置定时器值为 3 ,返回上次定时器剩余的时间,7-2 = 5
        sleep(7); //睡7秒,后会结束进程
    }
    return 0;
}

测试:Alarm clock 是定时器结束时提醒

 

 signal 信号使用例子1 : 

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void handler(int arg)
{
    printf("\nctrl + c\n");
//    exit(1);
//    raise(SIGKILL); 
    kill(getpid(),9); //9表示 SIGKILL 杀死信号
}

int main(int argc, const char *argv[])
{
    signal(SIGINT,handler);//定义自己的处理信号,当SIGINT信号发生时,调用 handler 函数处理
    while(1);
    return 0;
}

测试:

 

  signal 信号使用例子2 : 

 

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
int main(int argc, const char *argv[])
{
    signal(SIGINT,SIG_IGN);// ctrl +c 信号被忽略,SIG_IGN 表示忽略该信号
    while(1);

    return 0;
}

测试: 通过 ctrl + c 信号不能终止这个进程

 

Guess you like

Origin www.cnblogs.com/electronic/p/10939769.html