Linux内核同步机制

Carlos

信号量例子,纯信号量

#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <errno.h>

sem_t remain, apple, pear, mutex;
static unsigned int vremain = 20, vapple = 0, vpear = 0;

void *father(void *);
void *mother(void *);
void *son(void *);
void *daughter(void *);
void print_sem();


int main()
{
    pthread_t fa, ma, so, da;
    sem_init(&remain, 0 , 20);
    sem_init(&apple, 0, 0);
    sem_init(&pear, 0, 0);
    sem_init(&mutex, 0, 1);
    pthread_create(&fa, NULL, &father, NULL);
    pthread_create(&ma, NULL, &mother, NULL);
    pthread_create(&so, NULL, &son, NULL);
    pthread_create(&da, NULL, &daughter, NULL);

    while(1) {
        sem_wait(&mutex);
        printf("apple: %u, pear: %u, left: %u\n", vapple, vpear, vremain);
        sem_post(&mutex);
        sleep(1);
    }
}

void *father(void *arg)
{
    while(1) {
        sem_wait(&remain);
        sem_wait(&mutex);
        printf("father: put before, left = %u, apple = %u\n", vremain, vapple);
        vremain --;
        vapple ++;
        printf("father: put after, left = %u, apple = %u\n\n", vremain, vapple);
        sem_post(&mutex);
        sem_post(&apple);
        sleep(1);
    }
}

void *son(void *arg)
{
    while(1) {
        sem_wait(&apple);
        sem_wait(&mutex);
        printf("son: take before, left = %u, apple = %u\n", vremain, vapple);
        vremain ++;
        vapple --;
        printf("son: put after, left = %u, apple = %u\n\n", vremain, vapple);
        sem_post(&mutex);
        sem_post(&remain);
        sleep(3);
    }
}

void *mother(void *arg)
{
    while(1) {
        sem_wait(&remain);
        sem_wait(&mutex);
        printf("mother: put before, left = %u, pear = %u\n", vremain, vpear);
        vremain --;
        vpear ++;
        printf("mother: put after, left = %u, pear = %u\n\n", vremain, vpear);
        sem_post(&mutex);
        sem_post(&pear);
        sleep(3);
    }
}

void *daughter(void *arg)
{
    while(1) {
        sem_wait(&pear);
        sem_wait(&mutex);
        printf("dau: take before, left = %u, pear = %u\n", vremain, vpear);
        vremain ++;
        vpear --;
        printf("dau: take after, left = %u, pear = %u\n\n", vremain, vpear);
        sem_post(&mutex);
        sem_post(&remain);
        sleep(1);
    }
}

信号量和互斥锁(副线程 阻塞),非阻塞mainloop

#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <pthread.h>
#include <errno.h>

sem_t remain, apple, pear, mutex;
static unsigned int vremain = 20, vapple = 0, vpear = 0;
pthread_mutex_t lock;

void *father(void *);
void *mother(void *);
void *son(void *);
void *daughter(void *);
void print_sem();


int main()
{
    int ret = 0;
    pthread_t fa, ma, so, da;
    sem_init(&remain, 0 , 20);
    sem_init(&apple, 0, 0);
    sem_init(&pear, 0, 0);
    sem_init(&mutex, 0, 1);
    pthread_mutex_init(&lock, NULL);
    pthread_create(&fa, NULL, &father, NULL);
    pthread_create(&ma, NULL, &mother, NULL);
    pthread_create(&so, NULL, &son, NULL);
    pthread_create(&da, NULL, &daughter, NULL);

    while(1) {
        //sem_wait(&mutex);
        ret = pthread_mutex_trylock(&lock);
        if (ret != EBUSY) {
            printf("mainloop : apple: %u, pear: %u, left: %u\n\n", vapple, vpear, vremain);
            pthread_mutex_unlock(&lock);
        } else {
            printf("mainloop : lock is busy...\n\n");
        }
        //sem_post(&mutex);
        usleep(1000000);
    }
}

void *father(void *arg)
{
    while(1) {
        sem_wait(&remain);
        pthread_mutex_lock(&lock);
        //sem_wait(&mutex);
        printf("father: put before, left = %u, apple = %u\n", vremain, vapple);
        vremain --;
        vapple ++;
        printf("father: put after, left = %u, apple = %u\n\n", vremain, vapple);
        pthread_mutex_unlock(&lock);
        //sem_post(&mutex);
        sem_post(&apple);
        sleep(1);
    }
}

void *son(void *arg)
{
    while(1) {
        sem_wait(&apple);
        pthread_mutex_lock(&lock);
        //sem_wait(&mutex);
        printf("son: take before, left = %u, apple = %u\n", vremain, vapple);
        vremain ++;
        vapple --;
        printf("son: put after, left = %u, apple = %u\n\n", vremain, vapple);
        sleep(1);
        pthread_mutex_unlock(&lock);
        //sem_post(&mutex);
        sem_post(&remain);
    }
}

void *mother(void *arg)
{
    while(1) {
        sem_wait(&remain);
        //sem_wait(&mutex);
        pthread_mutex_lock(&lock);
        printf("mother: put before, left = %u, pear = %u\n", vremain, vpear);
        vremain --;
        vpear ++;
        printf("mother: put after, left = %u, pear = %u\n\n", vremain, vpear);
        pthread_mutex_unlock(&lock);
        //sem_post(&mutex);
        sem_post(&pear);
        sleep(3);
    }
}

void *daughter(void *arg)
{
    int ret = 0;
    while(1) {
        sem_wait(&pear);
        //sem_wait(&mutex);
        pthread_mutex_lock(&lock);
        printf("dau: take before, left = %u, pear = %u\n", vremain, vpear);
        vremain ++;
        vpear --;
        printf("dau: take after, left = %u, pear = %u\n\n", vremain, vpear);
        pthread_mutex_unlock(&lock);
        //sem_post(&mutex);
        sem_post(&remain);
        sleep(1);
    }
}
阅读 365

carlos
嵌入式 \ 半导体 \ Linux \ ARM \ DSP \ Security

嵌入式 \ Linux \ ARM \ Security

26 声望
8 粉丝
0 条评论

嵌入式 \ Linux \ ARM \ Security

26 声望
8 粉丝
文章目录
宣传栏