Monday 23 September 2013

Linux | Pipe - Read & Write Program


Read Pipe :

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<error.h>

int main()
{
int fd;

fd = open("MyPipe",O_RDONLY,0666);

if(fd<0)
{
perror("Open");
return 0;
}


char c;
int s;

while(1)
{
s=read(fd,&c,1);
if(!s)
break;
printf("%c",c);
}

printf("\n\n");

close(fd);

}


Write Pipe :


#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<error.h>

int main()
{
int fd;

fd = open("MyPipe",O_WRONLY,0666);

if(fd<0)
{
perror("Open");
return 0;
}


char c[]="Welcome";
int s;


s=write(fd,c,7);

printf("\n\n");

close(fd);

}

0 comments:

Post a Comment