Signal-Learning Notes in Linux

Keywords: Unix

I. Concept:
Signal is an event that UNIX system responds to certain conditions, and the process will take corresponding actions when it receives the signal.
Signals are generated by some error conditions, such as memory segment collisions, floating-point processor errors, or illegal instructions.

1. Classification: It can be divided into soft interrupt and hard interrupt signals, in which soft interrupt is actually completed by means of hardware.
2. Characteristics
   1) Simple; 2) can not carry a lot of information; 3) meet a certain condition before sending.
3. Traits
   Because the signal is realized by software method, it has a strong delay. For users, the time is very short and it is not easy to detect.
4. All signals received by each process are sent by the kernel and processed by the kernel.

2. Signal-related events or nouns
 1. The Basic Method of Generating Signals
  1) The system calls the execution function of the current process (kill)
  2) kill by command
  3) Hardware exception, segment error: memory error; bus error.
  4) Software condition generation (alarm, timer) 
  5) Key test (ctrl+c)

3. Signal Processing Method
 1. Default actions such as ctrl+c killing the process
   For example: terminate the process, terminate the process and generate core files (for debugging), ignore, pause, continue, etc.
2. Ignore (discard) signal processing, discard the signal, not in the pending set.
3. Signal capture, do not perform default actions
//Program 1 executes the program to kill the current process
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

//Man2 kill view header file

int main()
{
    int ret;
    ret=kill(getpid(),SIGKILL);//The parameter SIGKILL signal is sent to the current process.
    if(ret==-1)//Invocation failed
    {
        exit(1);
    }
    return 0;
}
Procedure execution effect:

//Program 2 creates five subprocesses and kills the third of them.
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define N 5

int main()
{
    int i;//Common variables
    pid_t pid,q;//Process return value
    for(i=0;i<N;i++)
    {
        pid=fork();//Create process
        if(pid==0)//Subprocesses
        {
            break;
        }
        if(i==2)
        {
            q=pid;
        }
    }
    if(i<N)
    {
        while(1)
        {
            printf("I am the child %d,pid=%d\n",i,getpid());
            sleep(1);
        }
    }
    else
    {
        kill(q,SIGKILL);//Kill the QTH process
    }
    return 0;
}
4. Timer
 1. Each process has only one timer.
2. Timing occurs regardless of the state of the process (running, ready, zombie, terminating).
3. Function: Set an alarm clock to send alarm signal
 4. Return value: Remaining signal before return.
5. Time command: Used to view program execution time, actual time = system time + user time + waiting time
 6. setitimer periodic timing, microseconds
  1) Calculating natural time 14
  2) Calculate only the CPU usage time of the process by 25
  3) Calculate CPU usage and system call time 26
 Structural struct itimerval, member:
  1)it_value: Set the trigger time.
  2)it_interval: Set the trigger period.
1) and 2) all have (int)tv_sec, (int)tv_usec structural members.

Supplement:
Process A generates signals and sends them to process B through the kernel immediately.
Blocking Signal Set: Signal Shielded Words
 Pending signal set: a set of signals that have not been processed
 Kill-l// View all current signals
//Program 3-1 Calculates the Number of Computer 1s Energy (Method 1)
#include <unistd.h>
#include <stdio.h>
//Time. / COUNT1 -- Displays the specific execution time
int main(void)
{
    int i;
    alarm(1);//Timing 1s
    for(i=0;;i++)
    {
        printf("%d\n",i);//Display numbers
    }
    return 0;
}

//Program 3-2 calculates the number of computers in 1s (Fa 2)
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

//Definition of Timing Function
unsigned int myalarm(unsigned int sec)
{
    struct itimerval it,oldit;
    int ret;
    it.it_value.tv_sec=sec;//Setting trigger time
    it.it_value.tv_usec=0;
    
    it.it_interval.tv_sec=sec;//Setting trigger period
    it.it_interval.tv_usec=0;
    
    ret=setitimer(ITIMER_REAL,&it,&oldit);//timing
    if(ret==-1)
    {
        exit(1);
    }
    return oldit.it_value.tv_sec;
}
//Principal function
int main(void)
{
    int i;
    myalarm(1);//Calling Timing Functions
    for(i=0;;i++)
    {
        printf("%d\n",i);//Output digits
    }
    return 0;
}

Procedure execution effect:

 

//Program 4 outputs the captured signal in a timing manner
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <signal.h>

void myfun(int signo)//Output hello world
{
    printf("hello world\n");
}

void myfun2(int signo)
{
    printf("close error\n");
}

int main(void)
{
    struct itimerval it,oldit;//Defining Structural Type Variables
    //Captured signal
    signal(SIGALRM,myfun);
    //Shielding ctrl+c
    //signal(SIGINT,myfun2);
    
    int ret;
    it.it_value.tv_sec=5;//5s trigger once
    it.it_value.tv_usec=0;
    
    it.it_interval.tv_sec=3;//3s interval
    it.it_interval.tv_usec=0;
    
    ret=setitimer(ITIMER_REAL,&it,&oldit);//Setting Timing
    if(ret==-1)
    {
        printf("settimer error");
        exit(1);
    }
    for(;;);//Continuous execution of programs
    return 0;
}

Procedure execution effect:

 

//Program 5 outputs the current process pending signal set and tests the shielding signal
#include <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

//Display function definition
void printfped(sigset_t *ped)
{
    int i;
    for(i=0;i<32;i++)
    {
        if(sigismember(ped, i)==1)//Judging whether there is signal concentration or not,'1'is existence
        {
            putchar('1');//Output 1
        }
        else
        {
            putchar('0');//Output 0
        }
    }
    printf("\n");
    sleep(1);
}
//Principal function
int main(void)
{
    //Custom Set, Influencing pcb, Blocking Signal Set
    sigset_t myset,oldset,ped_set;    
    sigemptyset(&myset);//Clear the custom signal set
    //Sigaddset (& myset, SIGQUIT); //Set ctrl+/Signal (3)
    sigaddset(&myset,SIGINT);//Setting ctrl+c signal (2)
    sigaddset(&myset,SIGTSTP);//Setting ctrl+z signal (19)   
    sigprocmask(SIG_BLOCK,&myset, &oldset);//Read or change the signal masking word for the process  
    //View pending signal sets
    while(1)
    {
        sigpending(&ped_set);    
        //display
        printfped(&ped_set);
    }
    return 0;
}
Procedure execution effect:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Posted by ilovetoast on Sat, 10 Aug 2019 04:16:05 -0700