Linux 上的 msemaphore?

发布于 2024-07-19 07:42:18 字数 299 浏览 10 评论 0原文

AIX(还有 HPUX,如果有人关心的话)有一个很好的小功能,称为 msemaphores,它可以轻松地同步多个进程共享的内存映射文件的细粒度部分(例如记录)。 有谁知道linux中有类似的东西吗?

需要明确的是,msemaphore 函数通过相关链接进行描述 此处

AIX (and HPUX if anyone cares) have a nice little feature called msemaphores that make it easy to synchronize granular pieces (e.g. records) of memory-mapped files shared by multiple processes. Is anyone aware of something comparable in linux?

To be clear, the msemaphore functions are described by following the related links here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可遇━不可求 2024-07-26 07:42:18

POSIX 信号量可以放置在进程之间共享的内存中,如果sem_init(3) 的第二个参数“pshared”为 true。 这似乎与 msem 的作用相同。

#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
int main() {
    void *shared;
    sem_t *sem;
    int counter, *data;
    pid_t pid;
    srand(time(NULL));
    shared = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE,
            MAP_ANONYMOUS | MAP_SHARED, -1, 0);
    sem_init(sem = shared, 1, 1);
    data = shared + sizeof(sem_t);
    counter = *data = 0;
    pid = fork();
    while (1) {
        sem_wait(sem);
        if (pid)
            printf("ping>%d %d\n", data[0] = rand(), data[1] = rand());
        else if (counter != data[0]) {
            printf("pong<%d", counter = data[0]);
            sleep(2);
            printf(" %d\n", data[1]);
        }
        sem_post(sem);
        if (pid) sleep(1);
    }
}

这是一个相当愚蠢的测试,但它有效:

$ cc -o test -lrt test.c
$ ./test
ping>2098529942 315244699
pong<2098529942 315244699
pong<1195826161 424832009
ping>1195826161 424832009
pong<1858302907 1740879454
ping>1858302907 1740879454
ping>568318608 566229809
pong<568318608 566229809
ping>1469118213 999421338
pong<1469118213 999421338
ping>1247594672 1837310825
pong<1247594672 1837310825
ping>478016018 1861977274
pong<478016018 1861977274
ping>1022490459 935101133
pong<1022490459 935101133
...

因为信号量在两个进程之间共享,所以 pong 不会从 ping 获取交错数据,尽管睡觉s。

POSIX semaphores can be placed in memory shared between processes, if the second argument to sem_init(3), "pshared", is true. This seems to be the same as what msem does.

#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
int main() {
    void *shared;
    sem_t *sem;
    int counter, *data;
    pid_t pid;
    srand(time(NULL));
    shared = mmap(NULL, sysconf(_SC_PAGE_SIZE), PROT_READ | PROT_WRITE,
            MAP_ANONYMOUS | MAP_SHARED, -1, 0);
    sem_init(sem = shared, 1, 1);
    data = shared + sizeof(sem_t);
    counter = *data = 0;
    pid = fork();
    while (1) {
        sem_wait(sem);
        if (pid)
            printf("ping>%d %d\n", data[0] = rand(), data[1] = rand());
        else if (counter != data[0]) {
            printf("pong<%d", counter = data[0]);
            sleep(2);
            printf(" %d\n", data[1]);
        }
        sem_post(sem);
        if (pid) sleep(1);
    }
}

This is a pretty dumb test, but it works:

$ cc -o test -lrt test.c
$ ./test
ping>2098529942 315244699
pong<2098529942 315244699
pong<1195826161 424832009
ping>1195826161 424832009
pong<1858302907 1740879454
ping>1858302907 1740879454
ping>568318608 566229809
pong<568318608 566229809
ping>1469118213 999421338
pong<1469118213 999421338
ping>1247594672 1837310825
pong<1247594672 1837310825
ping>478016018 1861977274
pong<478016018 1861977274
ping>1022490459 935101133
pong<1022490459 935101133
...

Because the semaphore is shared between the two processes, the pongs don't get interleaved data from the pings despite the sleeps.

稳稳的幸福 2024-07-26 07:42:18

这可以使用 POSIX 共享内存互斥体来完成:

pthread_mutexattr_t attr;
int pshared = PTHREAD_PROCESS_SHARED;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, &pshared);

pthread_mutex_init(&some_shared_mmap_structure.mutex, &attr);
pthread_mutexattr_destroy(&attr);

