Unformatted text preview:

Basic Multiprocessing Basic Multiprocessing in UNIXin UNIXWith ExamplesWith ExamplesParallel ApplicationsParallel ApplicationsModern computers have multiple CPU cores Modern computers have multiple CPU cores (and/or multiple CPUs) on board(and/or multiple CPUs) on boardWe have to be able to utilize the computing We have to be able to utilize the computing power by parallelizing our tasks power by parallelizing our tasksCPU InformationCPU InformationLinux computer: /proc/Linux computer: /proc/cpuinfocpuinfoCat /proc/Cat /proc/cpuinfocpuinfoexample:example:processor : 0processor : 0vendor_idvendor_id: : AuthenticAMDAuthenticAMDcpucpufamily : 15family : 15model : 65model : 65model name : Dualmodel name : Dual--Core AMD Core AMD Opteron(tmOpteron(tm) Processor 8220) Processor 8220stepping : 3stepping : 3cpucpuMHz : 2800.000MHz : 2800.000cache size : 1024 KBcache size : 1024 KBphysical id : 0physical id : 0siblings : 2siblings : 2core id : 0core id : 0cpucpucores : 2cores : 2fpufpu: yes: yesfpu_exceptionfpu_exception: yes: yescpuidcpuidlevel : 1level : 1wpwp: yes: yesflags : flags : fpufpuvmevmede de psepsetsctscmsrmsrpaepaemcemcecx8 cx8 apicapicsep sep mtrrmtrrpgepgemcamcacmovcmovpat pse36 pat pse36 clflushclflushmmxmmxfxsrfxsrssessesse2 ht sse2 ht syscallsyscallnxnxmmxextmmxextfxsr_optfxsr_optrdtscprdtscplm lm 3dnowext 3dnow 3dnowext 3dnow pnipnicx16 cx16 lahflahf_lm _lm cmp_legacycmp_legacysvmsvmextapicextapiccr8_legacycr8_legacybogomipsbogomips: 5625.16: 5625.16TLB size : 1024 4K pagesTLB size : 1024 4K pagesclflushclflushsize : 64size : 64cache_alignmentcache_alignment: 64: 64address sizes : 40 bits physical, 48 bits virtualaddress sizes : 40 bits physical, 48 bits virtualpower management: power management: tstsfid fid vidvidttpttptm tm stcstcProcesses in UNIXProcesses in UNIXUNIX is natively parallel operating systemUNIX is natively parallel operating systemA A processprocessis an instance of running a program is an instance of running a program Each process has a unique Each process has a unique process idprocess idShell command Shell command ““psps””gives the list of all running gives the list of all running processesprocessesUsing the shell commandsUsing the shell commandsIn any UNIX shell, In any UNIX shell, ““&&””will run the command in will run the command in background. background. The command will run in its own shell, which is a child The command will run in its own shell, which is a child of the current shellof the current shell[alekseyz@genome10]$ [alekseyz@genome10]$ run_command.shrun_command.sh&&““waitwait””command will wait for all child processes in the command will wait for all child processes in the current shell to finishcurrent shell to finishExample of & and waitExample of & and waitIn bash:In bash:#!/bin/bash#!/bin/bashlet NUM_CPUS=`cat /proc/let NUM_CPUS=`cat /proc/cpuinfocpuinfo||grepgrepprocessor|tailprocessor|tail--1|awk '{print $NF+1}'`1|awk '{print $NF+1}'`let counter=1;let counter=1;let let cpu_countercpu_counter=1;=1;echo "Total processes to echo "Total processes to run:"$max_counterrun:"$max_counterecho "Simultaneously echo "Simultaneously running:"$NUM_CPUSrunning:"$NUM_CPUSwhile [[ $counter while [[ $counter --le $1 ]];dole $1 ]];dowhile [[ $while [[ $cpu_countercpu_counter--le $NUM_CPUS && $counter le $NUM_CPUS && $counter --le $1 ]];dole $1 ]];do././echo_sleep_echo.shecho_sleep_echo.sh&&let counter=$counter+1let counter=$counter+1let let cpu_countercpu_counter=$cpu_counter+1;=$cpu_counter+1;donedonelet let cpu_countercpu_counter=1;=1;waitwaitdonedone------------------------------------------------------------------------------------------------------------------------------------------------------#!/bin/bash#!/bin/bashecho "Sleeping 10 seconds in shell "$$echo "Sleeping 10 seconds in shell "$$sleep 10sleep 10echo "Done"echo "Done"Using fork() and wait() in CUsing fork() and wait() in CThe fork() system call is the basic way to create a new The fork() system call is the basic way to create a new process. fork() is used to produce child shell.process. fork() is used to produce child shell.Returns twice(!!!!)Returns twice(!!!!)fork() causes the current process to be split into two fork() causes the current process to be split into two processes processes --a parent process, and a child process. a parent process, and a child process. All of the memory pages used by the original process All of the memory pages used by the original process get duplicated during the fork() call, so both parent and get duplicated during the fork() call, so both parent and child process see the child process see the exact same memory imageexact same memory image. .fork() continuedfork() continuedWhen fork() returns in the parent process, its return When fork() returns in the parent process, its return value is the process ID (PID) of the child process. value is the process ID (PID) of the child process. When it returns inside the child process, its return value When it returns inside the child process, its return value is '0'. is '0'. If for some reason fork() failed (not enough memory, If for some reason fork() failed (not enough memory, too many processes, etc.), no new process is created, too many processes, etc.), no new process is created, and the return value of the call is 'and the return value of the call is '--1'. 1'. Both child process and parent process continue from Both child process and parent process continue from the same place in the code where the fork() call was the same place in the code where the fork() call was used. used.Child processesChild processesWhen a child process exits, it sends a signal to its parent procWhen a child process exits, it sends a signal to its parent process, ess, which needs to acknowledge it's child's death. During this time which needs to acknowledge it's child's death. During this time the child process is in a state called the child process is in a state called zombiezombie. . When a process exits, if it had any children, they become When a process exits, if it had any children, they become orphansorphans. . An orphan process is automatically inherited by the An orphan


View Full Document
Download Basic Multiprocessing in UNIX
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Basic Multiprocessing in UNIX and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Basic Multiprocessing in UNIX 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?