PHP () system freezes even if a command in system () ends

I have a code something like this:

$file = fopen( "debug.txt", "w" ); $command = "myExe.exe param0 param1"; fprintf( $file, "starting\r\n" ); fflush( $file ); system( $command ); fprintf( $file, "the end...\r\n" ); fflush( $file ); 

He prints "beginning", but not "end ...". The system () function is hanging.

MyExe.exe is an application written in C ++ that actually terminates; those. The main function of myExe ends with the following code:

 FILE* f = fopen( "test.txt", "w" ); fclose(f); return 0; 
Is being created

test.txt, which means that "myExe.exe" works and ends normally.

This problem does not occur every time the php file is called; sometimes hanging, sometimes working ...

Any help & idea would be appreciated. Thanks in advance.

about; OS: win xp php server: wamp server 2.0

edit: my problem is not in my debug files. These are system () or exec () functions. I can delete all other lines.

my php script works well for about 4/5 attempts. After calling system (), I call some sql functions, but when system () freezes, my page will give a fatal error.

+4
source share
4 answers

Known bug in php on windows ( http://bugs.php.net/bug.php?id=44942 ).
The workaround you can try is to close the session (call session_write_close ()) before calling the exec function.

+1
source

Try using sprintf to print formatted text into a string, then write, for example:

 $file = fopen( "debug.txt", "w" ); $command = "myExe.exe param0 param1"; $startStr = sprintf( "starting\r\n" ); fwrite($file, $startStr); fflush( $file ); system( $command ); $endStr = sprintf( "the end...\r\n" ); fwrite($file, $endStr); fflush( $file ); 

Or, if you are not using any formatted strings (which in this case are not like you), get rid of sprintfs and just use write.

0
source

I would recommend you download / buy something like phpDesigner or RapidPHP so that you can go through the program logic and see what happens. Nothing pops up for me because you are mistaken in the program, but if there is, then any of the above programs will find it and display it in red.

You really don't need "\ r \ n" unless you just do double intervals. Just "\ n" should work fine.

Also, have you tried the php -l command to check for errors? It may seem like something.

Last but not least, there are other commands in PHP to run programs from outside - have you tried other commands yet?

Just thoughts.: -)

PS: I just thought: what is param0 and param1? If they contain special characters, this may affect what happens to the system command.

PPS: AH! I may have a partial answer. Only "return 0" can be the culprit. Try changing it to "exit (0)"; instead. The return statement does not match the output. He is trying to return to the calling program, and it is not there. However, the system may get confused and think that it should return to the PHP script (since the whole return command does this RET, which causes the JSR from the system command to try to catch the RET. Replacing it with the exit command tells the system command that you are executing with your program. The system command will then execute its own RET command back to PHP. If that makes sense. Basically, you do a double RET using the return command and you also have a zero (0) status code on the stack. Since the system usually returns its own status (via team do exit), what can happen is that the value null (0) is interpreted as a stop command for the system command IE: zero gets to the system stack, RET is generated, the system pushes RET, leaving zero (0 or zero) bytes which he does not expect and which freezes the system command. Again, the answer would have to switch to using "exit (0)" and not the return command. but I think I came across this many years ago when I did Perl, and then I needed to use exit instead of returning. Therefore, I believe that with PHP this could be the same problem. (If I thought about it for a while before I remembered it.)

0
source

I have a feeling, yes, just a feeling that in your process there are open handles that do not allow it to come out. Most of the time when this happens, STDIN and STDOUT are processed. Have you checked if fclose () succeeded in your C ++ application?

Download: http://technet.microsoft.com/en-us/sysinternals/bb896653

Use it to find out how many pens are open for myEXE.exe when it freezes.

Did you manage to greatly simplify myEXE.exe for SO purposes and skip something?

0
source

Source: https://habr.com/ru/post/1333697/


All Articles