免费注册 查看新帖 |

Chinaunix

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

在HP UINX上多线程编程问题求教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-05 15:23 |只看该作者 |倒序浏览
在HP-unix上,有大文件40G需要读其内容并做相应处理。为提高其处理效率,更好利用多个CPU,特设计如下:
启动CPU数量的线程,每个线程互斥读文件一行fgets,交给处理函数处理(耗费CPU操作),处理结束,读下一行,...,...一直到文件结束...
但使用cc   -D_REENTRAN  -O2 -lpthread thread4.c -o thread4.exe 编译成功后,执行时候报“Memory fault(coredump)”,不知到为何呢?,此程序执行需要指定环境变量:AUTO_DATA_FILE,请指定为输入文件。
附程序简化代码
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#define MAXTHRDS 2
#define ROWSIZE    64000
pthread_t callThd[MAXTHRDS];
pthread_mutex_t mutexinmod;
long gRows;/*行id*/
typedef long  Int32;

FILE *hostf;
FILE *hostLog;
FILE *hostErr;

void *thread_read(void *arg) {
  char *line;
    unsigned char row[ROWSIZE];
    unsigned char Outrow[ROWSIZE];
   
    unsigned long amount = 0;
    unsigned long len = 0;
    int  rc=0;
    int ret;   
   while(1)
    {
              
      pthread_mutex_lock (&mutexinmod);        
      if (feof(hostf) && hostf!=NULL)  {
               printf("FILE_EOF ,thread id is %d\n",(int) arg);
                pthread_mutex_unlock (&mutexinmod);
              break;
      }
        if ( ( line= fgets((char *)row, sizeof(row), hostf) ) == NULL )
        {
         
               pthread_mutex_unlock (&mutexinmod);
          /*  printf("FILE_EOF ,thread id is %d\n",(int) arg);  */
           break;
        }  
         
        //此处忽略3000行,耗费CPU的处理程序
        gRows++;
     printf("write pipe rows: %d by thread id: %d\n",gRows,(int) arg);
   
     pthread_mutex_unlock (&mutexinmod);  
        
    }
   
}
int Init()
{

    char hostfile[256];
   
    int status,i;
        pthread_attr_t attr;
        unsigned long amount = 0;

    fflush(stdout);
    gRows = 0;
   
    printf("INMOD: AUTO_DATA_FILE=%s\n", getenv("AUTO_DATA_FILE"));
    strcpy(hostfile, getenv("AUTO_DATA_FILE"));
    if ( (hostf = (FILE *)fopen64(hostfile,"r")) == NULL) {
      
        printf("Open hostdata file(%s) failed! \n",hostfile); fflush(stdout);
        return 12;
    }
  
        
     /*初始化线程*/
    pthread_mutex_init(&mutexinmod, NULL);
         
        /* Create threads to perform the dotproduct  */
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
       
        for(i=0;i<MAXTHRDS;i++) {
          /* Each thread works on a different set of data          */
          pthread_create( &callThd[i], &attr, thread_read, (void *)i);
          }
        for(i=0;i<MAXTHRDS;i++) {
          pthread_join( callThd[i], NULL);
          }
       
        /*printf("init:ThreadRunningCount is:%d\n",ThreadRunningCount);  */
    printf("Total thread Count is:%d\n",i);
    pthread_attr_destroy(&attr);
    /* Wait on the other threads */
   
   
       
    return 1;
}



int main(void) {

  pthread_t mythread;
  int i;

i=Init();
/* After joining, print out the results and cleanup */
printf ("Done. Threaded\n");

pthread_mutex_destroy(&mutexinmod);

  exit(0);

}

论坛徽章:
0
2 [报告]
发表于 2006-09-05 15:38 |只看该作者
一定是多线程使用 critical resource了.

论坛徽章:
0
3 [报告]
发表于 2006-09-08 10:55 |只看该作者

幅度撒

论坛徽章:
0
4 [报告]
发表于 2006-09-08 11:20 |只看该作者
1.man xdb/gdb/kwdb.
2.如果只是并发读取,那么不需要mutex。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP