• You are not logged in. | Login

Post a reply

March 5, 2007 3:06 pm

bobbee
Member
Ranks

Forking of processes, ‘switching’

I’m trying to understand the idea of processes forking; help me if I’m wrong…

It isn’t a real code, of course, just an idea…

// We create a socket and tie to some network interface and port 
$socket = socket_create (AF_INET, SOCK_STREAM, 0); 
socket_bind ($socket, $host, $port); 
socket_listen ($socket, 5); 

... 

// Awaiting connections… 
while (true) {  
    $msg_socket = socket_accept($socket); 
    $pid = pcntl_fork(); 
    if ($pid == -1) { 
        die('Could not fork child process'); 
    } else if ($pid) { 
        // parent
    } else { 
        // generated process 
        // we accomplish some applied logic… 
        SocketInit();     // read what received, socket_read 
        SocketAction(); // do something… 
        Socketwrite();  // write something in response 
        SocketClose();  // close 
        // now we need to kill… 
        posix_kill(posix_getpid(), 9);                
    } 
}

I think of it like this… smile Correct it, please.

Another situation.

As an ideal I’d like to do like this.

There is a server on locahost:2222 awaiting authorization. A client with some identifying himself key came to localhost:2222; if there is no key (a new client) we make for him pcntl_fork, ‘accomplish’ within this process and leave a child-process await until a client with the same key returns. If a client comes with the key for which child process has been already created, how to ‘switch’ a client to the necessary child-process to ‘accomplish’ there with some already existing environment?

Have you got any ideas?  smile


 

 

March 5, 2007 3:09 pm

Mr.SMart
Member
Ranks

Re: Forking of processes, ‘switching’

posix_kill(posix_getpid(), 9);

Would you like simply exit(0); instead?

If a client comes with the key for which child process has been already created, how to ‘switch’ a client to the necessary child-process to ‘accomplish’ there with some already existing environment?

There is no ability in PHP to transmit descriptor from one process to another. And the idea itself to save one process for each user is not the best one. And if there are few users they can be served within one process quite successfully.


 

 

March 5, 2007 3:14 pm

sirburpsalot
Member
Ranks

Re: Forking of processes, ‘switching’

It should function properly  smile

If a client comes with the key for which child process has been already created, how to ‘switch’ a client to the necessary child-process to ‘accomplish’ there with some already existing environment?

It’s complicated. By forking process gets the entire parent’s data but what happens afterwards (a new socket) is unknown to a child.
I.e. in general all this can be done, for example: a parent should analyze request, then transmit client’s socket to the necessary child (i.e. communication via UNIX-socket should occur and what to do if child is occupied with something else?) Generally speaking, such type task isn’t meant for PHP (although you may write a module for socket transmission).

Last edited by sirburpsalot (March 5, 2007 3:14 pm)


 

 

March 5, 2007 3:20 pm

bobbee
Member
Ranks

Re: Forking of processes, ‘switching’

Thanks! I’ve got some ideas…

I also thought about picking up socket unix in each child…and also I thought about what happens if this child-process is still occupied…


 

 

March 6, 2007 7:46 am

Mr.SMart
Member
Ranks

Re: Forking of processes, ‘switching’

I also thought about picking up socket unix in each child…

And what are you going to do with it?
Transmit requests-replies through it?


 

 

March 6, 2007 7:54 am

bobbee
Member
Ranks

Re: Forking of processes, ‘switching’

And what are you going to do with it? Transmit requests-replies through it?

Nothing, it’s just a fantasy. It has nothing to do with reality.

Would you like simply exit(0); instead?

The reason of my tortures is my own misunderstanding…

pcntl_signal(SIGHUP,   array(&$this, "SigHandler")); 
        pcntl_signal(SIGTERM, array(&$this, "SigHandler")); 
// (self::SigHandler) are likely to drop when child has exit(); ?  
        pcntl_signal(SIGCHLD, array(&$this, "SigHandler")); 

{...} 

        function SigHandler ($signo) { 
            file_put_contents('/var/www/aaa.aaa', "SigHandler(".$sig[$signo].")\n"); 
              
            switch($signo) {  
                case SIGCHLD:                     
                    while (($pid=pcntl_wait($status, WNOHANG)) > 0 ) { 
                          $this->child = array_diff( $this->child, array($pid)); 
                        if (!pcntl_wifexited($status)) { 
                             file_put_contents('/var/www/aaa.aaa', "Deleted process cancelled ".$pid."\n"); 
                          } 
                          else { 
                             file_put_contents('/var/www/aaa.aaa', "Accomplished process cancelled".$pid."\n"); 
                          } 
                       }                        
                break; 
                default:  
                    // file_put_contents('/var/www/aaa.aaa', "catch signal ".$signo."\n");  
                break;  
            }

But we don’t come to sighandler at all… sad


 

 
  • Actions
  • Top
ITCrimea. Ukraine Web Development Company. Professional Developers and Web Designers Team
Custom Web Designs, Internet Applications, E-Commerce Websites, Interactive Sites, Database-Driven Sites and Services