最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

Linux基于消息队列实现进程间通信的程序

来源:动视网 责编:小OO 时间:2025-10-02 00:50:25
文档

Linux基于消息队列实现进程间通信的程序

Linux基于消息队列实现进程间通信的程序:#include#include#include#include#include#include#defineMSGKEY175//#defineMSGKEY276structmsgform{intmtype;charmtext[1000];};structmsgformmsg1;intmsgqid1;cleanup(){msgctl(msgqid1,IPC_RMID,0);exit(0);}voidserver(){intj;for(j=0;j
推荐度:
导读Linux基于消息队列实现进程间通信的程序:#include#include#include#include#include#include#defineMSGKEY175//#defineMSGKEY276structmsgform{intmtype;charmtext[1000];};structmsgformmsg1;intmsgqid1;cleanup(){msgctl(msgqid1,IPC_RMID,0);exit(0);}voidserver(){intj;for(j=0;j
Linux基于消息队列实现进程间通信的程序:

#include

#include

#include

#include

#include

#include

#define MSGKEY1 75

//#define MSGKEY2 76

struct  msgform

  {  int mtype;

     char mtext[1000];

};

struct msgform msg1;

int  msgqid1;

cleanup( )

{ msgctl(msgqid1,IPC_RMID,0);

 

  exit(0);

}

void server( )

 { int j;

for(j=0;j<20;j++)

    signal(j,cleanup);

 msgqid1=msgget(MSGKEY1,0777|IPC_CREAT);  /*创建75#消息队列*/

do 

 {

 msgrcv(msgqid1,&msg1,1030,1,0);   /*接收消息*/

 printf("(server)received:%s\\n",msg1.mtext);

 sleep(1);

printf("(server):input the message that will be send:\\n") ;

 scanf("%s",msg1.mtext);

msg1.mtype=2;

msgsnd(msgqid1,&msg1,1024,0);     /*发送消息*/

}while(strcmp(msg1.mtext,"bye"));

exit(0);

}

void client()

{

msgqid1=msgget(MSGKEY1,0777|IPC_CREAT);  /*创建75#消息队列*/

do

{

printf("(client):input the message that will be send:\\n") ;

scanf("%s",msg1.mtext);

msg1.mtype=1;

msgsnd(msgqid1,&msg1,1024,0);     /*发送消息*/

printf("(client)send msg.mtext:%s\\n",msg1.mtext);

sleep(1);

msgrcv(msgqid1,&msg1,1030,2,0);   /*接收消息*/

printf("(client)received:%s\\n",msg1.mtext);

 

}while(strcmp(msg1.mtext,"bye"));

exit(0);

}

main( )

{  int pid1,pid2;

 while ((pid1=fork())==-1);

   if (!pid1) server( );

   system("ipcs  -q");

   while ((pid2=fork( ))==-1);

   if (!pid2) client( );

   wait(0);

   wait(0);

  system("ipcs  -q");

}

文档

Linux基于消息队列实现进程间通信的程序

Linux基于消息队列实现进程间通信的程序:#include#include#include#include#include#include#defineMSGKEY175//#defineMSGKEY276structmsgform{intmtype;charmtext[1000];};structmsgformmsg1;intmsgqid1;cleanup(){msgctl(msgqid1,IPC_RMID,0);exit(0);}voidserver(){intj;for(j=0;j
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top