有名信号量: 进程
无名信号量: 一般:线程
作用: 用于处理同步互斥
一. 有名信号量
1. 创建或者打开一个有名信号量 sem_open() — man 3 sem_open
//初始化并打开一个有名信号量 sem_open - initialize and open a named semaphore
#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, mode_t mode, unsigned int value); Link with -lrt or -pthread.
name: 有名信号量的名字,以"/"开头
oflag: 标志位 O_CREAT --> 就必须设置mode和value
O_EXCL --> 存在报错
mode: 八进制权限 0777 0666
value: 信号量初始化的值
2. P --> sem_post / V --> sem_wait
P : 资源数加1 灯亮 0 --> 1
V : 资源数减1 灯灭 1 --> 0#include <semaphore.h>
int sem_wait(sem_t *sem); int sem_post(sem_t *sem);
sem: 有名信号量的指针地址
返回值: 成功: 0 失败:-1
写入了数据后,资源数应该加1 什么时候才能读取数据?要一直等待到资源数能够减1为止,才能把数据读取出来!
3. 关闭信号量 – sem_close() – man 3 sem_close
#include <semaphore.h>
int sem_close(sem_t *sem);
sem: 有名信号量的指针地址
返回值: 成功: 0 失败:-1
- 删除信号量 – sem_unlink() — man 3 unlink
#include <semaphore.h>
int sem_unlink(const char *name);
name: 有名信号量的名字
返回值: 成功: 0 失败:-1
1.c 2.c --> 分别取一块共享内存中处理数据,但是使用有名信号量来处理同步互斥! 参考代码: named_sem/
标题示例代码:
发送端:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <semaphore.h>
int main()
{
key_t key;
int shmid;
sem_t *sem = NULL;
key = ftok(".",10);
shmid = shmget(key,8192,IPC_CREAT|0666);
char *p = (char *)shmat(shmid,NULL,0);
sem = sem_open("/semtest",O_CREAT,0666,0);
while(1)
{
//把单车开进去了
fgets(p,8192,stdin);
//共享内存中有数据,资源数+1
sem_post(sem);
if(strncmp(p,"quit",4) == 0)
{
break;
}
}
sem_close(sem);
return 0;
}
接收端
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/stat.h>
int main()
{
key_t key;
int shmid;
sem_t *sem = NULL;
key = ftok(".",10);
shmid = shmget(key,8192,IPC_CREAT|0666);
char *p = (char *)shmat(shmid,NULL,0);
sem = sem_open("/semtest",O_CREAT,0666,0);
while(1)
{
//资源数减1
sem_wait(sem);
printf("from shm:%s",p);
if(strncmp(p,"quit",4) == 0)
{
break;
}
}
sem_close(sem);
sem_unlink("/semtest");
return 0;
}
无名信号量
无名信号量没有名字,只能初始化
1. 初始化无名信号量 – sem_init — man 3 sem_init
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
sem: 信号量的地址
pshared: 0 --> 作用于线程 非0 --> 作用于进程
value: 信号量起始值
2. P --> sem_post / V --> sem_wait
P : 资源数加1 灯亮 0 --> 1
V : 资源数减1 灯灭 1 --> 0#include <semaphore.h>
int sem_wait(sem_t *sem); int sem_post(sem_t *sem);
sem: 有名信号量的指针地址
返回值: 成功: 0 失败:-1
写入了数据后,资源数应该加1 什么时候才能读取数据?要一直等待到资源数能够减1为止,才能把数据读取出来!
3. 销毁无名信号量 – sem_destroy
#include <semaphore.h>
int sem_destroy(sem_t *sem);
sem: 信号量的地址
返回值: 成功: 0 失败: -1
示例代码
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
sem_t sem;
void *routine(void *arg)
{
//哪个子线程能够把1变为0,就可以执行任务
sem_wait(&sem);
char buf[] = "helloworld\n";
int i;
for(i=0;buf[i] != '\0';i++)
{
fprintf(stderr,"%c",buf[i]);
usleep(500000);
}
//这个抢到资源的子线程做完任务了,就把信号量变为1
sem_post(&sem);
pthread_exit(NULL);
}
int main()
{
pthread_t tid[5];
int i;
sem_init(&sem,0,1);
//1. 产生5个子线程
for(i=0;i<5;i++)
{
pthread_create(&tid[i],NULL,routine,NULL);
}
//2. 等待5个子线程
for(i=0;i<5;i++)
{
pthread_join(tid[i],NULL);
}
sem_destroy(&sem);
return 0;
}