/*****************************************************
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;
}
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;
}