在本章中,我们将学习:
1.简介
2.如何获得System V国彩网量?
3.如何控制System V国彩网灯?
4.如何执行System V国彩网量?
5.如何销毁System V国彩网灯?
6.示例:SemInit.c
7.示例:SemDemo.c
8.示例:SemRemove.c
介绍:
在上一章中,我们了解了共享内存。当有多个进程试图访问和更改共享内存时,就会出现竞争状况的问题。
现在,我们有两种使用国彩网量的API。 System V国彩网灯是较旧的版本,而POSIX国彩网灯API是较新的版本。
在本章中,我们将学习System V国彩网量。
因此,要解决此问题,我们应该使用国彩网量来避免竞争情况。
我们将通过一系列简单的步骤然后再举一个完整的例子来学习国彩网量。
1.如何获得System V国彩网量?
要获取国彩网量,您需要使用“semget()” system call.
的function prototype is:
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int semget(key_t 键, int nsems, int semflg);
这里:
键 :它是唯一的标识符,如果任何进程想要连接到队列,则必须使用相同的密钥。
由于关键是“long”数据类型,可以使用任何整数来设置键。
或者你也可以使用“ftok()” known as “file to 键”。该函数接受2个参数,第一个是文件路径,第二个是ID。“ftok()”将使用这两个参数,然后创建一个唯一键。试图访问此队列的其他程序应使用相同的参数。
ftok的功能原型为:
键_t ftok(const char * path,int id);
nsems :这是当前国彩网量集中的国彩网量。
下面是创建国彩网量的方法:
#include <sys/ipc.h> #include <sys/sem.h> key_t 键; int Semid; key = ftok("/home/aj/myFile", 'E'); semid = semget(key, 10, 0666 | IPC_CREAT);
创建国彩网量后,它们’全部未初始化;要使它们免费,您需要使用semop()或semctl()。我们将在本章中进一步介绍。
2.如何控制System V国彩网灯?
要控制国彩网量使用“semctl()” system call.
“semctl()”将允许初始化国彩网量的正值,以使资源可用。
它允许您为单个国彩网量或完整的国彩网量设置。
函数原型是:
int semctl(int Semid, int semnum, int 指令, ... /*arg*/);
union semun { int val; /* used for 设定值 上 ly */ struct Semid_ds *buf; /* used for IPC_STAT and IPC_SET */ ushort *array; /* used for 得到所有 and 设置 */ };
3.如何执行System V国彩网灯的操作?
要对国彩网量执行操作,您需要使用semop()系统调用。
semop()的语法:
int semop(int Semid, struct sembuf *sops, unsigned int nsops);
这里:
Semid: It is the ID you get, when you call semget().
肥皂: 它是指向“struct sembuf”其中充满了国彩网量命令。
如果sem_op为负,则从国彩网量中减去其值。
如果sem_op为正,则为’的值添加到国彩网量。
如果sem_op为零(0),则调用过程将睡眠()直到国彩网量’s value is 0
4.如何销毁System V国彩网灯?
A semaphore can be destroyed by calling semctl() with the 指令 IPC_RMID.
例:
semctl(semid, 0, IPC_RMID);
SemInit.c [首先运行]
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> //for more tutorials 上 C, C ++, STL, DS visit www.ProDeveloperTutorial.com int main(void) { 键_t 键; int Semid; union semun arg; if ((key = ftok("semdemo.c", 'J')) == -1) { perror("ftok"); exit(1); } /* create a semaphore set with 1 semaphore: */ if ((semid = semget(key, 1, 0666 | IPC_CREAT)) == -1) { perror("semget"); exit(1); } /* initialize semaphore #0 to 1: */ arg.val = 1; if (semctl(semid, 0, 设定值, arg) == -1) { perror("semctl"); exit(1); } return 0; }
SemDemo.c
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> //for more tutorials 上 C, C ++, STL, DS visit www.ProDeveloperTutorial.com int main(void) { 键_t 键; int Semid; struct sembuf sb = {0, -1, 0}; /* set to allocate resource */ if ((key = ftok("semdemo.c", 'J')) == -1) { perror("ftok"); exit(1); } /* grab the semaphore set created by seminit.c: */ if ((semid = semget(key, 1, 0)) == -1) { perror("semget"); exit(1); } printf("Press return to lock: "); getchar(); printf("Trying to lock...\n"); if (semop(semid, &sb, 1) == -1) { perror("semop"); exit(1); } printf("Locked.\n"); printf("Press return to unlock: "); getchar(); sb.sem_op = 1; /* free resource */ if (semop(semid, &sb, 1) == -1) { perror("semop"); exit(1); } printf("Unlocked\n"); return 0; }
SemRemove.c
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> //for more tutorials 上 C, C ++, STL, DS visit www.ProDeveloperTutorial.com int main(void) { 键_t 键; int Semid; union semun arg; if ((key = ftok("semdemo.c", 'J')) == -1) { perror("ftok"); exit(1); } /* grab the semaphore set created by seminit.c: */ if ((semid = semget(key, 1, 0)) == -1) { perror("semget"); exit(1); } /* remove it: */ if (semctl(semid, 0, IPC_RMID, arg) == -1) { perror("semctl"); exit(1); } return 0; }
该网站上可用的教程列表:
C编程20+章 | C ++编程80+章 |
100多个编码问题 | 数据结构和算法85+章 |
系统设计20+章 | Shell脚本编写12章 |
4g LTE 60+章节 | 最常见的编码问题 |
5G NR 50+章 | Linux系统编程20+章 |