Operating System - Linux Interprocess Communication

1: Experimental topic

Linux interprocess communication

Two: The purpose of the experiment

The process communication mechanism (IPC) of the Linux system allows mass exchange of data between arbitrary processes. Through this experiment, understand

Familiar with the message communication mechanism supported by Linux.

Three: Experimental content (experimental principle/theoretical knowledge used, algorithm/program flow chart, steps and methods, key codes)

Experimental principle: 4 functions are mainly used

int msgget(key_t key, int msgflag);

key: the name of a message queue, generated by frok() (the return value of the function frok) or IPC_PRIVATE, to obtain the identifier of the message queue, create and access a message queue.

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

msqid: message queue identifier, obtained by msgget;

cmd: control identifier, IPC_SET command: set the owner's user identifier and group identifier, IPC_STAT and IPC_INFO commands: obtain resource status information, IPC_RMID command: release this resource;

buf: message queue management structure, please refer to the description of message queue kernel structure.

int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

msqid: message queue identifier;

msgp: Messages sent to the queue. msgp can be any type of structure, but the first field must be of long type, which indicates the type of the sent message, and msgrcv receives the message according to this. The reference format defined by msgp is as follows:

    struct s_msg{ /*The reference format defined by msgp*/

     long type; /* must be greater than 0, message type*/

           char mtext[256]; /*message text, can be any other type*/

    } msgp;

msgsz: the size of the message to be sent, excluding the 4 bytes occupied by the message type, that is, the length of mtext;

msgflg: Control bit that specifies what to do when the core runs out of internal buffer space

ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);

msqid: message queue identifier;

msgp: the structure for storing messages, the type of the structure should be the same as the type sent by the msgsnd function;

msgsz: the size of the message to be received, excluding the 4 bytes occupied by the message type;

msgtyp: 0: receive the first message; >0: receive the first message whose type is equal to msgtyp; <0: receive the first message whose type is equal to or less than the absolute value of msgtyp;

msgflg: Specifies what the core should do if there is no message in the queue. If the IPC_NOWAIT flag is set at this time, it will return immediately. If MSG_NOERROR is set in the flag, and the size of the received message is greater than size, the core will truncate the received message .

There are three conditions for msgrcv() to unblock:

1. There are messages that meet the conditions in the message queue.

2. The message queue represented by msqid is deleted.

3. The process calling msgrcv() is interrupted by a signal.

Use the above four functions to realize the communication under the Linux system.

Four: Experimental results and analysis

 

Result analysis:

The program first creates a message queue for message passing between processes. Use the msgget() function to create a message queue, and specify the key value of the message queue as MSGKEY.

The program then creates a child process by calling the fork() function. Call the SERVER() function in the child process as the server process, and call the CLIENT() function in the parent process as the client process.

The server process receives the message by calling the msgrcv() function and prints "(Server) received". The server process will keep receiving messages until it receives a message with a message type of 1.

The client process sends a message to the message queue by calling the msgsnd() function, and prints out "(client) sent". The client process sends 10 messages of different types, from 10 to 1.

After the server process receives the message whose message type is 1, it calls the msgctl() function to delete the message queue, and exits the process.

The main process waits for the end of the child process by calling the wait() function twice.

analyze:

The client process runs first and continuously sends 10 messages to the message queue.

The server process prints "(Server) received" when it receives the first message, and then goes into a loop waiting to receive other messages.

When the server process receives a message of type 1, the tenth message, print "(Server) received".

The main process waits for the child process to finish.

Please note that due to the uncertainty of process scheduling and the asynchronous nature of message passing, actual running results may vary. The above results represent only one possible scenario.

5. Summary and experience

1. Inter-process communication: This experiment implements inter-process communication through message queues. The server process receives messages through the msgrcv() function, and the client process sends messages through the msgsnd() function. Message queues provide a reliable communication mechanism that enables processes to interact asynchronously.

2. Message queue: A message queue is a container for storing messages. Processes can send messages to the queue and receive messages from the queue. Each message has a message type that identifies different messages. In this experiment, the server process receives all types of messages by specifying a message type of 0 until a message of type 1 is received.

3. Inter-process synchronization: In the experiment, the server process will not exit until it receives a specific type of message. This method realizes the synchronization between processes and ensures that the server process waits until the specified message is received.

4. Use of the fork() function: In the experiment, the fork() function was used to create a child process. By calling the fork() function multiple times, the purpose of creating a server process and a client process is achieved. The child process will inherit the message queue identifier of the parent process, so messages can be sent and received.

5. Collaboration between parent and child processes: The child processes created by the fork() function are executing different functions, realizing the collaboration between parent and child processes. The client process sends messages, the server process receives messages, and cooperates with each other to complete the communication task.

6. Asynchronous messaging: Since messaging is asynchronous, the client process can send multiple messages quickly, and the server process can receive them at its own pace. This asynchrony can improve the response speed and concurrency performance of the system.

Guess you like

Origin blog.csdn.net/CSH__/article/details/131382643