进程间同步-信号量

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

我的理解

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

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

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


信号量的使用

使用到的信号量函数有:

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也继续执行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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);
}
}

程序运行结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 北京 晴