#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int fd[2];
char str[256];
if (pipe(fd) < 0)
{
printf("create pipe failed!\n");
exit(1);
}
write(fd[1],"create the pipe successfuilly!\n",31);
read(fd[0],str,sizeof(str));
printf("%s\n",str);
printf("pipe file descriptors are %d,%d \n",fd[0],fd[1]);
close(fd[0]);
close(fd[1]);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <limits.h>
#define BUFSIZE PIPE_BUF
void err_quit(char* msg)
{
printf("%s\n",msg);
exit(1);
}
int main()
{
int pid;
int fd[2];
char buf[BUFSIZE] = "hello my child,i am zhaoyun that is your perent\n";
int len;
if (pipe(fd) < 0)
{
err_quit("pipe failed\n");
}
//create child
if ((pid = fork() < 0))
{
err_quit("fork failed\n");
}
else if (pid > 0)
{
close(fd[0]);
write(fd[1],buf,strlen(buf));
exit(0);
}
else
{
close(fd[1]);
len = read(fd[0],BUFSIZE);
if (len < 0)
{
err_quit("process failed when read a pipe\n");
}
else
{
// printf("%s\n",buf);
write(STDOUT_FILENO,len);
}
exit(0);
}
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。