From: http://bbs.chinaunix.net/forum.php? Mod = viewthread & tid = 4077304.
1. linux inter-process communication
Inheriting unix Process Communication: Pipeline Signal
AT&T: system v ipc communication process can only share memory on a single computer: semaphore message queue
BSD: socket-based inter-process communication mechanism TCP/IP
2. Pipelines
(1) unknown pipeline: Parent and Child Processes
# Include <unistd. h>
Int pipe (int pipefd [2]);
Create an MPS queue
Fd [0]: Read end
Fd [1]: Write end
Return Value:
0: Successful
-1: Failed
Note: (1) when the pipeline is full, write pipe will be blocked.
(2) When the MPs queue is empty, read pipe will be blocked.
(2) famous Pipeline: Any process
# Include <sys/types. h>
# Include <sys/stat. h>
Int mkfifo (const char * pathname, mode_t mode );
3. Signal
A simulated asynchronous communication method for interruptions at the software level.
Interruption: cpu interruption (hardware)
Signal: Process interruption (software)
(1) Signal Source
(1) program execution errors such as memory access out of bounds
(2) other processes send
(3) Send ctrl + c through the control terminal;
(4) The child process ends and sends a signal SIGCLD to the parent process.
(5) timer SIGALRM
(2) Signal
Kill-l view all signals of the current system
(3) Signal Processing Method
Ignore signal: Do not process any signal
Capture signal: Define the processing function to execute the corresponding processing function when the signal occurs.
Default operation: the default operation is specified in the linux system.
(4) signal transmission and capturing
# Include <sys/types. h>
# Include <signal. h>
Int kill (pid_t pid, int sig );
Pid: PID of the process that receives the signal
Sig: Signal
Return Value: 0: Successful
-1: Error
Int raise (int sig );
// Kill (getpid (), SIGSTOP );
Send a signal to the process itself
(5) timer signal capture
Unsigned int alarm (unsigned int seconds );
Process Timer
Scheduled time to send SIGALRM
SIGALRM: Default operation: Terminate the process
Return Value:
If the alarm has been set before the call, the last remaining time will be returned.
Otherwise, 0 is returned.
-1: Error
Int pause (void );
Pause a process
When a signal is received, the process will be awakened to continue execution.
(6) Signal Processing
# Include <signal. h>
Typedef void (* sighandler_t) (int );
Sighandler_t signal (int signum, sighandler_t handler );
Parameters:
Signum: Signal
Handler: SIG_IGN: Ignore this signal
SIG_DFL: Signal Processing by default
Pointer to a custom Signal Processing Function
Return Value: returns an address pointing to the signal processing function.
1) The parent process captures the signal of the child process.
2) Input text from the terminal and output it to the terminal again. If there is no input for 3 s, a prompt is displayed.
// SIGALRM
Alarm () and signal ()
4. Signal Blocking
(1) notify the system kernel to stop sending the specified signal to the Process
(2) The kernel caches the corresponding signals received by the process.
(3) When the process unblocks the corresponding signal
Set the reason for blocking:
(1) There are other signals when the signal processing function is being executed.
(2) The signal processing function and other processes read and write a shared area.
Sigset_t: Signal Set
Int sigemptyset (sigset_t * set );
Clear the Signal Set
Int sigfillset (sigset_t * set );
Fill in the Signal Set
Int sigaddset (sigset_t * set, int signum );
Add the corresponding signal to the blocked Signal Set
Int sigdelset (sigset_t * set, int signum );
Delete the corresponding signal from the blocked Signal Set
Int sigismember (const sigset_t * set, int signum );
Determine whether the signal is in the blocked Signal Set
Int sigprocmask (int how, const sigset_t * set, sigset_t * oldset );
// Set the blocking Signal Set
How: Set the signal blocking mask
SIG_BLOCK: blocked Signal Set
SIG_UNBLOCK: Relief Signal Set
SIG_SETMASK: sets the blocking mask.
Oldset: The old blocking set
Int sigpending (sigset_t * set );
// Obtain the blocked pending Signal
Int flag = 0;
Int flag = 1;
While (flag = 0 ){
Int sigsuspend (const sigset_t * mask );
}
// Waiting signal
(1) set the signal mask and block the process
(2) receive the signal to restore the original shielded word
(3) Call the signal processing function set by the Process
(4) sigsuspend () returns after the signal processing function returns
Atomic operation
Pause () ----- wait signal (except for blocked signals)
5. Message Queue
(1) message list
Queue ID
Message ID
(2) create
# Include <sys/types. h>
# Include <sys/ipc. h>
# Include <sys/msg. h>
Int msgget (key_t key, int msgflg );
Key: the specified ID to generate the queue ID. key: ftok () is obtained through the conversion file.
Msgflg: IPC_CREAT: Creates a message queue.
IPC_EXCL: an error is reported.
IPC_NOWAIT: non-blocking
Returned value: the ID of the returned queue.
# Include <sys/types. h>
# Include <sys/ipc. h>
Key_t ftok (const char * pathname, int proj_id );
Function: Get key
Pathname: file name ----> inode node number
Proj_id: specified by yourself
Merged by inode node number and proj_id
65538: 0x10002
38: 0x26
Key: 0x2610002
(4) receive messages
Ssize_t msgrcv (int msqid, void * msgp, size_t msgsz, long msgtyp, int msgflg );
Parameters:
Msqid: Message Queue ID ----- msgget ()
Msgp: Message Buffer
Msgsz: size of the message structure
Msgtyp: 0: receives the first message in the message queue.
Greater than 0: receives the first message in the message queue, Which is msgtyp.
Smaller than 0: the absolute value of the first message in the received message queue is not less than msgtyp.
Msgflg:
0: Ignore
MSG_NOERROR: If the received message is larger than the size, the message will be truncated to the size byte and will not be notified to the message sending process.
IPC_NOWAIT: If no message of the specified type is displayed, the error ENOMSG is returned.
Returned value: the actual number of bytes received.
Will delete the corresponding message
(5) send messages
Int msgsnd (int msqid, const void * msgp, size_t msgsz, int msgflg );
Parameters:
Msqid: Message Queue ID ----- msgget ()
Msgp: The sent message buffer.
Struct msgbuf {
Long mtype; // Message Type
Char mtext [1]; // message content
};
Msgsz: Message content size
Msgflg:
IPC_NOWAIT: if the sending condition is not met, the system will return immediately.
A): the queue message is full.
Create two child processes parent processes responsible for sending sub-processes 1: sending a message sub-process with the sending type 1 2: sending a message with the message type 2
Subprocess 1 receives messages of type 1
Subprocess 2 receives messages of type 2
(5) control functions
Int msgctl (int msqid, int cmd, struct msqid_ds * buf );
Msqid: Message Queue ID
Cmd:
IPC_STAT: Get the struct msqid_ds structure and save it to buf.
IPC_SET: sets the struct msgqid_ds structure.
IPC_RMID: deletes a message queue.
Buf: stores the struct msqid_ds Structure
Returned value: Success: 0 (IPC_STAT, IPC_SET, IPC_RMID)
Failed:-1