When execl fails in C


 
Thread Tools Search this Thread
Top Forums Programming When execl fails in C
# 1  
Old 03-17-2011
When execl fails in C

when execl fails using the command lss, it doesnt go into the next line
Code:
        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 called the function that runs execl, doesnt run...
its supposed to return a 127 value but it returns a 31 instead...

anyone know why?
# 2  
Old 03-17-2011
But it's not running lss -- it's running /bin/sh. execl will succeed even if command contains complete garbage. /bin/sh will complain about command not found though, but that's not the same thing as execl failing.
# 3  
Old 03-18-2011
maybe it is because a signal in the child process forced the main process quit. you can catch the terminal signals or ignore them.
# 4  
Old 03-19-2011
There is no parent process and child process in the code snippet you posted.

execl replaces the current program with a new one (in this case "/bin/sh"), all in the same process. By the time that /bin/sh generates the error, your execl and perror are long gone.

If you want to be able to continue after the execl, you need to put in in a child process (fork), wait for the child to terminate (wait or waitpid) and recover the exit status from the status variable written by wait/waitpid.
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

[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) { //... (6 Replies)
Discussion started by: lamachejo
6 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

How to specify path of a function within execl

Consider the following scenario program1: main() { ...... execl("path","function",...); ..... } function() { ----- ------- } Now i want to include the path of function in execl. How to do this. should the path be the path of function's executable file. If it so how... (1 Reply)
Discussion started by: bankpro
1 Replies

9. 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

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