泥鱼群,电视剧断奶剧情介绍,磁翻柱液位计
信号量比互斥锁高级,互斥锁只允许一个线程访问临界区,信号量可以多个,可以把信号量看作成互斥锁的升级版,但是如果能用互斥锁解决,就用互斥锁,互斥锁比信号量节省资源。
1,创建有名字的信号量,创建成功后,会在ubuntu的/dev/shm目录下,生成一个文件,名字为【sem.name】。【sem】是固定的,【name】是函数sem_open的第一个参数。
名字的信号量的生命周期和内核一样,只要系统不重启,它就一直存在。
#include <fcntl.h> /* for o_* constants */ #include <sys/stat.h> /* for mode constants */ #include <semaphore.h> sem_t *sem_open(const char *name, int oflag); sem_t *sem_open(const char *name, int oflag, mode_t mode, unsigned int value);
2,删除有名字的信号量,并删除文件。
#include <semaphore.h> int sem_unlink(const char *name);
返回值:成功0
失败:-1,设置errno
eacces:没有权限访问这个信号量对应的文件 enametoolong:信号量的名字长了 enoent:信号量不存在
3,取得信号量的value值
#include <semaphore.h> int sem_getvalue(sem_t *sem, int *sval);
4,如果信号量的value值大于0,把信号量的value值-1;如果信号量的value值小于1,阻塞等待,直到信号量的value值大于0。注意:在ubuntu下,如果sem_wait执行前,value值为0,sem_wait执行后,value也不会变成-1,再次执行sem_wait,value还是0。但是有的unix系统value会变成负数。即使value不变成负数,内核也会准确记录它的值。
#include <semaphore.h> int sem_wait(sem_t *sem); int sem_trywait(sem_t *sem); int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
sem:信号量指针
返回值:成功0;失败:
sem_trywait():不阻塞等待。
sem_timedwait():
5,把信号量的value值+1。
#include <semaphore.h> int sem_post(sem_t *sem);
semcreate.c
#include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <semaphore.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char** argv){ int c, flags; unsigned int val = 1; sem_t* sem; flags = o_rdwr | o_creat; while((c = getopt(argc, argv, "ei:")) != -1){ switch(c){ case 'e': flags |= o_excl; break; case 'i': val = atoi(optarg); break; } } if(optind != argc - 1){ printf("usage error\n"); return -1; } sem = sem_open(argv[optind], flags, 0664, val); if(sem == sem_failed){ perror("sem"); return -1; } sem_close(sem); exit(0); }
semunlink.c
#include <semaphore.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char** argv){ if(argc != 2){ printf("usage err\n"); exit(1); } if(sem_unlink(argv[1]) == -1){ perror("sem_unlink"); } }
semgetvalue.c
#include <semaphore.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char** argv){ sem_t* sem; int val; if(argc != 2){ printf("usage error\n"); exit(1); } sem = sem_open(argv[1], 0); sem_getvalue(sem, &val); printf("value = %d\n", val); exit(0); }
semwait.c
#include <semaphore.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(int argc, char** argv){ sem_t* sem; int val; if(argc != 2){ printf("usage error\n"); exit(1); } sem = sem_open(argv[1], 0); sem_wait(sem); sem_getvalue(sem, &val); printf("pid %ld has semaphore, value = %d\n", (long) getpid(), val); pause(); exit(0); }
sempost.c
#include <semaphore.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(int argc, char** argv){ sem_t* sem; int val; if(argc != 2){ printf("usage error\n"); exit(1); } sem = sem_open(argv[1], 0); sem_post(sem); sem_getvalue(sem, &val); printf("value = %d\n", val); exit(0); }
ubuntu$ ./semcreate test1 ubuntu$ ls -l /dev/shm/ total 4 -rw-r--r-- 1 ys ys 32 6月 21 16:25 sem.test1
ubuntu$ ./semgetvalue test1 value = 1
ubuntu$ ./semwait test1 pid 2995 has semaphore, value = 0 ^c ubuntu$
ubuntu$ ./semgetvalue test1 value = 0
ubuntu$ ./semwait test1 & [8] 3000 ubuntu$ ./semgetvalue test1 value = 0 ubuntu$ ./semwait test1 & [9] 3002 ubuntu$ ./semgetvalue test1 value = 0
ubuntu$ ./sempost test1 pid 3000 has semaphore, value = 0 //来之第一个sem_wait程序的输出 value = 0
ubuntu$ ./sempost test1 pid 3002 has semaphore, value = 0 //来之第二个sem_wait程序的输出 value = 0
ubuntu$ ./sempost test1 value = 1
c/c++ 学习互助qq群:877684253
本人微信:xiaoshitou5854
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论