| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 581 人关注过本帖
标题:多线程信号量程序输出结果为什么不一样。。。
只看楼主 加入收藏
天各一方2010
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2010-5-27
结帖率:83.33%
收藏
 问题点数:0 回复次数:1 
多线程信号量程序输出结果为什么不一样。。。
我写了一个关于控制信号量的程序,用来输出某个文件夹下所有符合条件的文件。
但是每次由于参数选择的不同输出的文件名都不同,谁知道为什么????
./a.out lili 3(随着这里输入的不同输出的文件数量不一样) (Filename)


#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/stat.h>
#include<pthread.h>
#include<limits.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<fnmatch.h>
#include<stddef.h>
#include<dirent.h>
#include<pthread.h>

#define ARGMAX sysconf(_SC_ARG_MAX)
#define LINEMAX sysconf(_SC_LINE_MAX)
#define ARG 1024
typedef struct {
    int s;
    pthread_mutex_t m;
    pthread_cond_t c;
}SEM;

SEM *sem_init(int n){
    SEM *s;
    s=(SEM*)malloc(sizeof(SEM));
    if(s==NULL){
    perror("malloc");
    exit(EXIT_FAILURE);
    }
    pthread_mutex_init(&s->m,NULL);
    pthread_cond_init(&s->c,NULL);
    s->s=n;
    return s;
}

void p(SEM*s){
    pthread_mutex_lock(&s->m);
    while(s->s<1){
    pthread_cond_wait(&s->c,&s->m);
    }
    s->s--;
    pthread_mutex_unlock(&s->m);
}

void v(SEM*s){
    pthread_mutex_lock(&s->m);
    s->s++;
    pthread_cond_broadcast(&s->c);
    pthread_mutex_unlock(&s->m);
}

static int crawl_thread(char *,int,char *);

static void *grep_thread(void *);

static SEM *sem_full;
static SEM *sem_free;
static pthread_mutex_t lock;

int main(int argc,char *argv[]){
    int  max_grep_thread;
    if(argc!=4) {
    printf("Usage:./palim <string> <max-grep-threads> <trees...>\n");
    }
    else {
    max_grep_thread=atoi(argv[2]);
    if((strlen(argv[1])<=LINEMAX)&&(strlen(argv[2])<=ARGMAX)){
        crawl_thread(argv[1],max_grep_thread,argv[3]);
    }
    }
return 0;
}

int crawl_thread(char *string,int thread_num,char *pathname){
    struct dirent *next;
    DIR *dir;
    int i;
    pthread_attr_t *attr=NULL;
    pthread_t tid[thread_num];
    sem_full=sem_init(0);
    sem_free=sem_init(thread_num);
    pthread_mutex_init(&lock,NULL);
    if((dir=opendir(pathname))==NULL){
    perror("opendir");
//    exit(EXIT_FAILURE);
    }
    else {
    char str[ARG];
    while((next=readdir(dir))!=NULL){
        struct stat sb;
        if((strcmp(next->d_name,".")==0)||(strcmp(next->d_name,"..")==0)){
        continue;
        }
        strcpy(str,pathname);
        strcat(str,"/");
        strcat(str,next->d_name);
        if(stat(str,&sb)==-1){
        perror("stat");
//        exit(EXIT_FAILURE);
        }      
        else {
        for(i=0;i<thread_num-1;i++){
            if(S_ISREG(sb.st_mode)){
            pthread_create(&tid[i],attr,grep_thread,(void*)str);
            p(sem_free);
            printf("1\n");
            v(sem_full);
            }
        else if(S_ISDIR(sb.st_mode)){
            crawl_thread(string,thread_num,str);
        }
        else continue;
        }
        }
    }
    }
    closedir(dir);
    return 0;
}

void *grep_thread(void *filename){
    p(sem_full);
    pthread_mutex_lock(&lock);
    printf("%s\n",filename);
    printf("2\n");
    pthread_mutex_unlock(&lock);
    v(sem_free);
}
搜索更多相关主题的帖子: 结果 线程 信号量 输出 
2010-12-13 08:11
天各一方2010
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2010-5-27
收藏
得分:0 
高手呢
2010-12-13 15:58
快速回复:多线程信号量程序输出结果为什么不一样。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014896 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved