操作系统 管道应用 实验_第1页
操作系统 管道应用 实验_第2页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、操作系统 管道应用 实验 /* * description: tpipe.c* copyright: (c) by zhanghonglie* function: 利用管道实现在在线程间传递整数*/#include stdio.h#include unistd.h#include stdlib.h#include pthread.hvoid task1(int *); /线程1执行函数原型void task2(int *); /线程2执行函数原型void task3(int *);/线程3int pipe12,pipe22,pipe32;/存放3个无名管道标号pthread_t thrd1,t

2、hrd2,thrd3;/存放3个线程标识int main(int argc,char *arg)int ret;int num1,num2,num3;/使用pipe()系统调用建立两个无名管道。建立不胜利程序退出,执行终止if(pipe(pipe1) 0)perror(pipe not create);exit(exit_failure);if(pipe(pipe2) 0)perror(pipe not create);exit(exit_failure);if(pipe(pipe3) 0)perror(pipe not create);exit(exit_failure);/使用pthrea

3、d_create系统调用建立3个线程。建立不胜利程序退出,执行终止num1 = 1 ;ret = pthread_create(thrd1,null,(void *)task1,(void *)num1);if(ret)perror(pthread_create:task1);exit(exit_failure);num2 = 2 ;ret = pthread_create(thrd2,null,(void *)task2,(void *)num2);if(ret)perror(pthread_create:task2);exit(exit_failure);num3 = 3 ;ret = p

4、thread_create(thrd3,null,(void *)task3,(void *)num3);if(ret)perror(pthread_create:task3);exit(exit_failure);/挂起当前线程切换到thrd3线程pthread_join(thrd3,null);/挂起当前线程切换到thrd2线程pthread_join(thrd2,null);/挂起当前线程切换到thrd1线程pthread_join(thrd1,null);exit(exit_success);/线程1执行f(x)函数,它首先向管道写,然后从管道读void task1(int *num)

5、int x=1,fx;if(x=1)fx=1;/每次循环向管道1的1端写入变量x的值,并从/管道2的0端读一整数写入x再对x加1,直到x大于10elsedofx=fx*x;write(pipe11,fx,sizeof(int);while(x=9);/读写完成后,关闭管道close(pipe11);close(pipe20);/线程1执行函数,它首先从管道读,然后向管道写void task2(int * num)int x,fy,fy1;if(x=1|x=2)fy=fy1=1;elsedofy1=fy+fy;fy=fy+fy1;write(pipe21,fy,sizeof(int);while( x=9 );/读写完成后,关闭管道close(pipe10);close(pipe21);void task3(int * num)int x=1,fy=1,fx=1,fxy=1;/每次循环从管道1的0端读一个整数放入变量fx中,/从管道2的0端读一个整数放入变量fy中doread(pipe10,fx,sizeof(int);read(pipe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论