免费注册 查看新帖 |

Chinaunix

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

能帮我找下错误吗 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-22 14:32 |只看该作者 |倒序浏览
程序源代码
#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的话,运行就不会结束,为什么会这样呢

论坛徽章:
0
2 [报告]
发表于 2007-10-22 14:42 |只看该作者
printf("%s", *i);
-->
printf("%s", i);

论坛徽章:
0
3 [报告]
发表于 2007-10-22 14:56 |只看该作者
pthread_create(&t1,NULL,test,(void *)av[1]);
         pthread_create(&t1,NULL,test,(void *)av[2]);

这里两个t1。

论坛徽章:
0
4 [报告]
发表于 2007-10-23 17:19 |只看该作者
谢谢大家了,没有想到是这些小错误呀,还以为犯了什么大的错误呢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP