How do you create Unix pipes dynamically? - c

I have a program that uses fork() to create child processes, and I want to have the children communicate back to the parent process using Unix pipes.
The problem is that multiple pipes don't seem to be created, or maybe there's a problem with my array. When I used prinf() in the parent program it reads identical data from every pipe, even though each child sends different data.
Here is my code:
// Variables
int pipes_count = 0;
int *pipes[MAXCLIENTS];
int new_pipefd[2];
int pipe_bytes;
char pipe_buffer[MAXDATASIZE];
while(1) {
// Pipe creation
pipe(new_pipefd);
pipes[pipes_count] = new_pipefd;
pipes_count++;
if (fork()) {
// unrelated code for parent here
close(new_pipefd[1]); // close the parent's write-end of the pipe
break;
} else {
// unrelated code for child here
close(new_pipefd[0]); // close the child's read-end of the pipe
break;
}
if (some condition) { break; } // The parent will stop creating pipes
}
while(condition that guarantees this is the child) {
write(new_pipefd[1], buffer, strlen(recv_buffer));
close(new_pipefd[1]);
return 0; // the child process ends
}
// This is a loop where the parent reads what the children sent
for (int i = 0; i < pipes_count; i++) {
pipe_bytes = read(pipes[i][0], pipe_buffer, sizeof pipe_buffer);
if (pipe_bytes == 0) {
close(pipes[i][0]);
} else {
printf("Testing: %s\n", pipe_buffer);
}
}

