|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I came across a different little issue that I would like to find a solution.
I am using system() in program1 (yes, I know, bad) to run program2 after formatting it and its arguments with snprintf(). I would have used execl(), but it stops running after the first execution and I need it to run a few times. The program runs a program2 and system() is happy to print its output, fine. But, when and if program2 crashes, it doesn't show that output (like a segmentation fault, etc). I want it to show every detail after execution, just like I would have ran it without using program1, and I want to see the error if the program2 I run crashes, as well. I hope my problem is understood, if not let me know and I'll try to explain it further. I appreciate your help and solutions as always. mj |
|
#2
|
||||
|
||||
|
Something like
Code:
system( "myprog 1>output.txt 2>errors.txt" );
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. |
|
#3
|
||||
|
||||
|
Quote:
|
|
#4
|
|||
|
|||
|
Using those outputs didn't work, but I found I can use execl() and it will show all output, but I guess it returns after execution and I need it to keep going (I need to execute around program2 about 5 times with different arguments). Any way around this?
|
|
#5
|
||||
|
||||
|
Quote:
In what way did they not work? The text was sent to a file (or two separate files). It is not entirely clear what you mean, or what you want. Whatever you are doing we probably need to know what OS you are using. execl will behave as documented. How are you using it? In Windows it spawns a process that replaces teh process that spawned it (as documented). If that is not what you wanted, you are using the wrong function. Perhaps you really wanted spawnl(), which spawn an additional process. You can opt to have the process run synchronously or concurrently to the spawning process. Clifford |
|
#6
|
|||
|
|||
|
I am using Linux.
program1 runs program2 with "1" as argument #1 program1 runs program2 with "1" as argument #1 and "10" as argument #2 program1 runs program2 with "100" as argument #1 program1 runs program2 with "1" as argument #1, #2, & #3 program1 runs program2 with "0" as argument #1 I need to write code that will do that, basically, all in one program (program1). As you suggested clifford, execl will replace the current program1 with program2, therefore leaving no control with program1, which program1 needs to run program2 again and again with different arguments. See my problem now? Note: I am mostly interested in the error/normal output of the executions. Thanks! Last edited by mokeyjoe : February 2nd, 2009 at 02:27 PM. |
|
#7
|
||||
|
||||
|
> but I found I can use execl() and it will show all output, but I guess it returns after execution and I need it to keep going
Did you call fork() before calling exec()? If not, then you did it wrong, and you would only get one shot. > Using those outputs didn't work Thanks for really clarifying it with zero evidence. Did you see any files created at all, even empty ones? Did you see the error message, despite attempts to redirect it? Did you... - got the picture? Post some actual code, and actual observations. |
|
#8
|
|||
|
|||
|
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++)
{
run(PATH, data);
}
(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$ You see, values for i=3 and i=4 caused a segmentation fault, which it does not show. I believe this is an error with the code. Reponse? |
|
#9
|
||||
|
||||
|
Mokey? Have you investigated at all the things your are using?
Do you realize that all the **** ups that you are experiencing have been ****ed up lebenty-zillion times before? Do you actuallly think that you have discovered, anew, these various weaknesses? You would have to work very hard to do that. Previously failures by competent people have resulted in various solutions, some bad some good. Don't mind me. I'm just musing, here.
__________________
Write no code whose complexity leaves you wondering what the hell you did. Politically Incorrect DaWei on Pointers Grumpy on Exceptions |
|
#10
|
||||
|
||||
|
1. Read the manual page for fork(). You're running your test code in the parent, not the child.
2. Read the manual page for execl(). exec functions do NOT deal with command line parsing at all. So if you're trying to pass command line parameters, you need to do more work. 3. Read the manual pages for dup(), pipe() and close() for how to redirect the likes of stdout to a file. Oh wait, is this just some pseudo-code you bashed in at the keyboard as a quick reply, and has no actual resemblance to your real code? |
|
#11
|
|||
|
|||
|
I don't understand what your talking about, especially sizeablegrin, hes very confusing.
![]() |
|
#12
|
|||
|
|||
|
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char small[32], big[128];
int i = 16;
if(strstr(argv[1], "t8")) { i = 8; }
if(strstr(argv[1], "t16")) { i = 16; }
if(strstr(argv[1], "t32")) { i = 32; }
if(strstr(argv[1], "t64")) { i = 64; }
if(strstr(argv[1], "t128")) { i = 128; }
memset(big, 'Z', sizeof(big));
snprintf(small, i, "%s", big);
printf("%s\n", small);
return 0;
}
That is the program I am running through the parent. As you can see, if the values are t64 or t128, the program crashes. I simply want to run that program 5 times with the 5 different values and be able to see the segmentation faults when they occur. Code:
bash$ ./bvss t8 ZZZZZZZ bash$ ./bvss t16 ZZZZZZZZZZZZZZZ bash$ ./bvss t32 ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ bash$ ./bvss t64 ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ Segmentation fault bash$ ./bvss t128 ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZ Segmentation fault bash$ bash$ ./test ZZZZZZZ ZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ ZZZZZZZZZZZZZZZZZZZZZZZZZZZ bash$ Code:
void run(char *bin, char *data)
{
if(fork() == 0)
{
if(execl(bin, bin, data, 0) == -1)
{
perror("execl");
_exit(1);
}
}
else
{
int pid, status;
pid = wait(&status);
}
}
.....
for(i = 0; i < 5; i++)
{
run(PATH, data);
}
See as I've explained my complete situation, I still can't get my code to work. I ask kindly for a solution. |
|
#13
|
||||
|
||||
|
If you fill a buffer completely with 'Z's and try to treat it as a C-string, what do you suppose will happen?
|
|
#14
|
||||
|
||||
|
Quote:
His program that fills the buffer with 'Z' is intended to crash. It's so he has something that will crash to test whether or not he's capturing the error stream. As for mokeyjoe, have you read the manual pages and such as they asked? http://www.manpagez.com/man/2/fork/ http://www.manpagez.com/man/3/exec/
__________________
Spidermonkey Tutorial; Fight me: http://aoeex.mybrute.com http://www.aoeex.com/gmap.php - Put yourself on the map (Now Updated!) |
|
#15
|
|||
|
|||
|
I finally got it to work using the signal catching solution.
Couple short questions: 1) snprintf() null terminates for us, so I don't need to do a snprintf(blah, sizeof(blah)-1 just sizeof(blah), correct? 2) With memset(blah, 'Z', sizeof(blah)), could I and would I need to null terminate blah like blah[SIZE] = '\0' or blah[SIZE-1] = '\0' or ? Thanks again |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > System() output |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|