IO、进程、线程06

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

#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 apple = 0;
pthread_mutex_t m;
pthread_cond_t c;                                                                                                                                         

void * run(void * arg)
{
    while(1){
        pthread_mutex_lock(&m);
        pthread_cond_wait(&c,&m);
        apple-=8;
        printf("消费者消费了8个苹果,现在苹果数量为:%d\n",apple);
        pthread_mutex_unlock(&m);
        sleep(3);
    }
}
int main(int argc, const char *argv[])
{
    pthread_mutex_init(&m,0);
    pthread_cond_init(&c,0);

    pthread_t id;
    pthread_create(&id,0,run,0);
    pthread_detach(id);

    while(1)
    {
        pthread_mutex_lock(&m);
        apple+=3;
        printf("生产者生产了3个苹果,当前苹果的数量为:%d\n",apple);
        if(apple>=8){
            pthread_cond_signal(&c);
        }
        pthread_mutex_unlock(&m);
        sleep(1);
    }
    return 0;
}
                                                                                                                                                          
                                                                                                                                                          
                                                                                                                                                      

2:第二题: 有一个盘子,盘子里面最多放3个苹果,5个橘子 2个生产者线程,一个每秒放1个苹果,另一个每秒2个橘子 放了苹果就不能放橘子,放了橘子就不能放苹果 2个消费者线程,1号消费者线程每秒消费2个苹果,2号消费者线程,每秒消费3个橘子 (解题思路: 苹果生产者和消费者之间,需要2组条件变量 橘子生产者和消费者之间,需要2组条件变量 苹果生产者和橘子生产者之间,需要一组互斥锁或者信号量 )

#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_detach(id1);
	pthread_detach(id2);
	pthread_detach(id3);
	pthread_detach(id4);
	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值

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值