As I noted in my comments, the problem is in the assignment pipes[pipes_count] = new_pipefd; at:
int pipes_count = 0;
int *pipes[MAXCLIENTS];
int new_pipefd[2];
int pipe_bytes;
char pipe_buffer[MAXDATASIZE];
while(1) {
// Pipe creation
pipe(new_pipefd);
pipes[pipes_count] = new_pipefd;
pipes_count++;
The trouble is that the variable new_pipefd is an array, so you're copying the address of the same array into each of the elements of pipes, which means that the parent only has access to the last pipe that was created.
I think you should be using code more like:
int pipes_count = 0;
int pipes[MAXCLIENTS]; // Changed type!
int new_pipefd[2];
char pipe_buffer[MAXDATASIZE];
while (1)
{
// Pipe creation
pipe(new_pipefd);
pipes[pipes_count++] = new_pipefd[0]; // Just the read end of the pipe
if (fork())
{
// unrelated code for parent here
close(new_pipefd[1]); // close the parent's write-end of the pipe
// break; // This break is not wanted
}
else
{
// unrelated code for child here
close(new_pipefd[0]); // close the child's read-end of the pipe
break;
}
if (some condition)
break; // The parent will stop creating pipes
}
while (condition that guarantees this is the child)
{
write(new_pipefd[1], buffer, strlen(recv_buffer));
close(new_pipefd[1]);
return 0; // the child process ends
}
// This is a loop where the parent reads what the children sent
for (int i = 0; i < pipes_count; i++) {
int pipe_bytes = read(pipes[i], pipe_buffer, sizeof(pipe_buffer));
if (pipe_bytes != 0)
printf("Testing: %.*s\n", pipe_bytes, pipe_buffer); // Safe!
close(pipes[i]);
}
Were it my code, I'd have a function (which I traditionally call be_childish()) to invoke in the 'if it is a child' block of code in the loop. The function would never return, and would be passed whatever resources it needs (new_pipefd for sure, maybe other information too). I often have a function be_parental() to do the parental activities. I find this cleans up most of the code, forcing clean separation of the activities.

Related

Piping between several processes in C

I'm writing a shell in C and am trying to implement multiple pipes. I've done this by creating a two dimensional array with pipes and using a separate pipe everytime. All commands in between pipes are separated by a parsing function and put into a struct. Every command line in-between pipes gets it's own process. And for all commands in the middle I'm trying to read from the previous process and write to the next one. Somewhere here the problem starts. It works fine for one pipe, however when I trying more than one pipe I don't get any output and the program gets stuck. In GDB I get a failed message from the execvp after forking the second process. What can this be due to?
int create_pipe(int* fd)
{
int pipe_id = pipe(fd);
if (pipe_id == -1)
{
return -1;
}
return 0;
}
void write_pipe(int* fd)
{
close(fd[READ]);
if ((dup2(fd[WRITE], STDOUT_FILENO)) < -1)
{
fork_error();
}
close(fd[WRITE]);
}
void read_pipe(int *fd)
{
close(fd[WRITE]);
if (dup2(fd[READ], STDIN_FILENO) < 0)
{
fork_error();
}
close(fd[READ]);
}
void need_to_pipe (int i, int (*fd)[2])
{
if (commands[i].pos == first)
{
write_pipe(fd[i * 2]);
}
else if (commands[i].pos == last)
{
read_pipe(fd[(i-1) *2]);
}
else //if (commands[i].pos == middle)
{
dup2(fd[(i-1)*2][READ], STDIN_FILENO);
close(fd[(i-1)*2][READ]);
close(fd[(i-1)*2][WRITE]);
//close(fd[(i)*2][READ]);
//close(fd[(i)*2][WRITE]);
close(fd[(i)*2][READ]);
dup2(fd[i*2][WRITE], STDOUT_FILENO);
close(fd[(i)*2][WRITE]);
}
}
/**
* Fork a proccess for command with index i in the command pipeline. If needed,
* create a new pipe and update the in and out members for the command..
*/
void fork_cmd(int i, int (*fd)[2]) {
pid_t pid;
switch (pid = fork()) {
case -1:
fork_error();
case 0:
// Child process after a successful fork().
if (!(commands[i].pos == single))
{
need_to_pipe(i, fd);
}
// Execute the command in the contex of the child process.
if (execvp(commands[i].argv[0], commands[i].argv)<0)
{
fprintf(stderr, "command not found: %s\n",
commands[i].argv[0]);
exit(EXIT_FAILURE);
}
default:
// Parent process after a successful fork().
break;
}
}
/**
* Fork one child process for each command in the command pipeline.
*/
void fork_cmds(int n, int (*fd)[2])
{
for (int i = 0; i < n; i++)
{
fork_cmd(i, fd);
}
}
void wait_once ()
{
wait(NULL);
}
/**
* Make the parents wait for all the child processes.
*/
void wait_for_all_cmds(int n)
{
for (int i = 0; i < n; i++)
{
wait_once();
//wait for number of child processes.
}
}
int main() {
int n; // Number of commands in a command pipeline.
size_t size = 128; // Max size of a command line string.
char line[size];
while(true) {
// Buffer for a command line string.
printf(" >>> ");
get_line(line, size);
n = parse_cmds(line, commands);
int fd[(n-1)][2];
for(int i =0;i<n-1;i++)
{
int pipe_id = pipe(fd[i*2]);
if (pipe_id == -1)
{
return -1;
}
}
fork_cmds(n, fd);
for(int i =0;i<n-1;i++)
{
int *fdclose= fd[i*2];
close (fdclose[READ]);
close (fdclose[WRITE]);
}
wait_for_all_cmds(n);
}
exit(EXIT_SUCCESS);
}
You [probably] have too many processes keeping pipe ends open (that do not belong to the given child) because your loop opens all pipes before any forking.
This places an undue burden on each child because it has to close many pipe ends to prevent it from holding open a pipe end, preventing other children from seeing an EOF on their input pipes.
To see this, for debug purposes in your present code, the child could do (just before the exec* call) (e.g.):
fprintf(stderr,"child: %d\n",getpid());
fflush(stderr);
system("ls -l /proc/self/fd 1>&2");
Each child should only have three open streams on stdin, stdout, and stderr (e.g. 0, 1, 2).
I think you'd find that there are many extraneous/detrimental streams open on the various children.
You only need two pipe arrays (e.g.): int pipeinp[2]; int pipeout[2]; Initially, pipeinp is all -1.
Roughly ...
Parent should do a single pipe call at the top of fork_cmd [before the fork] to pipeout.
The child dups (and closes) the read end of pipeinp [if not -1] to stdin.
Child dups/closes the write end of pipeout to stdout.
It closes the read end of pipeout.
After that, the parent should copy pipeout to pipeinp and close the write end of pipeinp
This should be repeated for all pipe stages.
No pipe to pipeout should be done for the last command. And, the [last] child should not change stdout.
For a working example, see my answer: fd leak, custom Shell

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.

Synchronizing pipe access for two way read/write

I have a legacy piece of code where I am delegating certain logic to a child process. The requirement is that parent writes to the pipe and the child reads on it. After reading, child writes something to a new pipe and parent reads from it.
In the code below, function send() is called periodically by a dedicated thread in the parent process.
LaunchWorker() ensures that the child process is forked only on the first call to it.
1) I am not being able to figure out how to close the read and write ends of the two descriptors so that the old data written to the pipe is flushed on every write.
2) Also is calling pipe() twice needed for the two descriptors?
Any input to make this code work will be greatly appreciated.
Thanks!
typedef struct
{
WebClient* wc; // user defined class
string url;
string data;
size_t dataLen;
DownloadObserver* downloadCallback; // user defined class
string* token;
}Payload;
PayLoad pl;
static pid_t worker_pid = 0;
int fd1[2];
int fd2[2];
bool done = false;
void LaunchWorker()
{
if (worker_pid != 0)
{
return;
}
pipe(fd1);
pipe(fd2);
worker_pid = fork();
}
void send()
{
//populate pl;
LaunchWorker();
if (worker_pid == 0)
{
while(true)
{
close(fd1[1]);
close(fd2[0]);
int cr = read(fd1[0], &pl, sizeof(Payload));
if (cr > 0)
{
// Upload some data to a remote http endpoint - uses libcurl
//if upload success, done = true
int cw = write(fd2[1], &done, sizeof(bool));
if (cw > 0)
{
// success
}
else
{
// failure
}
}
else
{
// failure
}
}
}
else if (workper_pid > 0)
{
close(fd1[0]);
close(fd2[1]);
int pw = write(fd1[1], &pl, sizeof(Payload));
if (pw > 0)
{
int pr = read(fd2[0], &done, sizeof(bool));
if (pr > 0)
{
// do something with value read
}
else
{
// failure
}
}
else
{
failure
}
}
}
Firstly, you need to close the unused ends of the pipe immediately after the fork, so the parent should close the read end of fd1 and the write end of fd2 and the child should close the write end of fd1 and the read end of fd2. If you don't do this, the pipes will not be closed when you finish.
Then, when you want to stop everything, I would have the writer of each pipe close the write end of the pipe. i.e.
parent closes write end of fd1
child reads eof on fd1 and closes it
child closes the write end of fd2
parent reads eof on fd2 and closes it.
Or the other way around if the child initiates shut down.
Finally, the parent process should issue the wait system call to collect the exit result of the child.
And yes, you do need two pipes. Pipes are one way.

