首頁  >  文章  >  php框架  >  swoole中信號量的用法是什麼

swoole中信號量的用法是什麼

WBOY
WBOY原創
2022-03-14 15:29:311988瀏覽

在swoole中,信號量主要用來保護共享資源,使得資源在一個時刻只有一個進程;信號量的值為正的時候,說明所測試的線程可以鎖定而使用,信號量的值若為0,則表示測試的執行緒要進入睡眠佇列中,等待被喚醒。

swoole中信號量的用法是什麼

本教學操作環境:Windows10系統、Swoole4版、DELL G3電腦

swoole中訊號量的用法是什麼

信號量的使用主要是用來保護共享資源,使得資源在一個時刻只有一個行程(執行緒)

#所擁有。信號量的值為正的時候,表示它空閒。所測試的執行緒可以鎖定而使用它。若為0,表示它被佔用,測試的執行緒要進入睡眠佇列中,等待被喚醒。

Linux提供兩種訊號量:

(1) 核心訊號量,由核心控制路徑使用

(2) 使用者狀態處理使用的信號量,這種訊號量又分成POSIX訊號量和SYSTEM

V信號量。

POSIX訊號量又分為有名訊號量和無名訊號量。

有名信號量,其值保存在檔案中, 所以它可以用於執行緒也可以用於進程間的同步。無名

信號量,其值保存在記憶體中。

核心信號量

核心訊號量的組成

核心訊號量類似於自旋鎖,因為當鎖定關閉時,它不允許內核控制路徑繼續進行。然而,

當核心控制路徑試圖取得核心訊號量鎖定保護的忙碌資源時,相應的進程就被掛起。只有在資源被釋放時,進程才會再次變成可運行。

只有可以睡眠的函數才能取得核心訊號量;中斷處理程序和可延遲函數都不能使用內部

核信號量。

內核信號量是struct semaphore類型的對象,它在

