interprocess communication (v)-signal

Source: Internet
Author: User
Tags function prototype set set

I'll use a few blogs to summarize some of the ways in which you can communicate between processes in Linux, and I'm going to make this summary part of the beginning in every blog in this series.

Ways to communicate between processes

    • Pipeline
    • Message Queuing
    • Signal
    • Signal Volume
    • Shared storage Area
    • Socket (socket)

Interprocess Communication (iii)-Semaphore Portal: http://www.cnblogs.com/lenomirei/p/5649792.html

interprocess communication (ii)-Message Queuing Portal: http://www.cnblogs.com/lenomirei/p/5642575.html

interprocess communication (i)-Pipeline Portal: http://www.cnblogs.com/lenomirei/p/5636339.html

I feel more and more like this, it's not like interprocess communication, it's more of an inter-process greeting ... The signal is this, xxx (this so many possibilities) to the process a signal, the process will be appropriate to deal with this signal ( This indicates that the process may not immediately process the signal ), what is the appropriate time? For example, when the interrupt returned, or the kernel state returned to the user state (this situation appears more) and so on (recommended this book "Linux kernel Design and implementation," said inside) ... This is not the subject of this article is not much.

First of all, how the signal is produced !

    • Generated by the hardware, do not enter the key combination from the keyboard to send a signal, the commonly used CTRL + C can be sent to the foreground process.
    • Sent by the process (or generated by the software), for example, we can send a signal to a process (command: Kill-Signal-designator PID) by entering the kill command under the shell process
    • Exception, when an exception occurs, it must send a signal.

And then say how the signal is handled, who will handle the signal? Must be the operating system , difficult or programmer. At the beginning of the article, the letter will not necessarily be processed immediately, the operating system will not be to deal with a signal to the current running process (switch process) or kill (certainly not kill Ah, do you see a signal to kill an innocent people), Suspended (process switching) is too expensive, and if it is not an emergency signal, it may not be processed immediately. The operating system chooses to process the signal when the kernel state switches back to the user state, which is handled by switching between the two (without a separate process switch to avoid wasting time).

is inevitable, because it is possible to receive a signal during the process of sleep, the operating system will not be willing to switch the current happy running process, so the signal must be stored ah, because it is the process received the signal, so the signal stored in the process of the only PCB (is Task_ struct). struct sigpending pending; The field is the signal table where the signal is stored, and then the pending is explained.

Process of signal processing

All signals can be viewed through the kill-l command

Some points to be aware of

    • No signal number No. 0.
    • No, 32, signal number 33rd.
    • Separate from signal number 31st, the front is the ordinary signal, the back is the real-time signal, this article is about the common signal
    • The signal is consistent with the previous label, you can use the label, or you can use the macro name

There are three ways to handle the signal.

    • Ignore (it is so 6, you send what I ignore you)
    • The default processing mode, the default processing by the operating system, terminates a process, which means that the process is killed by receiving a signal.
    • Custom processing, what do I have to say, you need a signal function
      • Function prototypes: sighandler_t signal (int signum, sighandler_t handler);
      • Header files: #include <signal.h>
      • Parameter resolution:
        • The first one is the signal designator, and the number is OK.
        • The key is the second parameter here, sighandler_t is a typedef, mainly for readability, the prototype is a void (*) (int) function pointer, the int parameter will be set to Signum
        • I can use this function to look at the usage in my test case

    • Correlation function parsing

This time there is no creation of functions or something, mainly signal related to some of the operation functions, but because of the more and more complicated, not good each write test cases to see the output, the final test program only used a part of the function, and not all use to

The previous said the kill command can signal the process, but I would like to write programs in C language, don't be afraid! You need the following function (theraise function can only signal to the process itself )

    • Function prototype: int raise (int signo);
    • Header files: #include <signal.h>
    • Parameter resolution:
      • Just one parameter is the signal, you can use the label, you can also use the macro name

Or you say you do not want to send yourself a signal, light yourself is very boring, then look at the following function

    • function prototype: int kill (pid_t pid, int signo);
    • Header files: #include <signal.h>
    • Parameter resolution:
      • The first parameter is the PID process number, you have to let the operating system know which process you want to send a signal, you can also send yourself OH
      • The second parameter is a signal ... Well, exactly which signal, you can use the label, you can also use the macro name

Look at this signal. (6) SIGABRT, this signal can cause the process to terminate abnormally, he has a corresponding function

    • Function prototype: void abort (void);
    • Header files: #include <stdlib.h>
    • Parameter resolution:
      • There's no argument at all! Then write something else.
      • The function does not return a value because the function execution absolutely does not fail (why?). Kill a process, talk about what failure (a knife can not be two knives!!) (jokes)), and the Exit function will never fail (important!). Say it two times! is different from the Exit function, exit sets the exit code.