Closing Nonduplex Unnamed Pipes in C

I have 8 children, and am trying to use 8 pairs of nonduplex unnamed pipes to communicate with them. Thus, I have 2 pipes for each child and 16 pipes in total (one for childRead_ParentWrite and the other for parentRead_ChildWrite).
Anyway, my main question is when to close the pipes. I was taught to initially close the sides that are not being used by the process, and then when the process is finished with its side of the pipe, to close it off. However, I am brand new to the subject and am having some trouble. Here is my code:
// The 16 pipes
int fd_childReads_ParentWrites[8][2]; // Parent closes 0, Child closes 1
int fd_parentReads_ChildWrites[8][2]; // Child closes 0, Parent closes 1
// The 16 buffers
char buf_ChildReads_ParentWrites[8][80];
char buf_ParentReads_ChildWrites[8][80];
// CREATE THE PIPES
// FORK THE CHILDREN
for(playerNumber = 0; playerNumber < NUM_PLAYERS; playerNumber++)
{
pid = fork();
if (pid < 0) // Error occurred
{
printf("Fork Failed\n");
exit(1);
}
else if (pid == 0) // Child
{
break;
}
}
// MANAGE PROCESSES
if (pid == 0) // CHILD
{
printf("I am the child: %d\n", getpid());
// Close the appropriate pipe ends
close(fd_childReads_ParentWrites[playerNumber][1]);
close(fd_parentReads_ChildWrites[playerNumber][0]);
// CHILD DOES STUFF WITH PIPES
// When finished, close the working child pipe ends
close(fd_childReads_ParentWrites[playerNumber][0]);
close(fd_parentReads_ChildWrites[playerNumber][1]);
}
else // PARENT
{
printf("I am the parent: %d\n", getpid()); // NOT BEING PRINTED
// Close the appropriate pipe ends
for (i = 0; i < NUM_PLAYERS; i++)
{
close(fd_childReads_ParentWrites[i][0]);
close(fd_parentReads_ChildWrites[i][1]);
}
// PARENT DOES STUFF WITH PIPES
// Finally, close the working parent pipe ends
for (i = 0; NUM_PLAYERS < 8; i++)
{
close(fd_childReads_ParentWrites[i][1]);
close(fd_parentReads_ChildWrites[i][0]);
}
// Wait for the children
for (playerNumber = 0; playerNumber < NUM_PLAYERS; playerNumber++)
{
wait(NULL);
}
}
I must be doing something wrong. The program prints out the correct number of children, but the parent's printf() line is never printed. When I take out all of the close() functions it prints, but even taking out solely the children's close()'s doesn't print he parent line.
If someone could explain to me the correct way to close nonduplex unnamed pipes in a situation like this, that would be awesome.

