进程间通信--两个进程操作同一个文件
来源:互联网 发布:iphone好玩的软件 编辑:程序博客网 时间:2024/04/29 14:10
a.txt文件内容如下:
hello,world。
编写两个不同的可执行程序,名称分别为a和b。在a程序中调用open函数打开a.txt文件,在b程序不可调用open或者fopen。只允许调用read函数来实现读取a.txt文件(a程序中可以使用fork和execv函数创建子进程)。
makefile
.SUFFIXES: .c .oCC=gccSRCS=a.c OBJS=$(SRCS:.c=.o)EXEC=aall: $(OBJS) $(CC) -o $(EXEC) $(OBJS) @echo '-------------ok--------------'.c.o: $(CC) -Wall -g -o $@ -c $< clean: rm -f $(OBJS) rm -f core*
a.c
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>int main(){ printf("a begin\n"); int fd = open("a.txt", O_RDONLY); if (fd == -1)//打开文件失败 { printf("error is %s\n", strerror(errno)); return -1; } pid_t pid = fork();//fork调用,在内存中克隆一个与自己一样的副本 if (pid < 0) { printf("fork failed %s\n", strerror(errno)); return -1; } if (pid > 0)//父进程 { close(fd);//父进程关闭打开的文件描述符fd } if (pid == 0)//子进程 { char s[128]; memset(s, 0, sizeof(s)); sprintf(s, "%d", fd);//将打开a.txt文件后的文件描述符fd格式化为字符串 char *args[] = { "b", s, NULL }; if (execve("b", args, NULL) == -1)//将文件描述符fd作为启动参数传递给b程序 { printf("execve failed %s\n", strerror(errno)); } } printf("a end..\n"); return 0;}
b.c
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>int main(int arg, char *args[]){printf("b begin..\n");if (args[1] == NULL)//b启动的时候,如果没有参数,main函数直接退出{printf("Usage: b xxx\n");return -1;}int fd = atoi(args[1]);//将第一个参数从字符串转化为整数,此参数就是文件描述符fdif (fd == 0)//如果参数为0,函数返回{return -1;}char buf[1024];memset(buf, 0, sizeof(buf));read(fd, buf, sizeof(buf));//读取文件printf("%s", buf);close(fd);//关闭文件描述符printf("b end..\n");return 0;}
0 0
- 进程间通信--两个进程操作同一个文件
- 两个apk运用于同一个进程
- 两个进程之间通信
- linux 文件操作,IO多路复用,IPC 进程间通信
- 进程间通信-文件锁
- 文件(进程间通信)
- 进程间通信:文件锁
- 在同一个进程空间运行两个程序
- 在同一个进程空间运行两个程序
- 在同一个进程空间运行两个程序
- 在同一个进程空间运行两个程序
- FIFO 两个进程同时写同一个FIFO
- 在同一个进程空间运行两个程序
- VB两个程序(进程)间通信
- 两个进程间通信的问题
- 两个程序的进程间通信小结
- wince6 两个应用程序进程间通信 OnCopyData
- 两个程序的进程间通信小结
- Linux那些事儿 之 戏说USB(19)设备的生命线(二)
- EXO同款手表 鹿晗吴世勋已入DW手表
- Jar mismatch! Fix your dependencies的问题
- 借助Ehcache缓存框架实现对页面的缓存
- Filed 类的使用方法以及java反射机制的案例
- 进程间通信--两个进程操作同一个文件
- android 开发总结一
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分裸题)
- XML 处理利器 : XStream
- 分解和组合的抽象方法
- java解压缩工具类
- C文件操作
- ubuntu14.04上安装tigervnc
- Linux关机命令