⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ppipe.c

📁 操作系统中进程通信的说明性实验程序
💻 C
字号:
#include<stdio.h>#include<unistd.h>#include<stdlib.h>int main(int argc,char *argv[]){int pid;int pipe1[2];int pipe2[2];int x;if(pipe(pipe1)<0){perror("pipe not create");exit(EXIT_FAILURE);}if(pipe(pipe2)<0){perror("pipe not create");exit(EXIT_FAILURE);}if((pid=fork())<0){perror("pipe not create");exit(EXIT_FAILURE);}else if(pid==0){close(pipe1[1]);close(pipe2[0]);do{read(pipe1[0],&x,sizeof(int));printf("child%dread:%d\n",getpid(),x++);write(pipe2[1],&x,sizeof(int));}while(x<=9);close(pipe1[0]);close(pipe2[1]);exit(EXIT_SUCCESS);}else{close(pipe1[0]);close(pipe2[1]);x=1;do{write(pipe2[1],&x,sizeof(int));read(pipe1[0],&x,sizeof(int));printf("parent%dread:%d\n",getpid(),x++);}while(x<=9);close(pipe1[1]);close(pipe2[0]);}return EXIT_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -