• c信号量demo


    1. semget创建/获取信号量

    2. semop操作信号量,pv

    3. semctl执行型号量命令,设置值、获取值、删除信号量等

    锁一般用于线程间通信,进程间通信可以用信号量。还有其他的共享内存,消息队列,socket,管道等

    模拟写了一个类似消费者/生产模式,消费者进程释放信号量,生产者进程获取信号量

    //
    // Created by gxf on 2020/2/23.
    //
    
    #ifndef UNTITLED_SEM_DEMO_H
    #define UNTITLED_SEM_DEMO_H
    
    extern char *ftokFilePath;
    extern int projId;
    
    extern int semId;
    
    int initSem();
    int semP();
    int semV();
    int delSem();
    
    #endif //UNTITLED_SEM_DEMO_H
    
    //
    // Created by gxf on 2020/2/23.
    //
    #include "sem-demo.h"
    #include <unistd.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int semId;
    char *ftokFilePath = "/Users/gxf/CLionProjects/untitled/tmp_sem_demo";
    int projId = 1;
    
    int initSem() {
        key_t  ftokRes = ftok(ftokFilePath, projId);
        // get sem
        semId = semget(ftokRes, 1, IPC_CREAT  | 0660);
        if (semId == -1) {
            perror("semget fail");
            return -1;
        }
        // init sem num 1
        union semun semunArg;
        semunArg.val = 1;
        int funRes = semctl(semId, 0, SETVAL, semunArg);
        if (funRes) {
            perror("semctl fail");
            return -1;
        }
        return 0;
    }
    
    int semP() {
        struct sembuf sembufVal;
        sembufVal.sem_num = 0;
        sembufVal.sem_op = -1;
        sembufVal.sem_flg = 0;
        int res = semop(semId, &sembufVal, 1);
        if (res) {
            perror("semop fail");
            return -1;
        }
        return 0;
    }
    
    int semV() {
        struct sembuf sembufVal;
        sembufVal.sem_op = 1;
        sembufVal.sem_num = 0;
        int res = semop(semId, &sembufVal, 1);
        if (res) {
            perror("semop fail");
            return -1;
        }
        return 0;
    }
    
    int delSem() {
        union semun semunVal;
        semId = 65544;
        int res = semctl(semId, 0, IPC_RMID, semunVal);
        if (res) {
            perror("semctl rmid fail");
            return -1;
        }
        return 1;
    }
    

      

    //
    // Created by gxf on 2020/2/23.
    //
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "sem-demo.h"
    #include <unistd.h>
    
    int main() {
        initSem();
        while (1) {
            if (semV()) {
                printf("comsumer v fail
    ");
            }
            printf("consumer
    ");
            sleep(1);
        }
    
        return 0;
    }
    

      

    //
    // Created by gxf on 2020/2/23.
    //
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    #include <unistd.h>
    #include "sem-demo.h"
    #include <pthread.h>
    
    void produce();
    
    int main() {
    //    delSem();
        initSem();
        pthread_t threads[10];
        for (int i = 0; i < 10; i++) {
            pthread_create(&threads[i], NULL, produce, NULL);
        }
    
        for (int i = 0; i < 10;i++) {
            pthread_join(threads[i], NULL);
        }
    
        return 0;
    }
    
    void produce() {
        while (1) {
            printf("producer tid:%d
    ", pthread_self());
            semP();
    //        sleep(3);
        }
    }

      

  • 相关阅读:
    父级div height:auto无效解决办法
    梦幻西游摇钱树苗怎么种
    检测QQ是否在线或启用
    ASP.NET MVC3 Model验证总结 @Html.ValidationSummary(true)
    cookie与session用法
    《梦幻西游》手游人宠抗防修炼点修消耗表
    Windows 8/8.1 Key_Windows 8.1|Windows 8|Windows 7|Windows Vista|Office 2013|Office 2010|Win Server 2012|Win Server 2008|Vista|各版
    My97DatePicker控件显示时分秒
    jQuery日期选择插件WdatePicker使用方法.日期范围限制
    DropDownList赋值,绑定,传值
  • 原文地址:https://www.cnblogs.com/luckygxf/p/12353911.html
Copyright © 2020-2023  润新知