作业:
第二题:
pthread_mutex_t m;
pthread_cond_t c1,c2;
int apple=0;
int orange=0;
void* task1(void* arg)
{
while(1)
{
pthread_mutex_lock(&m);
pthread_cond_wait(&c1,&m);
apple-=3;
printf("1# consumer eat 3 apples\n");
pthread_mutex_unlock(&m);
sleep(1);
}
}
void* task2(void* arg)
{
while(1)
{
pthread_mutex_lock(&m);
pthread_cond_wait(&c2,&m);
orange-=5;
printf("2# consumer eat 5 oranges\n");
pthread_mutex_unlock(&m);
sleep(2);
}
}
void* task3(void* arg)
{
while(1)
{
pthread_mutex_lock(&m);
apple++;
printf("count of apple:%d\n",apple);
if(apple>=3)
{
pthread_cond_signal(&c1);
}
pthread_mutex_unlock(&m);
sleep(1);
}
}
void* task4(void* arg)
{
while(1)
{
pthread_mutex_lock(&m);
orange+=2;
printf("count of orange:%d\n",orange);
if(orange>=5)
{
pthread_cond_signal(&c2);
}
pthread_mutex_unlock(&m);
sleep(1);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&m,0);
pthread_cond_init(&c1,0);
pthread_cond_init(&c2,0);
pthread_t id1,id2,id3,id4;
pthread_create(&id1,0,task1,0);
pthread_create(&id2,0,task2,0);
pthread_create(&id3,0,task3,0);
pthread_create(&id4,0,task4,0);
while(1);
return 0;
}
第三题:
int app=0,org=0;
pthread_mutex_t producerm;
pthread_mutex_t app_m,org_m;
pthread_mutex_t app_max_m,org_max_m;
pthread_mutex_t m;
pthread_cond_t app_c,org_c;
pthread_cond_t app_max_c,org_max_c;
void *app_p(void *arg)
{
sleep(1);
while(1)
{
pthread_mutex_lock(&producerm);
while(1)
{
pthread_mutex_lock(&app_m);
app++;
printf("count of app:%d\n",app);
if(app>=3)
{
pthread_cond_signal(&app_c);
}
pthread_mutex_unlock(&app_m);
sleep(1);
if(app==0)break;
pthread_cond_wait(&app_max_c,&app_max_m);
}
pthread_mutex_unlock(&producerm);
usleep(1000);
}
}
void *app_v(void *arg)
{
while(1)
{
pthread_mutex_lock(&app_m);
pthread_cond_wait(&app_c,&app_m);
if(app==10)
{
app-=3;
printf("app consumer eat 3 apples\n");
sleep(1);
}
pthread_mutex_unlock(&app_m);
}
}
void *org_p(void *arg)
{
while(1)
{
pthread_mutex_lock(&producerm);
while(1)
{
pthread_mutex_lock(&org_m);
org+=2;
printf("count of org:%d\n",org);
if(org>=5)
{
pthread_cond_signal(&org_c);
}
pthread_mutex_unlock(&org_m);
sleep(1);
if(org==0)break;
pthread_cond_wait(&org_max_c,&org_max_m);
}
pthread_mutex_unlock(&producerm);
usleep(1000);
}
}
void *org_v(void *arg)
{
while(1)
{
pthread_mutex_lock(&org_m);
pthread_cond_wait(&org_c,&org_m);
if(org>=18)
{
org-=5;
printf("org consumer eat 5 organes\n");
sleep(3);
}
pthread_mutex_unlock(&org_m);
}
}
void *watch(void *arg)
{
while(1)
{
if(app<=9)
{
pthread_cond_signal(&app_max_c);
}
if(org<=18)
{
pthread_cond_signal(&org_max_c);
}
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&producerm,0);
pthread_mutex_init(&app_max_m,0);
pthread_mutex_init(&org_max_m,0);
pthread_mutex_init(&app_m,0);
pthread_mutex_init(&org_m,0);
pthread_mutex_init(&m,0);
pthread_cond_init(&app_c,0);
pthread_cond_init(&org_c,0);
pthread_cond_init(&app_max_c,0);
pthread_cond_init(&org_max_c,0);
pthread_t app_pid,org_pid,app_vid,org_vid,watch_id;
pthread_create(&app_pid,0,app_p,0);
pthread_create(&org_pid,0,org_p,0);
pthread_create(&app_vid,0,app_v,0);
pthread_create(&org_vid,0,org_v,0);
pthread_create(&watch_id,0,watch,0);
pthread_detach(app_pid);
pthread_detach(org_pid);
pthread_detach(app_vid);
pthread_detach(org_vid);
pthread_detach(watch_id);
while(1);
return 0;
}