Akaitenshi
Akaitenshi

Reputation: 373

Creation of multiple named pipes

I am trying to make a program that makes a number of children defined by the user. The parent must use named pipes (it is a requirement) to send information back and forth with his children. So, I need to create a number of named pipes equal to the amount of children I am forking. How can I do this efficiently and have every child know what his pipe is named?

pid_t childpid;
for(i = 0; i < numWorker; i++){
    // char *pipeName = "somename";
    // change the pipeName to reflect the child by adding a suffix
    // mkfifo(pipeName, 0666);
    childpid = fork();
    if(childpid < 0){
        perror("fork\n");
    }
    else if(childpid == 0){
        signal(SIGCONT, handleSignalChild);
        // how can I open the fifo here and then carry on reading and writing 
        //inside the while() below?
        break; // child exits the creation loop.
    }
}


// Main program execution begins here
while(1){

    if(childpid == 0){
        // read and write to the already opened pipes.
        //code to handle child execution.
    }
    else{
        // open all fifo pipes and get ready to read and write stuff.
        //code to handle parent execution.
    }

}

EDIT: Reworded the question to make more sense.

Upvotes: 0

Views: 590

Answers (0)

Related Questions