Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Linux Network Programming - signal blocking and shielding (block, unblock)     - Hadoop2.6.3 build clusters and the development of MapReduce WIN7 by Eclipse on Linux demo (Server)

- SELinux security permissions HTTP + PHP service changes (Server)

- MyCAT separate read and write to achieve MySQL Practice (Database)

- Linux FTP setting file description (Server)

- Linux --- manual release system cache (Linux)

- Sublime Text 3 practical functions and shortcut keys used to collect (Linux)

- The Java Properties class action (Programming)

- Slice MyCAT common fragmentation rules of enumeration (Database)

- Java Graphics2D Paint (Programming)

- Linux rights management (Linux)

- Iptables small summary (Linux)

- Linux operating system, the internal and external security overview (Linux)

- Python Basics Tutorial - lambda keyword (Programming)

- Python uses multi-process pool (Programming)

- How to Install Apache on Linux servers (Server)

- Docker: Mirroring and container operations (Server)

- AFNetworking + Nginx HTTPS communication server + (Server)

- C language macro definition #define Usage (Programming)

- Top command: the Task Manager under linux (Linux)

- How to clean up your Ubuntu 14.10 / 14.04 / 13.10 system (Linux)

 
         
  Linux Network Programming - signal blocking and shielding (block, unblock)
     
  Add Date : 2018-11-21      
         
         
         
  Wait for the process under Linux when the process send a signal to a process from the signal generator to process the received signals and perform the appropriate action is called signal (uh, based on the understanding APUE translation). If a signal is not masked a process, then we can block the process operates on the signal in the program. It will conduct an operation such as when a program when receiving a SIGUSR1 signal, we can use the system API blocked (block) the procedure signal operation until we unblocked. As another realistic example: if a classmate asked me to help him with a meal, but now I have other things to do, and now I do first thing my hand until I have something on hand to help him complete before rice. The whole process is almost like this.

1. sigprocmask function provides shielding and releasing function mask signal.
Operation in order to achieve the key code is not interrupted.
Function is declared as follows:

  int sigprocmask (int how, const sigset_t * set, sigset_t * oldset);
Parameter which can be set for how: SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK
SIG_BLOCK:
Accordance with the parameters set to provide shielding word mask signal. And the original signal shielding saved to oldset in.
SIG_UNBLOCK:
According to the word of the parameter set shield provided by the unmasked signals. Set the de-screen signal for.
SIG_SETMASK:
Signal is set in accordance with the parameters set to provide a signal to reset the system settings.

2. Signal Shielding and solutions to achieve common screen
Method One: SIG_BLOCK, SIG_UNBLOCK paired achieve
Advantages oldset can matter.

Method two:
SIG_BLOCK setting shield, retain the original signal is set.
SIG_SETMASK restore the original settings.

3. The masking process in how to deal with the received signal
In the process of signal shielding, all blocked signals present, no matter how many times, after the signal unmasked, the system will perform the operation once blocked the signal.

Here we consider the main multi-threaded and non-threaded case.
Let's look at a piece of code, the program receives usr1, usr2 signal sets two global variables usr1 and usr2 value of 1. There are two main program loop, the first round received usr1 signals out of the loop, the second loop receives usr1, usr2 signal will be out of the loop.

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

int flag_sigusr1 = 0;
int flag_sigusr2 = 0;

void sig_usr1 (int signo) {
    fprintf (stdout, "caught SIGUSR1 \ n");
    flag_sigusr1 = 1;
    return;
}

void sig_usr2 (int signo) {
    fprintf (stdout, "caught SIGUSR2 \ n");
    flag_sigusr2 = 1;
    return;
}

int main (void) {
    sigset_t newmask, oldmask;

    signal (SIGUSR1, sig_usr1);
    signal (SIGUSR2, sig_usr2);

    fprintf (stdout, "first while catch sigusr1 can break \ n.");
    while (1) {
        if (flag_sigusr1) {
            fprintf (stdout, "break");
            break;
        }
        sleep (10);
    }
    flag_sigusr1 = 0;

    // Block SIGUSR1
    sigemptyset (& newmask);
    sigaddset (& newmask, SIGUSR1);
    if (sigprocmask (SIG_BLOCK, & newmask, & oldmask) <0) {
        perror ( "sigprocmask error");
    }

    fprintf (stdout, "first while catch sigusr1 or sigusr2 can break \ n.");
    while (1) {
        if (flag_sigusr1 || flag_sigusr2) {
            fprintf (stdout, "break");
            break;
        }
        sleep (10);
    }

    return 0;
}1
Not out of the loop when we choose a loop between the first and second cycles sigusr1 blocking signal, when the program runs to the second loop signal to send usr1 program.

