作业
第一题:
编写代码验证递归锁和错误检查锁的功能
#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 recursive_lock;
// 递归函数,尝试多次获取锁
void recursive_function(int level) {
if (level == 0) {
return;
}
// 尝试获取锁
pthread_mutex_lock(&recursive_lock);
printf("Thread %ld acquired lock at level %d\n", (long)pthread_self(), level);
// 假装我们在这里做一些工作
sleep(1); // 模拟耗时操作
// 递归调用
recursive_function(level - 1);
// 释放锁
pthread_mutex_unlock(&recursive_lock);
printf("Thread %ld released lock at level %d\n", (long)pthread_self(), level);
}
// 线程函数
void* thread_func(void* arg) {
int level = *(int*)arg;
recursive_function(level);
return NULL;
}
int main() {
pthread_t thread;
int level = 3; // 递归深度
// 初始化递归锁
if (pthread_mutex_init(&recursive_lock, NULL) != 0) {
printf("Mutex init has failed\n");
return 1;
}
// 设置递归锁属性(虽然在这个例子中我们直接初始化了,但通常你会在这里设置PTHREAD_MUTEX_RECURSIVE)
// pthread_mutexattr_t attr;
// pthread_mutexattr_init(&attr);
// pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
// pthread_mutex_init(&recursive_lock, &attr);
// pthread_mutexattr_destroy(&attr);
// 创建线程
if (pthread_create(&thread, NULL, thread_func, &level) != 0) {
printf("Thread creation failed\n");
return 1;
}
// 等待线程结束
if (pthread_join(thread, NULL) != 0) {
printf("Thread join failed\n");
return 1;
}
// 销毁锁
pthread_mutex_destroy(&recursive_lock);
return 0;
}
第二题:
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;
pthread_mutex_t m1,m2;
pthread_cond_t co1,co2;
int apple=0;
int tangerine=0;
void* task1(void* arg)
{
while(1)
{
pthread_mutex_lock(&m1);
apple++;
printf("1#生产苹果,苹果数量:%d\n",apple);
if(apple >=3)
{
pthread_cond_signal(&co1);
}
pthread_mutex_unlock(&m1);
sleep(1);
}
}
void* task2(void* arg)
{
while(1)
{
pthread_mutex_lock(&m2);
tangerine+=2;
printf("2#生产橘子,橘子数量:%d\n",tangerine);
if(tangerine >=5)
{
pthread_cond_signal(&co2);
}
pthread_mutex_unlock(&m2);
sleep(1);
}
}
void* task3(void* arg)
{
while(1)
{
pthread_mutex_lock(&m1);
pthread_cond_wait(&co1,&m1);
if(apple >= 3){apple-=3;}
printf("3#消费苹果,剩余苹果数量:%d\n",apple);
pthread_mutex_unlock(&m1);
sleep(1);
}
}
void* task4(void* arg)
{
while(1)
{
pthread_mutex_lock(&m2);
pthread_cond_wait(&co2,&m2);
if(tangerine >= 5){tangerine-=5;}
printf("4#消费橘子,剩余橘子数量:%d\n",tangerine);
pthread_mutex_unlock(&m2);
sleep(2);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&m1,0);
pthread_mutex_init(&m2,0);
pthread_cond_init(&co1,0);
pthread_cond_init(&co2,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);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
pthread_join(id3,NULL);
pthread_join(id4,NULL);
pthread_cond_destroy(&co1);
pthread_cond_destroy(&co2);
pthread_mutex_destroy(&m1);
pthread_mutex_destroy(&m2);
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;
pthread_mutex_t m1;
pthread_cond_t co1,co2;
int apple=0;
int tangerine=0;
int flag;
void* task1(void* arg)
{
while(1)
{
pthread_mutex_lock(&m1);
if((apple+tangerine) == 0)
{
srand(time(0));
flag=rand()%2;
if(flag ==0 )
{
apple+=2;
printf("生产苹果,苹果数量:%d\n",apple);
}
else if(flag == 1)
{
tangerine+=4;
printf("生产橘子,橘子数量:%d\n",tangerine);
}
}
else
{
if(apple>10 || tangerine>20)
{
printf("数量超出,此次不生产\n");
}
else
{
if(apple>0)
{
apple+=2;
printf("生产苹果,苹果数量:%d\n",apple);
}
else if(tangerine>0)
{
tangerine+=4;
printf("生产橘子,橘子数量:%d\n",tangerine);
}
}
}
if(apple >=3)
{
pthread_cond_signal(&co1);
}
if(tangerine >= 5)
{
pthread_cond_signal(&co2);
}
pthread_mutex_unlock(&m1);
if(apple>0){sleep(1);}
else{sleep(2);};
}
}
void* task3(void* arg)
{
while(1)
{
pthread_mutex_lock(&m1);
pthread_cond_wait(&co1,&m1);
if(apple >= 3){apple-=3;}
printf("==========3#消费苹果,剩余苹果数量:%d\n",apple);
pthread_mutex_unlock(&m1);
sleep(1);
}
}
void* task4(void* arg)
{
while(1)
{
pthread_mutex_lock(&m1);
pthread_cond_wait(&co2,&m1);
if(tangerine >= 5){tangerine-=5;}
printf("==========4#消费橘子,剩余橘子数量:%d\n",tangerine);
pthread_mutex_unlock(&m1);
sleep(2);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&m1,0);
pthread_cond_init(&co1,0);
pthread_cond_init(&co2,0);
pthread_t id1,id3,id4;
pthread_create(&id1,0,task1,0);
pthread_create(&id3,0,task3,0);
pthread_create(&id4,0,task4,0);
pthread_join(id1,NULL);
pthread_join(id3,NULL);
pthread_join(id4,NULL);
pthread_cond_destroy(&co1);
pthread_cond_destroy(&co2);
pthread_mutex_destroy(&m1);
return 0;
}