Posix有名信号量、基于内存的信号量

/*************************************************************************
    > File Name: main.c
    > Author: ndj
    > Mail: [email protected] 
    > Created Time: Thu 23 Nov 2017 02:12:15 PM CST
 ************************************************************************/
/*有名信号量*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define NBUFF 10
#define SEM_MUTEX "/mutex6011111"
#define SEM_NEMPTY "/nempty6011111"
#define SEM_NSTORED "/nstored6011111"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int nitems;
struct 
{
    int buff[NBUFF];
    sem_t *mutex, *nempty, *nstored;
}shared;

void *produce(void *arg);
void *consume(void *arg);

int main(int argc, char **argv)
{
    pthread_t tid_produce, tid_consume;/*生产者线程号、消费者线程号*/

    if(argc != 2)
    {
        fprintf(stderr, "argc != 2\n");
        exit(EXIT_FAILURE);
    }
    nitems = atoi(argv[1]);
    /*创建三个信号量*/
    if((shared.mutex = sem_open(SEM_MUTEX, O_CREAT | O_EXCL, FILE_MODE, 1)) == SEM_FAILED)
    {
        printf("mutex failed\n");
        exit(EXIT_FAILURE);
    }
    if((shared.nempty = sem_open(SEM_NEMPTY, O_CREAT | O_EXCL, FILE_MODE, NBUFF)) == SEM_FAILED)
    {
        printf("nempty failed\n");
        exit(EXIT_FAILURE);
    }
    if((shared.nstored = sem_open(SEM_NSTORED, O_CREAT | O_EXCL, FILE_MODE, 0)) == SEM_FAILED)
    {
        printf("nstored failed\n");
        exit(EXIT_FAILURE);
    }
    printf("semaphore create success\n");
    /*创建一个生产者线程和一个消费者线程*/
    pthread_setconcurrency(2);/*设置并发优先级*/
    if(pthread_create(&tid_consume, NULL, consume, NULL) != 0)
    {
        printf("thread 2 failed\n");
        exit(EXIT_FAILURE);
    }
    if(pthread_create(&tid_produce, NULL, produce, NULL) != 0)
    {
        printf("thread 1 failed\n");
        exit(EXIT_FAILURE);
    }
/*  if(pthread_create(&tid_consume, NULL, consume, NULL) != 0)
    {
        printf("thread 2 failed\n");
        exit(EXIT_FAILURE);
    }*/


    /*等待两个线程结束*/
    pthread_join(tid_produce, NULL);
    pthread_join(tid_consume, NULL);
    /*删除信号量*/
    sem_unlink(SEM_MUTEX);
    sem_unlink(SEM_NEMPTY);
    sem_unlink(SEM_NSTORED);

    exit(0);
}

void* produce(void *arg)
{
    int i;

    printf("produce start\n");
    for(i=0; i/*等待缓冲区未满, 返回则将nempty信号量的值减1*/
        sem_wait(shared.mutex);/*先获取信号量,相当于加锁*/
        shared.buff[i % NBUFF] = i;/*添加数据*/
        sem_post(shared.mutex);/*相当于解锁*/
        sem_post(shared.nstored);
    }
    printf("produce end\n");
    return NULL;
}

void* consume(void *arg)
{
    int i;

    printf("consume start\n");
    for(i=0; i/*等待缓冲区为非空,返回则将nstored信号量的值减1*/
        //sem_wait(shared.mutex);
        if(shared.buff[i%NBUFF] == i)
            printf("buff[%d] = %d\n", i, shared.buff[i % NBUFF]);
        sem_post(shared.mutex);
        sem_post(shared.nempty);
    }
    printf("consume end\n");
    return NULL;
}
/*基于内存的信号量*/
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define NBUFF 10
struct
{
    int buff[NBUFF];
    sem_t mutex, nempty, nstored;
}shared;
int nitems;
void *produce(void *arg);
void *consume(void *arg);

int main(int argc, char **argv)
{
    pthread_t tid_produce, tid_consume;

    if(argc != 2)
    {
        fprintf(stderr, "argc != 2\n");
        exit(EXIT_FAILURE);
    }
    nitems = atoi(argv[1]);
    /*初始化信号量*/
    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(EXIT_SUCCESS);
}

void* produce(void *arg)
{
    int i;

    for(i=0; ireturn NULL;
}

void* consume(void *arg)
{
    int i;

    for(i=0; iif(shared.buff[i%NBUFF] == i)
            printf("buff[%d] = %d\n", i, shared.buff[i%NBUFF]);
        sem_post(&shared.mutex);
        sem_post(&shared.nempty);
    }
    return NULL;
}

你可能感兴趣的:(Linux系统编程)