c语言亲缘线程通过管道通信一些疑问
亲缘线程在使用管道时,发现第一次使用管道进行进行通信完全正常(./a.out 1),但当重新运行并使用新管道文件时候出现数据无法读取的问题(./a.out 2)(./a.out 3),甚至出现子线程部分语句并未执行问题,下面程序中会出现子进程perror(write)及以后语句未执行得到问题
1 #include<sys/types.h>
2 #include<sys/stat.h>
3 #include<unistd.h>
4 #include<fcntl.h>
5 #include<stdio.h>
6 #include<string.h>
7 int main(int argc ,char *argv[]){
8 umask(0);
9 mkfifo(argv[1],0777);
10 int len=0;
11 int *p=&len;
12 pid_t pid=fork();
13 perror("fork");
14 if(pid==0){
15 int fd=open(argv[1],O_WRONLY);
16 //perror("open");
17 char buff[]={'0'};
18 printf("这是子进程!\n");
19 printf("请输入要通信的字符:");
20 scanf("%s",buff);
21 *p=strlen(buff);
22 write(fd,buff,strlen(buff));
23 perror("write");
24 printf("子进程len:%d\n",*p);
25 close(fd);
26 }
27
28 else{
29 int fd=open(argv[1],O_RDONLY);
30
31 //perror("open");
32 sleep(5);
33 printf("父进程len:%d\n",*p);
34 char buff[20]={0};
35 printf("这是父进程!\n");
36 read(fd,buff,20);
37 perror("read");
38 printf("父进程读出:%s\n",buff);
39 close(fd);
40 }
41 return 0;
42 }
[root@localhost pid]# ./a.out 1
fork: Success
fork: Success
这是子进程!
请输入要通信的字符:23osd
write: Success
子进程len:5
父进程len:0
这是父进程!
read: Success
父进程读出:23osd
[root@localhost pid]# ./a.out 2
fork: Success
fork: Success
这是子进程!
请输入要通信的字符:sdgksls
父进程len:0
这是父进程!
read: Success
父进程读出:
[root@localhost pid]# ./a.out 3
fork: Success
fork: Success
这是子进程!
请输入要通信的字符:dflhlf'fd
父进程len:0
这是父进程!
read: Success
父进程读出:
[root@localhost pid]# ./a.out 1
fork: File exists
fork: File exists
这是子进程!
请输入要通信的字符:wqla
write: File exists
子进程len:4
父进程len:0
这是父进程!
read: File exists
父进程读出:w
[root@localhost pid]# ./a.out 1
fork: File exists
fork: File exists
这是子进程!
请输入要通信的字符:hdshds
write: File exists
子进程len:6
父进程len:0
这是父进程!
read: File exists
父进程读出:hdshds
[root@localhost pid]# ./a.out 1
fork: File exists
fork: File exists
这是子进程!
请输入要通信的字符:wejhee
write: File exists
子进程len:6
父进程len:0
这是父进程!
read: File exists
父进程读出:wejhee
[root@localhost pid]# ./a.out 1
fork: File exists
fork: File exists
这是子进程!
请输入要通信的字符:ahjk2';
父进程len:0
这是父进程!
read: File exists
父进程读出:
[root@localhost pid]# ./a.out 1
fork: File exists
fork: File exists
这是子进程!
请输入要通信的字符:ajgaka
write: File exists
子进程len:6
父进程len:0
这是父进程!
read: File exists
父进程读出:ajgaka
[root@localhost pid]#
c语言亲缘线程通过管道通信一些疑问的更多相关文章
- java多线程通过管道流实现不同线程之间的通信
java中的管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据.一个线程发送数据到输出管道,另外一个线程从输入管道中读取数据.通过使用管道,实现不同线程间的通信,而不必借助类似 ...
- java 多线程:线程通信-等待通知机制wait和notify方法;(同步代码块synchronized和while循环相互嵌套的差异);管道通信:PipedInputStream;PipedOutputStream;PipedWriter; PipedReader
1.等待通知机制: 等待通知机制的原理和厨师与服务员的关系很相似: 1,厨师做完一道菜的时间不确定,所以厨师将菜品放到"菜品传递台"上的时间不确定 2,服务员什么时候可以取到菜,必 ...
- Java多线程编程-线程之间的通信
转载自:这里 学习了基础的线程知识 看到了 线程之间的通信 线程之间有哪些通信方式呢? 1.同步 这里讲的同步是指多个线程通过synchronized关键字这种方式来实现线程间的通信. public ...
- C++和C#进程之间通过命名管道通信(上)
C++和C#进程之间通过命名管道通信(上) "命名管道"是一种简单的进程间通信(IPC)机制.命名管道可在同一台计算机的不同进程之间,或在跨越一个网络的不同计算机的不同进程之间,支 ...
- PHP多进程编程(2):管道通信
一个进程如果是个人英雄主义,那么多进程就是集体主义.(不严格区分多进程 和 多线程的差别) 你不再是一个独行侠,而是一个指挥家. 独来独往,非常自由自在,但是,很多时候,不如众人拾柴火焰高. 这就是我 ...
- Linux IPC之管道通信
2017-04-07 管道通信在linux中使用较为频繁的进程通信机制.基于unix一切皆文件的传统,管道也是一种文件.所以可以使用一般的VFS接口对管道进行读写操作,如read.write.具体管道 ...
- java多线程三之线程协作与通信实例
多线程的难点主要就是多线程通信协作这一块了,前面笔记二中提到了常见的同步方法,这里主要是进行实例学习了,今天总结了一下3个实例: 1.银行存款与提款多线程实现,使用Lock锁和条件Condition. ...
- Java并发读书笔记:如何实现线程间正确通信
目录 一.synchronized 与 volatile 二.等待/通知机制 等待 通知 面试常问的几个问题 sleep方法和wait方法的区别 关于放弃对象监视器 三.等待通知典型 生产者消费者模型 ...
- windows10使用VS(VC++)创建c++多进程命名管道通信
代码可以在 这里 下载 代码主要涉及到: 管道通信 多线程(含临界区) 多进程通信 创建的子进程独立运行 更新日志: 04-12-2020 1. 去除自定义函数返回值,改为int作为函数返回值并增加相 ...
随机推荐
- yum的配置文件介绍
yum 的配置文件分为两部分:main 和repository main 部分定义了全局配置选项,整个yum 配置文件应该只有一个main.常位于/etc/yum.conf 中. reposito ...
- 数据渲染模板引擎,template-web的使用
一:下载 template-web.js 下载地址:https://aui.github.io/art-template/zh-cn/docs/installation.html 二:引用: 三:ht ...
- Unity --- sharedMaterial 、material
sharedMaterial: 无论如何更新材质的属性,内存中只会存在一份. material: 每次更新材质属性的时候,内存中都会重新new一份material作用于它,直到 Application ...
- Asp.net core 学习笔记 ( DI 依赖注入 )
比起 Angular 的依赖注入, core 的相对简单许多, 容易明白 所有 provider 都在 startup 里配置. public void ConfigureServices(IServ ...
- php递归方法
<?phpheader("Content-type:text/html;charset=utf-8");$city=array( array('id'=>1,'name ...
- 单细胞数据高级分析之消除细胞周期因素 | Removal of cell cycle effect
The normalization method described above aims to reduce the effect of technical factors in scRNA-seq ...
- 方差variance, 协方差covariance, 协方差矩阵covariance matrix | scatter matrix | weighted covariance | Eigenvalues and eigenvectors
covariance, co本能的想到双变量,用于描述两个变量之间的关系. correlation,相关性,covariance标准化后就是correlation. covariance的定义: 期望 ...
- Python实现Plugin(插件化开发)
https://www.cnblogs.com/terencezhou/p/10276167.html
- 小程序动态添加class及调接口传递多个参数
1.动态添加class <view class="step2 {{indication == 2 ?'on':''}}"> <view class='tc lef ...
- 『Python』skimage图像处理_旋转图像
一段简短的实现图像旋转的代码,使用了skimage库,据说和PIL相比,skimage对numpy等科学计算库的支持更好,这里是为了完成师兄给的帮他修改程序的任务,如果以后有需求的话可能会对pytho ...