SlideShare une entreprise Scribd logo
1  sur  37
Multi-tasking in PHP



Wednesday, December 5, 12
About Me
                   •        Formerly CTO for
                            Company52

                   •        Currently work at
                            Brandmovers on
                            Northside Drive

                   •        Self-taught

                   •        Full-time geek since
                            2007




Wednesday, December 5, 12
Wednesday, December 5, 12
Use Cases
                •      System resources are
                       not the bottleneck
                •      Batch processing
                       involving an API:
                      •     E-Mail
                      •     Geocoding
                      •     Electronic billing
                •      Daemons


Wednesday, December 5, 12
Alternatives

                   • Gearman
                   • curl_multi_*
                   • Other scripting languages


Wednesday, December 5, 12
Theory



Wednesday, December 5, 12
Two ways to multi-task
                             Multi-processing        Multi-threading

                             Separate memory          Shared memory

                             Errors are isolated   Errors are not isolated

                            Separate permissions     Same permissions

                                Linux/UNIX               Windows


Wednesday, December 5, 12
Multiprocessing                                     Multithreading

             Process 1        program
                                                                Process 1          program



                                                                  Thread 1
                 state:         virtual
                                              memory
                PC, stack     processor                              state:          virtual
                                                                    PC, stack      processor


             Process 2        program                             Thread 2
                                                                     state:          virtual
                                                                    PC, stack      processor
                                                                                                 memory
                 state:         virtual
                                              memory
                PC, stack     processor


                                                                  Thread m
                                                                     state:          virtual
             Process n        program
                                                                    PC, stack      processor




                 state:         virtual
                                              memory
                PC, stack     processor
                                                                Process n


                                               Courtesy www.fmc-modeling.org

Wednesday, December 5, 12
Multiprocessing

                   • “The simultaneous execution of two or
                            more programs by separate CPUs under
                            integrated control.”
                   • Clones the entire process, except resources
                   • Copy-on-write memory

Wednesday, December 5, 12
Forking




                            Diagram courtesy cnx.org



Wednesday, December 5, 12
Child Process

                   • A cloned copy of a parent process
                   • Receives a new process ID and a parent
                            process ID
                   • Does some work
                   • Dies

Wednesday, December 5, 12
...sort of.




                            Photo Credit: Christopher Brian (2011 Toronto Zombie Walk)



Wednesday, December 5, 12
Parent Responsibilities

                   • Reproduction
                   • Monitors child process status
                   • “Reap” zombie processes


Wednesday, December 5, 12
Process Signals
                             Signal     Description
                            SIGCHLD   Child process died

                            SIGINT     User Interrupt

                            SIGTERM       Terminate

                            SIGKILL   Forcibly terminate



Wednesday, December 5, 12
PHP Implementation



Wednesday, December 5, 12
Requirements
                   •        Unix-like operating system
                   •        PHP 4.1+
                   •        PHP PCNTL extension
                            (compile with --enable-pcntl)
                   •        PHP Semaphore extension, optional
                            (--enable-sysvsem, --enable-sysvshm, --enable-sysvmsg)
                   •        Plenty of memory
                   •        Multiple CPU cores



Wednesday, December 5, 12
Overview
                   1. Define signal handlers
                   2. Fetch a dataset
                   3. Fork off one child process for each item
                   4. Stop forking when a threshold is reached, and sleep
                   5. Reap a child process whenever SIGCHLD is received
                   6. If there’s more work to do, fork more processes
                   7. When all child processes have been reaped,
                      terminate


Wednesday, December 5, 12
declare(ticks = 1);

                 // Setup our signal handlers
                 pcntl_signal(SIGTERM, "signal_handler");
                 pcntl_signal(SIGINT,  "signal_handler");
                 pcntl_signal(SIGCHLD, "signal_handler");




