Thread: Fork() ... chuild still exist ..

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    12

    Fork() ... chuild still exist ..

    Hi

    i need to create 5 children ... and each must have open one end of five pipes ...

    so i create a this ...

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    #include <string.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <sys/wait.h>
    
    int g_fd[5][2];
    
    int main () 
    {
        // Create Pipes
        for(int i = 0; i<5; i++)
        {
            pipe(g_fd[i]);
        }
        // Create Children
        for(int j = 0; j<5; j++)
        {
            if(fork()==0)
            {
                //CloseAllPipies
                for(int i=0; i<5; i++)
                {
                    close(g_fd[i][0]);
                    printf("Process %d Pipe %d End 0 is CLOSE\n", j, i);
                    if(j==i)
                    {
                        printf("Process %d Pipe %d End 0 is OPEN\n", j, i);
                        //Write In child
                    }
                    else
                    {
                        printf("Process %d Pipe %d End 1 is CLOSE\n", j, i);
                        close(g_fd[i][1]);
                    }
                }
            }
            else //end Parent
            {
                return 0;
            } 
        }
        printf("Some out ... \n");
        return(0);
    }
    now my output is:
    Code:
    Process 0 Pipe 0 End 0 is CLOSE
    Process 0 Pipe 0 End 0 is OPEN
    Process 0 Pipe 1 End 0 is CLOSE
    Process 0 Pipe 1 End 1 is CLOSE
    Process 0 Pipe 2 End 0 is CLOSE
    Process 0 Pipe 2 End 1 is CLOSE
    Process 0 Pipe 3 End 0 is CLOSE
    Process 0 Pipe 3 End 1 is CLOSE
    Process 0 Pipe 4 End 0 is CLOSE
    Process 0 Pipe 4 End 1 is CLOSE
    Process 0 Pipe 0 End 0 is CLOSE
    Process 0 Pipe 0 End 0 is OPEN
    Process 0 Pipe 1 End 0 is CLOSE
    Process 0 Pipe 1 End 1 is CLOSE
    Process 0 Pipe 2 End 0 is CLOSE
    Process 0 Pipe 2 End 1 is CLOSE
    Process 0 Pipe 3 End 0 is CLOSE
    Process 0 Pipe 3 End 1 is CLOSE
    Process 0 Pipe 4 End 0 is CLOSE
    Process 0 Pipe 4 End 1 is CLOSE
    Process 1 Pipe 0 End 0 is CLOSE
    Process 1 Pipe 0 End 1 is CLOSE
    Process 1 Pipe 1 End 0 is CLOSE
    Process 1 Pipe 1 End 0 is OPEN
    Process 1 Pipe 2 End 0 is CLOSE
    Process 1 Pipe 2 End 1 is CLOSE
    Process 1 Pipe 3 End 0 is CLOSE
    Process 1 Pipe 3 End 1 is CLOSE
    Process 1 Pipe 4 End 0 is CLOSE
    Process 1 Pipe 4 End 1 is CLOSE
    Process 0 Pipe 0 End 0 is CLOSE
    Process 0 Pipe 0 End 0 is OPEN
    Process 0 Pipe 1 End 0 is CLOSE
    Process 0 Pipe 1 End 1 is CLOSE
    Process 0 Pipe 2 End 0 is CLOSE
    Process 0 Pipe 2 End 1 is CLOSE
    Process 0 Pipe 3 End 0 is CLOSE
    Process 0 Pipe 3 End 1 is CLOSE
    Process 0 Pipe 4 End 0 is CLOSE
    Process 0 Pipe 4 End 1 is CLOSE
    Process 1 Pipe 0 End 0 is CLOSE
    Process 1 Pipe 0 End 1 is CLOSE
    Process 1 Pipe 1 End 0 is CLOSE
    Process 1 Pipe 1 End 0 is OPEN
    Process 1 Pipe 2 End 0 is CLOSE
    Process 1 Pipe 2 End 1 is CLOSE
    Process 1 Pipe 3 End 0 is CLOSE
    Process 1 Pipe 3 End 1 is CLOSE
    Process 1 Pipe 4 End 0 is CLOSE
    Process 1 Pipe 4 End 1 is CLOSE
    Process 2 Pipe 0 End 0 is CLOSE
    Process 2 Pipe 0 End 1 is CLOSE
    Process 2 Pipe 1 End 0 is CLOSE
    Process 2 Pipe 1 End 1 is CLOSE
    Process 2 Pipe 2 End 0 is CLOSE
    Process 2 Pipe 2 End 0 is OPEN
    Process 2 Pipe 3 End 0 is CLOSE
    Process 2 Pipe 3 End 1 is CLOSE
    Process 2 Pipe 4 End 0 is CLOSE
    Process 2 Pipe 4 End 1 is CLOSE
    Process 0 Pipe 0 End 0 is CLOSE
    Process 0 Pipe 0 End 0 is OPEN
    Process 0 Pipe 1 End 0 is CLOSE
    Process 0 Pipe 1 End 1 is CLOSE
    Process 0 Pipe 2 End 0 is CLOSE
    Process 0 Pipe 2 End 1 is CLOSE
    Process 0 Pipe 3 End 0 is CLOSE
    Process 0 Pipe 3 End 1 is CLOSE
    Process 0 Pipe 4 End 0 is CLOSE
    Process 0 Pipe 4 End 1 is CLOSE
    Process 1 Pipe 0 End 0 is CLOSE
    Process 1 Pipe 0 End 1 is CLOSE
    Process 1 Pipe 1 End 0 is CLOSE
    Process 1 Pipe 1 End 0 is OPEN
    Process 1 Pipe 2 End 0 is CLOSE
    Process 1 Pipe 2 End 1 is CLOSE
    Process 1 Pipe 3 End 0 is CLOSE
    Process 1 Pipe 3 End 1 is CLOSE
    Process 1 Pipe 4 End 0 is CLOSE
    Process 1 Pipe 4 End 1 is CLOSE
    Process 2 Pipe 0 End 0 is CLOSE
    Process 2 Pipe 0 End 1 is CLOSE
    Process 2 Pipe 1 End 0 is CLOSE
    Process 2 Pipe 1 End 1 is CLOSE
    Process 2 Pipe 2 End 0 is CLOSE
    Process 2 Pipe 2 End 0 is OPEN
    Process 2 Pipe 3 End 0 is CLOSE
    Process 2 Pipe 3 End 1 is CLOSE
    Process 2 Pipe 4 End 0 is CLOSE
    Process 2 Pipe 4 End 1 is CLOSE
    Process 3 Pipe 0 End 0 is CLOSE
    Process 3 Pipe 0 End 1 is CLOSE
    Process 3 Pipe 1 End 0 is CLOSE
    Process 3 Pipe 1 End 1 is CLOSE
    Process 3 Pipe 2 End 0 is CLOSE
    Process 3 Pipe 2 End 1 is CLOSE
    Process 3 Pipe 3 End 0 is CLOSE
    Process 3 Pipe 3 End 0 is OPEN
    Process 3 Pipe 4 End 0 is CLOSE
    Process 3 Pipe 4 End 1 is CLOSE
    Process 0 Pipe 0 End 0 is CLOSE
    Process 0 Pipe 0 End 0 is OPEN
    Process 0 Pipe 1 End 0 is CLOSE
    Process 0 Pipe 1 End 1 is CLOSE
    Process 0 Pipe 2 End 0 is CLOSE
    Process 0 Pipe 2 End 1 is CLOSE
    Process 0 Pipe 3 End 0 is CLOSE
    Process 0 Pipe 3 End 1 is CLOSE
    Process 0 Pipe 4 End 0 is CLOSE
    Process 0 Pipe 4 End 1 is CLOSE
    Process 1 Pipe 0 End 0 is CLOSE
    Process 1 Pipe 0 End 1 is CLOSE
    Process 1 Pipe 1 End 0 is CLOSE
    Process 1 Pipe 1 End 0 is OPEN
    Process 1 Pipe 2 End 0 is CLOSE
    Process 1 Pipe 2 End 1 is CLOSE
    Process 1 Pipe 3 End 0 is CLOSE
    Process 1 Pipe 3 End 1 is CLOSE
    Process 1 Pipe 4 End 0 is CLOSE
    Process 1 Pipe 4 End 1 is CLOSE
    Process 2 Pipe 0 End 0 is CLOSE
    Process 2 Pipe 0 End 1 is CLOSE
    Process 2 Pipe 1 End 0 is CLOSE
    Process 2 Pipe 1 End 1 is CLOSE
    Process 2 Pipe 2 End 0 is CLOSE
    Process 2 Pipe 2 End 0 is OPEN
    Process 2 Pipe 3 End 0 is CLOSE
    Process 2 Pipe 3 End 1 is CLOSE
    Process 2 Pipe 4 End 0 is CLOSE
    Process 2 Pipe 4 End 1 is CLOSE
    Process 3 Pipe 0 End 0 is CLOSE
    Process 3 Pipe 0 End 1 is CLOSE
    Process 3 Pipe 1 End 0 is CLOSE
    Process 3 Pipe 1 End 1 is CLOSE
    Process 3 Pipe 2 End 0 is CLOSE
    Process 3 Pipe 2 End 1 is CLOSE
    Process 3 Pipe 3 End 0 is CLOSE
    Process 3 Pipe 3 End 0 is OPEN
    Process 3 Pipe 4 End 0 is CLOSE
    Process 3 Pipe 4 End 1 is CLOSE
    Process 4 Pipe 0 End 0 is CLOSE
    Process 4 Pipe 0 End 1 is CLOSE
    Process 4 Pipe 1 End 0 is CLOSE
    Process 4 Pipe 1 End 1 is CLOSE
    Process 4 Pipe 2 End 0 is CLOSE
    Process 4 Pipe 2 End 1 is CLOSE
    Process 4 Pipe 3 End 0 is CLOSE
    Process 4 Pipe 3 End 1 is CLOSE
    Process 4 Pipe 4 End 0 is CLOSE
    Process 4 Pipe 4 End 0 is OPEN
    Some out ...


    And want on my output this:
    Code:
    Process 0 Pipe 0 End 0 is CLOSE
    Process 0 Pipe 0 End 0 is OPEN
    Process 0 Pipe 1 End 0 is CLOSE
    Process 0 Pipe 1 End 1 is CLOSE
    Process 0 Pipe 2 End 0 is CLOSE
    Process 0 Pipe 2 End 1 is CLOSE
    Process 0 Pipe 3 End 0 is CLOSE
    Process 0 Pipe 3 End 1 is CLOSE
    Process 0 Pipe 4 End 0 is CLOSE
    Process 0 Pipe 4 End 1 is CLOSE
    Process 1 Pipe 0 End 0 is CLOSE
    Process 1 Pipe 0 End 1 is CLOSE
    Process 1 Pipe 1 End 0 is CLOSE
    Process 1 Pipe 1 End 0 is OPEN
    Process 1 Pipe 2 End 0 is CLOSE
    Process 1 Pipe 2 End 1 is CLOSE
    Process 1 Pipe 3 End 0 is CLOSE
    Process 1 Pipe 3 End 1 is CLOSE
    Process 1 Pipe 4 End 0 is CLOSE
    Process 1 Pipe 4 End 1 is CLOSE
    Process 2 Pipe 0 End 0 is CLOSE
    Process 2 Pipe 0 End 1 is CLOSE
    Process 2 Pipe 1 End 0 is CLOSE
    Process 2 Pipe 1 End 1 is CLOSE
    Process 2 Pipe 2 End 0 is CLOSE
    Process 2 Pipe 2 End 0 is OPEN
    Process 2 Pipe 3 End 0 is CLOSE
    Process 2 Pipe 3 End 1 is CLOSE
    Process 2 Pipe 4 End 0 is CLOSE
    Process 2 Pipe 4 End 1 is CLOSE
    Process 3 Pipe 0 End 0 is CLOSE
    Process 3 Pipe 0 End 1 is CLOSE
    Process 3 Pipe 1 End 0 is CLOSE
    Process 3 Pipe 1 End 1 is CLOSE
    Process 3 Pipe 2 End 0 is CLOSE
    Process 3 Pipe 2 End 1 is CLOSE
    Process 3 Pipe 3 End 0 is CLOSE
    Process 3 Pipe 3 End 0 is OPEN
    Process 3 Pipe 4 End 0 is CLOSE
    Process 3 Pipe 4 End 1 is CLOSE
    Process 4 Pipe 0 End 0 is CLOSE
    Process 4 Pipe 0 End 1 is CLOSE
    Process 4 Pipe 1 End 0 is CLOSE
    Process 4 Pipe 1 End 1 is CLOSE
    Process 4 Pipe 2 End 0 is CLOSE
    Process 4 Pipe 2 End 1 is CLOSE
    Process 4 Pipe 3 End 0 is CLOSE
    Process 4 Pipe 3 End 1 is CLOSE
    Process 4 Pipe 4 End 0 is CLOSE
    Process 4 Pipe 4 End 0 is OPEN
    Some out ...

  2. #2
    Registered User
    Join Date
    Nov 2020
    Posts
    12
    My mistake ... i found trouble ... in placeing return to wrong process ...

    this is correct ...

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <unistd.h>
    #include <signal.h>
    #include <string.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <sys/wait.h>
    
    
    
    
    int g_fd[5][2];
    
    
    void handler_readPipe(int sig)
    {
        printf("I have signal ... \n");
    }
    
    
    void handler_readPipeSIGIO(int sig, siginfo_t *info, void *secret) 
    {
        printf("I have signal ... \n");
    }
    
    
    int main () 
    {
        // Create Pipes
        for(int i = 0; i<5; i++)
        {
            pipe(g_fd[i]);
        }
    
    
        ///int Dady = fork();
    
    
        // Create Children
        for(int j = 0; j<5; j++)
        {
            if(fork()==0)
            {
            //CloseAllPipies
                for(int i=0; i<5; i++)
                {
                    close(g_fd[i][0]);
                    printf("Process %d Pipe %d End 0 is CLOSE\n", j, i);
                    if(j==i)
                    {
                        printf("Process %d Pipe %d End 0 is OPEN\n", j, i);
                        //printf("MyChildD %d \n", getpid());
                        //Write In child
                    }
                    else
                    {
                        printf("Process %d Pipe %d End 1 is CLOSE\n", j, i);
                        close(g_fd[i][1]);
                    }
                }
                return 0;
            }
            else 
            {
                //printf("Parent process %d \n", getppid());
                //printf("MyID %d \n", getpid());
                //return 0;
                wait(NULL);
            } 
        }
        wait(NULL);
        printf("Some out ... \n");
        return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if not exist, failed
    By mauric in forum C Programming
    Replies: 3
    Last Post: 07-24-2020, 08:06 AM
  2. Replies: 5
    Last Post: 02-27-2014, 03:53 AM
  3. The name ___ does not exist...
    By Xfer in forum C# Programming
    Replies: 4
    Last Post: 01-08-2011, 12:24 PM
  4. Does this exist?
    By Kennedy in forum Tech Board
    Replies: 2
    Last Post: 11-23-2006, 03:04 AM
  5. does uint4 exist?
    By rosgreco in forum C++ Programming
    Replies: 1
    Last Post: 06-11-2002, 11:44 AM

Tags for this Thread