[Linux管道和IPC]管道的实际应用1
2017-11-18 19:09
387 查看
/* 主进程创建2个子进程,子进程1每隔1秒向子进程2发送一个 字符串,子进程接收到该字符串之后将其写入一个指定的文件 */ #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <signal.h> #include <time.h> #include <fcntl.h> #include <string.h> #include <sys/types.h> //以下为主函数 int main(int argc,char *argv[]) { pid_t pid1,pid2; //进程的ID int fd; //文件描述符 char writebuf[] = "this is a test!\n"; //待写入字符串 char readbuf[25]; //读缓冲区 int writecounter = 0; //用于记录写入的偏移量 int temp = 0,seektemp = 0,j = 0; //都是用于计算文件偏移的临时变量 int pipefd[2]; //管道的文件描述符 if (argc != 2) //如果参数不正确 { printf("请输入正确的文件参数。\n"); return 0; } fd = open(*(argv+1),O_RDWR|O_CREAT,S_IRWXU); //打开或者创建一个文件 if(pipe(pipefd) < 0) //如果创建管道失败 { printf("创建管道失败。\n"); exit(0); //退出 } pid1 = fork(); //调用fork创建一个新的进程 if(pid1 != 0) //主进程 { pid2 = fork(); //创建第二个子进程 if(pid2 != 0) //主进程 { close(pipefd[0]); //关闭管道 close(pipefd[1]); } else //这是子进程2的操作 { close(pipefd[1]); while(1) { read(pipefd[0],readbuf,sizeof(writebuf)); //读管道 printf("这是子进程2.\n"); //屏幕输出提示 if(writecounter == 0) //第一次写入 { temp = write(fd,readbuf,strlen(readbuf)); //写入数据 seektemp = lseek(fd,0,SEEK_CUR); //获得当前偏移量 writecounter++; } else { j = strlen(readbuf)*writecounter; seektemp = lseek(fd,j,SEEK_SET); temp = write(fd,readbuf,strlen(writebuf)); writecounter++; } } } } else //子进程 { close(pipefd[0]); while(1) { sleep(1); write(pipefd[1],writebuf,sizeof(writebuf)); //将字符串写入管道 } } }
相关文章推荐
- [Linux管道和IPC]管道的实际应用2
- [Linux管道和IPC]管道的高级应用--使用popen函数创建管道
- 【编撰】linux IPC 002 - 匿名管道PIPE和有名管道FIFO的概念和实例,以及应用比较
- iptables 策略(实际应用)Linux防火墙设置
- Linux/shell命令的实际应用——wc命令获取CPU核心数
- [LinuxIPC] 管道的_PC_PIPE_BUFF语义和O_NONBLOCK语义的理解
- linux应用编程--pipe无名管道
- 初学linux并在项目中的实际应用
- linux应用编程笔记(10)有名管道编程
- [Linux管道和IPC]在父子进程中使用管道
- Linux 进程间通讯(IPC)详细总结 1管道。
- [Linux管道和IPC]在兄弟进程中使用管道
- Linux 进程间通信 (IPC) //管道
- 将 Windows IPC 应用移植到 Linux,第 1 部分: 进程和线程
- Linux中IPC机制:管道和FIFO
- Linux(九):进程通信IPC(三)之验证管道容量
- Linux 进程间通讯(IPC)详细总结 2有名管道。
- 超实用linux管道处理命令xargs的应用示例
- Linux/shell命令的实际应用——查看Port占用 netstat
- Linux进程间通信IPC学习笔记之有名管道