Wednesday, December 5, 12
function signal_handler($signal)
                 {
                 	

 switch ($signal)
                 	

 {
                 	

 	

 case SIGINT:
                 	

 	

 case SIGTERM:
                 	

 	

 	

 // kill all child processes
                 	

 	

 	

 exit(0);
                 	

 	

 case SIGCHLD:
                 	

 	

 	

 // reap a child process
                 	

 	

 	

 reap_child();
                 	

 	

 break;
                 	

 }
                 }


Wednesday, December 5, 12
$pid = pcntl_fork();

                  switch($pid)
                  {
                  	

 case 0:
                  	

 	

 // Child process
                  	

 	

 call_user_func($callback, $data);
                  	

 	

 posix_kill(posix_getppid(), SIGCHLD);
                  	

 	

 exit;
                  	

 case -1:
                  	

 	

 // Parent process, fork failed
                  	

 	

 throw new Exception("Out of memory!");
                  	

 default:
                  	

 	

 // Parent process, fork succeeded
                  	

 	

 $processes[$pid] = TRUE;
                  }

Wednesday, December 5, 12
Repeat for
                            each unit of work


Wednesday, December 5, 12
function reap_child()
             {
             	

 // Check if any child process has terminated,
             	

 // and if so remove it from memory
             	

 $pid = pcntl_wait($status, WNOHANG);
             	

 if ($pid < 0)
             	

 {
             	

 	

 throw new Exception("Out of memory");
             	

 }
             	

 elseif ($pid > 0)
             	

 {
             	

 	

 unset($processes[$pid]);
             	

 }	

             }

Wednesday, December 5, 12
Demo Time!
                            http://gist.github.com/4212160




Wednesday, December 5, 12
Wednesday, December 5, 12
DON’T:
                   •        DON’T fork a web process (CLI only!)
                   •        DON’T overload your system
                   •        DON’T open resources before forking
                   •        DO respect common POSIX signals
                   •        DO remove zombie processes
                   •        DO force new database connections in children
                            mysql_reconnect($s, $u, $p, TRUE);


Wednesday, December 5, 12
Challenges



Wednesday, December 5, 12
Race Conditions
                   •        A logic bug where the result is affected by the
                            sequence or timing of uncontrollable events
                   •        Adding debug logic can change timing
                   •        Dirty reads
                   •        Lost data
                   •        Unpredictable behavior
                   •        Deadlocks, hanging, crashing


Wednesday, December 5, 12
Wednesday, December 5, 12
Wednesday, December 5, 12
Solutions

                        • Handle I/O in the parent process
                            exclusively
                        • Manage resources with semaphores
                            and/or mutexes




Wednesday, December 5, 12
Semaphores

                   • Semaphore = atomically updated counter
                   • Mutex = binary semaphore with ownership
                   • PHP: sem_get(), sem_release()
                   • Transactional databases use semaphores

Wednesday, December 5, 12
Deadlocks




                                        Image credit: csunplugged.org




Wednesday, December 5, 12
Bonus Slides



Wednesday, December 5, 12
Shared Memory
                   •        Advanced inter-process communication

                   •        Pass data back to the parent process

                   •        PHP Shared Memory extension
                            (--enable-shmop)

                   •        PHP System V Shared Memory extension
                            (--enable-sysvshm)

                        •     More robust

                        •     Compatible with other languages


Wednesday, December 5, 12
Daemonization

                   • Fork, kill parent
                   • Orphaned child process continues running
                   • Signal and error handling are critical
                   • Server daemons usually fork child
                            processes to handle requests



Wednesday, December 5, 12
Wednesday, December 5, 12
Thank You!


                              @compwright
                            http://bit.ly/atlphpm




Wednesday, December 5, 12

Contenu connexe

Similaire à Multi-tasking in PHP

Malware analysis
Malware analysisMalware analysis
Malware analysisxabean
 
Linux-HA with Pacemaker
Linux-HA with PacemakerLinux-HA with Pacemaker
Linux-HA with PacemakerKris Buytaert
 
Linux-HA with Pacemaker
Linux-HA with PacemakerLinux-HA with Pacemaker
Linux-HA with PacemakerKris Buytaert
 
