{"id":134,"date":"2010-12-12T20:24:00","date_gmt":"2010-12-12T20:24:00","guid":{"rendered":"http:\/\/aldarias.es\/wordpress\/?p=134"},"modified":"2010-12-12T20:24:00","modified_gmt":"2010-12-12T20:24:00","slug":"multiproceso-perl-fork","status":"publish","type":"post","link":"http:\/\/aldarias.es\/wordpress\/?p=134","title":{"rendered":"MULTIPROCESO PERL FORK"},"content":{"rendered":"<p>1) INTRODUCCI\u00d3N.<br \/>Muchas veces realizamos acciones que pueden hacerse en paralelo,<br \/>por ejemplo generar 6 tomos de un libro en latex. Cada tomo puede generarse<br \/>de forma independiente.<\/p>\n<p>El objetivo es que si se tienen 4 procesadores cada procesador genere<br \/>un libro, y cuando termine siga generando otro libros.<\/p>\n<p>La conclusi\u00f3n es que  tarda 1\/3 parte menos de tiempo en realizar<br \/>las tareas.<\/p>\n<p>Esta basado en el programa jigl, que genera albunes de fotos, a una<br \/>velocidad de v\u00e9rtigo) el cual consulte las fuentes<br \/>para poder hacer mi propio script.<\/p>\n<p>2) SCRIPT EN PERL<\/p>\n<p>#!\/usr\/bin\/perl -w<br \/># Por Paco Aldarias<br \/># Fecha: 12\/12\/2010<br \/># Pasado en: jigl<br \/># Script que genera tomos en paralelo<\/p>\n<p>use Cwd;<\/p>\n<p>my $MP_MAX_JOBS = mp_detect_cpu();<br \/>print \u00abN\u00famero procesadores: $MP_MAX_JOBS\u00bb;<br \/>my $startDir = cwd;<\/p>\n<p>&amp;main();<\/p>\n<p>###########################################################<br \/>sub mp_detect_cpu<br \/>{<br \/>   my $r = `grep processor \/proc\/cpuinfo |wc -l`;<br \/>   mp_debug(\u00abDetected $r cpu.n\u00bb);<br \/>   return $r;<br \/>}<br \/>###########################################################<br \/>sub mp_debug($)<br \/>{<br \/>  print \u00abmp[$$]: \u00ab. $_[0];<br \/>}<br \/>###########################################################<br \/>sub mp_wait_jobs($)<br \/>{<br \/>   my($n) = @_;<\/p>\n<p>   while(1){<br \/>       my $running = scalar(keys %mp_jobs);<br \/>       mp_debug(\u00ab$running job(s) currently running.n\u00bb);<\/p>\n<p>       last if $running &lt;= $n;          mp_debug(\u00abWaiting&#8230;n\u00bb);          # Wait for a job to finish.         my $c = wait();         my $rc = $?;         mp_debug(\u00ab-&gt; $c ($rc)n\u00bb);<\/p>\n<p>       # Sanity checks.<br \/>       if($c &lt; 0){<br \/>           print \u00abmp: wait() returned $c. Strange!n\u00bb;<br \/>       }<\/p>\n<p>       if(!exists $mp_jobs{$c}){<br \/>           print \u00abmp: wait() returned $c, but job is not\u00bb<br \/>                   .\u00bb in mp_jobs. Strange!n\u00bb;<br \/>       }<\/p>\n<p>       # Check the return code and die if non-zero.<br \/>       die $mp_jobs{$c} if $rc;<\/p>\n<p>       # Forget job.<br \/>       delete $mp_jobs{$c};<br \/>   }<br \/>}<br \/>###########################################################<br \/>sub mp_wait_for_free_slot()<br \/>{<br \/>   mp_wait_jobs($MP_MAX_JOBS &#8211; 1);<br \/>}<br \/>###########################################################<br \/>sub mp_wait_for_all_jobs()<br \/>{<br \/>   mp_wait_jobs(0);<br \/>}<br \/>###########################################################<br \/>sub mp_launch($$)<br \/>{<br \/>   my($cmd, $die_msg) = @_;<\/p>\n<p>   # Wait for a free slot first.<br \/>   mp_wait_for_free_slot();<\/p>\n<p>   # Ok, fork.<br \/>   my $r = fork();<\/p>\n<p>   if(!$r){<br \/>       # We are in the child.<br \/>       # Do the job.<br \/>       mp_debug(\u00abRunning $cmd n\u00bb);<br \/>       exec($cmd);<\/p>\n<p>   } else {<br \/>       # We are in the father.<br \/>       # Remember the child as well as the die msg.<br \/>       mp_debug(\u00abLaunched $r.n\u00bb);<br \/>       $mp_jobs{$r} = $die_msg;<br \/>   }<br \/>}<\/p>\n<p>######################################################3<br \/>sub main {<\/p>\n<p>my @carpetas=`find . -type d -name \u00abtic1tomo?-*\u00bb | cut -d \u00ab\/\u00bb -f2`;<\/p>\n<p>foreach $c (@carpetas) {<\/p>\n<p>#Eliminamos saltos de linea.<br \/>$c =~ s\/n\/\/g;<br \/>$c =~ s\/ \/\/g;<\/p>\n<p>my @f = split(\u00ab-\u00ab,$c);<\/p>\n<p>print \u00abCarpeta: $c Fichero: $f[0]n\u00bb;<\/p>\n<p>chdir $startDir;<br \/>chdir \u00ab.\/\u00bb.$c;<br \/>$cmd = \u00abpdflatex -interaction nonstopmode \/home\/paco\/Dropbox\/ceed1011\/apuntes\/\u00bb.$c.\u00bb\/\u00bb.$f[0].\u00bb.tex -output-directory \/home\/paco\/Dropbox\/ceed1011\/apuntes\/\u00bb.$c.\u00bb\/\u00bb;<br \/>print \u00abComando: $c\u00bb;<br \/>$dieMsg = \u00abMensaje Errorn\u00bb;<br \/>mp_launch($cmd, $dieMsg);<\/p>\n<p>}<\/p>\n<p>mp_wait_for_all_jobs();<\/p>\n<p>}<br \/>######################################3<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1) INTRODUCCI\u00d3N.Muchas veces realizamos acciones que pueden hacerse en paralelo,por ejemplo generar 6 tomos de un libro en latex. Cada tomo puede generarsede forma independiente. El objetivo es que si se tienen 4 procesadores cada procesador genereun libro, y cuando &hellip; <a href=\"http:\/\/aldarias.es\/wordpress\/?p=134\">Sigue leyendo <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"episode_type":"","audio_file":"","podmotor_file_id":"","podmotor_episode_id":"","cover_image":"","cover_image_id":"","duration":"","filesize":"","filesize_raw":"","date_recorded":"","explicit":"","block":"","itunes_episode_number":"","itunes_title":"","itunes_season_number":"","itunes_episode_type":"","footnotes":""},"categories":[192,157,193,194],"tags":[],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-fork","category-latex","category-multiproceso","category-perl"],"_links":{"self":[{"href":"http:\/\/aldarias.es\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/aldarias.es\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/aldarias.es\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/aldarias.es\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/aldarias.es\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=134"}],"version-history":[{"count":0,"href":"http:\/\/aldarias.es\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/134\/revisions"}],"wp:attachment":[{"href":"http:\/\/aldarias.es\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/aldarias.es\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/aldarias.es\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}