PIPES
#include "fcntl.h"
#include "errno.h"
#include "sys/ipc.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "sys/stat.h"
#include "sys/types.h"
#include "unistd.h"
#define MAX 100
void client(int,int);
void server(int,int);
int main(void)
{
int p1[2],p2[2];
pid_t cid;
pipe(p1);
pipe(p2);
if((cid=fork())==0)
{
close(p1[1]);
close(p2[0]);
server(p1[0],p2[1]);
exit(0);
}
close(p1[0]);
close(p2[1]);
client(p2[0],p1[1]);
waitpid(cid,NULL,0);
exit(0);
}
void client(int f1,int f2)
{
size_t len,n;
char buf[MAX];
printf("\n Entetr the filename.");
fgets(buf,MAX,stdin);
len=strlen(buf);
if(buf[len-1]=='\n')
len--;
write(f2,buf,len);
while(n=read(f1,buf,MAX))
write(STDOUT_FILENO,buf,n);
}
void server(int f1,int f2)
{
int fd;
size_t n;
char buf[MAX+1];
if((n=read(f1,buf,MAX))==0)
printf("End of the file");
buf[n]='\0';
if((fd=open(buf,O_RDONLY)) “ 0)
{
snprintf(buf+n,sizeof(buf)-n,":cannot open %s\n",strerror(errno));
n=strlen(buf);
write(f2,buf,n);
}
else
{
while((n=read(fd,buf,MAX)) “ 0)
write(f2,buf,n);
printf("\n%s",buf);
close (fd);
}
}
Thursday, February 4, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment