VIP卷 此此章還(1 / 2)

#include

#include

#include

#include

#include

#include

#include

#include

#define FIFO "myfifo"

#define N 5

int lock_var;

time_t end_time;

char buf_r[100];

sem_t mutex,full,avail;

int fd;

void pthread1(void *arg);

void pthread2(void *arg);

int main(int argc, char *argv[])

{

pthread_t id1,id2;

pthread_t mon_th_id;

int ret;

end_time = time(NULL)+30;

if((mkfifo(FIFO,O_CREAT|O_EXCL)writepos+1)%BUFFER_SIZE==b->readpos) {

pthread_cond_wait(&b->notfull,&b->lock); }

b->buffer[b->writepos]=data;

b->writepos++;

if(b->writepos>=BUFFER_SIZE) b->writepos=0; ?pthread_cond_signal(&b->notempty);

pthread_mutex_unlock(&b->lock);

}

int get(struct producers *b)

{

int data;

pthread_mutex_lock(&b->lock);

while(b->writepos==b->readpos)

{

pthread_cond_wait(&b->notempty,&b->lock); }

data=b->buffer[b->readpos];

b->readpos++;

if(b->readpos>=BUFFER_SIZE) b->readpos=0; pthread_cond_signal(&b->notfull);

pthread_mutex_unlock(&b->lock);

return data;

}

struct producers buffer;

void *producer(void *data)

{

int n;

for(n=0;n

",n);

put(&buffer,n);

}

put(&buffer,OVER);

return NULL;

}

void *consumer(void *data)

{

int d;

while(1)

{

d=get(&buffer);

if(d==OVER) break;

printf("Consumer: --> %d

",d);

}

pthread_t tha,thb;

void *retval;

i#include

#include

#include

#include

#include

#include

#include

#include

#define FIFO "myfifo"

#define N 5

int lock_var;