第一题: 编写代码验证递归锁和错误检查锁的功能
第二题: 2p2v模型 2个生产者 1#每秒生产1个苹果 2#每秒生产2个橘子 2个消费者 3#没秒消费3个苹果 4#每2秒消费5个橘子
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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;
pthread_mutex_t m;
pthread_mutex_t n;
pthread_cond_t a;
pthread_cond_t o;
int apples =0;
int oranges=0;
void* task2(void* arg)
{
while(1)
{
pthread_mutex_lock(&n);
oranges+=2;
printf("生产者2生产橘子的个数为: %d\n",oranges);
pthread_cond_signal(&o);
pthread_mutex_unlock(&n);
sleep(1);
}
}
void* task3(void* arg)
{
while(1)
{
pthread_mutex_lock(&m);
while(apples<3)
{
pthread_cond_wait(&a,&m);
}
if(apples>=3)
{
apples-=3;
printf("消费者1消费三个苹果后的个数为:%d\n",apples);
}
pthread_mutex_unlock(&m);
sleep(1);
}
}
void* task4(void* arg)
{
while(1)
{
pthread_mutex_lock(&n);
while(oranges<5)
{
pthread_cond_wait(&o,&n);
}
if(oranges>=5)
{
oranges-=5;
printf("消费者2买过5个橘子后的个数为: %d\n",oranges);
}
pthread_mutex_unlock(&n);
sleep(2);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&m, 0);
pthread_mutex_init(&n, 0);
pthread_cond_init(&a, 0);
pthread_cond_init(&o, NULL);
pthread_t id1,id2,id3,id4;
pthread_create(&id2,0,task2,0);
pthread_create(&id3,0,task3,0);
pthread_create(&id4,0,task4,0);
pthread_detach(id1);
pthread_detach(id2);
pthread_detach(id3);
pthread_detach(id4);
while(1)
{
pthread_mutex_lock(&m);
apples++;
printf("生产者1生产的苹果数为:%d\n",apples);
pthread_cond_signal(&a);
pthread_mutex_unlock(&m);
sleep(1);
}
return 0;
}
第三题: 2p2v模型 2个生产者 1#每秒生产1个苹果 2#每秒生产2个橘子 2个消费者 3#没秒消费3个苹果 4#每2秒消费5个橘子 由于仓库有限:生产了橘子之后,就不能生产苹果,反之同理 同样由于仓库有限,仓库里面最多存放10个苹果或者20个橘子
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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;
pthread_mutex_t m;
pthread_mutex_t n;
pthread_mutex_t p;
pthread_cond_t a;
pthread_cond_t o;
int apples =0;
int oranges=0;
void* task2(void* arg)
{
while(1)
{
pthread_mutex_lock(&p);
pthread_mutex_lock(&n);
if(oranges<20)
{
oranges+=2;
printf("生产者2生产橘子的个数为: %d\n",oranges);
pthread_cond_signal(&o);
}
pthread_mutex_unlock(&n);
pthread_mutex_unlock(&p);
sleep(1);
}
}
void* task3(void* arg)
{
while(1)
{
pthread_mutex_lock(&m);
while(apples<3)
{
pthread_cond_wait(&a,&m);
}
if(apples>=3)
{
apples-=3;
printf("消费者1消费三个苹果后的个数为:%d\n",apples);
}
pthread_mutex_unlock(&m);
sleep(1);
}
}
void* task4(void* arg)
{
while(1)
{
pthread_mutex_lock(&n);
while(oranges<5)
{
pthread_cond_wait(&o,&n);
}
if(oranges>=5)
{
oranges-=5;
printf("消费者2买过5个橘子后的个数为: %d\n",oranges);
}
pthread_mutex_unlock(&n);
sleep(2);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&m, 0);
pthread_mutex_init(&n, 0);
pthread_mutex_init(&p,0);
pthread_cond_init(&a, 0);
pthread_cond_init(&o, NULL);
pthread_t id1,id2,id3,id4;
pthread_create(&id2,0,task2,0);
pthread_create(&id3,0,task3,0);
pthread_create(&id4,0,task4,0);
pthread_detach(id1);
pthread_detach(id2);
pthread_detach(id3);
pthread_detach(id4);
pthread_mutex_destroy(&m);
pthread_mutex_destroy(&n);
pthread_mutex_destroy(&p);
pthread_cond_destroy(&a);
pthread_cond_destroy(&o);
while(1)
{
pthread_mutex_lock(&p);
pthread_mutex_lock(&m);
if(apples<10)
{
apples++;
printf("生产者1生产的苹果数为:%d\n",apples);
pthread_cond_signal(&a);
}
pthread_mutex_unlock(&m);
pthread_mutex_unlock(&p);
sleep(1);
}
return 0;
}