Linux Process Learning

Source: Internet
Author: User

1. Communication between Linux processes
Inherit UNIX interprocess communication: Pipeline signal
At&t:system V IPC Communication process can only be on a single computer: Semaphore Message Queuing shared memory
BSD: Creating a socket-based interprocess communication mechanism TCP/IP
2. Pipeline
(1) Nameless pipe: parent-child process
#include <unistd.h>

int pipe (int pipefd[2]);
Create a pipeline
FD[0]: Read end
FD[1]: Write End
return value:
0: Success
-1: Failure
Note: (1) when the pipe is full, the write pipe will block
(2) When the pipe is empty, the read pipe will block



(2) Well-known pipelines: between any processes
#include <sys/types.h>
#include <sys/stat.h>
int Mkfifo (const char *pathname, mode_t mode);
3. Signal
A kind of analog asynchronous communication mode of signal interruption on software level
Interrupts: Interrupts on the CPU (hardware)
Signal: Interrupt to process (software)
(1) Source of the signal
(1) Program execution errors such as memory access out of bounds
(2) Other processes sent
(3) Send Ctrl + C via control terminal;
(4) The child process ends sending a signal to the parent process SIGCLD
(5) Timer SIGALRM
(2) Signal
Kill-l View all signals from the current system
(3) Signal processing method
Ignoring signals: no processing of signals
Capture signal: Define a handler function to execute the corresponding processing function when the signal occurs
Default action: The default action is defined in the middle of the Linux system
(4) signal transmission and capture
#include <sys/types.h>
#include <signal.h>

int Kill (pid_t pid, int sig);
PID: PID of the process receiving the signal
SIG: Signal
Return value: 0: Success
-1: Error
int raise (int sig);
Kill (Getpid (), SIGSTOP);
Send a signal to the process itself

(5) Signal capture of the timer
unsigned int alarm (unsigned int seconds);
Process Timers
Timing time to send SIGALRM
SIGALRM: Default action: Terminate process
return value:
If the alarm has been set before the call returns the last time remaining
otherwise returns 0
-1: Error
int pause (void);
Pausing a process
Wakes the process to continue when the signal is received
(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 the signal
SIG_DFL: Signal Processing by default
A pointer to a custom signal handler function
Return value: Returns an address that points to the signal handler function

1) The parent process captures the signal of the child process
2) from the terminal input text output to the terminal again, if 3s no input on the output hint
Sigalrm
Alarm () and signal ()
4, the signal blocking processing
(1) Notifies the system kernel to stop sending the specified signal to the process
(2) The kernel caches the corresponding signal received by the process
(3) When the process unlocks the corresponding signal blocking

To set the cause of blocking:
(1) There are other signals coming when the signal processing function is being executed
(2) Signal processing functions and other processes to read and write to a shared area
SIGSET_T: Signal Set
int Sigemptyset (sigset_t *set);
Clear the signal set.


int Sigfillset (sigset_t *set);
Fill up the signal set
int Sigaddset (sigset_t *set, int signum);
Add the corresponding signal to the block signal set

int Sigdelset (sigset_t *set, int signum);
Remove the corresponding signal from the set of blocking signals
int Sigismember (const sigset_t *set, int signum);
Determine if the signal is in the blocking signal set
int sigprocmask (int how, const sigset_t *set, sigset_t *oldset);
Set up a blocking signal collection

How to: Set the signal blocking mask
Sig_block: Block Signal set
Sig_unblock: Releasing the signal set
Sig_setmask: Setting the blocking mask
Oldset: Old Block Collection
int sigpending (sigset_t *set);
Get blocked signal pending signal

int flag = 0;
int flag = 1;
while (flag = = 0) {
int sigsuspend (const sigset_t *mask);
}
Wait for the signal
(1) Set the signal mask and block the process
(2) receive the signal to restore the original shielding word
(3) Signal processing function for invoking process settings
(4) Wait for the signal processing function to return after Sigsuspend () returns
Atomic operation
Pause ()-----wait signal (except for blocked signals)
5. Message Queuing
(1) List of messages
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: Specify the ID to generate the queue ID Key:ftok () by converting the file to get
Msgflg:ipc_creat: Creating a new Message queue
IPC_EXCL: There is an error
Ipc_nowait: Non-blocking
Return value: Returns the queue ID
#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: Specify Yourself

Synthesized by inode node number and proj_id

65538:0x10002

38:0x26

key:0x2610002


(4) Receiving messages
ssize_t MSGRCV (int msqid, void *msgp, size_t msgsz, Long msgtyp,int MSGFLG);
Parameters:
MSQID: Message Queue ID-----msgget ()
MSGP: The buffer of the message
MSGSZ: size of message structure
Msgtyp:0: Receiving the first message in a message queue
Greater than 0: receives the first message in the message queue as a Msgtyp
Less than 0: Receive Message queue first not less than the absolute value of Msgtyp by the smallest message
MSGFLG:
0: Ignore
Msg_noerror: The message is truncated to size byte without notifying the message sending process if the message received is larger than size
Ipc_nowait: A message with no specified type will return an error enomsg
Return value: The number of bytes actually received
The corresponding message is deleted
(5) Send a message
int msgsnd (int msqid, const void *MSGP, size_t msgsz, int msgflg);
Parameters:
MSQID: Message Queue ID-----msgget ()
MSGP: Sending a message buffer
struct msgbuf{
Long mtype;//Message Type
Char mtext[1];//message Content
};
MSGSZ: The size of the message content
MSGFLG:
Ipc_nowait: When the sending condition is not satisfied, it will return immediately

A): The queue message is full

Create 2 child processes the parent process is responsible for sending the child process 1: Sending a message of type 1 to the child process 2: Sending a message with a message type of 2
Child Process 1 receives a message of type 1
Child Process 2 receives a message of type 2


(5) Control function
int msgctl (int msqid, int cmd, struct msqid_ds *buf);

MSQID: Message Queue ID
Cmd:
Ipc_stat: Get struct MSQID_DS structure saved to BUF
Ipc_set: Set struct MSGQID_DS structure
IPC_RMID: Deleting Message Queuing
BUF: storage struct MSQID_DS structure

Return value: Success: 0 (Ipc_stat,ipc_set,ipc_rmid)
Failed:-1

Linux Process Learning

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.