Hello to all...I 'm new in programming and i want if anybody knows to help me with the following code.It 's about pipes in linux and i want help.So
in following code th function main makes pipe and executes fork. Client then runs in the procedure while server it is executed in the procedure - child.I want if anybody knows to replace the letters A-L so the code can run.
Code:
#include <stdio.h>
#include <unistd.h>
#define MAXBUFF 1024
main()
{
int childpid, pipe1[2], pipe2[2];
if (pipe(pipe1) < 0 || pipe(pipe2) < 0)
printf("can't create pipes\n");
if ((childpid = fork()) < 0)
{ printf("can't fork\n");
}
else if (childpid > 0)
{
/* parent */
close(A);
close(B);
client(C, D);
while (wait((int *) 0) != childpid) /* wait for child */ ;
close(E);
close(F);
exit(0);
} else
{ close(G);
close(H);
server(I, J);
close(K);
close(L);
exit(0);
}
} client(readfd, writefd) int readfd;
int writefd;
{ char buff[MAXBUFF];
int n;
/* Read the filename from standard input, write it to the IPC descriptor */
printf("Write the file name : ");
if (fgets(buff, MAXBUFF, stdin)==NULL)
printf("client: filename read error\n");
n=strlen(buff);
if (buff[n-1]=='\n') n--; /* ignore new line from fgets()
*/ if (write(writefd, buff, n) != n) printf("client: filename write error\n"); /* Read the data from the IPC descriptor and write to standard output */
while ((n=read(readfd, buff, MAXBUFF)) >0)
if (write(1, buff, n) != n)
/* fd 1 stdout */ printf("client: data write error\n");
if (n<0) printf("client: data read error\n");
} server(readfd, writefd) int readfd;
int writefd;
{
char buff[MAXBUFF];
char errmesg[256];
int n, fd;
extern int errno;
/* read the filename from the IPC descriptor */
if ((n=read(readfd, buff, MAXBUFF)) <= 0) printf("server: filename read error\n");
buff[n]='\0'; if ((fd=open(buff, 0)) < 0)
{
/* Error. Format an error message and send it back to the client */ sprintf(errmesg, ": can't open file\n");
strcat(buff, errmesg);
n = strlen(buff);
if (write(writefd, buff, n) != n) printf("server: errmesg write error\n");
}
else
{
/* read the data from the file and write to the IPC descriptor */
while ((n=read(fd, buff, MAXBUFF)) > 0)
if (write(writefd, buff, n) != n) printf("server: data write error\n");
if (n<0)
printf("server: read error\n"); } }
PLEASE HELP!!!!

Last edited by shabbir; 3Oct2007 at 04:09.. Reason: Code block