semaphore
简称SEM,主要用来进行进程间同步以下接口编码过程中都可以通过man sem_open
这种方式查看接口对具体使用方式,详细信息则不列举
sem_t *sem_open(const char *name, int oflag); //使用字符串创建一个信号量
sem_t *sem_open(const char *name, int oflag,mode_t mode, unsigned int value); //使用字符串创一个一个信号量,并初始化信号量的值
int sem_close(sem_t *sem);
int sem_post(sem_t *sem); //信号量的v操作 +1
int sem_wait(sem_t *sem); //信号量的p操作 -1
int sem_trywait(sem_t *sem); //信号量进行p操作,但是值已经为0,此时会立即返回错而非阻塞//信号量进行p操作,如信号量的值为0,则阻塞abs_timeout结构体中的时间内还是无法执行p操作,则返回错误
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
int sem_unlink(const char *name);//删除名称为name的信号量
int sem_getvalue(sem_t *sem, int *sval);//获取信号量sem所代表的value数值
#include
-lpthread
进行编译linux系统中创建的信号量存放路径为/dev/shm
sem_demo.c
#include
#include
#include
#include
#include int main (){ //创建信号量并初始化信号量的value值为4unsigned int sem_value = 4;sem_t *sem = sem_open("posix_sem", O_RDWR | O_CREAT,0777,sem_value);if (sem == SEM_FAILED) { printf("sem_open failed
");_exit(-1);}//获取信号量的value值if (sem_getvalue(sem,&sem_value) != -1) { printf("sem_getvalue is %d
",sem_value);}//对信号量进行p(-1)操作sem_wait(sem);sem_wait(sem);sem_wait(sem);sem_wait(sem);//sem_trywait(sem);//并不会发生阻塞,此时sem值已经为0,无法再进行p操作if(sem_getvalue(sem,&sem_value) != -1) { printf("sem_getvalue after sem_wait is %d
",sem_value);}//对信号量进行v(+1)操作sem_post(sem);sem_post(sem);sem_post(sem);sem_post(sem);if(sem_getvalue(sem,&sem_value) != -1) { printf("sem_getvalue after sem_post is %d
",sem_value);}if(sem_close(sem) != -1) { printf("sem_close success
");}printf("wait for unlink sem
");sleep(10);//删除名称为posix_sem信号量if(sem_unlink("posix_sem") != -1) { printf("sem_unlink success
");}return 0;
}
编译gcc sem_demo.c -lpthread
输出如下:
sem_getvalue is 4
sem_getvalue after sem_wait is 0
sem_getvalue after sem_post is 4
sem_close success
wait for unlink sem
sem_unlink success
同时在等待10秒删除期间我们查看/dev/shm
目录下的sem文件存在,当10秒过后sem文件则被删除
sem_comu.c
#include
#include
#include
#include
#include #define SEM_NAME "posix_sem_comu"int main (){ int i = 0, j = 0,sem_val = 0,ret;sem_t *sem;sem = sem_open(SEM_NAME, O_CREAT,0666,1);ret = fork();if (ret == -1) { printf("sem_open failed
");sem_close(sem);sem_unlink(SEM_NAME);_exit(-1);}//创建的子进程,执行10次p操作,每次间隔1秒else if(ret == 0) { while (i++ <= 10) { sem_wait(sem);if (sem_getvalue(sem,&sem_val) != -1) { printf("child process :sem value is %d
",sem_val);sleep(1);}}_exit(1);}//父进程执行10次v操作,每次间隔两秒else { while (j++ <=10) { sem_post(sem);if (sem_getvalue(sem,&sem_val) != -1) { printf("prarent process :sem value is %d
",sem_val);sleep(2);}}}//最终删除sem信号量sem_close(sem);if (sem_unlink(SEM_NAME) != -1) { printf("sem_unlink success
");}return 0;
}
编译:gcc sem_comu.c -o comu -lpthread
输出如下,可以很明显的看到子即使子进程的p操作频率快于父进程v操作,但是对同一信号量,他们仍然能够有序同步访问。因为子进程p操作频率较快,此时信号量的value会很快减为0,子进程此时再进行p操作会发生阻塞;直到父进程执行v操作将信号量的值变为1子进程才能继续访问
prarent process :sem value is 2
child process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
child process :sem value is 0
prarent process :sem value is 1
sem_unlink success
两个进程,使用同一个信号量共同访问一个临界区,利用信号量的pv操作,实现同步访问
sem_post.c
#include
#include
#include
#include
#include int main(){ char *name = "/posix_sem";int j = 0, sem_value = 4;sem_t *sem;sem = sem_open(name, O_CREAT, 0666, sem_value);if (sem == SEM_FAILED) { printf("sem open failed
");_exit(-1);}printf("sem_open %s success
",name);//对信号量进行5次 v操作,每次间隔5秒while(j++ <= 10) { if(sem_post(sem) == -1) { printf("sem _posd failed
");_exit(-1);}if(sem_getvalue(sem,&sem_value) != -1) { printf("sem post success %d
",sem_value);}sleep(5);}sleep(10);if(sem_close(sem) != -1) { printf("sem_close success
");}if (sem_unlink(name) != -1) { printf("sem_unlink success
");}return 0;
}
sem_wait.c
#include
#include
#include
#include
#include int main(){ char *name = "/posix_sem";int j = 0, sem_value = 4;sem_t *sem;sem = sem_open(name, O_CREAT, 0666, sem_value);if (sem == SEM_FAILED) { printf("sem open failed
");_exit(-1);}printf("sem_open %s success
",name);//对信号量进行p操作,每次间隔5秒,总共进行10次while(j++ <= 10) { if(sem_post(sem) == -1) { printf("sem _posd failed
");_exit(-1);}if(sem_getvalue(sem,&sem_value) != -1) { printf("sem post success %d
",sem_value);}sleep(5);}//执行结束后关闭信号量,并删除信名称为name的信号量sleep(10);if(sem_close(sem) != -1) { printf("sem_close success
");}if (sem_unlink(name) != -1) { printf("sem_unlink success
");}return 0;
}
编译:
gcc sem_post.c -o post -lpthread
gcc sem_wait.c -o wait -lpthread
可以看到输出如下,两进程是能够正常进行信号量value值的访问同步
生产者和消费者模型: 有若干个缓冲区,生产者不断向里填数据,消费者不断从中取数据两者不冲突的前提: 缓冲区有若干个,且是固定大小,生产者和消费者各有若干个生产者向缓冲区中填数据前需要判断缓冲区是否满,满了则等待,直到有空间消费者从缓冲区中取数据前需要判断缓冲区是否为空,空了则等待,直到缓冲区有数据在某一时刻,缓冲区中只允许有一个操作...
关于信号量和共享内存的相关描述已经在前几篇提到过; 信号量:即内核维护的一个正整数,可以使用内核提供的p/v接口进行该正整数的+/-操作,它主要用来表示系统中可用资源的个数,协调各个进程有序访问资源,防止发生冲突共享内存:操作系统从运行中的进程拥有的3G用户空间中提供一段用户态可以直接访问的内存块,并且该内存区域可以被其他进程共享...
翻页器
在src/main/resources/springmvc-servlet.xml中加入
本篇仅仅是一个记录 MergeOperator 的使用方式。 Rocksdb 使用MergeOperator 来代替Update 场景中的读改写操作,即用户的一个Update 操作需要调用rocksdb的 Get + Put 接口才能完成。 而这种情况下会引入一些额外的读写放大,对于支持SQL这种update 频繁的场景来说实在是不划...
看了很多人写的好几个去重方法,我在这里精简组合下,适用于已排序与未排序的数组。 废话不多说,上代码。