美文网首页C/C++踩坑
进程间同步-信号量

进程间同步-信号量

作者: 火影启源 | 来源:发表于2019-07-17 20:56 被阅读0次

同步博客:My Love

最近项目中看到有的代码中使用到了信号量,想到之前数据库操作时多个线程同时写数据库造成的程序异常,加上信号量就可以解决异常了。

我的理解

信号量是进程间或线程间同步的一种方式,这是与锁有区别的,锁是限制进程或线程访问相同的资源,某一时刻该资源只能由一个进程或线程访问,但是信号量可以指定一个或多个进程或线程同时执行某一个操作,用于进程间的同步。

另外需要注意的是进程间通信和线程间通信信号量的使用是不同的,这里主要介绍线程间通信时信号量的使用方法。

这里有一篇介绍信号量的文章,介绍得比较仔细,有兴趣可以看一下。我这里重点说一下我的理解。


信号量的使用

使用到的信号量函数有:

1,int sem_init(sem_t *sem, int pshared, unsigned int value);
初始化信号量sem_t,初始化的时候可以指定信号量的初始值(value),以及是否可以在多进程间共享(pshare为0为进程内部共享,否则进程间共享)。程序执行成功返回0,失败返回-1.

2,int sem_wait(sem_t *sem);
如果信号量的值大于0,该放回会将信号量的值减1,如果信号量的值小于等于0,那该函数就会阻塞,直到信号量的值大于等于1为止。
成功返回0,失败返回-1.

3,int sem_post(sem_t *sem);
该函数用于将信号量的值加1,表示其他进程的sem_wait函数可以正常执行了。
成功返回0,失败返回-1。

4,int sem_destroy(sem_t *sem);
对于使用完的信号量进行清理的函数,成功返回0,失败返回-1.

5,int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
与sem_wait的区别在于这个函数有等待时间限制,超出等待时间函数直接返回ETIMEDOUT错误。

实例

创建两个线程thread_function1和thread_function2,先让thread_function1执行起来,然后sleep一会儿,让thread_function2线程启动起来,在thread_function1中调用sem_wait等待信号量的值被加为1(因为信号量初始化时被赋值为0)。
在thread_function2中sem_post将信号量加1,这时thread_function1的sem_wait通过,然后thread_function1继续执行,thread_function2也继续执行。

#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("th1 running!\n");  
        sleep(1);  
    }  
}  
  
void *thread_function2(void *arg)  
{  
    printf("thread_function2--------------sem_post\n");  
    sem_post(&bin_sem);  
    printf("sem_post\n");  
    while (1)  
    {  
        printf("th2 running!\n");  
        sleep(1);  
    }  
}  
int main()  
{  
     int res;  
     pthread_t a_thread;  
     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_thread, NULL, thread_function2, NULL);  
     if (res != 0)  
     {  
      perror("Thread creation failure");  
     }  
     while (1)  
     {  
       printf("running !\n");  
       sleep(5);  
     }  
}  

程序运行结果如下:

sem_init
thread_function1
thread_function1--------------sem_wait
sleep
running !
thread_function2--------------sem_post
sem_wait
th1 running!
sem_post
th2 running!
th1 running!
th2 running!
th1 running!
th2 running!
th1 running!
th2 running!
^C

可以看到,当th1在等待的时候,当th2执行sem_post后,th1立马就接收到了这个信号量的值,然后开始执行th1下面的内容,
等到th1执行到sleep时,这时th2才开始从sem_post下面的语句开始执行,这说明sem_wait对信号量的捕获是很迅速的,只要
一满足条件立马就可以返回。

自己体会一下……

2018.5.22 北京 晴

相关文章

  • 共享内存同步机制

    1. 使用posix有名信号量进行同步 有名信号量既可用于线程间的同步,又可用于进程间的同步。 两个进程,对同一个...

  • 二.进程(3)进程同步

    2.3 进程同步 1. 理解同步的含义 2. 信号量机制控制进程同步 3. 管程 进程间有什么相互影响? 两种制约...

  • 进程间同步-信号量

    同步博客:My Love 最近项目中看到有的代码中使用到了信号量,想到之前数据库操作时多个线程同时写数据库造成的程...

  • 进程相关问题学习

    写在前面,进程同步控制有锁,信号量,事件进程间通信有 队列和管道 信号量相当于多个锁 事件控制进程的动作 红绿灯问...

  • Linux系统编程—进程间同步

    我们知道,线程间同步有多种方式,比如:信号量、互斥量、读写锁,等等。那进程间如何实现同步呢?本文介绍两种方式:互斥...

  • Linux下的信号量使用

    信号量 信号量原语 进程同步的主要方式之一。具体概念参考[《《操作系统概念精要》基本概念整理之进程同步篇(二)》]...

  • linux进程or线程安全之信号量sem_init

    1、信号量定义 linux sem信号量是一种特殊的变量,访问具有原子性,用于解决进程或者线程间共享资源引发的同步...

  • PV原语

    概念 PV原语通过操作信号量来完成进程间的同步和互斥。信号量由操作系统来维护 原语 P原语 为阻塞原语,负责把当前...

  • 第二章 进程通信、线程

    进程通信:进程通信是指进程之间的信息交换。 一、低级通信——进程之间的互斥和同步 信号量机制是有效的同步工具,但...

  • 理解操作系统之信号量的应用

    利用信号量实现进程互斥(模式) 利用信号量实现前驱关系(模式) 利用记录型信号量实现同步(模式) 1.利用信号量实...

网友评论

    本文标题:进程间同步-信号量

    本文链接:https://www.haomeiwen.com/subject/xpthlctx.html