Tim
Tim

Reputation: 313

Sharing file descriptors between a parent and child after the child performs an exec call (UNIX/C)

I have an assignment in my OS class. I am supposed to write three programs, one program reads from a file and writes to a pipe, one program reads from a pipe and writes to a file, and the third program is the parent which creates the pipe and does two forks in order to spawn the child processes, which immediately do exec() calls. What I'm currently doing to make use of the pipe, is to pass the file descriptor in the exec() call as part of argv[], it works but it seems like the wrong solution.

What I want to know is if there is a better way to do this. It seems awkward to be passing file descriptors through arguments.

Upvotes: 2

Views: 2680

Answers (1)

Maciej
Maciej

Reputation: 3723

Child process after fork inherits parent's opened file descriptors. See man 2 fork.

Ok. My first advice for you is to compile with flags -Wall -Wextra -pedantic to get all warnings. Now my changes in your code:

/*removed char pipe_readend[12];*/
char * arg[2] = {"test2", NULL}; /*first argument of program should be always it's name*/
char message[3] = {'h', 'i', '\0'}; /*c-string should be always terminated by '\0'*/
/*...*/
 case 0:
        /* child actions */
            close(pfd[1]); /* close write end */
            close(STDIN_FILENO); /*close standard input descriptor*/
            dup(pfd[0]); /*now standard input will be pfd[0]*/
            execve("test2", arg, NULL); /* start new process */
            break;

and test2.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main (void){
    char msg[3];
    read(STDIN_FILENO, msg, 3); /*read from standard input*/
    printf("test2: %s\n", msg);
    return 0;
}

So to sum up: after forking you're closing standard input descriptor and duplicates pfd[0], which will become new standard input. Then call execve(), which uses pfd[0] as it's standard input. See more in man 2 of functions fork, execve, pipe.

Upvotes: 3

Related Questions