现在您可以使用普通的 pthread_mutex_lock() 等调用从映射的多个进程中解锁和锁定 &some_shared_mmap_struct.mutex 。

事实上,您甚至可以按照以下方式实现 msem API:(未经测试)

struct msemaphore {
    pthread_mutex_t mut;
};

#define MSEM_LOCKED 1
#define MSEM_UNLOCKED 0
#define MSEM_IF_NOWAIT 1

msemaphore *msem_init(msemaphore *msem_p, int initialvalue) {
    pthread_mutex_attr_t attr;
    int pshared = PTHREAD_PROCESS_SHARED;

    assert((unsigned long)msem_p & 7 == 0); // check alignment

    pthread_mutexattr_init(&attr);
    pthread_mutexattr_setpshared(&attr, &pshared); // might fail, you should probably check
    pthread_mutex_init(&msem_p->mut, &attr); // never fails
    pthread_mutexattr_destroy(&attr);

    if (initialvalue)
        pthread_mutex_lock(&attr);

    return msem_p;
}

int msem_remove(msemaphore *msem) {
    return pthread_mutex_destroy(&msem->mut) ? -1 : 0;
}

int msem_lock(msemaphore *msem, int cond) {
    int ret;
    if (cond == MSEM_IF_NOWAIT)
        ret = pthread_mutex_trylock(&msem->mut);
    else
        ret = pthread_mutex_lock(&msem->mut);

    return ret ? -1 : 0;
}

int msem_unlock(msemaphore *msem, int cond) {
    // pthreads does not allow us to directly ascertain whether there are
    // waiters. However, a unlock/trylock with no contention is -very- fast
    // using linux's pthreads implementation, so just do that instead if
    // you care.
    //
    // nb, only fails if the mutex is not initialized
    return pthread_mutex_unlock(&msem->mut) ? -1 : 0;
}

This can be done using POSIX shared-memory mutexes:

pthread_mutexattr_t attr;
int pshared = PTHREAD_PROCESS_SHARED;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, &pshared);

pthread_mutex_init(&some_shared_mmap_structure.mutex, &attr);
pthread_mutexattr_destroy(&attr);

Now you can unlock and lock &some_shared_mmap_structure.mutex using ordinary pthread_mutex_lock() etc calls, from multiple processes that have it mapped.

Indeed, you can even implement the msem API in terms of this: (untested)

struct msemaphore {
    pthread_mutex_t mut;
};

#define MSEM_LOCKED 1
#define MSEM_UNLOCKED 0
#define MSEM_IF_NOWAIT 1

msemaphore *msem_init(msemaphore *msem_p, int initialvalue) {
    pthread_mutex_attr_t attr;
    int pshared = PTHREAD_PROCESS_SHARED;

    assert((unsigned long)msem_p & 7 == 0); // check alignment

    pthread_mutexattr_init(&attr);
    pthread_mutexattr_setpshared(&attr, &pshared); // might fail, you should probably check
    pthread_mutex_init(&msem_p->mut, &attr); // never fails
    pthread_mutexattr_destroy(&attr);

    if (initialvalue)
        pthread_mutex_lock(&attr);

    return msem_p;
}

int msem_remove(msemaphore *msem) {
    return pthread_mutex_destroy(&msem->mut) ? -1 : 0;
}

int msem_lock(msemaphore *msem, int cond) {
    int ret;
    if (cond == MSEM_IF_NOWAIT)
        ret = pthread_mutex_trylock(&msem->mut);
    else
        ret = pthread_mutex_lock(&msem->mut);

    return ret ? -1 : 0;
}

int msem_unlock(msemaphore *msem, int cond) {
    // pthreads does not allow us to directly ascertain whether there are
    // waiters. However, a unlock/trylock with no contention is -very- fast
    // using linux's pthreads implementation, so just do that instead if
    // you care.
    //
    // nb, only fails if the mutex is not initialized
    return pthread_mutex_unlock(&msem->mut) ? -1 : 0;
}
时间海 2024-07-26 07:42:18

在Linux下,你也许可以用SysV共享内存来实现你想要的; 快速谷歌搜索发现这个(相当旧的)指南可能会有所帮助。

Under Linux, you may be able to achieve what you want with SysV shared memory; quick googling turned up this (rather old) guide that may be of help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文