Communication between Linux processes

Source: Internet
Author: User
Tags signal handler

I. Processes and Threads
    1. A process is a running activity of a program on a data set, and a thread is an execution path in a process. The process has its own independent address space, and the thread does not, and the thread must depend on the process to exist.
    2. A process is a unit of system resource allocation, and a thread is a unit of system dispatch
    3. The operating system allocates resources in a process, each thread in the process shares the resources of the process, and each thread has its own program counter, stack.
two. Three ways to start a process
    1. System ("Ps-ef"), using the shell to start a new process
    2. The Exec family function replaces the original process with the new process, the system will run from the new process and the PID value of the process will be the same as the PID value of the original process.
    3. The fork function copies the process image, creates a new process, many of the properties of the new process are the same as the current process, and executes the code exactly the same, but the new process has its own data space, environment, and file descriptors.
three. Orphan process and zombie process

  In Linux, the child process is always generated by the parent process, the child process and the parent process are running at the same time, the parent process cannot predict when the child process will end, and when a process finishes its work, the parent process needs to call the Waitpid system call to get the terminating state of the child process. The orphan process means that the parent process is finished and its child processes are still running, and those child processes are called orphans. The orphan process will be adopted by the INIT process (process number 1) and the Init process completes the state collection for them. A Zombie process is a process that uses fork to create a child process, and if the child process exits, and the parent process does not call wait or waitpid to get state information for the child process, the process descriptor of the child process is still stored in the system. This process is called a zombie process and the process is in a zombie state . Obviously, if the process does not call Wait/waitpid, then the reserved piece of information will not be released, its process number will always be occupied, but the system can use the process number is limited, if a large number of zombie processes, The system cannot produce a new process because no process number is available. This is the threat to the zombie process and should be avoided. There are two ways to avoid a zombie process, one is to send a sigchild signal to the parent process when the child process exits, the parent process to process the sigchild signal, call wait in the signal handler function to handle the zombie process, and the other is to fork two times when the process is created, The principle is to make the child process an orphan process, so that its parent process becomes the Init process, and the INIT process can handle the zombie process. refer to " orphan process and zombie process summary " for details

four. Process and Signal

Signals provide a way to handle asynchronous events. Each signal has a name. These names start with a three-character sig. In the header file <signal.h>, these signals are defined as positive integers (signal numbers). No signal with number 0 is present. (The Kill function has a special application for signal number 0). A signal is a classic instance of an asynchronous event. The event that generates the signal is randomly occurring for the process. A process cannot simply test a variable (for example, errno) to determine if a signal has occurred, but must tell the kernel to "do the following when this signal appears". The following example deals with a zombie process through a signaling mechanism that sends a sigchild signal to the parent process when the child process exits, and the parent process processes the sigchild signal. Call wait in the signal handler function to process the zombie process.

How to generate a signal:

    • When the user presses certain terminal keys, the signal generated by the terminal is raised.
    • A signal is generated by a hardware exception.
    • The process calls the Kill (2) function to send a signal to another process or process group. (The owner of the receive signal process and the sending signal process must be the same, or the owner of the sending signal process must be superuser.) )
    • The user can use the Kill (1) command to send a signal to another process.

It is possible to require the kernel to be processed in one of the following three ways when a signal is present, which we call a signal processing or signal-related action.

    • Ignore this signal. Most signals can be processed using this method, but there are two types of signals that must not be ignored: Sigkill and Sigstop. The reason these two signals cannot be ignored is that they provide a reliable way for the superuser to terminate or stop the process. In addition, if some signal generated by a hardware exception is omitted (for example, divided by 0), the running behavior of the process is undefined.
    • Capture the signal. To do this, notify the kernel to invoke a user function when a signal occurs. In the user function, the user is expected to perform the processing of this event. Note that the Sigkill and sigstop signals cannot be captured.
    • Executes the system default action. Note that the system default action for most signals is terminate

1#include <stdio.h>2#include <unistd.h>3#include <errno.h>4#include <stdlib.h>5#include <signal.h>6 7 Static voidSig_child (intsigno);8 9 intMain ()Ten { One pid_t pid; A     //Create a snap subprocess exit signal - signal (sigchld,sig_child); -PID =fork (); the     if(PID <0) -     { -Perror ("Fork Error:"); -Exit1); +     } -     Else if(PID = =0) +     { Aprintf"I am child process,pid ID%d.i am exiting.\n", Getpid ()); atExit0); -     } -printf"I am father process. I'll sleep and the seconds\n"); -     //wait for the child process to exit first -Sleep2); -     //Output Process Information inSystem"Ps-o Pid,ppid,state,tty,command"); -printf"father Process is exiting.\n"); to     return 0; + } -  the Static voidSig_child (intSigno) * { $ pid_t pid;Panax Notoginseng      intStat; -      //Handling Zombie Processes the       while(PID = Waitpid (-1, &stat, Wnohang)) >0) +printf"Child %d terminated.\n", PID); A}
View Code

Five. Processes and Pipelines

Reference:

    • Orphan process and zombie process [summary]

    • Process and Signal

Communication between Linux processes

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.