亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 JNA 發送到 POSIX 信號量

使用 JNA 發送到 POSIX 信號量

慕少森 2023-03-17 15:27:00
我正在嘗試在 Linux 機器上使用 JNA 發布到信號量。出于某種原因,即使對于這個簡單的示例,我也總是收到 22 錯誤(無效參數)。以我的理解,下面的代碼不應該打開一個 POSIX 信號量,發布到它并再次關閉它嗎?public class Sample {  private static final int O_CREAT = 0x40;  public static void main(String[] args) throws Exception {    File notifier = new File("/tmp", "_test" + new Random().nextInt());      if (!notifier.isFile() && !notifier.createNewFile()) {        throw new IllegalStateException("Could not create notifier: " + notifier);      }      SempahoreLibrary library = Native.load("c", SempahoreLibrary.class);      Pointer semaphore = library.sem_open(notifier.getAbsolutePath(), O_CREAT, 666, 0);      try {        library.sem_post(semaphore);      } finally {        library.sem_close(semaphore);      }  }  interface SempahoreLibrary extends Library {    Pointer sem_open(String name, int flags, int mode, int value) throws LastErrorException;    int sem_post(Pointer pointer) throws LastErrorException;    int sem_close(Pointer pointer) throws LastErrorException;  }}
查看完整描述

1 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

我最初也不能讓它與 JNR 一起工作(強烈推薦而不是 JNA),并且很好奇。用 C 編寫它有幫助.. :)


C 端口上的 strace 清楚地表明您不必預先創建文件,然后將信號量“映射”到它。使用完整路徑也是錯誤的,因為信號量是在 /dev/shm 中創建的,而路徑中的“/”搞砸了一切:


futex(0x7f731b1190d0, FUTEX_WAKE_PRIVATE, 2147483647) = 0

openat(AT_FDCWD, "/dev/shm/sem.sema", O_RDWR|O_NOFOLLOW) = 3

fstat(3, {st_mode=S_IFREG|0644, st_size=32, ...}) = 0

因此,您應該能夠刪除整個文件/路徑創建,并在 sem_open 中為信號量使用常規的非路徑名稱。此外,文件模式應該是八進制的,并且您應該確保還加載了 pthread 庫——這是必需的。


這是 C 中的一個工作示例:


// clang -Wall sema.c -lpthread


#include <fcntl.h>

#include <sys/stat.h>

#include <semaphore.h>

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>


int main(int argc, char** argv)

{

    sem_t* s = sem_open("notifier", O_CREAT, 0644, 0);


    if (!s) {

        perror("sem_open");

        exit(errno);

    }


    printf("s: %p\n", s);


    sem_post(s);


    int value = -1;

    sem_getvalue(s, &value);

    printf("value: %d\n", value);


    sem_wait(s);

    sem_getvalue(s, &value);

    printf("value: %d\n", value);


    sem_close(s);


    exit(EXIT_SUCCESS);

}

這是一個使用 JNR 的工作 Java 版本:


import jnr.ffi.LastError;

import jnr.ffi.LibraryLoader;

import jnr.ffi.Pointer;

import jnr.ffi.Runtime;


public class Semaphore

{

    private static final int O_CREAT = 0x40;


    public interface SempahoreLibrary

    {

        Pointer sem_open(String name, int flags, int mode, int value);

        int sem_post(Pointer pointer);

        int sem_close(Pointer pointer);

    }


    public static void main(String[] args) throws Exception

    {


        LibraryLoader<SempahoreLibrary> loader = LibraryLoader.create(SempahoreLibrary.class);

        loader.library("c");

        loader.library("pthread");


        SempahoreLibrary library = loader.load();

        jnr.ffi.Runtime runtime = Runtime.getRuntime(library);


        Pointer semaphore = library.sem_open("notifier", O_CREAT, 0644, 0);

        if (semaphore == null)

        {

            int errno = LastError.getLastError(runtime);

            System.out.println("sem_open: " + errno);

            System.exit(errno);

        }


        System.out.println("semaphore: " + Long.toHexString(semaphore.address()));


        try

        {

            int error = library.sem_post(semaphore);

            System.out.println("post: " + (error == 0 ? "OK" : LastError.getLastError(runtime)));

        }

        finally

        {

            int error = library.sem_close(semaphore);

            System.out.println("close: " + (error == 0 ? "OK" : LastError.getLastError(runtime)));

        }

    }

}


查看完整回答
反對 回復 2023-03-17
  • 1 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號