0
点赞
收藏
分享

微信扫一扫

实验 进程通信【操作系统】


实验 进程通信

  • ​​推荐​​
  • ​​实例1​​
  • ​​代码​​
  • ​​msgque.c​​
  • ​​结果​​
  • ​​实例2​​
  • ​​代码​​
  • ​​msgsnd.c​​
  • ​​msgrcv.c​​
  • ​​结果​​
  • ​​实例3​​
  • ​​代码​​
  • ​​comm.h​​
  • ​​comm.c​​
  • ​​server.c​​
  • ​​client.c​​
  • ​​Makefile​​
  • ​​结果​​

推荐

​​linux 进程间通信三 消息队列以及实例​​​​【Linux】消息队列–实现进程间通信​​​​进程间通信具体实现代码(两个程序间互发信息)​​

实例1

实例1:一个程序,自己发消息,然后自己再从队列上读消息

代码

msgque.c

/*msgque.c*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUFSZ 512

struct message
{
long msg_type;
char msg_text[BUFSZ];
};

int main()
{
int qid;
key_t key;
int len;
struct message msg;

/*根据不同的路径和关键表示产生标准的key*/
if ((key = ftok(".", 'a')) == -1)
{
perror("ftok");
exit(1);
}

/*创建消息队列*/
if ((qid = msgget(key,IPC_CREAT|0666)) == -1)
{
perror("msgget");
exit(1);
}

printf("Opened queue %d\n",qid);
puts("Please enter the message to queue:");

if ((fgets((&msg)->msg_text, BUFSZ, stdin)) == NULL) //消息内容
{
puts("no message");
exit(1);
}

msg.msg_type = getpid(); //消息类型,可以理解为发信人名字
len = strlen(msg.msg_text);

/*添加消息到消息队列*/
if ((msgsnd(qid, &msg, len, 0)) < 0) //把消息加到队列
{
perror("message posted");
exit(1);
}

/*读取消息队列*/
if (msgrcv(qid, &msg, BUFSZ, getpid(), 0) < 0) //读发信人为getpid()的消息
{
perror("msgrcv");
exit(1);
}

printf("message is:%s\n",(&msg)->msg_text);

/*从系统内核中移走消息队列 */
if ((msgctl(qid, IPC_RMID, NULL)) < 0)
{
perror("msgctl");
exit(1);
}

exit(0);
}

结果

实验 进程通信【操作系统】_操作系统

实例2

实例2:一个程序发送消息 ,另一个接收消息,读的是第一条消息不判断是不是自己想要的消息

代码

msgsnd.c

/* msgsnd.c */


#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUFFER_SIZE 512


struct message
{
long msg_type;
char msg_text[BUFFER_SIZE];
};


int main()
{
int qid;
key_t key;
struct message msg;

/*根据不同的路径和关键表示产生标准的key*/
if ((key = ftok(".", 'a')) == -1)
{
perror("ftok");
exit(1);
}

/*创建消息队列*/
if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
{
perror("msgget");
exit(1);
}
printf("Open queue %d\n",qid);

while(1)
{
printf("Enter some message to the queue(enter 'quit' to exit):");
if ((fgets(msg.msg_text, BUFFER_SIZE, stdin)) == NULL)
{
puts("no message");
exit(1);
}

msg.msg_type = getpid();

/*添加消息到消息队列*/
if ((msgsnd(qid, &msg, strlen(msg.msg_text), 0)) < 0)
{
perror("message posted");
exit(1);
}

if (strncmp(msg.msg_text, "quit", 4) == 0)
{
break;
}
}

exit(0);
}

msgrcv.c

/* msgrcv.c */

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUFFER_SIZE 512

struct message
{
long msg_type;
char msg_text[BUFFER_SIZE];
};

