Posix信号量

#include 
sem_t *sem_open(const char *name,int oflag,···);
int sem_close(sem_t *sem);
int sem_unlink(const char *name);
int sem_wait(sem_t *sem);
int sem_trywait(sem_t *sem);
int sem_post(sem_t *sem);
int sem_getvalue(sem_t *sem,int *valp);
//成功返回0,出错返回-1
int sem_init(sem_t *sem,int shared,unsigned int value);
int sem_destroy(sem_t *sem);

有名信号量:open() close() unlink()
无名信号量:init() destroy()
sem_init():shared为0时,信号量是在线程间共享的。shared非零时,是在进程间共享的,该信号量必须存放在某种类型的共享内存内。

例子
多个生产者,单个消费者

#include "unpipc.h"

#define NBUFF 10
#define MAXNTHREADS 100

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

void *produce(void *),*consume(void *);

int
main(int argc, char **argv)
{
        int i,count[MAXNTHREADS];
        pthread_t tid_produce[MAXNTHREADS],tid_consume;

        if(argc!=3)
                err_quit("usage: prodcons3 <#items> <#producers>");
        nitems=atoi(argv[1]);
        nproducers=min(atoi(argv[2]),MAXNTHREADS);

        sem_init(&shared.mutex,0,1);
        sem_init(&shared.nempty,0,NBUFF);
        sem_init(&shared.nstored,0,0);

        for(i=0;i=nitems){
                        sem_post(&shared.nempty);
                        sem_post(&shared.mutex);
                        return (NULL);
                }
                shared.buff[shared.nput%NBUFF]=shared.nputval;
                shared.nput++;
                shared.nputval++;
                sem_post(&shared.mutex);
                sem_post(&shared.nstored);
                *((int *)arg)+=1;
        }
}

void *
consume(void *arg)
{
        int i;
        for(i=0;i

多个缓冲区

#include "unpipc.h"

#define NBUFF 8

struct{
        struct{
                char data[BUFFSIZE];
                ssize_t n;
        }buff[NBUFF];
        sem_t mutex,nempty,nstored;
}shared;

int fd;
void *produce(void*),*consume(void*);

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

        if(argc!=2)
                err_quit("usage: mycat2 ");
        fd=open(argv[1],O_RDONLY);

        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 *arg)
{
        int i;
        for(i=0;;){
                sem_wait(&shared.nempty);
                sem_wait(&shared.mutex);
                sem_post(&shared.mutex);
                shared.buff[i].n=read(fd,shared.buff[i].data,BUFFSIZE);
                if(shared.buff[i].n==0){
                        sem_post(&shared.nstored);
                        return(NULL);
                }
                if(++i>=NBUFF)
                        i=0;
                sem_post(&shared.nstored);
        }
}

void *
consume(void *arg)
{
        int i;
        for(i=0;;){
                sem_wait(&shared.nstored);
                sem_wait(&shared.mutex);
                sem_post(&shared.mutex);
                if(shared.buff[i].n==0)
                        return(NULL);
                write(STDOUT_FILENO,shared.buff[i].data,shared.buff[i].n);
                if(++i>=NBUFF)
                        i=0;
                sem_post(&shared.nempty);
        }
}

你可能感兴趣的:(Posix信号量)