计算机操作系统(1)

ex_a.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define key 80

struct msg {
   long mtype;
   int  pid;
   char tip[128];
};
int mlen=sizeof(struct msg) -sizeof(long);

main()
{
     int pid=getpid();
     struct msg m;

     int qid;


     qid=msgget(key,IPC_CREAT|777);

     printf("qid=%d,A.pid=%d\n",qid,pid);
     
     msgrcv(qid,&m,mlen,1,0);

     printf("A:pid=%d,tip=%s\n",m.pid,m.tip);
     
     m.mtype=m.pid;
     m.pid=pid;
     strcpy(m.tip,"REP from A process!");
   
     msgsnd(qid,&m,mlen,0);

     getchar();

     msgctl(qid,IPC_RMID,0);

}   

ex_b.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define key 80

struct msg {
   long mtype;
   int  pid;
   char tip[128];
};
int mlen=sizeof(struct msg) -sizeof(long);

main()
{
     int pid=getpid();
     struct msg m;

     int qid;


     qid=msgget(key,777);

     printf("B:qid=%d,A.pid=%d\n",qid,pid);
     

     m.mtype=1;
     m.pid=pid;
     strcpy(m.tip,"REQ from B process!");
   
     msgsnd(qid,&m,mlen,0);


     msgrcv(qid,&m,mlen,pid,0);

     printf("B RCV: pid=%d,tip=%s\n",m.pid,m.tip);

}   

 

posted @ 2022-11-24 21:18  马铭阳  阅读(22)  评论(0)    收藏  举报