免费注册 查看新帖 |

Chinaunix

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

linux下的多线程,在子线程中调用scanf()函数程序就终止了。。为什么? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-06 11:02 |只看该作者 |倒序浏览
经典的消费者生产者问题,设了2个生产者线程和2个消费者线程,缓冲区大小为10,现在暂定产品为int。

本人刚刚接触多线程,这个程序在producer函数中调用scanf()函数,想获取用户输入的int,作为产品,可是程序一运行到scanf处就结束了,很不解,请教!


代码如下:


#include<stdio.h>

#include<pthread.h>

#include<semaphore.h>



sem_t s;
   //公共信号量
sem_t p;
  //生产者信号量,初始化为10,代表有10个空位
sem_t c;
  //消费者线号量,初始化伪0
int in=0;

int out=0;

int buffer[10];





void *producer(){

        sem_wait(&p);

        sem_wait(&s);

        scanf("%d",&buffer[in]);

    printf("produce:%d\n",buffer[in]);

        in=(in+1)%10;

        sem_post(&c);

        sem_post(&s);

}



void *consumer(){

        sem_wait(&c);

        sem_wait(&s);

        printf("consume:%d\n",buffer[out]);

    out=(out+1)%10;

        sem_post(&p);

        sem_post(&s);

}



int main(){

        sem_init(&s,1,1);

    sem_init(&p,0,10);

    sem_init(&c,0,0);



    pthread_t p_1;

        pthread_t p_2;

        pthread_t c_1;

        pthread_t c_2;

   

        int i=0;

        while(i--)

        {

                pthread_create(&c_1,NULL,(void*)consumer,NULL);

                pthread_create(&c_2,NULL,(void*)consumer,NULL);

                pthread_create(&p_1,NULL,(void*)producer,NULL);

                pthread_create(&p_2,NULL,(void*)producer,NULL);

        }

    return 0;

}

论坛徽章:
0
2 [报告]
发表于 2010-01-06 17:29 |只看该作者
int i=0;
        while(i--)
        {
                pthread_create(&c_1,NULL,(void*)consumer,NULL);
                pthread_create(&c_2,NULL,(void*)consumer,NULL);
                pthread_create(&p_1,NULL,(void*)producer,NULL);
                pthread_create(&p_2,NULL,(void*)producer,NULL);
        }
你认为这样会进入while里面吗?
自己看清楚,
你可以改成这样试一试:
          pthread_create(&c_1,NULL,(void*)consumer,NULL);
                pthread_create(&c_2,NULL,(void*)consumer,NULL);
                pthread_create(&p_1,NULL,(void*)producer,NULL);
                pthread_create(&p_2,NULL,(void*)producer,NULL);
                sleep(10);// 让你输入int数据!
提醒一点, 如果你在网上找代码, 在copy上来之前自己先理理好吗?如果不是,那你的风格慢慢改一下吧!

论坛徽章:
0
3 [报告]
发表于 2010-01-06 18:03 |只看该作者
抱歉,i=0是笔误,应该是i=20.....

现在出现一个问题,如果在main函数中while(i--){创建线程}的话,这样就创建了n多个线程,但是现在我要的效果是创建2个生产者,2个消费者线程,所以我今天把程序改了以下:


#include<stdio.h>

#include<pthread.h>

#include<semaphore.h>



sem_t s;

sem_t p;

sem_t c;

int in=0;

int out=0;

char buffer[10];

pthread_mutex_t print_m;

void *producer(char *a)
{

    while(1)
   {
        sem_wait(&p);

        sem_wait(&s);

        pthread_mutex_lock(&print_m);
        printf("%s:",a);

        scanf("%c",buffer[in]);

        pthread_mutex_unlock(&print_m);
        in=(in+1)%10;

        sem_post(&c);

        sem_post(&s);

    }
}



