Jump to content

[SOLVED] Synchronizing system()


Xu Wei Jie

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.