linux 进程间通信--systemV 消息队列 实例

1:消息接收端

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include <sys/wait.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include <errno.h>
#include <sys/stat.h>
#define MAX_TEXT 512
struct my_msg_st
{
	int my_msg_type;
	char msg_text[MAX_TEXT];
};
#define MSG_FILE "/wlan/configap/mkwrielessconfig.sh"
#define MSG_FLAG IPC_CREAT|S_IRUSR|S_IWUSR
int msgid=0;
void signalHandler(int sig)
{
#if 0
    switch (sig)
    {
    case SIGINT:
    case SIGTERM:
        if (msgctl(msgid, IPC_RMID, NULL) == -1)
        {
            perror("msgget() failed to delete old queue");
        }
        else
        {
        	printf("remove msgid:%d\n",msgid);
        }

    }
#endif
    printf("main process exit\n");
    exit(0);
}
int checkSelect(int handfd,char *rwflag)
{
	int ret, retval=0xff;
    fd_set readfds, writefds;
	struct timeval	timeout;
    FD_ZERO(&readfds);
    FD_ZERO(&writefds);
    static int numfds = 0;
    if (handfd >= numfds)
    {
       	numfds = handfd + 1;              /* Record maximum fd + 1 */
    }
    if (strchr(rwflag, 'r') != NULL)
    {
        FD_SET(handfd, &readfds);
    }
    if (strchr(rwflag, 'w') != NULL)
    {
        FD_SET(handfd, &writefds);
    }
	while (1)
	{
		/*设置阻塞时间为5秒*/
		timeout.tv_sec = 0;
		timeout.tv_usec = 20;
		retval =0xff;
		ret = select(numfds, &readfds, &writefds, NULL, &timeout);
		switch (ret)
		{
			case 0:
				//exit(0);
				break;
			case -1:
				perror("select");
				exit(1);
				break;
			default:
				 if (FD_ISSET(handfd, &readfds))
				 {
					 retval =0;
					 FD_CLR(handfd, &readfds);
					 FD_SET(handfd, &readfds);
				 }
				 if(FD_ISSET(handfd, &writefds))
				 {
					 retval =1;
					 FD_CLR(handfd, &writefds);
					 FD_SET(handfd, &writefds);
				 }
				 break;
		  }
		// printf("time out1--------------\n");
		 return retval;
	}
}

int main(int argc,char *argv[])
{
	int running=1;
	int childPid;
	struct my_msg_st some_data;
	int fd=0;
	char buffer[BUFSIZ];
	int msg_to_receive=2;
	int stat_val;
    key_t key;
    if((key=ftok(MSG_FILE,'a'))==-1)
    {
		fprintf(stderr,"Creat Key Error:%s\n\n",strerror(errno));
		exit(1);
    }
	if((msgid=msgget(key,MSG_FLAG)) == -1)
	{
		perror("msgget");
		exit(0);
	}
#if 0
	childPid=fork();
	if( childPid < 0 )
	{
		exit(0);
	}
	else if( childPid == 0 )
	{
		while(running)
		{
			if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0)==-1)
			{
				perror("msgrcv");
				exit(EXIT_FAILURE);
			}
			printf("\nreceiver mssage:%s",some_data.msg_text);
			if(strncmp(some_data.msg_text,"end",3)==0)
			{
				running=0;
				kill(getppid(),SIGINT);
			}
		}
	}
	else
	{
		#if 1
			struct sigaction sa;
			sa.sa_handler = signalHandler;
			sa.sa_flags = 0;	/* Interrupt system calls */
			sigemptyset(&sa.sa_mask);
			sigaction(SIGINT, &sa, NULL);
			sigaction(SIGQUIT, &sa, NULL);
		#endif
		while(running)
		{
			printf("Enter the mssage to send:");
			fgets(buffer,BUFSIZ,stdin);
			some_data.my_msg_type=1;
			strcpy(some_data.msg_text,buffer);
			if((msgsnd(msgid,(void *)&some_data,MAX_TEXT,0))==-1)
			{
				perror("msgsnd");
				exit(EXIT_FAILURE);
			}
			if(strncmp(buffer,"end",3)==0)
			{
				running=0;
				printf("kill child pid:%d getpid:%d\n",childPid,getpid());
				kill(childPid,SIGINT);
			}
		}

	}
	printf("pid:%d getpid:%d\n",childPid,getpid());
	waitpid(childPid, &stat_val, 0);
	if (WIFEXITED(stat_val))
	{
		printf("Child exited with code %d\n", WEXITSTATUS(stat_val));
	}
	else if (WIFSIGNALED(stat_val))
	{
		printf("Child terminated abnormally, signal %d\n", WTERMSIG(stat_val));
	}
#endif

