信号量 sem_t 进程同步
sem_t分为有名和无名。有名的sem_t通过sem_open来创建, 而无名的sem_t通过sem_init的初始化。 用有名的sem_t来进程间同步是件很容易的事情,百度上一搜很多想相关的例子。
有名和无名的sem_t主要区别:
1. 效率:有名sem_t是放在文件,无名的sem_t是放在内存。
2.限制:有名的sem_t可以用来同步多线程,任意多进程。而无名的sem_t可以用来同步多线程,以及Fork出来的进程间的同步。
网上想关的例子很多,本文主要是测试一下用无名sem_t进程同步,比如你在使用nginx的时候,nginx会fork出很多works,如果在works间你希望能同步一些操作,那么这个时候就可以用它,注意下面API描述中的红色部分,明确说了需要放到共享内存(shared memory)。
Name
sem_init - initialize an unnamed semaphore
Synopsis #include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
Link with -pthread.
Description sem_init() initializes the unnamed semaphore at the address pointed to by sem. The value argument specifies the initial value for the semaphore.
The pshared argument indicates whether this semaphore is to be shared between the threads of a process, or between processes. If pshared has the value 0, then the semaphore is shared between the threads of a process, and should be located at some address that is visible to all threads (e.g., a global variable, or a variable allocated dynamically on the heap).
If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3), mmap(2), and shmget(2)). (Since a child created by fork(2) inherits its parent's memory mappings, it can also access the semaphore.) Any process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), etc. Initializing a semaphore that has already been initialized results in undefined behavior.
下面是一段简单的测试代码
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h> void *createSharedMemory(size_t size) {
void *addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -, );
if (addr == MAP_FAILED) {
return NULL;
}
return addr;
} void freeSharedMemory(void *addr, size_t size)
{
if (munmap(addr, size) == -) {
printf("munmap(%p, %d) failed", addr, (int)size);
}
} int main(int argc, char *argv[] ) { sem_t* mutex_share = createSharedMemory(sizeof(sem_t));
sem_t mutex_not_share;
if (mutex_share == NULL) {
printf("creat share memory error\n");
return ;
}
if( sem_init(mutex_share,,) < || sem_init(&mutex_not_share,,) < ) {
printf("semaphore initilization\n");
return ;
}
if (fork() == ) {
sem_wait(&mutex_not_share);
for(int j = ;j<;j++) {
printf("mutex_not_share child j = %d\n", j);
usleep();
}
sem_post(&mutex_not_share); sem_wait(mutex_share);
for (int i = ;i<;i++) {
printf("mutex_share child i = %d\n", i);
usleep();
}
sem_post(mutex_share); }
else {
sem_wait(&mutex_not_share);
for(int j = ;j<;j++) {
printf("mutex_not_share parent j = %d\n", j);
usleep();
}
sem_post(&mutex_not_share);
sem_wait(mutex_share);
for (int i = ;i<;i++) {
printf("mutex_share parent i = %d\n", i);
usleep();
}
sem_post(mutex_share);
}
freeSharedMemory(mutex_share,sizeof(sem_t));
return ;
}
运行结果可以看出,如果没有放到共享内存,就算将pshared设置为1,也起不了作用。

