linux信号量例子

 semaphore.h 提供的是 POSIX 标准定义的 semaphore 接口,而 sys/sem.h 里 提供的是符合 System V 标准的 semaphore接口 (semget, semop, ...),这些接口都比较老了, linux提供主要是为了兼容老代码

/** @file  main628.cpp
*  @note   System Technology Co., Ltd. All Right Reserved.
*  @brief
*  @author 
*  @date   2019-6-28
*  @note   v1.0.0 Created
*  @history
*  @warning
*/
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <semaphore.h>

#define NUM 5
int queue[NUM];
sem_t blank_number, product_number;

void *producer(void *arg)
{
    int p = 0;
    while (1) {
        sem_wait(&blank_number);
        queue[p] = rand() % 1000 + 1;
        printf("Produce %d\n", queue[p]);
        sem_post(&product_number);
        p = (p+1)%NUM;
        sleep(rand()%5);
    }
}

void *consumer(void *arg)
{
    int c = 0;
    while (1) {
        sem_wait(&product_number);
        printf("Consume %d\n", queue[c]);
        queue[c] = 0;
        sem_post(&blank_number);
        c = (c+1)%NUM;
        sleep(rand()%5);
    }
}

int main(int argc, char *argv[])
{
    pthread_t pid, cid;
    sem_init(&blank_number, 0, NUM);
    sem_init(&product_number, 0, 0);
    pthread_create(&pid, NULL, producer, NULL);
    pthread_create(&cid, NULL, consumer, NULL);
    pthread_join(pid, NULL);
    pthread_join(cid, NULL);
    sem_destroy(&blank_number);
    sem_destroy(&product_number);
    return 0;
}

 

/*!
* Email: xmain@gmail.com
* Auth: c
* Date: 2019-6-30
* File: semaphoreTest.cpp
* Class: %{Cpp:License:ClassName} (if applicable)
* Brief:
* Note:
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>

sem_t bin_sem;
void *thread_function1(void *arg)
{
    printf("thread_function1--------------sem_wait\n");
    sem_wait(&bin_sem);
    printf("sem_wait\n");
    while (1)
    {
        printf("thread_function1\n");
    }
}

void *thread_function2(void *arg)
{
    printf("thread_function2--------------sem_post\n");
    sem_post(&bin_sem);
    printf("sem_post\n");
    while (1)
    {
        printf("thread_function2\n");
    }
}



int main()
{
    int res;
    pthread_t a_thread, a_thread2;
    void *thread_result;

    res = sem_init(&bin_sem, 0, 0);
    if (res != 0)
    {
        perror("Semaphore initialization failed");
    }
    printf("sem_init\n");
    res = pthread_create(&a_thread, NULL, thread_function1, NULL);
    if (res != 0)
    {
        perror("Thread creation failure");
    }
    //printf("thread_function1\n");
    sleep (5);
    printf("sleep\n");
    res = pthread_create(&a_thread2, NULL, thread_function2, NULL);
    if (res != 0)
    {
        perror("Thread creation failure");
    }
    while (1)
    {
        printf("main\n");
    }
}

 

 

#include <iostream>
#include<stdio.h>
#include <pthread.h>
#include<stdlib.h>
#include <string.h>
#include <semaphore.h>
using namespace std;
//用户从终端输入任意字符然后统计个数显示,输入end则结束
//使用多线程实现:主线程获取用户输入并判断是否退出,子线程计数
char buf[100]={0};
static int flag = 0;
sem_t sem;
// 子线程程序,作用是统计buf中的字符个数并打印
void *func(void*arg)
{
    // 子线程首先应该有个循环
    // 循环中阻塞在等待主线程激活的时候,子线程被激活后就去获取buf中的字符
    // 长度,然后打印;完成后再次被阻塞
    printf("func before 1th wait\n");
    sem_wait(&sem);
    printf("func after 1th wait\n");
    while(flag==0)
    {

        printf("长度为:%d.\n",strlen(buf));
        memset(buf, 0, sizeof(buf));
        printf("func before 2th wait\n");
        sem_wait(&sem);
        printf("func after 2th wait\n");
    }

    pthread_exit(NULL);
}


int main(void)
{
    int ret=-1;
    pthread_t th;


    sem_init(&sem,0,0);

    ret=pthread_create(&th,NULL,func,NULL);
    if (ret != 0)
    {
        printf("pthread_create error.\n");
        return -1;
    }

    printf("输入一个字符串,以回车结束.\n");
    while(scanf("%s",buf))
    {
        // 去比较用户输入的是不是end,如果是则退出,如果不是则继续
        if(!strncmp(buf,"end",3))
        {
            printf("输入的字符串为:%s",buf);
            flag=1;
            printf("main before 1th wait\n");
            sem_post(&sem);
            printf("main after 1th wait\n");
            break;
        }
        // 主线程在收到用户收入的字符串,并且确认不是end后
        // 就去发信号激活子线程来计数。
        // 子线程被阻塞,主线程可以激活,这就是线程的同步问题。
        // 信号量就可以用来实现这个线程同步
        printf("main before 2th wait\n");
        sem_post(&sem);
        printf("main after 2th wait\n");
    }

    // 回收子线程
    printf("等待回收子线程\n");
    ret = pthread_join(th, NULL);
    if (ret != 0)
    {
        printf("pthread_join error.\n");
        exit(-1);
    }
    printf("子线程回收成功\n");
    sem_destroy(&sem);
    return 0;
}

 

转载于:https://www.cnblogs.com/guxuanqing/p/10780150.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值