Linux系統信號量機制互斥訪問共享內存區進行進程間通信
實現代碼
#include <bits/stdc++.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include <sys/shm.h>
using namespace std;
const int DELAY = 1;
const int BUFFER_SIZE = 1024;
const int SIGMA = 26;
union Semaphore {
int value;
semid_ds *buffer;
unsigned short *array;
seminfo *infomation;
};
static int shareMemoryID;
static int semaphoreID;
static bool initializeSemaphoreValue() {
Semaphore semaphoreUnion;
semaphoreUnion.value = 1;
return semctl(semaphoreID, 0, SETVAL, semaphoreUnion) == -1 ? false : true;
}
static bool semaphoreP() {
sembuf semaphoreBuffer;
semaphoreBuffer.sem_num = 0;
semaphoreBuffer.sem_op = -1;
semaphoreBuffer.sem_flg = SEM_UNDO;
return semop(semaphoreID, &semaphoreBuffer, 1) == -1 ? false : true;
}
static bool semaphoreV() {
sembuf semaphoreBuffer;
semaphoreBuffer.sem_num = 0;
semaphoreBuffer.sem_op = 1;
semaphoreBuffer.sem_flg = SEM_UNDO;
return semop(semaphoreID, &semaphoreBuffer, 1) == -1 ? false : true;
}
static void deleteSemaphoreSet() {
Semaphore semaphoreUnion;
if (semctl(semaphoreID, 0, IPC_RMID, semaphoreUnion) == -1) {
fprintf(stderr, "Failed to delete semaphore\n");
}
}
int main(int argc, char const *argv[])
{
if ((semaphoreID = semget(IPC_PRIVATE, 1, 0666 | IPC_CREAT)) == -1) {
fprintf(stderr, "Create semaphore set failed!\n");
abort();
}
if (!initializeSemaphoreValue()) {
fprintf(stderr, "Initialize semaphore failed!\n");
abort();
}
if ((shareMemoryID = shmget(IPC_PRIVATE, BUFFER_SIZE, 0666 | IPC_CREAT)) == -1) {
fprintf(stderr, "Shared memory created fail!\n");
abort();
}
pid_t pid = fork();
if (pid == -1) {
perror("Fork fail!\n");
abort();
} else {
char *buffer = (char*)shmat(shareMemoryID, NULL, 0);
if (pid == 0) {
while (true) {
if (!semaphoreP()) {
printf("Semaphore P failed!\n");
abort();
}
char ch = *buffer;
*buffer = tolower(ch);
if (!semaphoreV()) {
printf("Semaphore V failed!\n");
abort();
}
if (ch == 'Z') {
break;
}
}
} else {
for (int i = 0; i < SIGMA; i++) {
if (!semaphoreP()) {
printf("Semaphore P failed!\n");
abort();
}
*buffer = 'A' + i;
if (!semaphoreV()) {
printf("Semaphore V failed!\n");
abort();
}
usleep(100);
if (!semaphoreP()) {
printf("Semaphore P failed!\n");
abort();
}
printf("%c", *buffer);
fflush(stdout);
if (!semaphoreV()) {
printf("Semaphore V failed!\n");
abort();
}
sleep(DELAY);
}
}
if (shmdt(buffer) == -1) {
fprintf(stderr, "Delete share memory failed!\n");
abort();
}
}
if (pid > 0) {
wait(NULL);
deleteSemaphoreSet();
}
return 0;
}
相關函數
shmat()
- 函數原型
void *shmat(int shmid, const void *shmaddr, int shmflg)
- 參數
- shmid爲共享內存標識符
- shmaddr指定共享內存出現在進程內存地址的什麼位置,直接指定爲NULL讓內核自己決定一個合適的地址位置
- shmflg爲讀寫權限,設爲SHM_RDONLY爲只讀模式,其他爲讀寫模式
- 返回值
成功返回附加好的共享內存地址,否則返回-1
- 功能
連接共享內存標識符爲shmid的共享內存,連接成功後把共享內存區對象映射到調用進程的地址空間,隨後可像本地空間一樣訪問
shmget()
- 函數原型
int shmget(key_t key, size_t size, int shmflg)
- 參數
- key爲相應鍵值
- size爲新建共享內存大小
- shmflg爲讀寫權限
- 返回值
成功則返回共享內存標誌符,否則返回-1
- 功能
得到一個共享內存標識符或創建一個共享內存對象並返回共享內存標識符
shmdt()
- 函數原型
int shmdt(const void *shmaddr)
- 參數
- shmaddr爲連接的共享內存的起始地址
- 返回值
成功返回0,否則返回-1
- 功能
斷開與共享內存附加點的地址,禁止本進程訪問此片共享內存
輸出結果
abcdefghijklmnopqrstuvwxyz
注意
- 若註釋掉信號量相關代碼,不能保證兩個進程互斥訪問共享內存區,將會得到意料之外的結果
鳴謝
最後
- 由於博主水平有限,不免有疏漏之處,歡迎讀者隨時批評指正,以免造成不必要的誤解!