免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3069 | 回复: 4
打印 上一主题 下一主题

unix下多进程实现问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-09-05 10:23 |只看该作者 |倒序浏览
具体情况是这样的:先写了一个对文本进行处理的动态链接库,然后编写一个程序调用此动态连接库里的函数对文本进行处理,为了达到cpu处理的极限,便希望以多个进程来分别调用此函数,然后对不同的文本进行处理。
由于本人较笨,只好这样来编写程序,用多个if(fork()==0){....}else{if (fork==0){...}else...}这样来处理,最后调用wait()函数等待进程结束。但是wait()函数是等待最快结束的一个子进程,并不是等待所有进程结束。所以导致了最后的结果很怪异。
请问一下如何来解决等待所有子进程结束然后在开始运行父进程的下步程序。另外有没有什么其他办法来解决多进程调用函数运行(不要象我上面的那么的傻)。
希望赐教,先多谢了~

论坛徽章:
0
2 [报告]
发表于 2005-09-05 10:26 |只看该作者

unix下多进程实现问题

记住子进程的个数,必要时再记住各个PID,循环wait就行了。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2005-09-05 10:29 |只看该作者

unix下多进程实现问题

>;>;但是wait()函数是等待最快结束的一个子进程,并不是等待所有进程结束。所以导致了最后的结果很怪异。


处理SIGCHLD信号.用waitpid 可以等待这个组的所有子进程

论坛徽章:
0
4 [报告]
发表于 2005-09-05 11:42 |只看该作者

unix下多进程实现问题

发一个我以前写的程序,在生产上用的,看对你有没有帮助



  1. void main( int argc, char **argv )
  2. {
  3. EXEC SQL BEGIN DECLARE SECTION;
  4. long pid, secuid;
  5. EXEC SQL END DECLARE SECTION;
  6.         int ret, i, p_stat;
  7.         char profile[FILENAME_MAX], tmp[40];
  8.         struct Process *proc;

  9.         if( (proc=GetProcess( (char *)getenv("LOGNAME"), argv[0], &i))
  10.                 !=(struct Process *)NULL && i>;1 )
  11.         {
  12.                 printf( "Other process %s is running!\n", argv[0] );
  13.                 while( i>;0 ){
  14.                         if( proc[i-1].pid!=getpid() )
  15.                                 printf( "pid=[%d]\n", proc[i-1].pid );
  16.                         i--;
  17.                 }
  18.                 exit( -1 );
  19.         }

  20.         if( argc>;=2 && atoi(argv[1])!=0 )
  21.                 t_interval=atoi( argv[1] );
  22.         else {
  23.                 sprintf( profile, "%s/etc/%s", (char *)getenv("HOME"), PROFILE );
  24.                 if( GetProfileString( profile, "LOCALE INFO",
  25.                         "reverse_interval", tmp )==0 )
  26.                 {
  27.                         t_interval=atoi(tmp);
  28.                 } else
  29.                         t_interval=30;
  30.         }

  31.         signal(SIGINT,SIG_IGN);
  32.         signal(SIGPIPE,SIG_IGN);
  33.         signal(SIGQUIT,SIG_IGN);

  34.         if (( pid=fork() )<0) {
  35.                 printf("REVERSE SERVER START FAILED!!!\n");
  36.                 exit(-1);
  37.         } else if (pid>;0 )
  38.                 exit(0);

  39.         if(setpgrp()<0) {
  40.                 printf("CHANGE GROUP ERROR!!!\n");
  41.                 fflush(stdout);
  42.                 exit(-1);
  43.         }
  44.         signal(SIGHUP,SIG_IGN);
  45.         signal(SIGTERM,EXIT);

  46.         EXEC SQL database secudb;
  47.         if( SQLCODE!=0 ){
  48.                 printf( "OPEN DATABASE ERROR[%d]! EXIT\n", SQLCODE );
  49.                 exit( -1 );
  50.         }

  51.         EXEC SQL declare revcur cursor for
  52.                 select unique(secuid) from reverse
  53.                 where trans_date=today
  54.                 and result NOT in ( "11", "10", "01" );
  55.         if( SQLCODE!=0 ){
  56.                 printf( "DATABASE ERROR[%d]! EXIT\n", SQLCODE );
  57.                 exit( -1 );
  58.         }

  59.         printf("AUTO REVERSE SERVER START OK!!!\n");
  60.         fflush(stdout);

  61.         while( 1 ) {
  62.                 sleep( t_interval );

  63.                 EXEC SQL open revcur;
  64.                 if( SQLCODE!=0 ) {
  65.                         SDKerrlog( REVERSELOG, "%s|%d| open cursor error[%d]",
  66.                                 __FILE__, __LINE__, SQLCODE );
  67.                         continue;
  68.                 }
  69.                 p_head=(struct rev_t*)NULL;
  70.                 p=p_head;
  71.                 p_prev=NULL;
  72.                 p_next=NULL;
  73.                 i=0;
  74.                 while( 1 ) {
  75.                         EXEC SQL fetch revcur into :secuid;
  76.                         if( SQLCODE!=0 ){
  77.                                 if( SQLCODE==100 ) {
  78.                                         EXEC SQL close revcur;
  79.                                         break;
  80.                                 } else {
  81.                                         SDKerrlog( REVERSELOG, "%s|%d| fetch \
  82. next error[%d]", __FILE__, __LINE__, SQLCODE );
  83.                                         EXEC SQL close revcur;
  84.                                         break;
  85.                                 }
  86.                         }

  87.                         if( (pid=fork())<0 ) {
  88.                                 SDKerrlog( REVERSELOG, "%s|%d| Create new process \
  89. error[%s] secuid[%d]", __FILE__, __LINE__, strerror(errno), secuid );
  90.                                 continue;
  91.                         }

  92.                         if( pid>;0 ) { /* main process */
  93.                                 if( RegistProc( pid, secuid )<0 ){
  94.                                         SDKerrlog( REVERSELOG, "%s|%d| regist \
  95. process[%d] secuid[%d] error", __FILE__, __LINE__, pid, secuid );
  96.                                         continue;
  97.                                 }
  98.                                         i++;
  99.                         } else {/* sub process */
  100.                                 SubReverse( secuid );
  101.                                 exit( 1 );
  102.                         }
  103.                 }

  104. SDKerrlog( REVERSELOG, "%s|%d| Total [%d] reverse process. MainProcess[%d]",
  105.         __FILE__, __LINE__, i, getpid() );

  106.                 while( i>;0 ) {
  107.                         if( (pid=wait( &p_stat ))<0 ) {
  108.                                 SDKerrlog( REVERSELOG, "%s|%d| wait error[%s]",
  109.                                         __FILE__, __LINE__, strerror(errno) );
  110.                                 continue;
  111.                         }
  112.                         SDKerrlog( REVERSELOG, "%s|%d| process[%d] finished",
  113.                                 __FILE__, __LINE__, pid );
  114.                         if( ReleRevProc( pid )<0 )
  115.                                 continue;
  116.                         i--;
  117.                 }

  118.                 ReleRevTbl();
  119.         }
  120. }

复制代码


里面有些自定义函数,可以忽略

论坛徽章:
0
5 [报告]
发表于 2005-09-05 12:00 |只看该作者

unix下多进程实现问题

处理SIGCHLD信号,信号处理程序里用WNOHANG的方式循环waitpid,PID参数用-1
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP