Linux进程间通信学习笔记

进程间交换信息的方法---进程间通信(IPC)

IPC的方式有单机通信与多机通信(是否在同一台机子上进行通信)

单机通信:

  • 管道(包括无名管道和命名管道)
  • 半双工管道,FIFO
  • 全双工管道,命名全双工管道
  • 消息队列
  • 信号量
  • 共享存储

多机通信:

  • 套接字STREAMS
  • Socket

一:管道

通常为无名管道

特点:

  • 半双工,数据只能在一个方向上流动,有固定读端和写端
  • 只适用于有亲缘关系的进程,如父子进程与兄弟进程
  •  可以看作是一种特殊的文件,也可以使用fread和fwrite等函数进行读写,但是并不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中。当进程结束之后,管道也随之消失,不储存数据

原型:

#include <unist.h>

int pipe(int fd[2]);  //返回值,成功返回1,失败返回-1

  • 当管道开启时,会创建两个文件描述符,fd[0]为读而打开,fd[1]为写而打开
  • 关闭管道只要把文件描述符关闭即可
  • 若要数据从父进程流向子进程,则要关闭父进程的读端和子进程的写端,反之则是从子进程流向父进程
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	int fd[2];
	int pid;
	char buf[128];

	if(pipe(fd) == -1){
		printf("create pipe failed\n");
	}
	
	pid = fork();
	if(pid == -1){
		printf("create child failed\n");
	}
	else if(pid > 0){
		printf("this is father\n");
		close(fd[0]);  //关闭读端,开始写
		write(fd[1],"hello from father\n",strlen("hello from father\n"));
		wait();
	}
	else{
		printf("this is child\n");
		close(fd[1]);  //关闭写端,开始读
		read(fd[0],buf,128);
		printf("the message is %s\n",buf);
		exit(0);
	}	

	return 0;
	
}

二:FIFO

又称命名管道

特点:

  • FIFO可以在无关的进程之间交换数据,与无名管道不同
  • FIFO与路径名相关联,以一种特殊设备文件形式存在于文件系统中

原型:

int mkfifo(const char *pathname,mode_t mode);

mode参数与open中的mode参数使用相同 

//创建管道

#include <sys/types.h>
#include <sys/stat.h>

//       int mkfifo(const char *pathname, mode_t mode);
int main()
{
	mkfifo("./file",0600);
	return 0;
}

 

可以通过perror("why");了解失败原因

增加调试原因,避免随便报错 

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
//       int mkfifo(const char *pathname, mode_t mode);
int main()
{
	if((mkfifo("./file",0600) == -1) && errno != EEXIST){
		printf("create fifo failed\n");
	}
	
	return 0;
}

 实现两个进程间通信

//read.c

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

//       int mkfifo(const char *pathname, mode_t mode);
int main()
{
	char buf[30] = {0};
	int nread = 0;

	if((mkfifo("./file",0600) == -1) && errno != EEXIST){
		printf("create fifo failed\n");
	}
	
	int fd = open("./file",O_RDONLY);
	printf("open success\n");

	while(1){
		nread = read(fd,buf,30);
		printf("read %d byte,content: %s\n",nread,buf);
		if(nread == 3){
			break;
		}
	}

	close(fd);
	return 0;
}

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux设备驱动程序是用于控制和管理硬件设备的软件模块。学习Linux设备驱动程序可以帮助开发人员理解和掌握Linux内核的工作原理,以及如何编写和调试设备驱动程序。 以下是一些学习Linux设备驱动程序的笔记和建议: 1. 理解Linux设备模型:Linux设备模型是一种用于管理设备的框架,它提供了一种统一的方式来表示和操作设备。学习Linux设备模型可以帮助你理解设备的注册、初始化和销毁过程。 2. 学习字符设备驱动程序:字符设备是一种以字节为单位进行读写的设备,如串口、终端等。学习字符设备驱动程序可以帮助你了解字符设备的打开、关闭、读写等操作,并学习如何实现设备文件的注册和操作。 3. 学习块设备驱动程序:块设备是一种以块为单位进行读写的设备,如硬盘、闪存等。学习块设备驱动程序可以帮助你了解块设备的分区、缓存、IO调度等操作,并学习如何实现块设备的注册和操作。 4. 学习中断处理:中断是设备向处理器发送信号的一种机制,用于通知处理器设备的状态变化。学习中断处理可以帮助你了解中断的注册、处理和释放过程,并学习如何编写中断处理程序。 5. 学习设备驱动程序的调试技巧:设备驱动程序的调试是一个重要的技能,可以帮助你快速定位和解决问题。学习设备驱动程序的调试技巧可以帮助你理解和使用调试工具,如 printk、kprobe等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值