通过消息队列实现进程之间通信代码

#include 
struct msgbuf
{
	long int mtype; 
	char mtext[1024]; 
};
//定义一个消息大小
#define MSGSIZE sizeof(struct msgbuf)-sizeof(long int)
int main(int argc, const char *argv[])
{
	//1、创建key值以便创建消息队列
	key_t key = ftok("/", 'k');
	if(key == -1)
	{
		perror("ftok error");
		return -1;
	}
	//2、使用key值,打开一个消息队列
	int msgid;
	if((msgid=msgget(key, IPC_CREAT|0664)) == -1)
	{
		perror("msgget error");
		return -1;
	}
	//创建进程
	pid_t pid;
	pid = fork();
	if(pid <0)
	{
		perror("fork error");
		return -1;
	}else if(pid == 0)
	{
		while(1)
		{
			struct msgbuf buf = {.mtype=1};
			//清空数组
			bzero(buf.mtext, sizeof(buf.mtext));
			//从终端输入数据到正文
			fgets(buf.mtext, sizeof(buf.mtext), stdin);   
			//将'\n'换成'\0'
			buf.mtext[strlen(buf.mtext)-1] = '\0';
			//将数据放入消息队列中
			if(msgsnd(msgid, &buf, MSGSIZE, 0) !=0)
			{
				perror("msgsnd error");
				return -1;
			}
			if(strcmp(buf.mtext,"quit") == 0)
			{
				break;
			}
		}
		exit(EXIT_SUCCESS);
	}else
	{
		//向消息队列中存放数据
		struct msgbuf buf = {.mtype =2};
		while(1)
		{
			//清空数组
			bzero(buf.mtext, sizeof(buf.mtext));
			//从消息队列中读取信息
			if(msgrcv(msgid, &buf, MSGSIZE,2, 0) == -1)
			{
				perror("msgrcv error");
				return -1;
			}
			printf("读取的消息为:%s\n", buf.mtext);
			if(strcmp(buf.mtext,"quit") == 0)
			{
				break;
			}
		}
		//删除消息队列
		if(msgctl(msgid, IPC_RMID, NULL) == -1)
		{
			perror("msgctl error");
			return -1;
		}
		wait(NULL);
	}
	return 0;
}
#include 
struct msgbuf
{
    long int mtype; 
    char mtext[1024]; 
};
//定义一个消息大小
#define MSGSIZE sizeof(struct msgbuf)-sizeof(long int)
int main(int argc, const char *argv[])
{
	//1、创建key值以便创建消息队列
    key_t key = ftok("/", 'k');
    if(key == -1)
    {
        perror("ftok error");
        return -1;
    }
	//2、使用key值,打开一个消息队列
    int msgid;
    if((msgid=msgget(key, IPC_CREAT|0664)) == -1)
    {
        perror("msgget error");
        return -1;
    }
	//创建进程
	pid_t pid;
	pid = fork();
	if(pid <0)
	{
		perror("fork error");
		return -1;
	}else if(pid == 0)
	{
		//定义一个消息变量
    struct msgbuf buf = { .mtype = 1};
    while(1)
    {
        //清空数组
        bzero(buf.mtext, sizeof(buf.mtext));
        //从消息队列中读取信息
        if(msgrcv(msgid, &buf, MSGSIZE, 1, 0) == -1)
        {
            perror("msgrcv error");
            return -1;
        }
        printf("读取的消息为:%s\n", buf.mtext);
        if(strcmp(buf.mtext,"quit") == 0)
        {
            break;
        }
    }
    //删除消息队列
    if(msgctl(msgid, IPC_RMID, NULL) == -1)
    {
        perror("msgctl error");
        return -1;
    }
    exit(EXIT_SUCCESS);
	}else
	{
    while(1)
    {
		struct msgbuf buf = {.mtype=2};
        //清空数组
        bzero(buf.mtext, sizeof(buf.mtext));
		//从终端输入数据到正文
        fgets(buf.mtext, sizeof(buf.mtext), stdin);   
        //将'\n'换成'\0'
        buf.mtext[strlen(buf.mtext)-1] = '\0';
        //将数据放入消息队列中
        if(msgsnd(msgid, &buf, MSGSIZE, 0) !=0)
        {
            perror("msgsnd error");
            return -1;
        }
        if(strcmp(buf.mtext,"quit") == 0)
        {
            break;
        }
    }
    wait(NULL);
	}
	return 0;
}

通过消息队列实现进程之间通信代码_第1张图片

你可能感兴趣的:(java,算法,数据结构)