void *consumer(char *a){


    while(1)
    {
        sem_wait(&c);

        sem_wait(&s);

        pthread_mutex_lock(&print_m);
        printf("%s:%c\n",a,buffer[out]);

        pthread_mutex_unlock(&print_m);
        out=(out+1)%10;

        sem_post(&p);

        sem_post(&s);

     }
}



int main(){

   
    sem_init(&s,1,1);

    sem_init(&p,0,10);

    sem_init(&c,0,0);



    pthread_mutex_init(&print_m,NULL);
    pthread_t p_1;

    pthread_t p_2;

    pthread_t c_1;

    pthread_t c_2;

   

    pthread_create(&c_1,NULL,(void*)consumer,"C1");
    pthread_create(&c_2,NULL,(void*)consumer,"C2");
    pthread_create(&p_1,NULL,(void*)producer,"P1");
    pthread_create(&p_2,NULL,(void*)producer,"P2");

    return 0;

}






现在的情况是:没等我输入数据程序就结束了,我搞不清楚为什么。。。。我的预期是main函数生成四个线程p1,p2,c1,c2,前两个调用producer函数要求用户输入(就是生产产品),后两个调用consumer函数,产生输入(如果buffer数组中有数据的话)。。

但是就是得不到预想的结果,我是在ubuntu下调试的.请高手赐教。

我感觉是scanf()的问题。


ps:所有的代码都是我自己写的。

论坛徽章:
0
4 [报告]
发表于 2010-01-06 18:58 |只看该作者
问题解决了.

我在main最后加了四句话,附上完整的程序:


#include<stdio.h>

#include<pthread.h>
#include<semaphore.h>
#include<string.h>

sem_t s;
sem_t p;
sem_t c;
int in=0;
int out=0;
char buffer[10];
pthread_mutex_t print_m;

void pp()
{
        int i=0;
        printf("buffer: ");
        for(i;i!=strlen(buffer);i++)
                printf("%c ",buffer);
        printf("in=%d out=%d\n",in,out);
}

void* producer(char *a)
{
        pthread_mutex_lock(&print_m);
        printf("%s is running\n",a);
    pthread_mutex_unlock(&print_m);
        while(1)
        {
                sem_wait(&p);
                sem_wait(&s);
        pthread_mutex_lock(&print_m);
        printf("%s Produced:",a);
                //setbuf(stdin,NULL);
                scanf("%c",&buffer[in]);
                while ((buffer[in]<'a'||buffer[in]>'z')&&(buffer[in]<'A'||buffer[in]>'Z'))
                        scanf("%c",&buffer[in]);
                in=(in+1)%10;
                pp();
                printf("\n");
        pthread_mutex_unlock(&print_m);
                sem_post(&c);
                sem_post(&s);
        }
}

void* consumer(char *a)
{
        pthread_mutex_lock(&print_m);
        printf("%s is running\n",a);
    pthread_mutex_unlock(&print_m);
        while(1)
        {
                sem_wait(&c);
                sem_wait(&s);
        pthread_mutex_lock(&print_m);
                printf("%s Consumed:%c\n",a,buffer[out]);
        out=(out+1)%10;
                pp();
                printf("\n");
        pthread_mutex_unlock(&print_m);
                sem_post(&p);
                sem_post(&s);
        }
}

int main()
{
        sem_init(&s,1,1);
    sem_init(&p,0,10);
    sem_init(&c,0,0);
       
        pthread_mutex_init(&print_m,NULL);

        pthread_t p_1;
        pthread_t p_2;
        pthread_t c_1;
        pthread_t c_2;

        pthread_create(&c_1,NULL,(void*)consumer,"C1");
        pthread_create(&p_1,NULL,(void*)producer,"P1");
        pthread_create(&c_2,NULL,(void*)consumer,"C2");
        pthread_create(&p_2,NULL,(void*)producer,"P2");

        pthread_join(p_1,NULL);
        pthread_join(p_2,NULL);
        pthread_join(c_1,NULL);
        pthread_join(c_2,NULL);
   
        return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP