Give a network MM help

xiaoxiao2021-03-06  52

LINUX process communication research

Linux implements inter-process communication methods include PIPE, FIFO pipeline, message queue, semaphore, memory sharing, etc. In Linux process communication, PIPE is more useful because of simple and half-duplex communication. FIFO can be developed on the PIPE and achieve two-way communication mechanisms, which can be used between communication between different ancestors.

The message queue is based on a shared implementation of multi-process communication needs, and can transmit large amounts of data, each process can get data in the shared queue.

Research on Linux IPC Message Queue implements a multi-process communication program. The program execution environment is in Redhat Linux 9.0, GCC-2.95.

The program is divided into two parts, a part is a background program BGT, which is responsible for the response to other processes, and the other is an interrogator OGT. Ask the content is simple, but the full frame of Message Queue IPC is implemented, which has more meaningful accumulation for future development.

Back Ground Task is implemented, I call it BGT

BGT.C

#include

#include

#include

#include

#include

#include

#include

#include

#include "comm.h"

Static int msg_que_id = -1;

Void catch_sig_term (int Signo)

{

int R;

Rev = msgctl (msg_que_id, ipc_rmid, 0);

IF (REV <0) {

Printf ("Remove the Queue Fail./N);

EXIT (-1);

}

Printf ("Remove the Queue OK / N);

exit (0);

}

int main ()

{

Struct my_msg limited_msg, send_msg;

INT RECE_LEN, Send_len, Result

Struct SigAction Action;

Int Num;

MSG_QUE_ID = Msgget (my_ipc_key, ipc_creat | 0666);

IF (MSG_QUE_ID <0) {

Printf ("CREATE IPC Queue Fail");

EXIT (-1);

}

Action.sa_handler = catch_sig_term;

SiGemptySet (& Action.SA_MASK);

Action.sa_flags = 0;

SigAction (Sigterm, & Action, NULL);

Action.sa_handler = SIG_IGN;

SiGemptySet (& Action.SA_MASK);

Action.sa_flags = 0;

SigAction (Sigint, & Action, NULL);

SigAction (SIGQUIT, & ACTION, NULL);

SigAction (SIGHUP, & ACTION, NULL);

While (1) {

RECE_LEN = MSGRCV (MSG_QUE_ID, & RECE_MSG, (SIZEOF (STRUCT MY_MSG) - SIZEOF (Long), MyInput, 0); IF (Rece_len <0) {

Printf ("BGT Receive Message Error / N");

Raise (SIGTERM);

}

Send_msg.m_type = Rece_msg.Process_ID;

NUM = Rece_msg.m_buf [0];

Switch (NUM) {

Case 1:

Send_msg.m_buf [0] = 1;

Break;

Case 2:

Send_msg.m_buf [0] = 2;

Break;

Case 3:

Send_msg.m_buf [0] = 3;

Break;

DEFAULT:

;

}

Send_len = sizeof (int) sizeof (int);

Result = msgsnd (MSG_QUE_ID, & Send_msg, send_len, 0);

IF (Result <0) {

Printf ("BGT Send Message Fail./N");

Raise (SIGTERM);

}

}

Return 0;

}

This program is an inquiry program that can run multiple programs at the same time, and the background program BGT will send the corresponding echo.

OGT.C

#include

#include

#include

#include

#include

#include

#include "comm.h"

Static int msg_que_id = -1;

Int main (int Argc, char * argv [])

{

Int results, send_len, reca;

STRUCT MY_MSG Send_msg, Rece_MSG;

INT Question_num;

MSG_QUE_ID = Msgget (my_ipc_key, 0);

IF (MSG_QUE_ID <0) {

Printf ("Message Queue Use Error./N");

EXIT (-1);

}

While (1) {

Printf ("/ n / n ----------------------------- Linda Documnet ------------ ---------------- / n / n ");

Printf ("You Now Enter Linda Document, You Must Be Valid./n Any Contract Linda./n Thanks./N");

Printf ("You CAN Ask 3 Questions: / N");

Printf ("1. How Many Firends Linda Have? / n");

Printf ("2. How much money linda spend one day? / n");

Printf ("3. What is linda like? / n / n");

Printf ("Please select Question Number 1, 2 or 3./N");

Scanf ("% d", & quhip_num);

Send_msg.m_type = myinput; send_msg.process_id = get pid ();

Send_msg.m_buf [0] = quothes_num;

Send_len = sizeof (int) sizeof (int);

Result = msgsnd (MSG_QUE_ID, & Send_msg, send_len, 0);

IF (Result <0) {

Printf ("OGT Send Message Fail./N);

EXIT (-1);

}

RECE_LEN = MSGRCV (MSG_QUE_ID, & RECE_MSG, (SIZEOF (Struct My_MSG) - SIZEOF (LONG), GetPID (), 0);

IF (Rece_len <0) {

Printf ("OGT Receive Message Fail./N");

EXIT (-1);

}

Switch (Rece_Msg.m_buf [0]) {

Case 1:

Printf ("Linda Have a Lot of Firends./N");

exit (0);

Case 2:

Printf ("Linda Spend ABOUT 10 RMB./N");

exit (0);

Case 3:

Printf ("Linda Like Life./N");

exit (0);

DEFAULT:

;

}

}

Return 0;

}

The execution program is simple, enter the program

Make

./bgt &

./ogt

Three cases may occur, as follows:

The first case:

----------------------------------------------------------------------------------------------------------------- -----------

You now Enter Linda Document, You Must Be Valid.

Any Contract Linda.

THANKS.

You can ask 3 quintions:

How Many Firends Linda Have?

2. How much Money Linda Spend One Day?

3. What is linda like?

Please select Question Number 1, 2 OR 3.

1

Linda Have A Lot of Firends.

Second in the second situation:

[root @ localhost linda] # ./ogt

----------------------------------------------------------------------------------------------------------------- -----------

You now Enter Linda Document, You Must Be Valid.

Any Contract Linda.

THANKS.

You can ask 3 quintions:

How Many Firends Linda Have?

2. How much Money Linda Spend One Day?

3. What is linda like?

Please select Question Number 1, 2 OR 3.

2

Linda Spend ABOUT 10 RMB.

The third case:

[root @ localhost linda] # ./ogt

----------------------------------------------------------------------------------------------------------------- -----------

You now Enter Linda Document, You Must Be Valid.Any Contract Linda.

THANKS.

You can ask 3 quintions:

How Many Firends Linda Have?

2. How much Money Linda Spend One Day?

3. What is linda like?

Please select Question Number 1, 2 OR 3.

3

Linda Like Life.

Linda

2004.2

转载请注明原文地址:https://www.9cbs.com/read-57674.html

New Post(0)