multiple fork, pipes???

Discussion in 'C' started by algafan, Oct 25, 2007.

  1. algafan

    algafan New Member

    Joined:
    Oct 20, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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){//this
                            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. If someone could explain...Thanks.
     
  2. algafan

    algafan New Member

    Joined:
    Oct 20, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
     
  3. apostilleus

    apostilleus New Member

    Joined:
    May 8, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    self employed
    Location:
    USA
    Home Page:
    http://www.apostille.us/
    i just modified my kernel so that it act like SCHED_FIFO scheduling policy...now i have to write a program to test my policy this is the part that i have trouble with.

    For example, i want to create 10 process and realtime priority (1-99) assigned to each and it will print out a different message just to prove that it has been executed. To create 10 processes we have to create a loop like this:
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice