该例子主要是读取两个文件,然后把内容分别放入消息队列,类型分别为1,2
由接受进程根据类型读出
filesend.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "user.h"
close_all(int msgid,int fd1,int fd2)
{
close(fd1);
close(fd2);
msgctl(msgid,IPC_RMID,0);
}
main(int argc,char **argv)
{
int fd1,fd2,msgid;
int end1=1,end2=1;
key_t key;
struct msg_data data1,data2;
if(argc!=3)
{
printf("usage:argv[0]file1 file2<CR>\n");
exit(-1);
}
if((fd1=open(argv[1],O_RDONLY))==-1)
{
printf("%s can not opened\n",argv[1]);
exit(-1);
}
if((fd2=open(argv[2],O_RDONLY))==-1)
{
printf("%s can not opened\n",argv[2]);
exit(-1);
}
if((key=ftok("/tmp",'a'))==-1)
{
close(fd1);
close(fd2);
exit(-1);
}
if((msgid=msgget(key,IPC_CREAT|IPC_EXCL|0666))==-1)
{
close(fd1);
close(fd2);
exit(-1);
}
printf("\n send:messages queue created!\n");
data1.type=F_TYPE1;
data2.type=F_TYPE2;
data1.size=read(fd1,data1.buf,BUF_SIZE);
data2.size=read(fd2,data2.buf,BUF_SIZE);
while(data1.size||data2.size)
{
if(data1.size)
{
if(msgsnd(msgid,(struct my_msgbuf*)&data1,sizeof(struct msg_data),0)==-1)
{
printf("send error!\n");
exit(-1);
}
printf("read file1!\n");
data1.size=read(fd1,data1.buf,BUF_SIZE);
}
if(data2.size)
{
if(msgsnd(msgid,(struct my_msgbuf*)&data2,sizeof(struct msg_data),0)==-1)
{
printf("send error!\n");
exit(-1);
}
printf("read file2!\n");
data2.size=read(fd2,data2.buf,BUF_SIZE);
}
}//end while
printf("sender:data transmission to message queue completed!\n");
while(end1 && end2)
{
struct my_msgbuf rec;
if(msgrcv(msgid,&rec,sizeof(struct my_msgbuf),TYPE_END1,IPC_NOWAIT)!=-1)
{
printf("the file1 end!\n");
end1=0;
}
if(msgrcv(msgid,&rec,sizeof(struct my_msgbuf),TYPE_END2,IPC_NOWAIT)!=-1)
{
printf("the file2 end!\n");
end2=0;
}
}
printf("sender:disconnection of the two receiver process!\n");
close_all(msgid,fd1,fd2);
}
filereceive.c
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include "user.h"
main(int argc,char **argv)
{
int fd,ret,msgid;
int no;
int type;
key_t key;
struct msg_data data;
struct my_msgbuf dis_msg;
if(argc!=3)
{
printf("usage:argv[0]file1 file2<CR>\n");
exit(-1);
}
no=atoi(argv[1]);
if(no!=1&&no!=2)
{
printf("error!\n");
exit(-1);
}
if(no==1)
{
printf("the no is %d",no);
type=F_TYPE1;
}
else
{
printf("the no is %d\n",no);
type=F_TYPE2;
}
if((key=ftok("/tmp",'a'))==-1)
{
printf("create key failed!\n");
exit(-1);
}
printf("create key success!\n");
if((msgid=msgget(key,IPC_EXCL))==-1)
{
printf("create msgid failed!\n");
exit(-1);
}
if((fd=open(argv[2],O_WRONLY))==-1)
{
printf("can not open file!\n");
}
printf("receive data begin!\n");
while(data.size)
{
if(msgrcv(msgid,(struct my_msgbuf*)&data,sizeof(struct msg_data),type,0)==-1)
{
printf("receive error!\n");
exit(-1);
}
printf("receive data success!\n");
printf("the data size is %d\n",data.size);
if(data.size<=0)
{
printf("the data size is 0\n");
break;
}
ret=write(fd,data.buf,data.size);
printf("the file2 isd %d\n",ret);
if(ret==-1)
{
close(fd);
msgctl(msgid,IPC_RMID,0);
printf("receive!\n");
exit(-1);
}
break;
}
close(fd);
printf("receive:data transmission to message queue completed!\n");
if(no==1)
{
dis_msg.mtype=TYPE_END1;
}
else
{
dis_msg.mtype=TYPE_END2;
}
msgsnd(msgid,&dis_msg,sizeof(struct my_msgbuf),0);
}
user.h文件
#define F_KEY "copyfile"
#define F_TYPE1 1
#define F_TYPE2 2
#define TYPE_END1 3
#define TYPE_END2 4
#define BUF_SIZE 256
struct msg_data
{
long type;
int size;
char buf[BUF_SIZE];
};
struct my_msgbuf
{
long int mtype;
char mtex[256];
};
编译运行:
gcc -o send filesend.c
gcc -o recv filereceive.c
./send dbfile1 dbfile2
./recv 1 file1
./recv 2 file2
转载于:https://blog.51cto.com/feciceo/893036