Xu Wei Jie Posted March 22, 2009 Share Posted March 22, 2009 Anyone knows how to synchronize system() processes in php so when multiple processes of them are running, they won't end up snatching the same resource. i.e. system("php $x 2>error.txt",$retval); I do get errors that goes something like file is not able to be accessed while another process is doing so. Quote Link to comment https://forums.phpfreaks.com/topic/150572-solved-synchronizing-system/ Share on other sites More sharing options...
.josh Posted March 22, 2009 Share Posted March 22, 2009 I believe you are talking about threading, in which case, php does not support that. You can look into forking, which mostly simulates it. Quote Link to comment https://forums.phpfreaks.com/topic/150572-solved-synchronizing-system/#findComment-790907 Share on other sites More sharing options...
MadTechie Posted March 22, 2009 Share Posted March 22, 2009 if your outputing to error.txt while its inuse it will fail. (that down do the OS not PHP) Quote Link to comment https://forums.phpfreaks.com/topic/150572-solved-synchronizing-system/#findComment-790916 Share on other sites More sharing options...
Xu Wei Jie Posted March 22, 2009 Author Share Posted March 22, 2009 Yes, it is doing forking but I have problems with redirection of outputs when multiple system() processes are running at the same time Firstly, it will be the issue that the file is being accessed by 1 process and not able to by another. Secondly, when I do system("php test.php 2>&1 1>file.txt",$retval), i will often run into the problem that standard error is either written to file.txt or standard output with 2 different forking instances. Thus, I wish to control their order of execution. I tried php's semaphores but I guess it pertains to only php 4 and 5. Anyone has any ingenious idea to solve this? Quote Link to comment https://forums.phpfreaks.com/topic/150572-solved-synchronizing-system/#findComment-790921 Share on other sites More sharing options...
.josh Posted March 22, 2009 Share Posted March 22, 2009 have you looked into pcntl? I've never really used it but if I understand it correctly, pcntl_fork returns the child's process id if you're in the parent, 0 if you're in the child, so you should be able to use an if statement to figure out whether you're currently in the parent or child process, and do stuff based off that. Like for instance, you can call pcntl_wait or pcntl_waitpid to make the current process wait for the other one to finish. Quote Link to comment https://forums.phpfreaks.com/topic/150572-solved-synchronizing-system/#findComment-790929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.