简体   繁体   English

在两个进程之间共享信号量数组| Linux C

[英]Share array of Semaphores between two processes | linux C

Everyone, I'm here to ask you how can I share an array of semaphores between 2 process? 大家好,我在这里问你如何在2个进程之间共享一个信号量数组? ?Since I created an array with semget(..) but I can't use shmat(..) on it ! ?因为我创建了一个带有semget(..)的数组,但是不能在其上使用shmat(..)

With shared memory segment I usually use shmget() first and then shmat(..) so the child process can access to it. 对于共享内存段,我通常先使用shmget() ,然后再使用shmat(..)以便子进程可以访问它。 But How it works with an array of semaphores? 但是它如何与一系列信号量一起使用?

I can't find any similar method able to attach() !!! 我找不到任何能够attach()的类似方法!

Here I Use the semget() to create an array of 5 semaphores: 在这里,我使用semget()创建一个由5个信号量组成的数组:

/* allocate semaphores */
    if ((semid = semget(IPC_PRIVATE,5,IPC_CREAT|0666)) == -1) {
        printf("shmget() fallita sem id\n");
        perror("semget()");
        //releaseAll(bufferid,Tid,Did,semid);
        exit(-4);

Here in another process I try to attach this array before use it (but i'm using shmat and seems not working..) 在这里,在另一个过程中,我尝试在使用此数组之前将其附加 (但是我正在使用shmat,似乎无法正常工作..)

sem_t* addr1;

    if((addr1=(sem_t*)shmat(semid,NULL,0))==-1){
        printf("shmat() fallita sem id\n");
        perror("shmat() for content");

        exit(-1);// +1 per \0 finale
    }

There are two kinds of semaphores available on linux: sysV and POSIX. 在Linux上有两种信号量:sysV和POSIX。 You are using sysV semaphores. 您正在使用sysV信号量。

sysV semaphores are maintained in the kernel so you don't need to allocate space for them or put them in shared memory to share them. sysV信号量在内核中维护,因此您无需为其分配空间或将其放入共享内存中即可共享它们。 But you do need a way for every process using them to find them. 但是您确实需要为每个使用它们的过程找到一种方法。 The mechanism to do that is key_t type. 该机制是key_t类型。

The function ftok takes a pathname and an id and returns a key_t . 函数ftok使用路径名和ID并返回key_t This allows every process specifying the proper path and id to generate the same key_t . 这允许每个指定正确路径和id的进程生成相同的key_t

semget takes the generated key_t , the number of requested semaphores, and some permissions flags and returns the semaphore identifier, that is the semid . semget接受生成的key_t ,请求的信号量的数量和一些权限标志,并返回信号量标识符,即semid The semid is used in all subsequent calls to semctl and semop . 在所有后续对semctlsemop调用中都使用semctl

An alternative, when you don't need to share the identity of the semaphore with other processes is to call semget with IPC_PRIVATE instead of a generated key_t . 当您不需要与其他进程共享信号灯的标识时,另一种方法是使用key_t而不是生成的key_t调用semget You are implicitly acknowledging by the use of IPC_PRIVATE that no unrelated processes need to know the returned semid and thus access the semaphore. 您通过使用semid隐式地确认,没有不相关的进程不需要知道返回的semid ,从而访问信号量。 This is useful for single processes with multiple threads that share memory and thus the semid ; 这对于具有多个线程共享内存的单进程很有用,从而可以进行semid ; or for related processes that create the semaphores before forking but share a copy of the semid after the fork. 或在分叉前创建信号灯但在分叉后共享Semid的相关过程。

So your problem seems to be that you have two unrelated processes that need to share some semaphores. 因此,您的问题似乎是您有两个不相关的进程需要共享一些信号量。 For that to work you need to use the ftok mechanism to generate the key so everyone involved can find them. 为此,您需要使用ftok机制生成密钥,以便所有相关人员都可以找到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM