1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Help

Discussion in 'C' started by funkyjohny, Oct 2, 2007.

  1. funkyjohny

    funkyjohny New Member

    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 a moderator: Oct 2, 2007
  2. DaWei

    DaWei New Member

    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.
     
  3. shabbir

    shabbir Administrator Staff Member

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

Share This Page