#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int number; // 被保护的全局变量
sem_t sem_id;
void* thread_one_fun(void *arg)
{
sem_wait(&sem_id);
printf("thread_one have the semaphore\n");
number++;
printf("number = %d\n",number);
sem_post(&sem_id);
}
void* thread_two_fun(void *arg)
{
sem_wait(&sem_id);
printf("thread_two have the semaphore \n");
number--;
printf("number = %d\n",number);
sem_post(&sem_id);
}
int main(int argc,char *argv[])
{
number = 1;
pthread_t id1, id2;
sem_init(&sem_id, 0, 1);
pthread_create(&id1,NULL,thread_one_fun, NULL);
pthread_create(&id2,NULL,thread_two_fun, NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
printf("main,,,\n");
return 0;
}

上面的例程,到底哪個執行緒先申請到信號量資源,這是隨機的。如果想要某個特定的順

序的話,可以用2個訊號量來實現。例如下面的例程是執行緒1先執行完,然後執行緒2才繼

續執行,直到結束。

int number; // 被保护的全局变量
sem_t sem_id1, sem_id2;
void* thread_one_fun(void *arg)
{
sem_wait(&sem_id1);
printf(“thread_one have the semaphore\n”);
number++;
printf(“number = %d\n”,number);
sem_post(&sem_id2);
}
void* thread_two_fun(void *arg)
{
sem_wait(&sem_id2);
printf(“thread_two have the semaphore \n”);
number–;
printf(“number = %d\n”,number);
sem_post(&sem_id1);
}
int main(int argc,char *argv[])
{
number = 1;
pthread_t id1, id2;
sem_init(&sem_id1, 0, 1); // 空闲的
sem_init(&sem_id2, 0, 0); // 忙的
pthread_create(&id1,NULL,thread_one_fun, NULL);
pthread_create(&id2,NULL,thread_two_fun, NULL);
pthread_join(id1,NULL);
pthread_join(id2,NULL);
printf(“main,,,\n”);
return 0;
}

(b)無名信號量在相關進程間的同步

說是相關進程,是因為本程式中共有2個進程,其中一個是另外一個的子進程(由

fork

產生)的。

本來對於fork來說,子進程只繼承了父進程的程式碼副本,mutex理應在父子進程

中是相互獨立的兩個變數,但由於在初始化mutex的時候,由pshared = 1指

定了mutex處於共享記憶體區域,所以此時mutex變成了父子進程共享的一個變

量。此時,mutex就可以用來同步相關進程了。

#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc, char **argv)
{
int fd, i,count=0,nloop=10,zero=0,*ptr;
sem_t mutex;
//open a file and map it into memory
fd = open("log.txt",O_RDWR|O_CREAT,S_IRWXU);
write(fd,&zero,sizeof(int));
ptr = mmap( NULL,sizeof(int),PROT_READ |
PROT_WRITE,MAP_SHARED,fd,0 );
close(fd);
/* create, initialize semaphore */
if( sem_init(&mutex,1,1) < 0) //
{
perror("semaphore initilization");
exit(0);
}
if (fork() == 0)
{ /* child process*/
for (i = 0; i < nloop; i++)
{
sem_wait(&mutex);
printf("child: %d\n", (*ptr)++);
sem_post(&mutex);
}
exit(0);
}
/* back to parent process */
for (i = 0; i < nloop; i++)
{
sem_wait(&mutex);
printf("parent: %d\n", (*ptr)++);
sem_post(&mutex);
}
exit(0);
}

2.有名信號量

有名信號量的特徵是把信號量的值保存在檔案中。

這決定了它的用途非常廣:既可以用於線程,也可以用於相關進程間,甚至是不相關

進程。

(a)有名信號量能在進程間共享的原因

由於有名信號量的值是保存在檔案中的,所以對於相關進程來說,子進程是繼承了父

進程的檔案描述符,那麼子程序所繼承的檔案描述符所指向的檔案是和父行程一樣的,當

#然檔案裡面保存的有名信號量值就共享了。

(b)有名信號量相關函數說明

有名信號量在使用的時候,和無名信號量共享sem_wait和sem_post函數。

差異是有名信號量使用sem_open取代sem_init,另外在結束的時候要像關閉檔案

一樣去關閉這個有名訊號量。

(1)開啟一個已存在的有名信號量,或建立並初始化一個有名信號量。一個單一的呼叫就完

成了信號量的創建、初始化和權限的設定。

sem_t *sem_open(const char *name, int oflag, mode_t mode , int value);

name是檔案的路徑名稱;

Oflag 有O__L| EXCL兩個取值;

mode_t控制新的訊號量的存取權限;

Value指定訊號量的初始化值。

注意:

這裡的name不能寫成/tmp/aaa.sem這樣的格式,因為在linux下,sem都是建立

在/dev/shm目錄下。你可以將name寫成“/mysem”或“mysem”,創建出來的檔案都

是“/dev/shm/sem.mysem”,千萬不要寫路徑。也千萬不要寫「/tmp/mysem」之類的。

當oflag = O_CREAT時,若name指定的信號量不存在時,則會建立一個,而且後

面的mode和value參數必須有效。若name指定的信號量已存在,則直接開啟該信號量,

同時忽略mode和value參數。

當oflag = O_CREAT|O_EXCL時,若name指定的信號量已存在,則函數會直接回傳

回error。

(2) 一旦你使用了信號量,銷毀它們就變得很重要。

在做这个之前,要确定所有对这个有名信号量的引用都已经通过sem_close()函数

关闭了,然后只需在退出或是退出处理函数中调用sem_unlink()去删除系统中的信号量,

注意如果有任何的处理器或是线程引用这个信号量,sem_unlink()函数不会起到任何的作

用。

也就是说,必须是最后一个使用该信号量的进程来执行sem_unlick才有效。因为每个

信号灯有一个引用计数器记录当前的打开次数,sem_unlink必须等待这个数为0时才能把

name所指的信号灯从文件系统中删除。也就是要等待最后一个sem_close发生。

(c)有名信号量在无相关进程间的同步

前面已经说过,有名信号量是位于共享内存区的,那么它要保护的资源也必须是位于

共享内存区,只有这样才能被无相关的进程所共享。

在下面这个例子中,服务进程和客户进程都使用shmget和shmat来获取得一块共享内

存资源。然后利用有名信号量来对这块共享内存资源进行互斥保护。

File1: server.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SHMSZ 27
char SEM_NAME[]= "vik";
int main()
{
char ch;
int shmid;
key_t key;
char *shm,*s;
sem_t *mutex;
//name the shared memory segment
key = 1000;
//create & initialize semaphore
mutex = sem_open(SEM_NAME,O_CREAT,0644,1);
if(mutex == SEM_FAILED)
{
perror("unable to create semaphore");
sem_unlink(SEM_NAME);
exit(-1);
}
//create the shared memory segment with this key
shmid = shmget(key,SHMSZ,IPC_CREAT|0666);
if(shmid<0)
{
perror("failure in shmget");
exit(-1);
}
//attach this segment to virtual memory
shm = shmat(shmid,NULL,0);
//start writing into memory
s = shm;
for(ch=&#39;A&#39;;ch<=&#39;Z&#39;;ch++)
{
sem_wait(mutex);
*s++ = ch;
sem_post(mutex);
}
//the below loop could be replaced by binary semaphore
while(*shm != &#39;*&#39;)
{
sleep(1);
}
sem_close(mutex);
sem_unlink(SEM_NAME);
shmctl(shmid, IPC_RMID, 0);
exit(0);
}
<u>File 2: client.c</u>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SHMSZ 27
char SEM_NAME[]= "vik";
int main()
{
char ch;
int shmid;
key_t key;
char *shm,*s;
sem_t *mutex;
//name the shared memory segment
key = 1000;
//create & initialize existing semaphore
mutex = sem_open(SEM_NAME,0,0644,0);
if(mutex == SEM_FAILED)
{
perror("reader:unable to execute semaphore");
sem_close(mutex);
exit(-1);
}
//create the shared memory segment with this key
shmid = shmget(key,SHMSZ,0666);
if(shmid<0)
{
perror("reader:failure in shmget");
exit(-1);
}
//attach this segment to virtual memory
shm = shmat(shmid,NULL,0);
//start reading
s = shm;
for(s=shm;*s!=NULL;s++)
{
sem_wait(mutex);
putchar(*s);
sem_post(mutex);
}
//once done signal exiting of reader:This can be replaced by
another semaphore
*shm = &#39;*&#39;;
sem_close(mutex);
shmctl(shmid, IPC_RMID, 0);
exit(0);
}

SYSTEM V信号量

这是信号量值的集合,而不是单个信号量。相关的信号量操作函数由

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
static int nsems;
static int semflg;
static int semid;
int errno=0;
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
}arg;
int main()
{
struct sembuf sops[2]; //要用到两个信号量,所以要定义两个操作数组
int rslt;
unsigned short argarray[80];
arg.array = argarray;
semid = semget(IPC_PRIVATE, 2, 0666);
if(semid < 0 )
{
printf("semget failed. errno: %d\n", errno);
exit(0);
}
//获取0th信号量的原始值
rslt = semctl(semid, 0, GETVAL);
printf("val = %d\n",rslt);
//初始化0th信号量,然后再读取,检查初始化有没有成功
arg.val = 1; // 同一时间只允许一个占有者
semctl(semid, 0, SETVAL, arg);
rslt = semctl(semid, 0, GETVAL);
printf("val = %d\n",rslt);
sops[0].sem_num = 0;
sops[0].sem_op = -1;
sops[0].sem_flg = 0;
sops[1].sem_num = 1;
sops[1].sem_op = 1;
sops[1].sem_flg = 0;
rslt=semop(semid, sops, 1); //申请0th信号量,尝试锁定
if (rslt < 0 )
{
printf("semop failed. errno: %d\n", errno);
exit(0);
}
//可以在这里对资源进行锁定
sops[0].sem_op = 1;
semop(semid, sops, 1); //释放0th信号量
rslt = semctl(semid, 0, GETVAL);
printf("val = %d\n",rslt);
rslt=semctl(semid, 0, GETALL, arg);
if (rslt < 0)
{
printf("semctl failed. errno: %d\n", errno);
exit(0);
}
printf("val1:%d val2: %d\n",(unsigned int)argarray[0],(unsigned int)argarray[1]);
if(semctl(semid, 1, IPC_RMID) == -1)
{
Perror(“semctl failure while clearing reason”);
}
return(0);
}

推荐学习: swoole教程

以上是swoole中信號量的用法是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn