#include<unistd.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
int main(void)
{
//define some var
pid_t pc;
int pp;
int mfd[2];
char parray[10];
char carray[10];
//create a pipe
pp=pipe(mfd);
//judge if wrong
if(pp<0)
{
printf("pipe error\n");
}
pc=fork();
//parent process
if(pc<0)
{
printf("create process error\n");
}
else
if(pc>0)
{
parray="helloworld";
int a=write(mfd[1],parray,10);
if(a<0)
printf("write error");
}
//child process
if(pc==0)
{
int b=read(mfd[0], carray,10);
if(b>0)
prntf("read ok");
printf("child read is: %s\n",carray);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
int main(void)
{
//define some var
pid_t pc;
int pp;
int mfd[2];
char parray[10];
char carray[10];
//create a pipe
pp=pipe(mfd);
//judge if wrong
if(pp<0)
{
printf("pipe error\n");
}
pc=fork();
//parent process
if(pc<0)
{
printf("create process error\n");
}
else
if(pc>0)
{
parray="helloworld";
int a=write(mfd[1],parray,10);
if(a<0)
printf("write error");
}
//child process
if(pc==0)
{
int b=read(mfd[0], carray,10);
if(b>0)
prntf("read ok");
printf("child read is: %s\n",carray);
}
return 0;
}
本文介绍了一个简单的父进程与子进程间通过管道进行通信的C语言程序示例。该程序首先创建一个管道,然后通过fork()函数创建子进程。父进程向管道写入字符串“helloworld”,随后子进程从管道中读取数据并打印出来。
2488

被折叠的 条评论
为什么被折叠?



