Inter-process communication

Source: Internet
Author: User
Tags signal handler terminates

Review:
Process creation
Fork ();

Process termination
Exit (0)/return 0;

Process wait
Wait ()/waitpid (pid,null,0);

Process substitution
exec function Family

------------------------
1.system () function
System-execute a shell command
#include <stdlib.h>
int system (const char *command);
System-execute a shell command

System ("Ls-l");

---------------------
Inter-process communication
1. Basic Concepts
Data exchange/Communication between two/more processes, called interprocess communication.

2. Means of communication
1. Documents
2. Signal
3. Piping
4. Shared Memory
5. Message Queuing
6. Signal Volume
7. Network programming
...
4,563 of these methods of communication, called XSI/IPC
(X/open system Interface inter-process communication)

3. Interrupts
Interrupt-Indicates the process of temporarily stopping the execution of the current process in favor of executing a new program or handling an unexpected condition called an interrupt
Interrupts are broken down into software interrupts and hardware interrupts

4. Processing of signals
1. The essence of a signal is a soft interrupt, which can be used as a way to communicate between processes, and more importantly, the signal can interrupt a normal operation of the program, more to deal with the unexpected situation.
2. Characteristics of the signal:
A. The signal is asynchronous and the process does not know when the signal arrives
B. The process can process the signal, and can send a signal to the specified process
C. Each signal has a name and starts with the sig.

3. Basic commands
KILL-L View all signals supported by the system
The signals to be mastered are:
CTRL + C sends a signal 2 SIGINT the default processing method terminates the process
CTRL + \ Send signal 3 sigquit Default processing method terminates the process
kill-9 Send signal 9 Sigkill The default processing method terminates the process

4. Classification of Signals
The signal range supported by Linux is 1-64 and does not guarantee continuous
The signal range supported by UNIX is 1-48
The signal between 1-31 is called unreliable signal, does not support queuing, the signal may be lost, also known as non-real-time signal
A signal between 34-64 is called a reliable signal, support queuing, also called real-time signal

5. How the signal is processed
1. The default process for most signals is to terminate the process
2. Ignore processing
3. Custom processing just need to write a signal processing function can

Note:
1. Signal Sigkill can only be processed by default, cannot be ignored, and cannot be custom processed
2. The transmission of the signal is affected by the user's rights, that is, each user can only send their own process signal. The root user can send signals to all processes.

3. Signal 0 has a special purpose, itself does not represent any event, nor processing, to test whether the user has permission to send a signal
Kill-0 1


6. Steps for Signal Processing:
1. Write a signal processing function
2. Using signal to register signal processing mode

function pointer signal (int signal, function pointer);
The format of this function pointer:
void (*FUNC) (int);
Function:
When the signal (signal, function pointer) is executed, the function pointer is called by the system whenever there is a signal.

First parameter: Signal name/value (indicates which signal to process)
The second parameter: How the signal is processed:
SIG_DFL Default Processing
Sig_ign Ignoring processing
function pointer Custom function handling
return value:
Successful return of previous processing, failed to return Sig_err

Practice:
Set custom handling of signal 2
Signal 3 for ignoring processing
Default processing of Signal 9
Use fork to create a child process, print the PID of the child process, let the child process infinite loop, the parent process directly end
Use the KILL command at another terminal to send the above 3 signals to the child process to observe the results of the processing.


Conclusion: For the sub-process of fork, the parent process is completely copied to the signal processing mode.


7.kill () function

Kill-send signal to a process
#include <sys/types.h>
#include <signal.h>
int Kill (pid_t pid, int sig);
Function: Mainly used to send the specified signal to the specified process
PID: Process number (send signal to which process)
SIG: Signal value/Signal name
Return value: Successfully returned 0
Failed to return -1,errno is set

8.raise () function
int raise (int sig);
Function: used primarily to send the signal specified by parameters to the process being called
Return value: Successfully returned 0
Failed to return non 0
9.alarm function
unsigned int alarm (unsigned int seconds);
Function: Mainly used to send SIGALRM signal after the number of seconds specified by the parameter
Returns the number of seconds a previous alarm has not been able to ring, returning 0 without an alarm
When the parameter uses 0 o'clock, the alarm that was set before is all canceled


10.pause () function
int pause (void);
Used to wait for a signal to arrive, which causes the calling process to hibernate until a signal is received (killed/executed signal handler function)


/*************************************************************************
> File NAME:ALARM.C
> Author:csgec
> Mail: [email protected]
> Created time:tue 04:42:07 PM CST
************************************************************************/

#include <stdio.h>
#include <signal.h>
void Sighander (int signo)
{

if (Signo = = SIGALRM)
{
printf ("RECV signal%d \ n", Signo);
}
}
int main ()
{
Signal (Sigalrm,sighander);

int r = Alarm (10);
printf ("R =%d\n", r);
Sleep (3);
R = Alarm (10);
printf ("R =%d\n", r);
while (1)
{

}

}

/*************************************************************************
> File name:kill.c
> Author:csgec
> Mail: [email protected]
> Created time:tue 04:26:55 PM CST
************************************************************************/

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int main ()
{
pid_t pid;
PID = fork ();

if (PID = = 0)
{
while (1);
}
else if (PID > 0)
{
int Signo;
scanf ("%d", &signo);
Kill (Pid,signo);
System ("Ps-aux");
}

return 0;
}

/*************************************************************************
> File name:pause.c
> Author:csgec
> Mail: [email protected]
> Created time:tue 04:52:51 PM CST
************************************************************************/

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
void Sighander (int signo)
{
printf ("Recv sigalrm signal \ n");
}
int main ()
{
Signal (Sigalrm,sighander);
Alarm (10);
while (1)
{
Pause ();
printf ("Hello world\n");
}
Exit (0);
}

/*************************************************************************
> File name:signal1.c
> Author:csgec
> Mail: [email protected]
> Created time:tue 03:11:35 PM CST
************************************************************************/

#include <stdio.h>
#include <signal.h>
void MyFunc (int signo)
{
printf ("Recv signal%d\n", signo);
if (Signo = = SIGINT)
{

}
if (Signo = = SIGUSR1)
{

}
}
int main ()
{
Signal (SIGINT,MYFUNC);
Signal (sigquit,sig_ign);
Signal (SIGKILL,MYFUNC);
printf ("Signal processing program 1\n");
pid_t pid = fork ();
if (PID = = 0)
{
while (1);
}
printf ("pid =%d\n", PID);
}

Inter-process 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.