multiple forks, pipes?

This is a discussion on multiple forks, pipes? within the C Programming forums, part of the General Programming Boards category; What this code does is, parent process forks a child and then child forks a child. Parent writes a string ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    multiple forks, pipes?

    What this code does is, parent process forks a child and then child forks a child. Parent writes a string to the pipe, child reads it and then writes it to the next pipe, the one connecting child and child's child. Child's child reads it and prints it in stdout
    Code:
    #define BSIZ    20      
    
    int fdpipe[2][2];
    
    int main(){
            pipe(fdpipe[0]);
            if(fork()!=0){//parent
                    close(fdpipe[0][0]);
                    
                    write(fdpipe[0][1],"str1\nstr2\nstr3\n",BSIZ);
                    int status;
                    waitpid(-1,&status,0);
            }
            else{
                    close(fdpipe[0][1]);
                    
                    char buf[100];
                    read(fdpipe[0][0],buf,BSIZ);
    
                    pipe(fdpipe[1]);
                    if(fork()!=0){
                            close(fdpipe[1][0]);    
                            
                            write(fdpipe[1][1],buf,BSIZ);
                            
                            int status;
                            waitpid(-1,&status,0);
                    }
                    else{//child's child
                            dup2(fdpipe[1][0],0);
                            close(fdpipe[1][1]);
    
                            char buf2[100];
                            read(fdpipe[1][0],buf2,BSIZ);
                            printf("read from pipe:\n%s",buf2);
            /*              
                            char *args[2];
                            args[0]="sort";
                            args[1]=NULL;
                            execvp(args[0],args);
            */
                    }
            }
    }
    my problem is I tried to change what the last forked child does. Instead of reading from pipe and print (which works fine), I tried to run a sort which I suppose reads from stdin, which has been "dupped" so should read from the pipe and then print to stdout. Anyway, this is the last block,the commented out one. So commenting out read, printf and uncomment the sort is not working, meaning it doesnt print a thing just hangs there, like sort gets no input. If someone could explain...Thanks.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    char *args[2];
    args[0]="sort";
    args[1]=NULL;
    execvp(args[0],args);
    Thats because you are just sending the sort command, thats it. But sort command need some paramter to work up on. Or in more general what do u need to sort. In the your case u are just ending up with an NULL. which is not right. Look at some sort command examples here Sort command Examples.

    ssharish

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    5
    What do you mean sort needs a parameter?
    Code:
    ls|sort
    works without a parameter for sort, no parameter means read from stdin, no? This is the case here, just instead of an ls output, there is another string, which is send in the pipe and sort should read it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks in one client connection (ftpclient)
    By Dynamo in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-16-2011, 12:41 PM
  2. problems with multiple pipes.
    By LightsOut06 in forum Linux Programming
    Replies: 3
    Last Post: 12-02-2010, 02:38 PM
  3. "ls | sort" with pipes and forks?
    By alwut in forum C Programming
    Replies: 3
    Last Post: 01-09-2008, 01:59 AM
  4. Problem with multiple pipes...
    By LightsOut06 in forum C++ Programming
    Replies: 0
    Last Post: 10-28-2005, 11:15 AM
  5. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21