How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...
How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...
How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...DataWorks Summit/Hadoop Summit
 
Lisa12 methodologies
Lisa12 methodologiesLisa12 methodologies
Lisa12 methodologiesBrendan Gregg
 
MySQL HA with Pacemaker
MySQL HA with  PacemakerMySQL HA with  Pacemaker
MySQL HA with PacemakerKris Buytaert
 
Devopsdays se-2011
Devopsdays se-2011Devopsdays se-2011
Devopsdays se-2011lusis
 
Distributed Fuzzing Framework Design
Distributed Fuzzing Framework DesignDistributed Fuzzing Framework Design
Distributed Fuzzing Framework Designbannedit
 
Hadoop - Disk Fail In Place (DFIP)
Hadoop - Disk Fail In Place (DFIP)Hadoop - Disk Fail In Place (DFIP)
Hadoop - Disk Fail In Place (DFIP)mundlapudi
 
VistA GT.M & Linux Security 062506
VistA GT.M & Linux Security 062506VistA GT.M & Linux Security 062506
VistA GT.M & Linux Security 062506ckuyehar
 
Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...Nikolay Savvinov
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and Performancepradeepfn
 
Inside the Atlassian OnDemand Private Cloud
Inside the Atlassian OnDemand Private CloudInside the Atlassian OnDemand Private Cloud
Inside the Atlassian OnDemand Private CloudAtlassian
 
An Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive ApplicationsAn Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive ApplicationsXiao Qin
 
2012-11-30-scalable game servers
2012-11-30-scalable game servers2012-11-30-scalable game servers
2012-11-30-scalable game serversWooga
 
Os Madsen Block
Os Madsen BlockOs Madsen Block
Os Madsen Blockoscon2007
 
Infrastructure Around Hadoop
Infrastructure Around HadoopInfrastructure Around Hadoop
Infrastructure Around HadoopDataWorks Summit
 

Similaire à Multi-tasking in PHP (20)

Malware analysis
Malware analysisMalware analysis
Malware analysis
 
Linux-HA with Pacemaker
Linux-HA with PacemakerLinux-HA with Pacemaker
Linux-HA with Pacemaker
 
Linux-HA with Pacemaker
Linux-HA with PacemakerLinux-HA with Pacemaker
Linux-HA with Pacemaker
 
How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...
How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...
How to overcome mysterious problems caused by large and multi-tenancy Hadoop ...
 
You suck at Memory Analysis
You suck at Memory AnalysisYou suck at Memory Analysis
You suck at Memory Analysis
 
Lisa12 methodologies
Lisa12 methodologiesLisa12 methodologies
Lisa12 methodologies
 
MySQL HA with Pacemaker
MySQL HA with  PacemakerMySQL HA with  Pacemaker
MySQL HA with Pacemaker
 
Devopsdays se-2011
Devopsdays se-2011Devopsdays se-2011
Devopsdays se-2011
 
Distributed Fuzzing Framework Design
Distributed Fuzzing Framework DesignDistributed Fuzzing Framework Design
Distributed Fuzzing Framework Design
 
Hadoop - Disk Fail In Place (DFIP)
Hadoop - Disk Fail In Place (DFIP)Hadoop - Disk Fail In Place (DFIP)
Hadoop - Disk Fail In Place (DFIP)
 
Hadoop, Taming Elephants
Hadoop, Taming ElephantsHadoop, Taming Elephants
Hadoop, Taming Elephants
 
VistA GT.M & Linux Security 062506
VistA GT.M & Linux Security 062506VistA GT.M & Linux Security 062506
VistA GT.M & Linux Security 062506
 
Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...Using the big guns: Advanced OS performance tools for troubleshooting databas...
Using the big guns: Advanced OS performance tools for troubleshooting databas...
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and Performance
 
Inside the Atlassian OnDemand Private Cloud
Inside the Atlassian OnDemand Private CloudInside the Atlassian OnDemand Private Cloud
Inside the Atlassian OnDemand Private Cloud
 
