SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Forking process using PHP

    Hello, i'm a newbie in PHP world
    Can someone teach me how to do the forking process in PHP code?
    And if i want to run my code in different platform, i.e. not only Windows, do i need to create a different code for every platform?
    Help me, pls. Thank you

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cemplukcute2003 View Post
    Hello, i'm a newbie in PHP world
    Can someone teach me how to do the forking process in PHP code?
    Would be more descriptive with your situation please? Could not be clear for only Forking Process in PHP?
    And if i want to run my code in different platform, i.e. not only Windows, do i need to create a different code for every platform?
    Help me, pls. Thank you
    As far as i know for the basic purpose you dont have to change anything on the code for the different platforms. But for advanced you might have to changes on some of the codings. I could not list here what changes are to be changed right now.

    But since you are the newbie you don't have to worry i think.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    Would be more descriptive with your situation please? Could not be clear for only Forking Process in PHP?


    As far as i know for the basic purpose you dont have to change anything on the code for the different platforms. But for advanced you might have to changes on some of the codings. I could not list here what changes are to be changed right now.

    But since you are the newbie you don't have to worry i think.
    I have a php script which will call java class. Me and my group want to fork this process, so everytime there's a need to call a java class, php will fork the process first before calling java class...

  4. #4
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both open and proc_open should be available both on Windows a Unix-like platforms, however fork() isn't available on Windows because it's a Unix/BSD/Linux specific.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HarryR View Post
    Both open and proc_open should be available both on Windows a Unix-like platforms, however fork() isn't available on Windows because it's a Unix/BSD/Linux specific.
    Hi, Harry.
    Just now i got a sample code to do forking process, but i got an error message when i tried to run it.
    Here is the code :
    Code:
    <?php
    
    	$pid = pcntl_fork();
    	if ($pid == -1) {
    	     die('could not fork');
    	} else if ($pid) {
    	     // we are the parent
    	     pcntl_wait($status); //Protect against Zombie children
    	} else {
    	     // we are the child
    	}
    ?>
    Is proc_open the same as pcntl_fork?

  6. #6
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, open and proc_open are completely different to fork, they simply create another process from an executable you create and allow you to send/recieve data programmatically from it's in & out streams.

    For pcntl_fork to work, you need to be running the CGI/CLI version on a Unix-like platform with the pcntl extensions compiled in.

    Are you sure you need to use fork() ? 80&#37; of the time you could probably do it some other way.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm, what about if there're 10 users access that php script at the same time?
    Won't there any problems if i don't use forking process?
    Because my group mates afraid if there is a problem in the future.
    Currently, we're doing our final project

  8. #8
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've this php script :
    Code:
    ?php
    	echo nl2br("Hello from PHP!\n");
    	
    	$DBUser = "root";
    	$DBPwd = "admin";
    	$database = "FYP";
    	$host = "localhost";
    
    	mysql_connect($host, $DBUser, $DBPwd);
    
    	@mysql_select_db($database) or die("Unable to select database");
    
    	$query = "SELECT * FROM User";
    	$query_result = mysql_query($query);
    
    	$email = mysql_result($query_result, 0, "username");
    
    	echo nl2br("Hi, my email is " . $email . "\n");
    
    	$gabung = "encrypt Aileen.png " . $email;
    	passthru("java AESfile $gabung", $returnVal);
    
    	echo nl2br("Result passthru : " . $returnVal);
    
    	echo nl2br("\nBye! ");
    ?>
    I need to fork the process that calls java class.
    Can someone help me, pls? Thanks

  9. #9
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that you want to actually use "fork" in this case.

    Fork creates an identical replica of the current process which is probably not what you want to do if you just want to return the output.

    Like you can see you're using passthru, but that doesn't return everything that the application outputted on stdout, it only returns the status code, for example 0 or 1.

    If you use the backtick operator you can do this like:
    Code PHP:
    $aes = `java AESfile $gabung`;
    echo "AES = $aes\n";

  10. #10
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is an extension for PHP, which allows you to integrate Java and PHP with each together. I think this is the way you'd want to go.

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, Harry. Thanks for the tips. I'll try it

    Hi, kyberfabrikken. I know that there's an extension for Java in PHP.
    The problem is our webhosting doesn't provide this extension for us and they don't want to install it. Thanks for your tips


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •