第二题: 2p2v模型
2个生产者 1#每秒生产1个苹果 2#每秒生产2个橘子
2个消费者 3#没秒消费3个苹果 4#每2秒消费5个橘子
代码如下:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int app = 0;
int org = 0;
pthread_mutex_t app_m;
pthread_cond_t app_c;
pthread_mutex_t org_m;
pthread_cond_t org_c;
void *app_p(void *arg)
{
while(1)
{
pthread_mutex_lock(&app_m);
app++;
printf("苹果生产了:%d\n",app);
if(app >= 3)
{
pthread_cond_signal(&app_c);
}
pthread_mutex_unlock(&app_m);
sleep(1);
}
}
void *app_v(void *arg)
{
while(1)
{
pthread_mutex_lock(&app_m);
pthread_cond_wait(&app_c,&app_m);
app -= 3;
printf("消费了3个苹果,剩余苹果数量为:%d\n",app);
pthread_mutex_unlock(&app_m);
sleep(1);
}
}
void *org_p(void *arg)
{
while(1)
{
pthread_mutex_lock(&org_m);
org += 2;
printf("橘子生产了:%d\n",org);
if(org >= 5)
{
pthread_cond_signal(&org_c);
}
pthread_mutex_unlock(&org_m);
sleep(1);
}
}
void *org_v(void *arg)
{
while(1)
{
pthread_mutex_lock(&org_m);
pthread_cond_wait(&org_c,&org_m);
org -= 5;
printf("消费橘子5个,剩余橘子数量为:%d\n",org);
pthread_mutex_unlock(&org_m);
sleep(2);
}
}
int main(int argc, const char *argv[])
{
pthread_t app_p_id,app_v_id,org_p_id,org_v_id;
pthread_mutex_init(&app_m,0);
pthread_cond_init(&app_c,0);
pthread_mutex_init(&org_m,0);
pthread_cond_init(&org_c,0);
pthread_create(&app_p_id,0,app_p,0);
pthread_create(&app_v_id,0,app_v,0);
pthread_create(&org_p_id,0,org_p,0);
pthread_create(&org_v_id,0,org_v,0);
pthread_detach(app_p_id);
pthread_detach(app_v_id);
pthread_detach(org_p_id);
pthread_detach(org_v_id);
while(1);
return 0;
}
运行结果:
第三题: 2p2v模型
2个生产者 1#每秒生产1个苹果 2#每秒生产2个橘子
2个消费者 3#没秒消费3个苹果 4#每2秒消费5个橘子
由于仓库有限:生产了橘子之后,就不能生产苹果,反之同理 同样由于仓库有限,仓库里面最多存放10个苹果或者20个橘子
代码如下:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
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("生产了苹果数量为:%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("消费了3个苹果,剩余苹果数量为:%d\n",app);
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("生产了橘子数量为:%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("消费了5个橘子,剩余橘子数量为:%d\n",org);
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;
}
运行结果: