Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Unix XSI IPC- message queuing routine

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Note:

Linux (2.4.22) restrictions:

The maximum number of message bytes that can be sent is 8192 queues maximum capacity bytes 16384 queues maximum queue capacity 16

Instructions for using key_t ftok (char* path,int id):

Ftok creates a key, which is the external ID number of the queue in the kernel. Because the message queue is in the kernel, only the creator and the kernel know the ID number of the queue in the kernel, so other processes cannot know the ID number of the queue in the kernel, so associate an external key to id (1-255) to return the ID number of the kernel message queue.

For other notes, please check out unix advanced environment programming, or if you have any questions to discuss, please call me back!

Server.c

# include "msg.h" # include # include # include int main (int argc, char** argv) {int queid = open_msg ("/ root", 100); while (1) {fputs ("Please enter the type to send: 1 or 2\ n", stdout); int type;scanf ("% d", & type); switch (type) {case MYTYPE_ONE: {msg_send (queid, "MYTYPE_ONE", MYTYPE_ONE); break } case MYTYPE_TWO: {msg_send (queid, "MYTYPE_TWO", MYTYPE_TWO); break;} default: {fputs ("input type error, please re-enter\ n", stdout); break;} fputs ("input: Q means exit, other means continue\ n", stdout); if (getchar () = ='q') {fputs ("exit successful! \ n ", stdout); break;} else {fputs (" continue sending messages\ n ", stdout);}} / / No queuing for exiting requires removal of del_que (queid); return 0;}

Client.c

# include "msg.h" # include # include # include int main (int argc, char** argv) {int queid = open_msg ("/ root", 100); while (1) {fputs ("Please receive Type to send: 1 or 2\ n", stdout); int type;scanf ("% d", & type); switch (type) {case MYTYPE_ONE: {msg_rec (queid,MYTYPE_ONE); break;} case MYTYPE_TWO: {msg_rec (queid,MYTYPE_TWO); break } default: {fputs ("input type error, please re-enter\ n", stdout); break;}} fputs ("input: Q means exit, other means continue\ n", stdout); if (getchar () = ='q') {fputs ("exit successful! \ n ", stdout); break;} else {fputs (" continue sending messages\ n ", stdout);}} / / queue remove del_que (queid); return 0;}

Msg.c

# include#include # include#include "msg.h" / / open if there is no queue, create int open_msg (char* path, int id) {/ / get a key of the IPC object key_t key = ftok (path, id); if (- 1 = = key) {perror ("ftok\ n"); exit (1);} / create a queue int queid = msgget (key, IPC_CREAT | 0666) If (- 1 = = queid) {perror ("msgget\ n"); exit (1);} return queid;} / send messages to queue void msg_send (key_t key,char* text, long msgtype) {/ / initialize content struct MSG tmp;memset (& tmp,sizeof (struct MSG), 0); tmp.mytype = msgtype;strcpy (tmp.mytext,text); / / send messages if (msgsnd (key, & tmp, TEXTSIZE, 0)) {perror ("msgsnd\ n"); exit (1) }} / / get a message from the message queue and display void msg_rec (key_t key,long msgtype) {struct MSG tmp;if (- 1 = = msgrcv (key, & tmp, TEXTSIZE, msgtype, MSG_NOERROR)) {perror ("msgrcv\ n"); exit (1);} printf ("receive content:% s\ n", tmp.mytext) Delete void del_que (key_t key) {if (msgctl (key,IPC_RMID,NULL)) {perror ("msgsnd\ n"); exit (1);}}

Msg.h

# ifndef MSG_H#define MSG_H#include # define TEXTSIZE 100#define ARRYSIZE 2#define MYTYPE_ONE 1#define MYTYPE_TWO 2struct MSG {long mytype;char mytext [TEXTSIZE];}; int open_msg (char*,int); void msg_send (key_t,char*,long); # endif / / end MSG_H attachment: http://down.51cto.com/data/2362206

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report