#if 1
	while(1)
	{

		if(checkSelect(fd,"r")==0)
		{
			printf("Enter the mssage to send:");
			fgets(buffer,BUFSIZ,stdin);
			some_data.my_msg_type=1;
			strcpy(some_data.msg_text,buffer);
			if((msgsnd(msgid,(void *)&some_data,MAX_TEXT,0))==-1)
			{
				perror("msgsnd");
				exit(EXIT_FAILURE);
			}
			if(strncmp(buffer,"end",3)==0)
			{
				running=0;
			}
	    }
		if(running)
		{
			if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,IPC_NOWAIT)==-1)
			{
			}
			else
			{
				printf("\nreceiver mssage:%s",some_data.msg_text);
				if(strncmp(some_data.msg_text,"end",3)==0)
				{
					running=0;
				}
			}
		}
		if(running==0)
		{
			if(msgctl(msgid,IPC_RMID,0)==-1)
			{
				if(EINVAL == errno)
				{
					msgctl(msgid,IPC_RMID,0);
					exit(EXIT_FAILURE);
				}
			}
			else
			{
				printf("remove msgid:%d\n",msgid);
			}
			exit(0);
		}
	}
#endif
	return 0;
}


2:消息发送端

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include <sys/wait.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include <errno.h>
#include <sys/stat.h>
#define MSG_FILE "/wlan/configap/mkwrielessconfig.sh"
#define MSG_FLAG IPC_CREAT|S_IRUSR|S_IWUSR
int msgid=0;
#define MAX_TEXT 512
struct my_msg_st
{
	int my_msg_type;
	char msg_text[MAX_TEXT];
};
void signalHandler(int sig)
{
#if 0
	    printf("-----------------------\n");
		if(SIGINT == sig)
		{
			if (msgctl(msgid, IPC_RMID, NULL) == -1)
			{
				perror("msgget() failed to delete old queue");
			}
			else
			{
				printf("remove msgid:%d\n",msgid);
			}

		}
#endif
	    printf("main process exit\n");
		exit(0);
}
int main(int argc,char *argv[])
{
	int running=1;
	int childPid;
	struct my_msg_st some_data;
	char buffer[BUFSIZ];
	int msg_to_receive=1;
	int stat_val;
    key_t key;
    if((key=ftok(MSG_FILE,'a'))==-1)
    {
		fprintf(stderr,"Creat Key Error:%s\n\n",strerror(errno));
		exit(1);
    }
	if((msgid=msgget(key,MSG_FLAG)) == -1)
	{
		perror("msgget");
		exit(0);
	}
	childPid=fork();
	if( childPid < 0 )
	{
		exit(0);
	}
	else if( childPid == 0 )
	{
		while(running)
		{
			if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0)==-1)
			{
				perror("msgrcv");
				exit(EXIT_FAILURE);
			}
			printf("\nreceiver mssage:%s",some_data.msg_text);
			if(strncmp(some_data.msg_text,"end",3)==0)
			{
				running=0;
				kill(getppid(),SIGINT);
			}
		}
	}
	else
	{
		#if 1
			struct sigaction sa;
			sa.sa_handler = signalHandler;
			sa.sa_flags = 0;	/* Interrupt system calls */
			sigemptyset(&sa.sa_mask);
			sigaction(SIGINT, &sa, NULL);
			sigaction(SIGQUIT, &sa, NULL);
		#endif
		while(running)
		{
			printf("Enter the mssage to send:");
			fgets(buffer,BUFSIZ,stdin);
			some_data.my_msg_type=2;
			strcpy(some_data.msg_text,buffer);
			if((msgsnd(msgid,(void *)&some_data,MAX_TEXT,0))==-1)
			{
				perror("msgsnd");
				exit(EXIT_FAILURE);
			}
			if(strncmp(buffer,"end",3)==0)
			{
				running=0;
				printf("kill child pid:%d getpid:%d\n",childPid,getpid());
				kill(childPid,SIGINT);
			}
		}

	}
	printf("pid:%d getpid:%d\n",childPid,getpid());
	waitpid(childPid, &stat_val, 0);
	if (WIFEXITED(stat_val))
	{
		printf("Child exited with code %d\n", WEXITSTATUS(stat_val));
	}
	else if (WIFSIGNALED(stat_val))
	{
		printf("Child terminated abnormally, signal %d\n", WTERMSIG(stat_val));
	}
	if(msgctl(msgid,IPC_RMID,0)==-1)
	{
		if(EINVAL == errno)
		{
			msgctl(msgid,IPC_RMID,0);
			exit(EXIT_FAILURE);
		}
	}
	else
	{
		printf("remove msgid:%d\n",msgid);
	}
	return 0;
}

3:执行结果

消息查询

发送端及接收端结果




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

家有工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值