linux管道应用实例,[Linux管道和IPC]管道的实际应用1

/*

主进程创建2个子进程,子进程1每隔1秒向子进程2发送一个

字符串,子进程接收到该字符串之后将其写入一个指定的文件

*/

#include 

#include 

#include 

#include 

#include 

#include 

#include 

#include 

//以下为主函数

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) 

{

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));   //将字符串写入管道

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值