The Accidental DBA
The Accidental DBAThe Accidental DBA
The Accidental DBA
 
An Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive ApplicationsAn Active and Hybrid Storage System for Data-intensive Applications
An Active and Hybrid Storage System for Data-intensive Applications
 
2012-11-30-scalable game servers
2012-11-30-scalable game servers2012-11-30-scalable game servers
2012-11-30-scalable game servers
 
Os Madsen Block
Os Madsen BlockOs Madsen Block
Os Madsen Block
 
Infrastructure Around Hadoop
Infrastructure Around HadoopInfrastructure Around Hadoop
Infrastructure Around Hadoop
 

Plus de Jonathon Hill

2019 SC Legislative Session Highlights
2019 SC Legislative Session Highlights2019 SC Legislative Session Highlights
2019 SC Legislative Session HighlightsJonathon Hill
 
DynamoDB in real life
DynamoDB in real lifeDynamoDB in real life
DynamoDB in real lifeJonathon Hill
 
Building for success and failure with Disqus
Building for success and failure with DisqusBuilding for success and failure with Disqus
Building for success and failure with DisqusJonathon Hill
 
Estimation Protips - NCDevCon 2014
Estimation Protips - NCDevCon 2014Estimation Protips - NCDevCon 2014
Estimation Protips - NCDevCon 2014Jonathon Hill
 
Amazon: deal or debacle?
Amazon: deal or debacle?Amazon: deal or debacle?
Amazon: deal or debacle?Jonathon Hill
 

Plus de Jonathon Hill (6)

2019 SC Legislative Session Highlights
2019 SC Legislative Session Highlights2019 SC Legislative Session Highlights
2019 SC Legislative Session Highlights
 
DynamoDB in real life
DynamoDB in real lifeDynamoDB in real life
DynamoDB in real life
 
Building for success and failure with Disqus
Building for success and failure with DisqusBuilding for success and failure with Disqus
Building for success and failure with Disqus
 
Estimation Protips - NCDevCon 2014
Estimation Protips - NCDevCon 2014Estimation Protips - NCDevCon 2014
Estimation Protips - NCDevCon 2014
 
Estimation Protips
Estimation ProtipsEstimation Protips
Estimation Protips
 
Amazon: deal or debacle?
Amazon: deal or debacle?Amazon: deal or debacle?
Amazon: deal or debacle?
 

Dernier

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 

