Process learning pipeline and message queue in UNIX

Source: Internet
Author: User

 

 

LINUX multi-process

A zombie process has been terminated but has not been cleared from the Progress table.
If the child process is terminated before the parent process, and the parent often does not call wait to receive information about the child process, the child process will be converted into a zombie process until the parent process ends.
Methods To avoid zombie Processes
1) The wait parent process takes the initiative to call wait (or call it after receiving the signal) to receive the death report of the child process and release the system progress table resources occupied by the child process.
2) If the parent process dies before the child process, all its child processes are adopted by the INIT process.
3) Ignore the sigc (h) LD signal. When the parent process ignores this signal, even if wait is not executed, the child process will not generate a zombie process when it ends.
4) Capture sigc (h) LD signal parent process capture sigc (h) LD signal, and wait for wait sub-process in the capture function.

Daemon
A daemon is a process that runs on the background for a long time. It is independent of the control terminal and periodically executes a task.
Writing a daemon includes at least the following items:
1) Implement the runtime method in the background
2) independent control terminal setsid ()
3) Clear the file creation mask umask (0)
4) processing signals

Query IPC objects
IPCS
Delete An IPAC object
Ipcrm
All three IPC objects have UNIX restrictions, such as the maximum number of bytes in the message queue and the maximum number of signals in the semaphore. You can change the kernel parameters.

 

Inter-process communication
Inter-process communication has MPs queue semaphores shared memory
MPs queue:
Unknown pipe: int pipe (INT Fildes [2])
In UNIX, pipe and popen functions are used to create an unknown pipeline. pclose is used to close an unknown pipeline.
Create mknod and mkfifo, and create mkfifo.
An unknown MPs queue is used in parent and child processes. A famous MPs queue is stored in external storage devices such as disks as files and can be used in any two processes.

Message Queue
IPCS-a-Q

Currently, there are two main types of Message Queues: POSIX message queues and System V message queues. System V message queues are widely used. Considering the portability of applications, newly developed applications should use POSIX message queues whenever possible.

The System V Message Queue continues with the kernel. It is deleted only when the kernel restarts or explicitly deletes a message queue. Therefore, the data structure (struct ipc_ids msg_ids) of the Message Queue recorded in the system is located in the kernel. All message queues in the system can find the access entry in the msg_ids structure. A message queue is a linked list of messages. Each message queue has a queue header, which is described by the structure struct msg_queue. The queue header contains a large amount of information about the message queue, including the key value, user ID, group ID, and number of messages in the message queue, the ID of the read/write process of the message queue. You can access this information or set some information.

Message Queue is a first-in-first-out linked list structure in the Unix kernel.
Managing message queues in the msgid_s structure of UNIX
Create an int msgget (key_t key, int msgflag) for a message queue)
Message Queue sends int msgsnd (INT msgid, void * msgp, int msgsz, int msgflg)
Message Queue receiving int msgrcv (INT msgid, void * msgp, int msgsz, long msgtype, int msgflg)
Message Queue control int msgctl (INT msgid, int cmd, struct msgid_ds * BUF)

Message Queue communication is more flexible than pipeline communication. the communication process does not have the kinship requirement, nor does it need to be synchronized. the sending process can write messages at any time or end execution before receiving messages. pipeline, whether it is an unknown pipeline or a famous Pipeline, both processes of communication must be running.

Message Queue is a type of IPC object. It has the following features compared with the pipeline that provides the first-in-first-out service.
1) The Message Queue provides the automatic splitting function for message data, and cannot receive messages sent twice at the same time.
2) A Message Queue provides services that are not completely read randomly. After a message type is introduced, a queue can logically become a linked list of different message types, you can choose to receive messages from a logical linked list instead of the first message in the queue.
3) The Message Queue provides a completely asynchronous read/write service. When using the MPs queue, both read and write ends must be opened at the same time. Without this restriction, the process can open the MQ at any time, you can also read messages that do not exist from the queue.

Msgget is used to create a message queue, msgsnd is used to send a message queue, msgrcv is used to receive messages from a message queue, and msgctl is used to control message queues, it can query the data structure of message queues, change the access permissions of message queues, change the owner information of message queues, and delete message queues.

 

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.