POSIX 信号量创建
1、有名信号量创建
#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);
参数:
name
: 可访问的目录或文件
oflag
:if oflag == O_CREAT ,表示如果信号量不存在则创建信号量,存在则不创建; if oflag == O_CREAT|O_EXCL,如果信号量存在,函数调用会出错。
mode
:用户可读写权限
value
:初始化值
2、无名信号量创建
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
参数:
pshared
: if pshared == 0, 表示线程间共享信号量;if pshared != 0,表示进程间共享信号量。
sem
:若是线程间共享信号量,此值要设置为全局变量或动态内存(即是线程都可以访问);若是进程间共享信号量,此值应该在共享内存里面(即是进程都可以访问)。
value
:初始化值。
POSIX 信号量的销毁
1、有名信号量的销毁
#include <semaphore.h>
int sem_close(sem_t *sem);
如果进程没有调用sem_close 就退出, 那么内核会将自动关闭任何打开的信号量。但是信号量的值不会改变,也就是说内核没有SEM_UNDO的功能,不能自动销毁信号量。
#include <semaphore.h>
int sem_unlink(const char *name);
如果没有打开的信号量引用,则信号量会被销毁;否则该信号量会延时到最后一个打开的引用关闭。
2、无名信号量的销毁
#include <semaphore.h>
int sem_destroy(sem_t *sem);
一般使用在资源回收阶段,若是有进程或线程在占用这个型号量,就释放信号量,会出现未定义行为。执行了这个函数,需要使用sem_init才能再次使用信号量。
POSIX 信号量的修改
1、信号量计数减 1
#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);
man page:
DESCRIPTION
sem_wait() decrements (locks) the semaphore pointed to by sem. If the semaphore's value is greater than zero, then the decrement proceeds, and the function returns, immediately. If the semaphore currently has the value zero, then the call blocks until either it becomes possible to perform the decrement (i.e., the semaphore value rises above zero), or a signal handler interrupts the call.
sem_trywait() is the same as sem_wait(), except that if the decrement cannot be immediately performed, then call returns an error (errno set to EAGAIN) instead of blocking.
sem_timedwait() is the same as sem_wait(), except that abs_timeout specifies a limit on the amount of time that the call should block if the decre‐ment cannot be immediately performed. The abs_timeout argument points to a structure that specifies an absolute timeout in seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). This structure is defined as follows:
struct timespec {
time_t tv_sec; /* Seconds */
long tv_nsec; /* Nanoseconds [0 .. 999999999] */
};
If the timeout has already expired by the time of the call, and the semaphore could not be locked immediately, then sem_timedwait() fails with a timeout error (errno set to ETIMEDOUT).
If the operation can be performed immediately, then sem_timedwait() never fails with a timeout error, regardless of the value of abs_timeout. Fur‐ thermore, the validity of abs_timeout is not checked in this case.
2、信号量计数加 1
#include <semaphore.h>
int sem_post(sem_t *sem);
API封装
sem.h:
#ifndef _SEM_H
#define _SEM_H
#include <semaphore.h>
#include <fcntl.h>
#include <time.h>
sem_t * Sem_Open(const char *name, int oflag, mode_t mode, unsigned int value);
int Sem_Close(sem_t *sem);
int Sem_Unlink(const char *name);
int Sem_Trywait(sem_t *sem);
int Sem_Wait(sem_t *sem);
int Sem_Timedwait(sem_t *sem, const struct timespec *pstTime);
int Sem_Post(sem_t *sem);
int Sem_Init(sem_t *sem, int pshared, unsigned int value);
int Sem_Destroy(sem_t *sem);
#endif
sem.c:
#include "sem.h"
#include <stdio.h>
#include <errno.h>
sem_t * Sem_Open(const char *name, int oflag, mode_t mode, unsigned int value)
{
sem_t *sem = NULL;
sem = sem_open(name, oflag, mode, value);
if(SEM_FAILED == sem)
{
perror("sem_open:");
}
return sem;
}
int Sem_Close(sem_t *sem)
{
int val = sem_close(sem);
if(val < 0)
{
perror("sem_close error:");
}
return val;
}
int Sem_Unlink(const char *name)
{
int val = sem_unlink(name);
if(val < 0)
{
perror("sem_unlink error:");
}
return val;
}
int Sem_Trywait(sem_t *sem)
{
return sem_trywait(sem);
}
int Sem_Wait(sem_t *sem)
{
int val = 0;
val = sem_wait(sem);
if(val < 0)
{
perror("sem_wait error:");
}
return val;
}
int Sem_Timedwait(sem_t *sem, const struct timespec *pstTime)
{
int val = 0;
val = sem_timedwait(sem, pstTime);
if(val < 0)
{
perror("sem_timedwait error:");
}
return val;
}
int Sem_Post(sem_t *sem)
{
return sem_post(sem);
}
int Sem_Init(sem_t *sem, int pshared, unsigned int value)
{
int val = sem_init(sem, pshared, value);
if(val < 0)
{
perror("sem_init error:");
}
return val;
}
int Sem_Destroy(sem_t *sem)
{
printf("Sem_Destroy\n");
int val = sem_destroy(sem);
if(val < 0)
{
perror("sem_destroy error:");
}
return val;
}
例程
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <stdlib.h>
#include <semaphore.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "sem.h"
int main(void)
{
sem_t *sem;
sem_t ss_sem;
int shmid;
key_t key = ftok("/usr", 0x01);
if(key < 0)
{
perror("ftok error");
}
shmid = shmget(key, sizeof(sem_t), IPC_CREAT| 0666);
if(shmid < 0)
{
perror("shmget error");
}
sem_t * mm = (sem_t *)shmat(shmid, 0, 0);
if(NULL == mm)
{
perror("shmat error");
}
Sem_Init((sem_t *)mm, 1, 1);
pid_t pid = 0;
Sem_Unlink("/home");
Sem_Unlink("/usr");
pid = fork();
if(0 == pid)
{
key_t key = ftok("/home", 0x01);
if(key < 0)
{
perror("ftok error");
}
shmid = shmget(key, sizeof(sem_t), IPC_CREAT| 0666);
sem_t * mm = (sem_t *)shmat(shmid, 0, 0);
sem = Sem_Open("/usr", O_CREAT, 0666, 1);
while(1)
{
//Sem_Wait(sem);
Sem_Wait(mm);
fprintf(stderr,"===> child process\n");
usleep(3000000);
Sem_Post(mm);
//Sem_Post(sem);
}
}
//sem = Sem_Open("/usr", O_CREAT, 0666, 1);
int count = 0;
struct timespec tm;
while(1)
{
clock_gettime(CLOCK_REALTIME, &tm);
tm.tv_sec += 2;
//Sem_Wait(sem);
//Sem_Wait(mm);
if(Sem_Timedwait(mm, &tm) == 0)
{
fprintf(stderr, "###parent process\n");
Sem_Post(mm);
}
//Sem_Post(mm);
//Sem_Post(sem);
usleep(300000);
}
return 0;
}
网友评论