Linux inter-process communication-signal communication

Source: Internet
Author: User

Signal

Signals are asynchronous interprocess communication modes

How the process responds to the signal:
<1> Ignore
SIGKILL and SIGSTOP cannot be ignored

<2> capture
When the process receives a signal, the signal processing function executed at this time

<3> Default

The SIGSTOP signal is used to pause a process and cannot be blocked, ignored, and processed, and the default action is to pause the process
Most signals are the default action for processes that kill processes
When the child process state changes, the operating system sends SIGCHLD to the parent process, which is ignored by default

Sending and setting of signals

1. Signal send Kill () and raise ()

int Kill (pid_t pid, int sig);

Parameters:
PID: You may choose to have the following four kinds of

1. PID is greater than zero, send signal to process number PID process.
2. When the PID equals zero, the signal is sent to all processes that belong to the same use group as the process that called Kill ().
3. When PID equals-1, the signal is sent to all processes in the process table, except for process 1 (init).
4. When the PID is less than 1, the signal is sent to the process identified by the-pid as the group.

SIG: The signal code to be sent, if its value is zero, no signal is sent out, but the system performs an error check, usually using the sig value for zero to verify that a process is still executing.


Return value Description: Returns 0 when successful execution. Failed return-1

Errno is set to one of the following values:

EINVAL: The specified letter number is invalid (parameter sig is not valid)

Eperm: Insufficient permissions to transmit signal to the specified process

Esrch: The process or process group specified by the parameter PID does not exist

int raise (int signo);

Note: The raise function only allows processes to send signals to themselves

Examples are as follows:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
int main (int argc, const char *argv[])
{

pid_t pid;
int ret;

if ((Pid=fork ()) <0)
{
printf ("Fork error\n");
Exit (Exit_failure);
}
if (pid==0)
{
printf ("Child (pid:%d) was waiting for any signal\n", getpid ());
Raise (SIGSTOP);//Use the Raise () function in a subprocess to emit a SIGSTOP signal to pause the child process
Exit (exit_success);
}
Else
{
if (Waitpid (Pid,null,wnohang) ==0)
{
Sleep (10);
Kill (Pid,sigkill);
printf ("Parent Kill child Process%d\n", PID);
}
}
Waitpid (pid,null,0);
return 0;
}

typedef void (*sighandler_t) (int);
sighandler_t signal (int signum, sighandler_t handler);
Function: Set process to signal processing mode
Parameters:
Number of @signum signal
@handler
Sig_ign: Ignore Signal
SIG_DFL: Using the default processing method
Function Name: Capture mode processing

return value:
Successfully returned handler, failed to return Sig_err

Practice:
How to do non-blocking, non-rotation ways to reclaim zombie child processes


2. Set a timer in the process

unsigned int alarm (unsigned int seconds);
Parameters:
@seconds timed time, in seconds

Attention:
Once the timed time is complete, the operating system sends a SIGALRM signal to the process

Homework:
Complete file transfer via a well-known pipeline

./a file1------->fifo------->./b file2

A process:
Read a file, write a pipeline

A process end condition: The file has no data to read

B Process:
Read pipelines, write files

B Process End Condition: At write end, read end does not block, if there is no data in the pipeline, the read pipeline will return 0

Linux inter-process communication-signal communication

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.