线程互斥锁(信号量)的相关使用及静态库的封装

思维导图:

1.使用互斥锁或者信号量,实现一个简单的生产者消费者模型 一个线程每秒生产3个苹果,另一个线程每秒消费8个苹果

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.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_mutex_t m2;
int apple=0;
void *run(void *arg)
{
	while(1)
	{
		pthread_mutex_lock(&m1);
		apple-=8;
		printf("消费者消费了8个苹果,当前苹果数量为:%d\n",apple);
		sleep(1);
	}
}
int main(int argc, const char *argv[])
{
	//使用互斥锁或者信号量,实现一个简单的生产者消费者模型
	//一个线程每秒生产3个苹果,另一个线程每秒消费8个苹果
	pthread_mutex_init(&m1,0);
	pthread_mutex_init(&m2,0);
	pthread_t id;
	pthread_create(&id,0,run,0);
	pthread_detach(id);
	pthread_mutex_lock(&m1);
	while(1)
	{
		pthread_mutex_lock(&m2);
		apple+=3;
		printf("生产者生产了3个苹果,当前苹果数量为:%d\n",apple);
		if(apple>=8)
		{
			pthread_mutex_unlock(&m1);//当苹果的数量达到一定数量时,才给消费者解锁
		}
		pthread_mutex_unlock(&m2);
		sleep(1);
	}
	return 0;
}

2.有一个盘子,盘子里面最多放3个苹果,5个橘子 2个生产者线程,一个每秒放1个苹果,另一个每秒2个橘子 放了苹果就不能放橘子,放了橘子就不能放苹果 2个消费者线程,1号消费者线程每秒消费2个苹果,2号消费者线程,每秒消费3个橘子

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.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_mutex_t m2;
pthread_mutex_t m3;
pthread_mutex_t m4;
pthread_cond_t c1;
pthread_cond_t c2;
int apple=0;
int orange=0;
void *run1(void *arg)//1号苹果消费线程
{
	while(1)
	{
		pthread_mutex_lock(&m1);
		pthread_cond_wait(&c1,&m1);
		apple-=2;
		printf("消费了2个苹果,盘子里还有%d个苹果,还有%d个橘子\n",apple,orange);
		pthread_mutex_unlock(&m1);
		sleep(1);
	}
}
void *run2(void *arg)//2号橘子消费线程
{
	while(1)
	{
		pthread_mutex_lock(&m2);
		pthread_cond_wait(&c2,&m2);
		orange-=3;
		printf("消费了3个橘子,盘子里还有%d个苹果,还有%d个橘子\n",apple,orange);
		pthread_mutex_unlock(&m2);
		sleep(1);
	}
}
void *run3(void *arg)//1号苹果生产线程
{
	while(1)
	{
		pthread_mutex_lock(&m3);
		if(apple <3)
		{
			apple+=1;
			printf("盘子里放入了1个苹果,当前苹果数量为:%d,当前橘子数量为:%d\n",apple,orange);
		}
		if(apple >=2 && apple <=3)
		{
			pthread_cond_signal(&c1);
		}
		pthread_mutex_unlock(&m3);
		sleep(1);
	}
}
void *run4(void *arg)//2号橘子生产线程
{
	while(1)
	{
		pthread_mutex_lock(&m4);
		if(orange <4)
		{
			orange+=2;
			printf("盘子里放入了2个橘子,当前苹果数量为:%d,当前橘子数量为:%d\n",apple,orange);
		}
		if(orange >= 3 && orange <=5)
		{
			pthread_cond_signal(&c2);
		}
		pthread_mutex_unlock(&m4);
		sleep(1);
	}
}
int main(int argc, const char *argv[])
{
	pthread_mutex_init(&m1,0);
	pthread_mutex_init(&m2,0);
	pthread_mutex_init(&m3,0);
	pthread_mutex_init(&m4,0);
	pthread_cond_init(&c1,0);
	pthread_cond_init(&c2,0);
	pthread_t id1,id2,id3,id4;
	pthread_create(&id1,0,run1,0);
	pthread_create(&id2,0,run2,0);
	pthread_create(&id3,0,run3,0);
	pthread_create(&id4,0,run4,0);
	pthread_join(id1,0);
	pthread_join(id2,0);
	pthread_join(id3,0);
	pthread_join(id4,0);
	return 0;
}

3.#include <termios.h>

#include <unistd.h>

#include <sys/types.h>

#include <stdio.h>

#include <assert.h>

int getch(){

int c=0;

struct termios org_opts, new_opts;

int res=0;

res=tcgetattr(STDIN_FILENO, &org_opts);

assert(res==0);

new_opts = org_opts;

new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);

tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);

c=getchar();

res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);

assert(res==0);

return c;

//将上述函数封装成静态库,并测试,描述该函数的功能

}

函数的作用:输出终端输入对应字符的ASCII码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值