Race Condition with fork() and pipe()

I am writing a program for an assignment where a parent plays connect four against N child processes. The program uses pipes to communicate game moves between processes.
I am having a problem fixing a race condition that is present in my program, however. There is a condition where the child process hangs one its read() call after the game has finished. This only seems to happen when there is more than one child process.
I've tried several things, such as named semaphores, but I'm still pretty new to forks, pipes, and IPC. I've posted a gist with relevant code (I tried to clean it up as best as I can for readability) here:
Gist with relevant code
Any help would be greatly appreciated
EDIT
Here's the relevant source from the gist with the declarations added.
int main (int argc, char const *argv[])
{
int dimension = 8, children = 2, i;
int child_play_to_win = 0;
int fd[children][4];
pid_t childpid[children];
Board** boards = (Board**) malloc(sizeof(Board*) * children);
GameMove* lastMove, *tmpMove;
char buf[80];
for(i = 0; i < children; i++) {
generate_board(&(boards[i]), dimension);
int tmp[2];
pipe(tmp);
// child read
fd[i][0] = dup(tmp[0]);
// parent write
fd[i][1] = dup(tmp[1]);
pipe(tmp);
// parent read
fd[i][2] = dup(tmp[0]);
// child write
fd[i][3] = dup(tmp[1]);
childpid[i] = fork();
if(childpid[i] == -1) {
perror("fork");
exit(1);
}
if(childpid[i] == 0) {
srand(getpid());
close(fd[i][1]);
close(fd[i][2]);
while(!boards[i]->finished) {
// Read in move from parent
printf("child[%d] about to read\n", getpid());
read(fd[i][0], &buf, sizeof(GameMove));
// repeat parent move on this board
if(gameNotFinished) {
// make child move
// write move back to parent
write(fd[i][3], lastMove, sizeof(GameMove));
// If the board is finished (there was a win),
if (!gameNotFinihsed) {
// Child wins
close(fd[i][0]);
close(fd[i][3]);
printf("child[%d] ending\n", getpid());
break;
}
}
else {
// Parent won
close(fd[i][0]);
close(fd[i][3]);
break;
}
}
dealloc(boards[i]);
exit(0);
}
}
// When this hits children amount, all games are done
int games_complete = 0;
// Make first move to all children
for (i = 0; i < children; i++) {
close(fd[i][0]);
close(fd[i][3]);
lastMove = placePieceAtBestPosition(boards[i], 1);
printf("parent writing to child[%d]\n", childpid[i]);
write(fd[i][1], lastMove, sizeof(GameMove));
}
while (games_complete != children) {
for (i = 0; i < children; i++) {
// Read move from child
read(fd[i][2], &buf, sizeof(GameMove));
// repeat child move
// Check for a child win...
if (!checkForWin(boards[i], 2)) {
// No win yet, place piece at best position
lastMove = placePieceAtBestPosition(boards[i], 1);
// check for win again
boards[i]->finished = checkForWin(boards[i], 1);
// Write move back to child
write(fd[i][1], lastMove, sizeof(GameMove));
// If we won, close everything up and increment
// the games_complete counter.
if(boards[i]->finished) {
close(fd[i][1]);
close(fd[i][2]);
games_complete++;
}
} else {
// write back child move if there was a win
write(fd[i][1], lastMove, sizeof(GameMove));
close(fd[i][1]);
close(fd[i][2]);
printf("Parent lost! ):\n");
games_complete++;
}
}
}
I think I know what your problem is. When you fork each child, you close the parent side of its pipes. however, each child still has open the parent side of the pipes for all of the previous children. Because of this, only the last-created child will have its parent-sides of its pipes closed.
Suggest you change:
close(fd[i][1]);
close(fd[i][2]);
to something like:
for (j = 0; j <=i; j++) {
close(fd[j][1]);
close(fd[j][2]);
}

Resources