POSIX 信號量創(chuàng)建
1笼痹、有名信號量創(chuàng)建
#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);
參數(shù):
name
: 可訪問的目錄或文件
oflag
:if oflag == O_CREAT ,表示如果信號量不存在則創(chuàng)建信號量,存在則不創(chuàng)建; if oflag == O_CREAT|O_EXCL惫东,如果信號量存在,函數(shù)調(diào)用會出錯。
mode
:用戶可讀寫權(quán)限
value
:初始化值
2廉沮、無名信號量創(chuàng)建
#include <semaphore.h>
int sem_init(sem_t *sem, int pshared, unsigned int value);
參數(shù):
pshared
: if pshared == 0, 表示線程間共享信號量颓遏;if pshared != 0,表示進程間共享信號量滞时。
sem
:若是線程間共享信號量叁幢,此值要設(shè)置為全局變量或動態(tài)內(nèi)存(即是線程都可以訪問);若是進程間共享信號量坪稽,此值應(yīng)該在共享內(nèi)存里面(即是進程都可以訪問)曼玩。
value
:初始化值。
POSIX 信號量的銷毀
1窒百、有名信號量的銷毀
#include <semaphore.h>
int sem_close(sem_t *sem);
如果進程沒有調(diào)用sem_close 就退出演训, 那么內(nèi)核會將自動關(guān)閉任何打開的信號量。但是信號量的值不會改變贝咙,也就是說內(nèi)核沒有SEM_UNDO的功能,不能自動銷毀信號量拂募。
#include <semaphore.h>
int sem_unlink(const char *name);
如果沒有打開的信號量引用庭猩,則信號量會被銷毀;否則該信號量會延時到最后一個打開的引用關(guān)閉陈症。
2蔼水、無名信號量的銷毀
#include <semaphore.h>
int sem_destroy(sem_t *sem);
一般使用在資源回收階段,若是有進程或線程在占用這個型號量录肯,就釋放信號量趴腋,會出現(xiàn)未定義行為。執(zhí)行了這個函數(shù)论咏,需要使用sem_init才能再次使用信號量优炬。
POSIX 信號量的修改
1、信號量計數(shù)減 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厅贪、信號量計數(shù)加 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;
}