首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

linux c 多线程 生产者-消费者二

2012-12-19 
linux c 多线程 生产者--消费者2实在不好意思,第一个版本有些问题,是局部变量和堆里面变量的区别。今天做了

linux c 多线程 生产者--消费者2

实在不好意思,第一个版本有些问题,是局部变量和堆里面变量的区别。今天做了一下修改。代码如下。

#ifndef _LIST_H_#define _LIST_H_#include <string.h>#include <stdio.h>#include <stdlib.h>struct List{char * buffer;char * cursor;char * begin;char * end;};// you must add struct, otherwise it will prompt "expected ‘)’ before ‘*’ token"void init(struct List * p_list);int get_buffer_length (struct List * p_list);void put_into_buffer(struct List* p_list,char *pc_char);char get_from_buffer(struct List* p_list);#endif

?

?? 主要是更改了结构体List的定义,buffer必须定义为堆里面的变量。

???#include "list.h"

void init(struct List * p_list){char *temp=(char *)malloc(10);memset(temp,'\0',10);p_list->buffer=temp;p_list->cursor=(p_list->buffer)+10;p_list->begin=p_list->buffer;p_list->end=(p_list->buffer)+10;}int get_buffer_length(struct List * p_list){return p_list->end-p_list->cursor;}void put_into_buffer(struct List * p_list,char * pc_char){printf("put ........  %c\n",*pc_char);if(p_list->cursor<=p_list->begin||p_list->cursor>p_list->end)printf(" put error\n");//put the char into cursor's former place(p_list->cursor)--;memset(p_list->cursor,(int)(*pc_char),1);}char get_from_buffer(struct List * p_list){if(p_list->cursor<p_list->begin||p_list->cursor>p_list->end)printf(" get error\n");       int length=get_buffer_length(p_list);    printf("list length now is %d\n",length);    (p_list->cursor)++;    char result=*(p_list->end-1);    printf("get ------------------- %c\n",result);        if(length==1)    {     return result;    }   else   {   char *temp=(char *)malloc(10);bcopy(p_list->cursor-1,temp+10-length+1,length-1);free(p_list->buffer);p_list->buffer=temp;p_list->begin=temp;p_list->end=temp+10;p_list->cursor=p_list->end-length+1;      }return 0;}

?

?? ?#include <pthread.h>

#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include "List.h"pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;//define the Producer threadvoid * produce(void * arg){//arg is a char * pointerstruct List * p_list=(struct List *)arg;//if buffer length =10,than wait,because buffer is full,while(1){pthread_mutex_lock(&lock);  //正确位置,//printf("111111\n");if(get_buffer_length(p_list)==10){//printf("22222222\n");int num1=pthread_cond_wait(&has_product, &lock);//printf("33333333\n");//printf(" num11111 %d\n",num1);}//buffer'length !=10,not full, we can put char into buffer.//printf("444444\n");//pthread_mutex_lock(&lock); //错误位置//printf("555555\n");char zifu=(char)(1+(int)(128.0*rand()/(RAND_MAX+1.0)));put_into_buffer(p_list,&zifu);//after put, if the length>0,we can notify the wait thread,means that you can get char nowif(get_buffer_length(p_list)==1){pthread_cond_signal(&has_product);//printf("66666\n");}//printf("777777777777\n");int num3=pthread_mutex_unlock(&lock);//printf("unlocknumber  1111111111111 is %d\n",num3);//printf("888888888888\n");sleep(1);    //printf("99999999\n");   }}//define the second threadvoid * comsume(void * arg){//arg is a char * pointerstruct List * p_list=(struct List *)arg;while(1){//if buffer length =10,than wait,because buffer is full,pthread_mutex_lock(&lock);  //正确位置,//printf("aaaaaaaaaa\n");if(get_buffer_length(p_list)==0){//printf("bbbbbbbbbbbbbb\n");int num2=pthread_cond_wait(&has_product, &lock);//printf("cccccccccccc\n");//printf(" num2222 %d\n",num2);}//buffer'length !=0,not full, we can get char into buffer.//printf("dddddddddddddddd\n");//int num4=pthread_mutex_lock(&lock); //错误位置//printf("unlocknumber 222222222 is %d\n",num4);//printf("eeeeeeeeeeeeee\n");char zifu=get_from_buffer(p_list);//after get, if the length<10,we can notify the put thread,means that you can put now!if(get_buffer_length(p_list)<10){pthread_cond_signal(&has_product);}//printf("ffffffffffff\n");pthread_mutex_unlock(&lock);//printf("gggggggggggg\n");    sleep(2);   // printf("hhhhhhhhhhhhhh\n");   }}int main (int argc, char ** argv){pthread_t tidA, tidB;    struct List common;    init(&common);pthread_create(&tidB, NULL, &comsume, &common);sleep(3);pthread_create(&tidA, NULL, &produce, &common);sleep(120);return 0;}
?

热点排行