Multithreading common signal processing function for each thread, but each thread can choose whether they block a signal.

Look at a multi-threaded example: sub-thread function above, the main thread receives the signal hup usr2 sends a signal to the child thread.

#include < stdio.h>
#include < signal.h>
#include < unistd.h>
#include < pthread.h>

int flag_sigusr1 = 0;
int flag_sigusr2 = 0;
int flag_sighup = 0;

void sig_usr1 (int signo) {
    fprintf (stdout, "sig | caught SIGUSR1 \ n");
    flag_sigusr1 = 1;
    return;
}

void sig_usr2 (int signo) {
    fprintf (stdout, "sig | caught SIGUSR2 \ n");
    flag_sigusr2 = 1;
    return;
}

void sig_hup (int signo) {
    fprintf (stdout, "sig | caught SIGHUP \ n");
    flag_sighup = 1;
    return;
}

void * thread_control_signal (void * arg) {
    sigset_t newmask, oldmask;
    sigemptyset (& newmask);

    // Thread block sighup
    sigemptyset (& newmask);
    sigaddset (& newmask, SIGHUP);
    if (pthread_sigmask (SIG_BLOCK, & newmask, & oldmask) <0) {
        perror ( "sigprocmask error");
    }

    fprintf (stdout, "thread | first while catch sigusr1 or sigusr2 can break \ n.");
    while (1) {
        if (flag_sigusr1 || flag_sigusr2) {
            fprintf (stdout, "thread | break \ n");

            break;
        }
        sleep (10);
    }
    flag_sigusr1 = 0;

    // Thread block SIGUSR1
    sigaddset (& newmask, SIGUSR1);
    if (pthread_sigmask (SIG_BLOCK, & newmask, & oldmask) <0) {
        perror ( "sigprocmask error");
    }

    fprintf (stdout, "thread | first while catch sigusr2 can break \ n.");
    while (1) {
        if (flag_sigusr1 || flag_sigusr2) {
            fprintf (stdout, "break \ n");
            break;
        }
        sleep (10);
    }

    fprintf (stdout, "thread | thread exit \ n");
    return (void *) 0;
}

int main (void) {
    sigset_t newmask;
    pthread_t tid;
    int signo;

    // Signal action
    signal (SIGUSR1, sig_usr1);
    signal (SIGUSR2, sig_usr2);
    signal (SIGHUP, sig_hup);

    if (pthread_create (& tid, NULL, thread_control_signal, NULL) <0) {
        perror ( "create pthread failed");
        return -1;
    }

 // Main thread block sigusr1
    sigemptyset (& newmask);
    sigaddset (& newmask, SIGUSR1);
    if (pthread_sigmask (SIG_BLOCK, & newmask, NULL) <0) {
        perror ( "sigprocmask error");
    }

    // Main thread wait sighup
    sigemptyset (& newmask);
    sigaddset (& newmask, SIGHUP);
    if (sigwait (& newmask, & signo) <0) {
        perror ( "sigwait failed");
        return -1;
    }
    fprintf (stdout, "main | get SIGHUP \ n");

    pthread_kill (tid, SIGUSR2);
    pthread_kill (tid, SIGUSR2);
    pthread_join (tid, NULL);

    fprintf (stdout, "main | exit \ n");
    return 0;
}
     
         
         
         
  More:      
 
- How to determine whether the Linux server was hacked (Linux)
- Graphing tool: Gnuplot (Linux)
- iscsiadm command usage (Linux)
- In the case of using cgroups Ubuntu 14.04 and Docker (Linux)
- New features of Java 9 HTTP2 and REPL (Programming)
- C ++ sequence containers basics summary (Programming)
- Unable to solve the official version of Android Studio online update problem (Linux)
- To configure linux transparent firewall (Linux)
- A custom implementation of the Android sidebar (Programming)
- How do I upgrade to Ubuntu 15.04 (Beta) (Linux)
- Recover accidentally deleted Nginx logs (Server)
- How to use the process on the desktop xkill end Linux (Linux)
- ASM learning overview (Database)
- CentOS 5.x install Lua 5.2.3 error (LIBS = -lncurses) (Linux)
- Spring use Cache (Programming)
- Linux system on a virtual machine to access the Internet (Linux)
- How to install and use the malware detection tool LMD and ClamAV antivirus engine on Linux (Linux)
- CentOS 6.5 minimal installation and configuration VMware tools (Linux)
- AWR Report (Database)
- Linux Security Module (LSM) Introduction (Linux)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.