1:使用互斥锁或者信号量,实现一个简单的生产者消费者模型 一个线程每秒生产3个苹果,另一个线程每秒消费8个苹果。
#include<myheadIO.h>
pthread_mutex_t mutex1;
pthread_mutex_t mutex2;
int apple=0;
void* fun1()
{
while(1)
{
pthread_mutex_lock(&mutex2);
pthread_mutex_unlock(&mutex1);
apple-=8;
printf("消费者消费了8个苹果,还剩%d个苹果\n",apple);
pthread_mutex_unlock(&mutex2);
if(apple<8)
{
pthread_mutex_lock(&mutex2);
}
sleep(1);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex1,0);
pthread_mutex_init(&mutex2,0);
pthread_mutex_lock(&mutex2);
pthread_t consumer;
pthread_create(&consumer,0,fun1,0);
pthread_detach(consumer);
while(1)
{
pthread_mutex_unlock(&mutex1);
apple+=3;
printf("生产者生产了3个苹果,共有%d个苹果\n",apple);
if(apple>=8)
{
pthread_mutex_unlock(&mutex2);
}
pthread_mutex_lock(&mutex1);
sleep(1);
}
return 0;
}
运行结果:
2:第二题: 有一个盘子,盘子里面最多放3个苹果,5个橘子 2个生产者线程,一个每秒放1个苹果,另一个每秒2个橘子 放了苹果就不能放橘子,放了橘子就不能放苹果 2个消费者线程,1号消费者线程每秒消费2个苹果,2号消费者线程,每秒消费3个橘子 解题思路: 苹果生产者和消费者之间,需要2组条件变量 橘子生产者和消费者之间,需要2组条件变量 苹果生产者和橘子生产者之间,需要一组互斥锁或者信号量。
#include<myheadIO.h>
#include<pthread.h>
int apple=0;
int orange=0;
pthread_mutex_t mutex_apple;
pthread_mutex_t mutex_orange;
pthread_mutex_t mutex_producer1;
pthread_mutex_t mutex_producer2;
pthread_cond_t cond_apple;
pthread_cond_t cond_orange;
void* fun1()
{
while(1)
{
pthread_mutex_lock(&mutex_apple);
pthread_cond_wait(&cond_apple,&mutex_apple);
apple-=2;
printf("1号消费者消费了2个苹果,还剩%d个苹果\n",apple);
pthread_mutex_unlock(&mutex_apple);
pthread_mutex_unlock(&mutex_producer1);
pthread_mutex_unlock(&mutex_producer2);
sleep(1);
}
}
void* fun2()
{
while(1)
{
pthread_mutex_lock(&mutex_orange);
pthread_cond_wait(&cond_orange,&mutex_orange);
orange-=3;
printf("2号消费者消费了3个橘子,还剩%d个橘子\n",orange);
pthread_mutex_unlock(&mutex_orange);
pthread_mutex_unlock(&mutex_producer1);
pthread_mutex_unlock(&mutex_producer2);
sleep(1);
}
}
void* fun3()
{
while(1)
{
pthread_mutex_lock(&mutex_producer2);
pthread_mutex_trylock(&mutex_producer1);
pthread_mutex_lock(&mutex_orange);
if(orange+2>5){
orange+=1;
printf("生产者放了1个橘子,还剩%d个橘子\n",orange);
}else{
orange+=2;
printf("生产者放了2个橘子,还剩%d个橘子\n",orange);
}
pthread_mutex_unlock(&mutex_orange);
if(apple>=3)
{
pthread_cond_signal(&cond_apple);
}
if(orange>=5)
{
pthread_cond_signal(&cond_orange);
}
pthread_mutex_unlock(&mutex_producer2);
sleep(1);
}
}
int main(int argc, const char *argv[])
{
pthread_mutex_init(&mutex_apple,0);
pthread_cond_init(&cond_apple,0);
pthread_mutex_init(&mutex_orange,0);
pthread_cond_init(&cond_orange,0);
pthread_mutex_init(&mutex_producer1,0);
pthread_mutex_init(&mutex_producer2,0);
pthread_t id1;
pthread_t id2;
pthread_t id3;
pthread_create(&id1,0,fun1,0);
pthread_create(&id2,0,fun2,0);
pthread_create(&id3,0,fun3,0);
while(1)
{
pthread_mutex_lock(&mutex_producer1);
pthread_mutex_trylock(&mutex_producer2);
pthread_mutex_lock(&mutex_apple);
apple+=1;
printf("生产者放了1个苹果,还剩%d个苹果\n",apple);
pthread_mutex_unlock(&mutex_apple);
pthread_mutex_unlock(&mutex_producer1);
if(apple>=3)
{
pthread_cond_signal(&cond_apple);
}
if(orange>=5)
{
pthread_cond_signal(&cond_orange);
}
sleep(1);
}
return 0;
}
测试结果:
静态库的封装: 将下列函数封装成静态库,并测试,描述该函数的功能
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; }
gcc -c XXX.c -o XXX.o ------------------> 第一步:生成二进制文件
ar cvr libXXX.a -o XXX.o ------------------> 第二步:生成静态库文件第三步可有可没有:将静态库文件放在usr/lib目录下
使用静态库:有第三步,可以直接在编译指令后面加"-lXXX"即可,若没有第三步,还要继续加"-L静态库文件所在目录"即可
函数功能:从获取一个字符,返回其ascall码值。