哪位大神有鬼吹灯黄皮子坟网盘百度网盘的资源啊,求分享【拜托】

/*2.利用管道实现进程间的通信
编写程序,父进程创建管道和两个子进程p1和p2
子进程p1打开给定文件(如果没有,则创建文件),并向文件中写数据,写完关闭文件,然后向管道写入一条消息&ok",目的是通知进程p2可以读取文件内容了。
子进程p2通过管道读取消息,如果消息是&ok&,则打开文件,读取文件内容,并将其输出道屏幕上**/
#include&unistd.h&
#include&stdio.h&
#include&string.h&
#include&stdlib.h&
#include &fcntl.h&
#define BUFFER_SIZE 1024
int writetofile()
int fd = open("a.txt",O_WRONLY|O_CREAT, 0660);//打开指定路径的文件,如果不存在自动创建
if (-1 == fd)
printf("Open
file error!\n");
char msg[]="Hello,file is ready.";
write(fd, msg, sizeof(msg));
close(fd);
int readfromfile(char *buffer)
int fd = open("a.txt",O_RDONLY, 0660);//打开指定路径的文件,如果不存在自动创建
if (-1 == fd)
printf("Open
file error!\n");
int ret= read(fd, buffer, BUFFER_SIZE);
close(fd);
int main() {
int pipefd[2];
char buf[100];
memset(buf, 0, sizeof(buf));//清空buf内容为0
if(pipe(pipefd) & 0) {//父进程创建管道
perror("pipe");
pid = fork();//创建第一个子进程
if(pid == 0) { //子进程p1
if(writetofile())
close(pipefd[0]);//关闭管道读端
char *msg="ok";
write(pipefd[1], msg, 50);//从管道写端向管道写入消息&ok&
else if(pid & 0) {
pid = fork();//创建第二个子进程
if(pid == 0) { //子进程p2
close(pipefd[1]);//关闭写端
read(pipefd[0], buf, sizeof(buf));//从读端读消息
if(!strcmp(buf,"ok"))//如果是&ok&,则访问文件,读取文件内容
char data[BUFFER_SIZE];
readfromfile(data);
printf("Read from file : %s.\n",data);//打印到屏幕上
else if(pid & 0) exit(0);
阅读(...) 评论() &【图文】实验3 进程的管道通信实验_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
实验3 进程的管道通信实验
上传于||暂无简介
大小:110.00KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢应用管道实现父子进程之间的通信_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
应用管道实现父子进程之间的通信
上传于||暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
定制HR最喜欢的简历
你可能喜欢 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
进程间通信之管道通信C
下载积分:30
内容提示:C语言中的进程间管道通信的方法与介绍。
文档格式:PDF|
浏览次数:210|
上传日期: 05:57:40|
文档星级:
该用户还上传了这些文档
进程间通信之管道通信C
官方公共微信欢迎加入我们,一同切磋技术。 &
用户名: &&&
密 码: &
共有 1346 人关注过本帖
标题:两个进程间通过服务器双向通信的问题,自己搞了半天才弄的基本可以通信了, ...
来 自:成都
等 级:蜘蛛侠
帖 子:325
专家分:1131
结帖率:100%
&&已结贴√
&&问题点数:20&&回复次数:10&&&
两个进程间通过服务器双向通信的问题,自己搞了半天才弄的基本可以通信了,不过好像没解决同步互斥问题
______server.c_____
#include &stdio.h&
#include &stdlib.h&
#include &errno.h&
#include &sys/types.h&
#include &sys/stat.h&
#include &semaphore.h&
#include &pthread.h&
#include &fcntl.h&
#include &string.h&
#include &unistd.h&
#define SIZE 1024
sem_t sem[4];
void f_exchange1_2(void);
void f_exchange3_4(void);
int main()
&&&&&if(0 & mkfifo(&/tmp/FIFO1&, 0666));&&& //建4个管道
&&&&&&&&&if(EEXIST != errno)
&&&&&&&&&{
&&&&&&&&&&&& perror(&mkfifo1&);
&&&&&&&&&&&& exit(-1);
&&&&&&&&&}
&&&&&if(0 & mkfifo(&/tmp/FIFO2&, 0666));
&&&&&&&&&if(EEXIST != errno)
&&&&&&&&&{
&&&&&&&&&&&& perror(&mkfifo2&);
&&&&&&&&&&&& exit(-1);
&&&&&&&&&}
&&&&&if(0 & mkfifo(&/tmp/FIFO3&, 0666));
&&&&&&&&&if(EEXIST != errno)
&&&&&&&&&{
&&&&&&&&&&&& perror(&mkfifo3&);
&&&&&&&&&&&& exit(-1);
&&&&&&&&&}
&&&&&if(0 & mkfifo(&/tmp/FIFO4&, 0666));
&&&&&&&&&if(EEXIST != errno)
&&&&&&&&&{
&&&&&&&&&&&& perror(&mkfifo4&);
&&&&&&&&&&&& exit(-1);
&&&&&&&&&}
&&&&&if(0 &sem_init(&sem[0], 0, 1))&&& //四个信号灯,准备用来解决同步互斥的,还没用上,所以这儿应该有安全隐患
&&&&&&&&&perror(&sem_init0&);
&&&&&&&&&exit(-1);
&&&&&if(0 &sem_init(&sem[1], 0, 0))
&&&&&&&&&perror(&sem_init1&);
&&&&&&&&&exit(-1);
&&&&&if(0 &sem_init(&sem[2], 0, 1))
&&&&&&&&&perror(&sem_init2&);
&&&&&&&&&exit(-1);
&&&&&if(0 &sem_init(&sem[3], 0, 0))
&&&&&&&&&perror(&sem_init3&);
&&&&&&&&&exit(-1);
&&&&&pthread_t pid[2];
&&&&&int ret1 = pthread_create(&pid[0], NULL, (void *)f_exchange1_2, NULL); //建第一个线程
&&&&&if(0 & ret1)
&&&&&&&&&printf(&pthread_create0:%s&,strerror(ret1));
&&&&&&&&&exit(-1);
&&&&&int ret2 = pthread_create(&pid[1], NULL, (void *)f_exchange3_4, NULL);//第二个线程
&&&&&if(0 & ret2)
&&&&&&&&&printf(&pthread_create0:%s&,strerror(ret2));
&&&&&&&&&exit(-1);
&&&&&pthread_join(pid[0], NULL);&&&//等线程一
&&&&&pthread_join(pid[1], NULL); //等线程二
&&& return 0;
void f_exchange1_2(void)&&&&&&//线程一调用函数,用于管道1的数据传到管道2(即客户1的数据传给客户2)
&&&&&char buf1[SIZE] = {0};&&& //buf1用于中间交换的字符数组(之后都是buf用于中间缓存)
&&&&&int fd1 = open(&/tmp/FIFO1&, O_RDWR);
&&&&&if(0 & fd1)
&&&&&&&&&printf(&open1:%s\n&,strerror(fd1));
&&&&&&&&&exit(-1);
&&&&&int fd2 = open(&/tmp/FIFO2&, O_RDWR);
&&&&&if(0 & fd2)
&&&&&&&&&printf(&open2:%s\n&,strerror(fd2));
&&&&&&&&&exit(-1);
&&&&&while(1)
&&&&&{&&& //sem_wait(&sem[0]);
&&&&&&&&&read(fd1, buf1, sizeof(buf1)-1);
&&&&&&&&&write(fd2,buf1,sizeof(buf1));
&&&&&&&&&//sem_post(&sem[1]);
void f_exchange3_4(void)&&&//线程一调用函数,用于管道3的数据传到管道4(即客户2的数据传给客户1)
&&&&&char buf2[SIZE] = {0};
&&&&&int fd4 = open(&/tmp/FIFO4&, O_RDWR);
&&&&&if(0 & fd4)
&&&&&&&&&printf(&open4:%s\n&,strerror(fd4));
&&&&&&&&&exit(-1);
&&&&&int fd3 = open(&/tmp/FIFO3&, O_RDWR);
&&&&&if(0 & fd3)
&&&&&&&&&printf(&open3:%s\n&,strerror(fd3));
&&&&&&&&&exit(-1);
&&&&&while(1)
&&&&&{&&& //sem_wait(&sem[2]);
&&&&&&&&&read(fd3, buf2, sizeof(buf2)-1);
&&&&&&&&&write(fd4,buf2,sizeof(buf2));
&&&&&&&&&//sem_post(&sem[3]);
&________quest1.c_________
#include &stdio.h&
#include &stdlib.h&
#include &errno.h&
#include &sys/types.h&
#include &sys/stat.h&
#include &semaphore.h&
#include &pthread.h&
#include &fcntl.h&
#include &string.h&
#include &unistd.h&
#define SIZE 1024
sem_t sem[2];
pthread_t pid[2];
void f_write(void );
void f_read(void );
int main()
&&&&&if(0 &sem_init(&sem[0], 0, 1))
&&&&&&&&&perror(&sem_init0&);
&&&&&&&&&exit(-1);
&&&&&if(0 &sem_init(&sem[1], 0, 0))
&&&&&&&&&perror(&sem_init1&);
&&&&&&&&&exit(-1);
&&&&&int ret1 = pthread_create(&pid[0], NULL, (void *)f_write, NULL); //客户1的第1个线程
&&&&&if(0 & ret1)
&&&&&&&&&printf(&pthread_create0:%s&,strerror(ret1));
&&&&&&&&&exit(-1);
&&&&&int ret2 = pthread_create(&pid[1], NULL, (void *)f_read, NULL); //第二个线程
&&&&&if(0 & ret2)
&&&&&&&&&printf(&pthread_create0:%s&,strerror(ret2));
&&&&&&&&&exit(-1);
&&&&&pthread_join(pid[0], NULL);
&&&&&pthread_join(pid[1], NULL);
&&& return 0;
void f_write(void)&&& //线程1调用的函数,用于管道1中写数据(即发消息给客户2)
&&&&&int fd1 = open(&/tmp/FIFO1&, O_RDWR);
&&&&&if(0 & fd1)
&&&&&&&&&printf(&open1:%s\n&,strerror(fd1));
&&&&&&&&&exit(-1);
&&&&&char buf[SIZE] = {0};
&&&&&while(1)
&&&&&&&&//sem_wait(&sem[0]);
&&&&&&&&&printf(&Enter a string:\n&);
&&&&&&&&&fgets(buf, sizeof(buf)-1,stdin);
&&&&&&&&&write(fd1, buf, sizeof(buf)-1);
&&&&&&&&&//sem_post(&sem[1]);
void f_read(void)&&//线程2的调用函数,用于读管道4(即接收客户2发的消息)
&&&&&int fd4 = open(&/tmp/FIFO4&, O_RDWR);
&&&&&if(0 & fd4)
&&&&&&&&&printf(&open4:%s\n&,strerror(fd4));
&&&&&&&&&exit(-1);
&&&&&char buf[SIZE] = {0};
&&&&&while(1)
&&&&&&&&&//sem_wait(&sem[1]);
&&&&&&&&&read(fd4, buf, sizeof(buf));
&&&&&&&&&printf(&:&);
&&&&&&&&&puts(buf);
&&&&&&&&&memset(buf, 0, sizeof(buf));
&&&&&&&&&//sem_post(&sem[0]);
&________quest2.c_________
#include &stdio.h&
#include &stdlib.h&
#include &errno.h&
#include &sys/types.h&
#include &sys/stat.h&
#include &semaphore.h&
#include &pthread.h&
#include &fcntl.h&
#include &string.h&
#include &unistd.h&
#define SIZE 1024
sem_t sem[2];
pthread_t pid[2];
void f_write(void );
void f_read(void );
int main()
&&&&&if(0 &sem_init(&sem[0], 0, 1))
&&&&&&&&&perror(&sem_init0&);
&&&&&&&&&exit(-1);
&&&&&if(0 &sem_init(&sem[1], 0, 0))
&&&&&&&&&perror(&sem_init1&);
&&&&&&&&&exit(-1);
&&&&&int ret1 = pthread_create(&pid[0], NULL, (void *)f_write, NULL);&&//客户2的第一个线程
&&&&&if(0 & ret1)
&&&&&&&&&printf(&pthread_create0:%s&,strerror(ret1));
&&&&&&&&&exit(-1);
&&&&&int ret2 = pthread_create(&pid[1], NULL, (void *)f_read, NULL);&&//客服2的第二个线程
&&&&&if(0 & ret2)
&&&&&&&&&printf(&pthread_create0:%s&,strerror(ret2));
&&&&&&&&&exit(-1);
&&&&&pthread_join(pid[0], NULL); //等待线程1
&&&&&pthread_join(pid[1], NULL);//等待线程2
&&& return 0;
void f_write(void) //线程1调用函数,用于写管道3(即客户2给客户1发消息)
&&&&&int fd3 = open(&/tmp/FIFO3&, O_RDWR);
&&&&&if(0 & fd3)
&&&&&&&&&printf(&open3:%s\n&,strerror(fd3));
&&&&&&&&&exit(-1);
&&&&&char buf[SIZE] = {0};
&&&&&while(1)
&&&&&&&&//sem_wait(&sem[0]);
&&&&&&&&&printf(&Enter a string:\n&);
&&&&&&&&&fgets(buf, sizeof(buf)-1,stdin);
&&&&&&&&&write(fd3, buf, sizeof(buf)-1);
&&&&&&&&&//sem_post(&sem[1]);
void f_read(void) // 线程2调用的函数,用于读管道2(即接收客户1发的消息)
&&&&&int fd2 = open(&/tmp/FIFO2&, O_RDWR);
&&&&&if(0 & fd2)
&&&&&&&&&printf(&open2:%s\n&,strerror(fd2));
&&&&&&&&&exit(-1);
&&&&&char buf[SIZE] = {0};
&&&&&while(1)
&&&&&&&&&//sem_wait(&sem[1]);
&&&&&&&&&read(fd2, buf, sizeof(buf));
&&&&&&&&&printf(&:&);
&&&&&&&&&puts(buf);
&&&&&&&&&memset(buf, 0, sizeof(buf));
&&&&&&&&&//sem_post(&sem[0]);
各位大哥不好意思,昨天写完就很晚了,所以就没说明就发上来了,由于初学逻辑上肯定没把握好,所以显得有点乱,请各位谅解!我会进步的。我想请教的是线程之间的同步互斥问题,我也看了书了,还是不怎么明白,怎么用信号灯或互斥锁解决这问题,还有就是函数回调问题?望指点小弟,谢谢各位!
[ 本帖最后由 遗矢的老人 于
18:58 编辑 ]
搜索更多相关主题的帖子:
来 自:成都
等 级:蜘蛛侠
帖 子:325
专家分:1131
不好意思,写的有点杂,我刚刚才学这个,就是同步互斥问题不怎么搞得懂
来 自:北京
等 级:贵宾
威 望:94
帖 子:6779
专家分:16751
你把 include 都隐藏掉是什么意思?有意不让我们拷贝调试?
等 级:论坛游民
帖 子:38
专家分:50
哟。还是linux的程序啊。linux下程序接触少啊,帮不了你。不过建议你去看下信号量啊。 这个可以同步的。具体没弄过。
等 级:青峰侠
帖 子:1160
专家分:1797
估计网上复制的。
来 自:成都
等 级:蜘蛛侠
帖 子:325
专家分:1131
回复 2楼 遗矢的老人
呵呵 不是的&&我也不知道,上传就自己隐藏的,不好意思
来 自:成都
等 级:蜘蛛侠
帖 子:325
专家分:1131
回复 2楼 遗矢的老人
#include &stdio.h&
#include &stdlib.h&
#include &errno.h&
#include &sys/types.h&
#include &sys/stat.h&
#include &semaphore.h&
#include &string.h&
#include &pthread.h&
#include &fcntl.h&
#include &string.h&
#include &unistd.h&
来 自:成都
等 级:蜘蛛侠
帖 子:325
专家分:1131
回复 5楼 Devil_W
瞎说,我昨晚自己写的代码怎么就变成网上复制啦
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
好复杂 学习了
来 自:成都
等 级:蜘蛛侠
帖 子:325
专家分:1131
回复 2楼 遗矢的老人
怎么把图片发上来?
[ 本帖最后由 遗矢的老人 于
13:37 编辑 ]
版权所有,并保留所有权利。
Powered by , Processed in 0.034622 second(s), 8 queries.
Copyright&, BCCN.NET, All Rights Reserved

我要回帖

更多关于 鬼吹灯之黄皮子坟上映 的文章

 

随机推荐