int main()
{
int qid;
key_t key;
struct message msg;

/*根据不同的路径和关键表示产生标准的key*/
if ((key = ftok(".", 'a')) == -1)
{
perror("ftok");
exit(1);
}

/*创建消息队列*/
if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
{
perror("msgget");
exit(1);
}
printf("Open queue %d\n", qid);

do
{
/*读取消息队列*/
memset(msg.msg_text, 0, BUFFER_SIZE);
if (msgrcv(qid, (void*)&msg, BUFFER_SIZE, 0, 0) < 0) //读取消息不管是谁发的
{
perror("msgrcv");
exit(1);
}
printf("The message from process %d : %s", msg.msg_type, msg.msg_text);

} while(strncmp(msg.msg_text, "quit", 4));


/*从系统内核中移走消息队列 */
if ((msgctl(qid, IPC_RMID, NULL)) < 0)
{
perror("msgctl");
exit(1);
}

exit(0);
}

结果

实验 进程通信【操作系统】_操作系统_02

实例3

​​进程间通信具体实现代码(两个程序间互发信息)​​

设计两个程序,要求用消息队列实现聊天程序。
输入bye,结束聊天

代码

comm.h

#ifndef _COMM_H_
#define _COMM_H_

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#define SERVER 1
#define CLIENT 2
#define PATHNAME "."
#define PROJ_ID 0x6666
struct msgbuf{
long mtype;
char mtext[1024];
};
int creat();//创建消息队列
int send(int msgid,int who,char *msg); //发送消息
int recv(int msgid, int type, char out[]);//接收消息
int destory(int msgid);//销毁消息队列
int get();//获得消息队列的标识符
#endif

comm.c

#include "comm.h"
static int com(int flags){
key_t key=ftok(PATHNAME,PROJ_ID);
if(key<0){
perror("ftok");
return -1;
}

int msgid=msgget(key,flags);
if (msgid<0){
perror("msgget");
return -2;

}
return msgid;
}
int creat(){
return com(IPC_CREAT|IPC_EXCL|0666);
}
int destory(int msgid){

if(msgctl(msgid,IPC_RMID,NULL)<0){
perror("msgctl");
return -1;
}

}
int get(){
return com(IPC_CREAT);
}
int send(int msgid,int who,char*msg){
struct msgbuf buf;
buf.mtype=who;
strcpy(buf.mtext,msg);
if(msgsnd(msgid,(void*)&buf,sizeof(buf.mtext),0)<0) {
perror("msgsnd");
return -1;
}
}
int recv(int msgid,int type,char out[]){
struct msgbuf buf;
if(msgrcv(msgid,(void*)&buf,sizeof(buf.mtext),type,0)<0){
perror("msgrcv");
return -1;
}
strcpy(out,buf.mtext);
return 0;

}

server.c

#include"comm.h"
int main(){
int msgid=creat();
char buf[1024];
while(1){
buf[0]=0;
recv(msgid,CLIENT,buf);
printf("client say #%s\n",buf);
if(strcmp(buf,"bye\n")==0){
printf("通信结束\n");
break;
}
printf("please enter#");
fflush(stdout);
ssize_t s=read(0,buf,sizeof(buf)-1);
if(s>0){
buf[s]=0;
send (msgid,SERVER,buf);
}

}
destory(msgid);
return 0;
}

client.c

#include"comm.h"
int main(){
int msgid=get();
char buf[1024];
while(1){
buf[0]=0;
printf("please enter#");
fflush(stdout);

ssize_t s=read(0,buf,sizeof(buf)-1);
if(s>0){
buf[s]=0;
send (msgid,CLIENT,buf);
}
recv(msgid,SERVER,buf);
printf("server say #%s\n",buf);
if(strcmp(buf,"bye\n")==0){
printf("通信结束\n");
break;
}
}
destory(msgid);
return 0;
}

Makefile

.PHONY:all
all:server client

server:server.c comm.c
gcc -o $@ $^
client:client.c comm.c
gcc -o $@ $^
.PHONY:clean
clean:
rm -f server client

结果

实验 进程通信【操作系统】_linux_03

实验 进程通信【操作系统】_linux_04

实验 进程通信【操作系统】_linux_05


举报

相关推荐

0 条评论