信号量 sem_t 进程同步的更多相关文章
- 信号量进程同步,王明学learn
信号量进程同步 一组并发进程进行互相合作.互相等待,使得各进程按一定的顺序执行的过程称为进程间的同步. 信号量在进程同步时初始值为:0 信号量在进程互斥时初始值为:大于0的 本章节主要使用信号量,使的 ...
- system V信号量和Posix信号量
一.函数上的区别 信号量有两种实现:传统的System V信号量和新的POSIX信号量.它们所提供的函数很容易被区分:对于所有System V信号量函数,在它们的名字里面没有下划线.例如,应该是sem ...
- Linux 多线程信号量同步
PV原子操作 P操作: 如果有可用的资源(信号量值>0),则此操作所在的进程占用一个资源(此时信号量值减1,进入临界区代码); 如果没有可用的资源(信号量值=0),则此操作所在的进程被阻塞直到系 ...
- [转]一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程
一个简单的Linux多线程例子 带你洞悉互斥量 信号量 条件变量编程 希望此文能给初学多线程编程的朋友带来帮助,也希望牛人多多指出错误. 另外感谢以下链接的作者给予,给我的学习带来了很大帮助 http ...
- 关于Linux下进程间使用共享内存和信号量通信的时的编译问题
今天在编译一个使用信号量实现进程同步时,出现了库函数不存在的问题.如下图 编译结果实际上是说,没include相应的头文件,或是头文件不存在(即系统不支持该库函数) 但我man shm_open是可以 ...
- 线程间同步之 semaphore(信号量)
原文地址:http://www.cnblogs.com/yuqilin/archive/2011/10/16/2214429.html semaphore 可用于进程间同步也可用于同一个进程间的线程同 ...
- Linux多线程实践(5) --Posix信号量与互斥量解决生产者消费者问题
Posix信号量 Posix 信号量 有名信号量 无名信号量 sem_open sem_init sem_close sem_destroy sem_unlink sem_wait sem_post ...
- linux POSIX 信号量介绍
信号量一.什么是信号量信号量的使用主要是用来保护共享资源,使得资源在一个时刻只有一个进程(线程)使用.多线程可以同时运行多个线程函数完成功能,但是对于共享数据如果不加以锁定,随意改变共享数据的值会发生 ...
- Linux多线程--使用信号量同步线程【转】
本文转载自:http://blog.csdn.net/ljianhui/article/details/10813469 信号量.同步这些名词在进程间通信时就已经说过,在这里它们的意思是相同的,只不过 ...
随机推荐
- GCD中有哪几种Queue?你自己建立过串行Queue吗?背后的线程模型是什么样的
一共有五种,看图 Paste_Image.png 主线程也就是那个main,一般后台处理数据就就用default那个.创建过一个queue,处理NSMutableArray的时候都在在这一个queue ...
- C#中的 IList, ICollection ,IEnumerable 和 IEnumerator
IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...
- hdu 4640 Island and study-sister(状态压缩dp)
先处理前两个学长到达各个点所需要的最少时间,在计算前两个学长和最后一个学长救出所有学妹的最少时间. #include<stdio.h> #include<string.h> # ...
- MacBook Pro 下Bash Shell 利用Alias 简化命令
编辑~/.bashrc或者~/.bash_profile alias go="xxxxx" 返回bash,执行 source ~/.bash_profile 即可. 我的常用别名 ...
- 嵌入式Linux的一点学习心得
Linux本身是一个发展中的操作系统.它有很多前期不完善的机制,被后代用新的机制代替.但是老的机制不可能一下子就消亡,因此由于“历史原因”,会产生很多新旧机制共存的情况.而且Linux的教科书数不胜数 ...
- Android系统默认Home应用程序(Launcher)的启动过程源码分析
在前面一篇文章中,我们分析了Android系统在启动时安装应用程序的过程,这些应用程序安装好之后,还须要有一个Home应用程序来负责把它们在桌面上展示出来,在Android系统中,这个默认的Home应 ...
- [AngularJS] 5 simple ways to speed up your AngularJS application
Nowdays, Single page apps are becoming increasingly popular among the fornt-end developers. It is th ...
- MySQL的字符编码体系(二)——传输数据编码
MySQL的字符编码体系能够分成两部分:一部分是关于数据库server本身存储数据表时怎样管理字符数据的编码:还有一部分是关于client与数据库server数据传输怎样编码.上一篇MySQL的字符编 ...
- Linux 调度器模拟
http://www.ibm.com/developerworks/cn/linux/l-linux-scheduler-simulator/ LinSched LinSched 是驻留在用户空间中的 ...
- Oracle V$SQLAREA
V$SQLAREA 记录shared SQL area中语句统计信息: V$SQLAREA持续跟踪所有shared pool中的共享cursor,在shared pool中的每一条SQL语句都对应一列 ...