Linux中的进程间通信

谈到操作系统不得不提的就是进程,谈到进程必须要谈的一块就是通信,那么问题来了,如何做到是两个进程通信,就变成了很大的问题。在做项目的时候发现,单独用一个进程不会有很多头疼的问题,但是如果两个进程同时运行,通信问题就是首要解决的一个大问题。

那么系统为进程间通信提供了三种方式:消息队列、共享内存以及信号量机制。

先引用一个简单地例子做父子进程间的消息传递,下面把代码贴出来大家参考一下:

/*
 ************************************************
 *Name : pipe.c                           *
 *Date : 2015-05-24                       *
 *Author : sniper                         *
 *Aim : It will finish the InterProcess   *
 *      Communication. Using the pipe.    *
 ************************************************
 */

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

int main()
{
	int n;
	int fd[2];
	pid_t pid;
	char message[100];
	char receive_message[100];
	
	memset(message,'\0',sizeof(message));
	memset(receive_message,'\0',sizeof(receive_message));
	strcpy(message,"Hello I am child!\n");
	if(pipe(fd)<0)
	{
		perror("pipe error!\n");
	}
	if((pid = fork())<0)
	{
		perror("create the pid error!\n");
	}
	else if(0==pid)
	{
		close(fd[0]);
		write(fd[1],message,strlen(message));
	}
	else 
	{
		close(fd[1]);
		n = read(fd[0],receive_message,100);
		write(STDOUT_FILENO,receive_message,n);
	}
	return 0;
}
运行结果可以发现代码中只有父进程那个进程号中存在向标准输出流输出:


下面有个新的问题,这种简单地管道只能解决一些简单地问题,那么如果出现A进程需要B进程的消息该怎么办?

那么就需要使用开篇提到的三种方式了,下面我做了一个消息队列的例子,共大家参考:

/*
 ************************************************
 *Name : message_queue.c                  *
 *Date : 2015-05-24                       *
 *Author : sniper                         *
 *Aim : Create the message queue to send  *
 *      the message for IPC.              *
 ************************************************
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/msg.h>

#define queue1 10

typedef struct 
{
	long messagetype;
	char message[200];	
}messagestruct;

int main()
{
	messagestruct mes_str;
	int message_queue_num=0;
	int ret_value=0;
	
	mes_str.messagetype = 1;
	strcpy(mes_str.message,"I am message_queue.c\n");
	message_queue_num = msgget(queue1,IPC_EXCL);
	
	/*
	 *This process will send the message to another process.
	 *So if the message queue not exist,it will create the 
	 *message queue.
	 */
	if(message_queue_num<0)
	{
		message_queue_num = msgget(queue1,IPC_CREAT|0666);
		if(message_queue_num < 0)
		{
			perror("message create failure\n");
			exit(-1);
		}
	}
	/* 
	 *Send the message
	 */
	there:ret_value = msgsnd(message_queue_num,&mes_str,sizeof(messagestruct),IPC_NOWAIT);
	if(ret_value<0)
	{
		printf("message write failed!\n");
		sleep(1);
		goto there;
	}	
	return 0;
}
这个.c文件用于发送消息,下面这个.c用于接收上面这个程序发送过来的消息:

/*
 ************************************************
 *Name : message_queue_receive.c          *
 *Date : 2015-05-24                       *
 *Author : sniper                         *
 *Aim : This programm will receive the    *
 *      message from the system queue.    *
 ************************************************
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/msg.h>

#define queue1 10

typedef struct 
{
	long messagetype;
	char message[200];		
}messagestruct;


int main()
{
	messagestruct mes_str;
	int message_queue_num=0;
	int ret_value=0;
	
	message_queue_num = msgget(queue1,IPC_EXCL);
	if(message_queue_num<0)
	{		
		printf("temperature create message queue failed!");
		exit(-1);
	}
	/*
	 *receive the message
	 */
	ret_value = msgrcv(message_queue_num,&mes_str,sizeof(messagestruct),0,0);
	if(ret_value<0)
	{
		printf("receive the message failure!\n");
	}
	printf("receive message is %s\n",mes_str.message);
	return 0;
}

运行过程先运行第一个.c,然后再运行第二个.c

结果如图:


可以看出无论是进程间通信还是socket通信,首先要做的就是建立通信“管道”,使得发送和接收的双方都能找到对应的“管道”,那么接发消息就是一句话的事了,define 中定义的宏变量queue1就是这个“管道”的ID。

以上就是个人对进程通行的一点看法,望大家批评指正!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值