b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

Linux下的线程间通信-信号量

电脑杂谈  发布时间:2020-07-12 04:12:45  来源:网络整理

java线程间通信_线程间通信锁_线程间通信模型

1. 生产者-消费者模型

使用的功能:

sem_init()初始化

sem_destroy()破坏

线程间通信锁_java线程间通信_线程间通信模型

sem_wait()应用程序,资源消耗(当应用程序不可用时挂起)

sem_post()生产资源

单一生产者-单一消费者:

/*************************************************************************
 > File Name: my_senc.c
 > Author: HonestFox
 > Mail: zhweizhi@foxmail.com 
 > Created Time: Mon 18 Jul 2016 09:04:20 PM CST
 ************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t space_nums;
sem_t data_nums;
#define _SIZE 5
int buf[_SIZE];
void *consumer(void *arg)
{
 int index = 0;
 while(1)
 {
  sleep(1);
  sem_wait(&data_nums);
  int data = buf[index];
  printf("consumer done... data is : %d  index is : %d\n", data, index);
  sem_post(&space_nums);
  index++;
  index %= _SIZE;
 }
}
void *productor(void *arg)
{
 int index = 0;
 while(1)
 {
  sem_wait(&space_nums);
  buf[index] = rand() % 1234;
  printf("productor done ... data is : %d   index is : %d\n", buf[index], index);
  sem_post(&data_nums);
  ++index;
  index %= _SIZE;
 }
}
int main()
{
 sem_init(&space_nums, 0, _SIZE);
 sem_init(&data_nums, 0, 0);
 pthread_t id1, id2;
 
 pthread_create(&id1, NULL, &consumer, NULL);
 pthread_create(&id2, NULL, &productor, NULL);
 pthread_join(id1, NULL);
 pthread_join(id2, NULL);
 sem_destroy(&space_nums);
 sem_destroy(&data_nums);
 return 0;
}

线程间通信锁_java线程间通信_线程间通信模型

运行结果:

wKioL1eM8kPAr5zWAACRy7kec7E057.png

多生产者多消费者

如果对主函数的内容进行少量修改,它将变成1个生产者和2个消费者

线程间通信锁_java线程间通信_线程间通信模型

wKiom1eM8tiQnVGEAAEWMArVCP0492.png

可以看出,两个生产者发生冲突. 消费者最终阅读了生产者写的内容,后来写了.

因此,我们应该进行一些限制,以便只有一个生产者可以进入关键区域并同时访问关键资源. 具体方法是添加“锁”

类似地线程间通信模型,对于生产者,应该添加一个“锁”以确保只有一个生产者可以同时生产(即线程间通信模型,确保生产过程是原子的)

java线程间通信_线程间通信锁_线程间通信模型

添加锁后,操作结果如下,确实是正确的

完整代码如下

/*************************************************************************
 > File Name: my_senc.c
 > Author: HonestFox
 > Mail: zhweizhi@foxmail.com 
 > Created Time: Mon 18 Jul 2016 09:04:20 PM CST
 ************************************************************************/
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t space_nums;
sem_t data_nums;
pthread_mutex_t consuming = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t producting= PTHREAD_MUTEX_INITIALIZER;
#define _SIZE 6
int buf[_SIZE];
void *consumer(void *arg)
{
 int index = 0;
 while(1)
 {
  sleep(1);
  sem_wait(&data_nums);
  pthread_mutex_lock(&consuming);
  int data = buf[index];
  printf("NO.%ud consumer done... data is : %d, index = %d\n", pthread_self(), data, index);
  sem_post(&space_nums);
  index++;
  index %= _SIZE;
  pthread_mutex_unlock(&consuming);
 }
 printf("\n");
}
void *productor(void *arg)
{
 int index = 0;
 while(1)
 {
  pthread_mutex_lock(&producting);
  sem_wait(&space_nums);
  buf[index] = rand() % 1234;
  printf("         ^ No.%ud productor done ... data is : %d, index = %d\n", pthread_self(), buf[index], index);
  sem_post(&data_nums);
  ++index;
  index %= _SIZE;
  pthread_mutex_unlock(&producting);
 }
}
int main()
{
 sem_init(&space_nums, 0, _SIZE);
 sem_init(&data_nums, 0, 0);
 pthread_mutex_init(&consuming, NULL);
 pthread_mutex_init(&producting, NULL);
 
// 4 consumer, 2 productor 
 pthread_t p_id1, p_id2, p_id3, p_id4;
 pthread_t c_id1, c_id2;
 pthread_create(&p_id1, NULL, &consumer, NULL);
// pthread_create(&p_id2, NULL, &consumer, NULL);
// pthread_create(&p_id3, NULL, &consumer, NULL);
// pthread_create(&p_id4, NULL, &consumer, NULL);
 pthread_create(&c_id1, NULL, &productor, NULL);
 pthread_create(&c_id2, NULL, &productor, NULL);
//
 pthread_join(p_id1, NULL);
// pthread_join(p_id2, NULL);
// pthread_join(p_id3, NULL);
// pthread_join(p_id4, NULL);
 pthread_join(c_id1, NULL);
// pthread_join(c_id2, NULL);
 sem_destroy(&space_nums);
 sem_destroy(&data_nums);
 return 0;
}

2. 读写器模型


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-277237-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...