[C] execl and pipes?


 
Thread Tools Search this Thread
Top Forums Programming [C] execl and pipes?
# 1  
Old 12-24-2011
[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:

Code:
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 ?
# 2  
Old 12-24-2011
Code:
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 {

Quote:
Originally Posted by lamachejo
I don't know how to send the data to the pipes in the execl program.
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.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 12-24-2011
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.
# 4  
Old 12-25-2011
Quote:
Originally Posted by lamachejo
The data is sent as an argument to the Master program. The data are two ints, one bigger than other (ex: 1000 and 2000).
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:

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

Quote:
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.
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)
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-25-2011
Quote:
Originally Posted by Corona688
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:

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

Thanks! actually the program did nothing when executed Smilie


Quote:
Originally Posted by Corona688
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)
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?
# 6  
Old 12-25-2011
Quote:
Originally Posted by lamachejo
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.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 12-25-2011
Quote:
Originally Posted by Corona688
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.
Oh dear, now all has become clear, knowing this makes it really easy Smilie

Thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Execl() command

Hi, If I write in a c file : execlp("date","date",NULL); printf("A\n"); And then run through the terminal would "A" be printed ? I understood that execlp will exit the program after it finished so the next lines of code won`t be executed afterwards.. Is that true ? (1 Reply)
Discussion started by: uniran
1 Replies

2. Programming

When execl fails in C

when execl fails using the command lss, it doesnt go into the next line execl("/bin/sh", "/bin/sh", "-c", command, NULL); perror("execl failed"); exit(127); for some reason the child process just stops and also the parent process also stops so the line after the line that... (3 Replies)
Discussion started by: omega666
3 Replies

3. Red Hat

execl command

how to use find command in execl function, I used: execl("/usr/bin/find","find","~","-name","filename.c",0); but it shows find: ~ no file and directory i need to get the path of the file from the home .:wall: (2 Replies)
Discussion started by: Mahendravarma
2 Replies

4. Programming

time execl

hello everybody how can i time the execution of execl() command inside my C code? for example, i wrote.. execl("md5sum","md5sum","myprog",NULL); i want to count the duration of the execl command! thanx in advance! (2 Replies)
Discussion started by: nicos
2 Replies

5. Programming

execl function at GDB

Hi, we would appreciate if any one answer the below query. void main() { printf(“ I am in main\n”); execl(“/HOME/source/file2”,” /HOME/source/file2”,1,0); printf(“after execl\n”); } How to step the file2 source code in GDB. (2 Replies)
Discussion started by: RAMESHPRABUDASS
2 Replies

6. Shell Programming and Scripting

need help with execl command

I want to make simultanous sh commands in an exec command for example I want to counts the lines in a file wc -l my file.txt | awk -F" " '{print $1}'` works fine in sh but I want to implement it in a c code the first part works like this execl("/usr/bin/wc", "wc", "-l", "myfile.txt",... (1 Reply)
Discussion started by: walnut
1 Replies

7. Programming

execl()

can anyone explain how to pass arguments of a program in execl function pls explain with a sample code. (2 Replies)
Discussion started by: bankpro
2 Replies

8. Programming

execl / execv ?

Hi, Is it possible to run a program from my C program using only the full pathname? for example if I wanna call: "ls", so I whould have to use: execl("/bin/ls", "ls", NULL); Is it possible to do this using only: "/bin/ls" thanks (1 Reply)
Discussion started by: owijust
1 Replies

9. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies

10. Programming

execl, execv or execp

Hi! I'm writing a C program which gets from the command line a shell command (such as "ls" ) and I should execute it. My Q is: how can I send a command to the shell? I know I have to use one of the above functions, but I don't know how to use them. Thanks eyal (1 Reply)
Discussion started by: azran
1 Replies
Login or Register to Ask a Question