C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-03-2009, 07:38 PM   #1
Registered User
 
Join Date: Feb 2009
Posts: 9
Question execl()/fork() output

Code:
void run(char *bin, char *data)
{

if(fork())
{

     execl(bin, bin, data, 0);

}

else
{

int pid, status;

     pid = wait(&status);

}
}

.....

for(i = 0; i < 5; i++)
{

     printf("test %d\n", i);
     run(PATH, data);

}
output (with 'data' being different each time):

Code:
bash$ ./test
test 0
test 1
test 2
test 3
test 4
value 1 added
value 14 added
value 65 added
bash$
The purpose of my program is to run a program 5 different times with different arguments using execl() and fork() and be able to see the full output. As you can see in the above output, values for i=3 and i=4 cause the program I am running do a segmentation fault, which is what is supposed to happen, but it shows no output. I would like to see that output just like the previous outputs.

I think it is a problem with the code not fork()ing correctly, but I am unsure. Been hitting my head against a wall for days trying to figure this one out. A solution would be simply terrific.

Thanks so much,

Todd
tadams is offline   Reply With Quote
Old 02-03-2009, 07:45 PM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
As you can see in the above output, values for i=3 and i=4 cause the program I am running do a segmentation fault, which is what is supposed to happen, but it shows no output. I would like to see that output just like the previous outputs.
I have to call a real sleuth for this. Are you trying to say that you are surprised that you cannot find any output after the segfault?
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 02-03-2009, 07:51 PM   #3
Registered User
 
Join Date: Feb 2009
Posts: 9
Well, yes, since running the program manually using i=3 or i=4 shows the output then the next line "segmentation fault"., which is fine, its supposed to, but I want it to show that when I run it throw the parent process too.
tadams is offline   Reply With Quote
Old 02-03-2009, 08:09 PM   #4
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Try: SLEUTH

And if you want a spanking: the real meaning of sleuth
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 02-03-2009, 08:33 PM   #5
Registered User
 
Join Date: Feb 2009
Posts: 9
I honestly don't understand what your talking about or how 'sleuth' can help me.
tadams is offline   Reply With Quote
Old 02-03-2009, 09:10 PM   #6
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
DITTO, or possibly this
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 02-03-2009, 09:15 PM   #7
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
I'm guessing that if you want to know whether bad things happened to your execl program, you're going to have to check for it yourself (since your program is running the file, error messages aren't going to go to the terminal necessarily) via the return value of execl.
tabstop is offline   Reply With Quote
Old 02-03-2009, 11:49 PM   #8
Registered User
 
Join Date: Feb 2009
Posts: 9
I hate to ask, but how exactly do I do that?
tadams is offline   Reply With Quote
Old 02-04-2009, 07:47 AM   #9
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,844
The parent has to detect the situation and do the printf() as to what happened to the child. Normally, the shell does this for you.

>> via the return value of execl.
You mean wait/waitpid - execl never returns.
http://www.opengroup.org/onlinepubs/...s/waitpid.html

gg
Codeplug is offline   Reply With Quote
Old 02-04-2009, 10:44 AM   #10
Registered User
 
Join Date: Feb 2009
Posts: 9
But I am using wait()... I don't understand, is there something wrong with my code?
tadams is offline   Reply With Quote
Old 02-04-2009, 10:49 AM   #11
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Only by omission -- you need to check status to see what happened to the process.
tabstop is offline   Reply With Quote
Old 02-04-2009, 11:02 AM   #12
Registered User
 
Join Date: Feb 2009
Posts: 9
I know if I printf() the status it is going to just print any integer, I believe, so once again, thanks for your help, but I need a solution
tadams is offline   Reply With Quote
Old 02-04-2009, 11:28 AM   #13
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,844
>> you need to check status to see what happened to the process.
http://www.opengroup.org/onlinepubs/...s/waitpid.html

Let us know if the documentation is unclear.

gg
Codeplug is offline   Reply With Quote
Old 02-04-2009, 11:42 AM   #14
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
http://forums.devshed.com/c-programm...ut-587772.html
Your code still looks like some non-compilable pseudo-code you just typed out, rather than anything which you've run.

What's PATH?
Because execl has some rather strict rules, you can't just pass any old path to it.

Code:
if ( fork() == 0 ) {
  close(1);
  if ( open("stdout.txt",O_WRONLY) == -1 ) {
    perror("Can't redirect");
  }
  if ( execl( bin, bin, data, 0) == -1 ) {
    perror("Can't exec");
    _exit(1);
  }
} else {
  // wait
}
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 02-04-2009, 12:18 PM   #15
Registered User
 
Join Date: Feb 2009
Posts: 9
Code:
void run(char *bin, char *data)
{

if(fork() == 0)
{

     close(1);

if(open("stdout.txt", O_WRONLY) == -1)
{

     perror("open");

}

if(execl(bin, bin, data, 0) == -1)
{

     perror("execl");
     _exit(1);

}
}

else
{

int pid, status;

     pid = wait(&status);

}
}
Gives me: open: No such file or directory

Then when I touch stdout.txt, it doesn't give me that error anymore, but it doesnt write anything to stdout.txt either, and I still see output from the child.

Also two questions: I'm guessing close(1) closes stdout? and Why _exit(1) vs exit(1)?

Thanks.
tadams is offline   Reply With Quote
Reply

Tags
execl, fault, fork, linux, output

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
code output... roaan C Programming 6 07-03-2009 02:22 AM
Help for my output array qwertysingh C Programming 1 02-17-2009 03:08 PM
More digits in output, particular digits output, output in *.txt Ene Dene C++ Programming 4 11-30-2005 04:44 PM
Formatting output into even columns? Uncle Rico C Programming 2 08-16-2005 05:10 PM
Output problems with structures Gkitty C Programming 1 12-16-2002 05:27 AM


All times are GMT -6. The time now is 09:52 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22