#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int data;
int file_pipes[2];
const char some_data[]="1234";
char buffer[BUFSIZ+1];
pid_t fork_result;
memset(buffer,'\0',sizeof(buffer));
if(pipe(file_pipes)==0)
{
fork_result=fork();
if(fork_result==-1)
{
fprintf(stderr,"Fork failure");
exit(EXIT_FAILURE);
}
if(fork_result==0)
{
sprintf(buffer,"%d",file_pipes[0]);
(void)execl("pipe4","pipe4",buffer,(char *)0);
exit(EXIT_FAILURE);
}
else
{
data=write(file_pipes[1],some_data,strlen(some_data));
printf("%d - wrote %d byte\n",getpid(),data);
}
}
exit(EXIT_SUCCESS);
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int data;
int file_pipes[2];
const char some_data[]="1234";
char buffer[BUFSIZ+1];
pid_t fork_result;
memset(buffer,'\0',sizeof(buffer));
if(pipe(file_pipes)==0)
{
fork_result=fork();
if(fork_result==-1)
{
fprintf(stderr,"Fork failure");
exit(EXIT_FAILURE);
}
if(fork_result==0)
{
sprintf(buffer,"%d",file_pipes[0]);
(void)execl("pipe4","pipe4",buffer,(char *)0);
exit(EXIT_FAILURE);
}
else
{
data=write(file_pipes[1],some_data,strlen(some_data));
printf("%d - wrote %d byte\n",getpid(),data);
}
}
exit(EXIT_SUCCESS);
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
int data;
char buffer[BUFSIZ+1];
int file_des;
memset(buffer,'\0',sizeof(buffer));
sscanf(argv[1],"%d",&file_des);
data=read(file_des,buffer,BUFSIZ);
printf("%d-read %d bytes:%s\n",getpid(),data,buffer);
exit(EXIT_SUCCESS);
}
上面一个程序在运行的过程中启动第二个程序,两个程序通过管道进行通信