Dernier (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 

Multi-tasking in PHP

  • 2. About Me • Formerly CTO for Company52 • Currently work at Brandmovers on Northside Drive • Self-taught • Full-time geek since 2007 Wednesday, December 5, 12
  • 4. Use Cases • System resources are not the bottleneck • Batch processing involving an API: • E-Mail • Geocoding • Electronic billing • Daemons Wednesday, December 5, 12
  • 5. Alternatives • Gearman • curl_multi_* • Other scripting languages Wednesday, December 5, 12
  • 7. Two ways to multi-task Multi-processing Multi-threading Separate memory Shared memory Errors are isolated Errors are not isolated Separate permissions Same permissions Linux/UNIX Windows Wednesday, December 5, 12
  • 8. Multiprocessing Multithreading Process 1 program Process 1 program Thread 1 state: virtual memory PC, stack processor state: virtual PC, stack processor Process 2 program Thread 2 state: virtual PC, stack processor memory state: virtual memory PC, stack processor Thread m state: virtual Process n program PC, stack processor state: virtual memory PC, stack processor Process n Courtesy www.fmc-modeling.org Wednesday, December 5, 12
  • 9. Multiprocessing • “The simultaneous execution of two or more programs by separate CPUs under integrated control.” • Clones the entire process, except resources • Copy-on-write memory Wednesday, December 5, 12
  • 10. Forking Diagram courtesy cnx.org Wednesday, December 5, 12
  • 11. Child Process • A cloned copy of a parent process • Receives a new process ID and a parent process ID • Does some work • Dies Wednesday, December 5, 12
  • 12. ...sort of. Photo Credit: Christopher Brian (2011 Toronto Zombie Walk) Wednesday, December 5, 12
  • 13. Parent Responsibilities • Reproduction • Monitors child process status • “Reap” zombie processes Wednesday, December 5, 12
  • 14. Process Signals Signal Description SIGCHLD Child process died SIGINT User Interrupt SIGTERM Terminate SIGKILL Forcibly terminate Wednesday, December 5, 12
  • 16. Requirements • Unix-like operating system • PHP 4.1+ • PHP PCNTL extension (compile with --enable-pcntl) • PHP Semaphore extension, optional (--enable-sysvsem, --enable-sysvshm, --enable-sysvmsg) • Plenty of memory • Multiple CPU cores Wednesday, December 5, 12
  • 17. Overview 1. Define signal handlers 2. Fetch a dataset 3. Fork off one child process for each item 4. Stop forking when a threshold is reached, and sleep 5. Reap a child process whenever SIGCHLD is received 6. If there’s more work to do, fork more processes 7. When all child processes have been reaped, terminate Wednesday, December 5, 12
  • 18. declare(ticks = 1); // Setup our signal handlers pcntl_signal(SIGTERM, "signal_handler"); pcntl_signal(SIGINT,  "signal_handler"); pcntl_signal(SIGCHLD, "signal_handler"); Wednesday, December 5, 12
  • 19. function signal_handler($signal) { switch ($signal) { case SIGINT: case SIGTERM: // kill all child processes exit(0); case SIGCHLD: // reap a child process reap_child(); break; } } Wednesday, December 5, 12
  • 20. $pid = pcntl_fork(); switch($pid) { case 0: // Child process call_user_func($callback, $data); posix_kill(posix_getppid(), SIGCHLD); exit; case -1: // Parent process, fork failed throw new Exception("Out of memory!"); default: // Parent process, fork succeeded $processes[$pid] = TRUE; } Wednesday, December 5, 12
  • 21. Repeat for each unit of work Wednesday, December 5, 12
  • 22. function reap_child() { // Check if any child process has terminated, // and if so remove it from memory $pid = pcntl_wait($status, WNOHANG); if ($pid < 0) { throw new Exception("Out of memory"); } elseif ($pid > 0) { unset($processes[$pid]); } } Wednesday, December 5, 12
  • 23. Demo Time! http://gist.github.com/4212160 Wednesday, December 5, 12
  • 25. DON’T: • DON’T fork a web process (CLI only!) • DON’T overload your system • DON’T open resources before forking • DO respect common POSIX signals • DO remove zombie processes • DO force new database connections in children mysql_reconnect($s, $u, $p, TRUE); Wednesday, December 5, 12
  • 27. Race Conditions • A logic bug where the result is affected by the sequence or timing of uncontrollable events • Adding debug logic can change timing • Dirty reads • Lost data • Unpredictable behavior • Deadlocks, hanging, crashing Wednesday, December 5, 12
  • 30. Solutions • Handle I/O in the parent process exclusively • Manage resources with semaphores and/or mutexes Wednesday, December 5, 12
  • 31. Semaphores • Semaphore = atomically updated counter • Mutex = binary semaphore with ownership • PHP: sem_get(), sem_release() • Transactional databases use semaphores Wednesday, December 5, 12
  • 32. Deadlocks Image credit: csunplugged.org Wednesday, December 5, 12
  • 34. Shared Memory • Advanced inter-process communication • Pass data back to the parent process • PHP Shared Memory extension (--enable-shmop) • PHP System V Shared Memory extension (--enable-sysvshm) • More robust • Compatible with other languages Wednesday, December 5, 12
  • 35. Daemonization • Fork, kill parent • Orphaned child process continues running • Signal and error handling are critical • Server daemons usually fork child processes to handle requests Wednesday, December 5, 12
  • 37. Thank You! @compwright http://bit.ly/atlphpm Wednesday, December 5, 12