- 论坛徽章:
- 0
|
程序源代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <ctype.h>
int total_words = 0;
main(int ac , char * av[ ])
{
pthread_t t1, t2;
void *test(void *);
void *count_words(void *);
if(ac!=3){
printf("usage: % s file1 file2\n", av[0]);
exit(1);
}
pthread_create(&t1,NULL,test,(void *)av[1]);
pthread_create(&t1,NULL,test,(void *)av[2]);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("%5d: total words\n", total_words);
}
void *count_words(void *f)
{
char *filename = (char * )f;
FILE * fp;
int c, temp, prevc = '\0';
if((fp = fopen (filename, "r" )) !=NULL)
{
while((c = getc(fp))!= EOF)
{
if(!isalnum(c)&& isalnum(prevc))
{
temp=total_words;
sleep(0.1); /*延时,使产生脏数据*/
total_words=temp+1;
}
prevc =c;
}
fclose(fp);
}
else
perror(filename);
return NULL;
}
void *test(void *f)
{
char *i=(char *)f;
printf("%s",*i);
}
程序出现了段错误
core dump
Core was generated by `./3 1.txt 2.txt'.
Program terminated with signal 11, Segmentation fault.
warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/tls/i686/cmov/libpthread.so.0...done.
Loaded symbols for /lib/tls/i686/cmov/libpthread.so.0
Reading symbols from /lib/tls/i686/cmov/libc.so.6...done.
Loaded symbols for /lib/tls/i686/cmov/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
#0 0xb7e9d27b in strlen () from /lib/tls/i686/cmov/libc.so.6
如果把线程中的执行程序换成了Count_words的话,运行就不会结束,为什么会这样呢 |
|