- 论坛徽章:
- 0
|
在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);
} |
|