Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help (http://www.go4expert.com/forums/help-t6652/)

funkyjohny 2Oct2007 23:54

Help
 
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!!!!

DaWei 3Oct2007 01:19

Re: Help
 
Let me give you some advice. You want free help. You need to read the "Before you make a query" thread to see how things work.

First of all, you need to use a descriptive title for your post, so other people needing help with the same thing can find it.

Second, you need to put your code in code tags, so that the indentation/formatting is retained (it's a readability thang, doanchano).

Now, I could edit your post and do that for you, but I suspect you're a big boy, now. You should check these things out, in advance, and handle them.

shabbir 3Oct2007 04:11

Re: Help
 
Please put your code in the format so that its readable and well indented.


All times are GMT +5.5. The time now is 10:25.