LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 726|回复: 0

请各位高手帮忙(急!!)

[复制链接]
发表于 2004-5-31 22:39:48 | 显示全部楼层 |阅读模式
我在调试一个生产者消费者问题时遇到了麻烦,程序是我从网上下载的,可是编译没错,运行的时候说段错误.gdb调试告诉程序非正常结束.程序如下:请各位大哥帮忙看看.不胜感激!!!!
/* example91.c */


#include        <stdio.h>


#include        <stdlib.h>


#include        <pthread.h>


#include        <errno.h>





#define                MAXNITEMS        1000000


#define                MAXNTHREADS        100





#define min(a,b)        ((a) < (b) ? (a) : (b))





int        nitems;


struct {


        pthread_mutex_t        mutex;


        int                buff[MAXNITEMS];


        int                nput;


        int                nval;


}shared = {


        PTHREAD_MUTEX_INITIALIZER


};


void        *produce(void *);


void        *consume(void *);








void


Pthread_create(pthread_t *tid, const pthread_attr_t *attr,


                                           void * (*func)(void *), void *arg)


{


        int             n;


       


        if ( (n = pthread_create(tid, attr, func, arg)) == 0)


                return;


        errno = n;


        printf("pthread_create error");


}





void


Pthread_join(pthread_t tid, void **status)


{


                int             n;


               


                if ( (n = pthread_join(tid, status)) == 0)


                        return;


                errno = n;


                printf("pthread_join error");


}








int


set_concurrency(int level)


{


#ifdef        HAVE_THR_SETCONCURRENCY_PROTO


        int                thr_setconcurrency(int);





        return(thr_setconcurrency(level));


#else


return(0);


#endif


}





void


Set_concurrency(int level)


{


        if (set_concurrency(level) != 0)


                printf("set_concurrency error");


}








int


main(int argc, char **argv)


{


        int        i, nthreads, count[MAXNTHREADS];


        pthread_t        tid_produce[MAXNTHREADS], tid_consume;


        if (argc != 3) {


                printf("usage: prodcons3 <#items> <#threads>\n");


                return 0;


        }


        nitems = min(atoi(argv[1]), MAXNITEMS);


        nthreads = min(atoi(argv[2]), MAXNTHREADS);


        Set_concurrency(nthreads + 1);


        for (i=0; i<nthreads; i++) {


                count = 0;


                Pthread_create(&tid_consume, NULL, produce, &count);


        }


        Pthread_create(&tid_consume, NULL, consume, NULL);


        for (i=0; i<nthreads; i++) {


                Pthread_join(tid_produce, NULL);


                printf("count[%d] = %d\n", i, count);


        }


        Pthread_join(tid_consume, NULL);


        exit(0);


}


void *


produce(void *arg)


{


        for( ; ; ) {


                pthread_mutex_lock(&shared.mutex);


                if (shared.nput >= nitems) {


                        pthread_mutex_unlock(&shared.mutex);


                        return(NULL);


                }


                shared.buff[shared.nput] = shared.nval;


                shared.nput++;


                shared.nval++;


                pthread_mutex_unlock(&shared.mutex);


                *((int *)arg) += 1;


        }


}


void


consume_wait(int i)


{


        for ( ; ; ) {


                pthread_mutex_lock(&shared.mutex);


                if (i < shared.nput) {


                        pthread_mutex_unlock(&shared.mutex);


                        return;


                }


                pthread_mutex_unlock(&shared.mutex);


        }


}


void *


consume(void *arg)


{


        int        i;


        for (i=0; i<nitems; i++) {


                consume_wait(i);


                if (shared.buff != i)


                        printf("buff[%d] = %d\n", i, shared.buff);


        }


        return(NULL);


}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表