Look at this signal (SIGALRM), this signal is the alarm clock signal, it can be sent by this function

    • Function prototype: unsigned int alarm (unsigned int seconds);
    • Header files: #include <unistd.h>
    • Parameter resolution:
      • The parameters are written so clearly! The number of seconds to wait will send a SIGALRM signal to the current process.

Next, let's explain the blocking signal, which is (pending).

    • Blocking signal (pending signal)

As mentioned before, can ignore a signal, then I said, blocking and ignoring is not the same , blocking is the process is not receiving the signal, ignoring is to receive the signal, but do not make any response

The actual execution signal processing action is called the Signal recursion (Delivery), the signal from produces to the recursion state, is called the Signal outstanding (Pending). A process can choose to block (block) a signal. When the blocked signal is generated, it remains in the pending state until the process has unblocked the signal and executes the recursive action.

There are similar fields in the Task_struct, and the previously said signal may not be executed immediately and will be stored in the pending table.

Once the signal is block, when there is signal generation will always be pending, because the pending is not the meaning of processing, block can not be processed, pending will always have that bit

For the PCB to use 32 bits to represent the block and the pending table, so the non-real-time signal even if sent more than just one. The real-time signal will be queued, and a few will be queued

Block table: A bit of 0 indicates that the signal corresponding to the digit is not blocked, 1 is blocking

Pending table: A bit of 0 indicates that the signal has not been generated or has been processed

Signal No. 2nd of two tables at the same time 1 indicates that a signal 2nd has been generated, but because the signal number 2nd is blocked, it has been pending.

Describes several functions that manipulate these two tables

  • Function Prototypes:

    int Sigemptyset (sigset_t *set); used to initialize a signal set (the newly created sigset_t object must first execute this function before the operation)
    int Sigfillset (sigset_t *set); Initialize signal set, all position bit 1, indicate support for all signals (as if all signals were added)
    int Sigaddset (sigset_t *set, int signo); Add a signal of interest (want to operate) to the signal set

    int Sigdelset (sigset_t *set, int signo); kick the signal from the signal set with the interest (want to operate)
    int Sigismember (const sigset_t *set, int signo); determine if your incoming Signo is a member of the set set

  • Header files: #include <signal.h>
  • Parameter resolution:
    • The parameters of the sigset_t structure represent the signal set , and the signal operation is performed in the form of a signal set, which requires the creation of an object in advance of the structure, and then the signal to be manipulated is added to the signal set object .
    • Signo is the signal label.

How do I block a signal? Use the following function

    • function prototype: int sigprocmask (int how, const sigset_t *set, sigset_t *oset);
    • header file: #include <signal.h>
    • parameter resolution:
        • sig_unblock removed from the block table
        • sig_ Setmask Set the Block table (unlike the first sig_block, this is to directly make the current process and set exactly equal, not to add the set to the current block table)
      • set represents the collection to be set, for example, you have 1234 in the collection, that's all, so this function sets the number according to the first parameter,
      • oset means that the old set is the block table information before the setting is set and can be given null

How do I get information from the current pending table? You need it ~

    • Function prototype: int sigpending (sigset_t *set);
    • Header files: #include <signal.h>
    • Parameter resolution
      • This is an output parameter that prints the pending table of the current process to the incoming set set

Things have been done, the basic operation is finished, nonsense less,Show me the Code

function look at the result diagram: First paste the result diagram, there is no signal at the beginning, so the pending table is all 0, I passed CTRL + C incoming signal 2nd, see pending table has 2nd is set, after 10 seconds to unblock, 2nd signal is processed (after my custom function)

My program has only one file server.c

1#include <stdio.h>2#include <sys/signal.h>3#include <sys/types.h>4#include <signal.h>5 6 7 8 voidFuncintnum)9 {Tenprintf"Catch signal number is%d", num); One  A } -  -  the voidPrintfpendingsignal (sigset_t *Set) - { -   inti; -    for(i=1;i< +;++i) +   { -     if(Sigismember (Set, i)) +     { Aprintf"1"); at  -     } -     Else -     { -printf"0"); -     } in   } -printf"\ n"); to } +  -  the intMain () * {  $ sigset_t S,p,o;Panax Notoginseng signal (SIGINT,FUNC); -Sigemptyset (&s); theSigemptyset (&p); +Sigemptyset (&o); ASigaddset (&s,sigint); theSigprocmask (sig_setmask,&s,&o); +   intCount=0; -    while(1) $   { $Sigpending (&p); -Printfpendingsignal (&p); -Sleep1); the     if(count++==Ten) -     {Wuyiprintf"recover!\n"); theSigprocmask (sig_setmask,&o,null); -     } Wu   } -   return 0; About}

Interprocess communication (v)-signal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.