无名管道,进程练习,小写转大写

/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd. 
File name:
Author:Jerey_Jobs    Version:0.1    Date: 
Description:
Funcion List: 
*****************************************************/


#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>//pipe函数
#include <stdlib.h>


int main()
{
    int fd_source[2];
int fd_order[2];
char r_buf[100];
char w_buf[100];
pid_t pid;
    int i;
if(pipe(fd_source) < 0)
{
perror("pipe error!\n");
exit(-1);
}
if(pipe(fd_order) < 0)
{
perror("pipe error!\n");
exit(-1);
}


memset(r_buf,0,sizeof(r_buf));
memset(w_buf,0,sizeof(w_buf));


pid = fork();


if (pid == 0)//关闭父进程的写端和子进程的读端
{
close(fd_source[1]);
close(fd_order[0]);
sleep(10);
read(fd_source[0],r_buf,sizeof(r_buf));
printf("get string from father:%s\n",r_buf);


for(i = 0 ; i < strlen(r_buf) ; i++)
{
if(r_buf[i] >= 'a' && r_buf[i] <='z')
{
r_buf[i] = r_buf[i] - '0' - 32 + '0';
}
}
write(fd_order[1],r_buf,strlen(r_buf)+1);
printf("wait father to read.\n");


close(fd_source[1]);
close(fd_order[0]);
exit(-1);
}
else if (pid > 0)//父进程,关闭父进程的读端,子进程的写端
{
close(fd_source[0]);
close(fd_order[1]);
printf("please enter a string:\n");
scanf("%s",w_buf);


write(fd_source[1],w_buf,strlen(w_buf)+1);
printf("wait son read!\n");
sleep(15);


memset(w_buf,0,sizeof(w_buf));
read(fd_order[0],w_buf,sizeof(w_buf));
printf("get string from son:%s\n",w_buf);


close(fd_order[0]);
close(fd_source[1]);
}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值