有名信號量 用于 無血緣的進程間互斥
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);
//信號量不存在
sem_t* sem_open(const char* name,int oflag,mode_t mode, unsigned int value);
功能:
????????創建一個信號量
參數:
????????name:信號量的名字
????????oflag:sem_open函數的權限標志
????????mode:文件權限(可讀、可寫、可執行 0777)的設置
????????value:信號量的初始值
返回值:
????????信號量的地址,
????????失敗:SEM_FAILED
2、信號量的關閉:
int sem_close(sem_t* sem);
功能:
????????關閉信號量
參數:
????????信號量的的地址
返回值:
????????成功0
????????失敗-1
3、信號量文件的刪除
#include<semaphore.h>
int sem_unlink(const char* name);
功能:
????????刪除信號量的文件
參數:
????????信號量的文件名
返回值:
????????成功0
????????失敗-1
示例代碼
08_taskA.c
#include<stdio.h>
#include<semaphore.h>
#include<unistd.h>
#include<fcntl.h>?
#include<sys/stat.h>void my_printf(char* str){
????int i = 0;
????while (str[i] != '\0')?
? ?{
? ? ? ? ? printf("%c", str[i++]);?
? ? ? ? ?fflush(stdout);
? ? ? ? ? sleep(1);?
? ? ?}
? ?return;
}
int main(int argc,char const* argv[])
{
? ???????//創建一個有名信號量sem_open 最后一個參數為初始值
? ? ? ? ?sem_t *sem = sem_open("sem", O_RDWR|O_CREAT, 0666, 1);
? ? ? ? ?sem_wait(sem);
? ? ? ? ?my_printf("bbbbbbld");
? ??????sem_post(sem);?
? ? ? ? sem_close(sem);
????????sem_destroy(sem);?
????? ? return 0;
}
08_taskB.c
#include<stdio.h>
#include<semaphore.h>
#include<unistd.h>
#include<fcntl.h>?
#include<sys/stat.h>
void my_printf(char* str)
{
? ? int i = 0;
? ? while (str[i] != '\0')
? ? {
? ? ? ? ? printf("%c", str[i++]);
? ? ? ? ? fflush(stdout);
? ? ? ? ? sleep(1);
? ? ? }
? ?return;
}
int main(int argc,char const* argv[]){
? ? ? ? ?//創建一個有名信號量sem_open 最后一個參數為初始值
? ? ? ? ?sem_t *sem = sem_open("sem", O_RDWR|O_CREAT, 0666, 1);?
? ? ? ? ?sem_wait(sem);
? ? ? ? ?my_printf("hsaafdd");
? ? ? ? ?sem_post(sem);
? ? ? ? ?sem_close(sem);
? ? ? ? ?sem_destroy(sem);
? ? ? ? ?return 0;
}
有名信號量 用于 無血緣的進程間同步
示例代碼
09_taskA.c
#include<stdio.h>
#include<semaphore.h>
#include<unistd.h>
#include<fcntl.h>?
#include<sys/stat.h>
void my_printf(char* str){
? ? int i = 0;
? ? while (str[i] != '\0')
? ? {
? ? ? ? ? printf("%c", str[i++]);
? ? ? ? ? fflush(stdout);
? ? ? ? ? sleep(1);
? ? ? }
? ?return;
}
int main(int argc,char const* argv[])
{
? ? ? ? ?//創建一個有名信號量sem_open 最后一個參數為初始值
? ? ? ? ?sem_t *sem1 = sem_open("sem1", O_RDWR|O_CREAT, 0666, 1);?
? ? ? ? ?sem_t *sem2 = sem_open("sem2", O_RDWR|O_CREAT, 0666, 1);?
? ? ? ? ?? ? ? ? ?sem_wait(sem1);
? ? ? ? ?my_printf("bbbbbbld");
? ? ? ? ?sem_post(sem2);
? ? ? ? ?????????sem_close(sem1);
? ? ? ? ?sem_close(sem2);
? ? ? ? ?????????sem_destroy(sem1);
? ? ? ? ?sem_destroy(sem2);
? ? ? ? ?return 0;
}
09_taskB.c
#include<stdio.h>
#include<semaphore.h>
#include<unistd.h>
#include<fcntl.h>?
#include<sys/stat.h>
void my_printf(char* str)
{
? ? int i = 0;
? ? while (str[i] != '\0')
? ? {
? ? ? ? ? printf("%c", str[i++]);
? ? ? ? ? fflush(stdout);
? ? ? ? ? sleep(1);
? ? ? }? ?
? ? ? return;
}
int main(int argc,char const* argv[])
{
? ? ? ? ?//創建一個有名信號量sem_open 最后一個參數為初始值
? ? ? ? ?sem_t *sem1 = sem_open("sem1", O_RDWR|O_CREAT, 0666, 1);
? ???????sem_t *sem2 = sem_open("sem2", O_RDWR|O_CREAT, 0666, 1);?? ? ? ? ?sem_wait(sem2);
? ? ? ? ?my_printf("bbbbbbld");
? ? ? ? ?sem_post(sem1);?
? ? ? ? ?sem_close(sem1);
? ? ? ? ?sem_close(sem2);
? ? ? ? ?sem_destroy(sem1);
? ? ? ? ?sem_destroy(sem2);
? ? ? ? ?return 0;
}