[C] execl and pipes?

Hi, I have two programs, one is named "Master" and the other one "slave". What I want to do is , when I execute Master, inside slave will be called by excecl, do some calculations, and send those to the master program... A little example of what I am failing to do:

if ((PID1=fork())==0) { // child
    close(fd[0]);
    dup2(fd[1],STDOUT_FILENO)
    execl("~/Escritorio/SO/todos-P-Modulo2-Sesion4/esclavo","2","10");    // I open the slave program   
} else {

// I want to print the calculations made in the program opened by execl. 

I don't know how to send the data to the pipes in the execl program. Should I also there, declare a int fd[2] , do a close system call and a write to fd ?

if ((PID1=fork())==0) { // child
    close(fd[0]);
    dup2(fd[1],STDOUT_FILENO)
    close(fd[1]); // close ALL loose ends!
    execl("~/Escritorio/SO/todos-P-Modulo2-Sesion4/esclavo","2","10");    // I open the slave program   
} else {

If you want to send data to esclavo's standard input, you will need another pipe.

If you want to read data from esclavo's standard output, read() from the read-end of the pipe in the parent.

You don't need declare anything or do anything special in the esclavo program itself. It can just read standard input and write to standard output as usual. By overriding the value of stdout with dup2(), you will cause esclavo to write to the pipe whenever it writes to standard output.

Otherwise, I don't understand your question.

1 Like

The data is sent as an argument to the Master program. The data are two ints, one bigger than other (ex: 1000 and 2000). The master program will execl the slave program, the slave program has a code that calculates the prime numbers between those two numbers, and once they are calculated, the slave program will send back the prime numbers to the master program, and the master program will printf them.

The problem is, when I fork, I know the fd are inherited in that process, but once I call the slave program with execl, I don't know how to connect the two programs with a pipe.

I have master and slave, two programs. Master will call slave with a execl system call, and I want a pipe between the two of them so they can communicate.

Your program will likely crash or malfunction, since argv[0] is traditionally the name of the program, and the argument after the last one you give execl must be null, so try this:

const char *arg="/path/to/program";
execl(arg, arg, "1", "2", NULL);

They already are. The copies of the pipe you get in the child talk to the same pipe. They're already joined. Write in the child, read in the parent. There's nothing else to be done (except closing the write-end in the parent, anyway. Don't leave extra ends around, they cause your program to freeze)

1 Like

Thanks! actually the program did nothing when executed :wall:

Yes, but what if I want to write in the pipe through the excel program? the program executed by excel, is also sharing the same pipe? As far as I know, can the child read the data from excel?
for example, the program executed by excel outputs this:
10 20 30 40 50

can the child read those values?

The program executed by execl is the child.

The parent can read those values, yes. The child writes to the pipe, the parent reads. It's the same pipe even after fork() so they are connected by it.

1 Like

Oh dear, now all has become clear, knowing this makes it really easy :wall:

Thanks a lot