设为首页 加入收藏

TOP

Linux 信号量之Posix基于内存的信号量(一)
2019-06-22 20:05:59 】 浏览:191
Tags:Linux 信号 Posix 基于 内存

信号量(semaphore),也和互斥锁一样提供了线程间或者进程间的同步功能。

信号量有三种:

信号量比互斥锁高级,互斥锁只允许一个线程访问临界区,信号量可以多个,可以把信号量看作成互斥锁的升级版,但是如果能用互斥锁解决,就用互斥锁,互斥锁比信号量节省资源。

这篇文章只介绍Posix基于内存的信号量

1,单个生产者和单个消费者

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>       
#include <sys/stat.h>    
#include <semaphore.h>

#define NBUFF 10

int nitems;
struct {
  int buff[NBUFF];
  sem_t mutex, nempty, nstored;
} shared;

void* produce(void *args);
void* consume(void* args);

int main(int argc, char** argv){

  pthread_t tid_produce, tid_consume;

  if(argc != 2){
    printf("usage error\n");
    exit(1);
  }

  nitems = atoi(argv[1]);

  //create 3 semaphore
  sem_init(&shared.mutex, 0, 1);
  sem_init(&shared.nempty, 0, NBUFF);
  sem_init(&shared.nstored, 0, 0);

  pthread_create(&tid_produce, NULL, produce, NULL);
  pthread_create(&tid_consume, NULL, consume, NULL);

  pthread_join(tid_produce, NULL);
  pthread_join(tid_consume, NULL);

  sem_destroy(&shared.mutex);
  sem_destroy(&shared.nempty);
  sem_destroy(&shared.nstored);
  exit(0);
}

void* produce(void *args){
  int i;
  for(i = 0; i < nitems; ++i){
    sem_wait(&shared.nempty);
    sem_wait(&shared.mutex);
    shared.buff[i % NBUFF] = i;
    sem_post(&shared.mutex);
    sem_post(&shared.nstored);
  }

  return NULL;
}

void* consume(void* args){
  int i;
  for(i = 0; i < nitems; ++i){
    sem_wait(&shared.nstored);
    sem_wait(&shared.mutex);
    shared.buff[i % NBUFF] = i;
    sem_post(&shared.mutex);
    sem_post(&shared.nempty);
  }

  return NULL;
}

2,多个生产者和单个消费者

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>       
#include <sys/stat.h>    
#include <semaphore.h>

#define NBUFF 10
#define MAXTHRS 100
#define min(x,y) ( x > y ? y:x )

int nitems, nproducers;

struct {
  int buff[NBUFF];
  int idx;
  int val;
  sem_t mutex, nempty, nstored;
} shared;

void* produce(void *args);
void* consume(void* args);

int main(int argc, char** argv){

  int i, count[MAXTHRS];
  pthread_t tid_produce[MAXTHRS], tid_consume;

  if(argc != 3){
    printf("usage error\n");
    exit(1);
  }

  nitems = atoi(argv[1]);
  nproducers = min(atoi(argv[2]), MAXTHRS);

  //create 3 semaphore
  sem_init(&shared.mutex, 0, 1);
  sem_init(&shared.nempty, 0, NBUFF);
  sem_init(&shared.nstored, 0, 0);

  for(i = 0; i < nproducers; ++i){
    count[i] = 0;
    pthread_create(&tid_produce[i], NULL, produce, &count[i]);
  }
  pthread_create(&tid_consume, NULL, consume, NULL);

  for(i = 0; i < nproducers; ++i){
    pthread_join(tid_produce[i], NULL);
    printf("count[%d] = %d\n", i, count[i]);
  }
  pthread_join(tid_consume, NULL);

  sem_destroy(&shared.mutex);
  sem_destroy(&shared.nempty);
  sem_destroy(&shared.nstored);
  exit(0);
}

void* produce(void *arg){
  int i;
  for(i = 0; i < nitems; ++i){
    sem_wait(&shared.nempty);
    sem_wait(&shared.mutex);

    if(shared.idx >= nitems){
      sem_post(&shared.nempty);//注意点
      sem_post(&shared.mutex);
      return NULL;// all done
    }
    
    shared.buff[shared.idx % NBUFF] = shared.val;
    shared.idx++;
    shared.val++;
    sem_post(&shared.mutex);
    sem_post(&shared.nstored);
    *((in
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C的结构体函数 下一篇C语言----循环结构1(基础篇五)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目