![]() |
| | #1 |
| Registered User Join Date: Feb 2009
Posts: 9
| 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);
}
Code: bash$ ./test test 0 test 1 test 2 test 3 test 4 value 1 added value 14 added value 65 added bash$ 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 | |
| | #2 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #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 | |
| | #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 | |
| | #7 |
| and the Hat of Guessing 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 | |
| | #8 |
| Registered User Join Date: Feb 2009
Posts: 9
| I hate to ask, but how exactly do I do that? |
| tadams is offline | |
| | #9 |
| Registered User 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 | |
| | #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 | |
| | #11 |
| and the Hat of Guessing 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 | |
| | #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 | |
| | #13 |
| Registered User 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 | |
| | #14 |
| and the hat of vanishing 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 | |
| | #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);
}
}
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 | |
![]() |
| Tags |
| execl, fault, fork, linux, output |
| Thread Tools | |
| Display Modes | |
|
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 |