小编hum*_*uru的帖子

如何同步两个进程?

我想在同一工作站上的两个应用程序(进程)中使用一个HW接口.HW需要单个初始化调用,然后应用程序使用相同的函数(在同一个库中)与HW进行许多事务.

所以每个应用程序都应该这样:

main()
    // I don't know if another app already init'ed the HW
    ret = hw_init_lock(non-blocking)

    if ret = OK
        // no one else has done this, I have to
        init_hw()
    else
       //someone else has already init'ed the HW, I gotta make sure it stays that way
       //as long as I'm alive
       increment_hw_init_ref_counter()

    hw_trans_lock(blocking)
    hw_trans()
    hw_trans_unlock()
    ....

    //exit app, uninit hw if we are last out
    ret = decrement_hw_init_ref_counter()
    if ret == 0
        uninit_hw()

    exit(0)
Run Code Online (Sandbox Code Playgroud)

我可以在两个应用程序之间共享的锁定和引用计数调用中使用什么机制?我在想命名管道,即mkfifo().

hardware synchronization pthreads process named-pipes

6
推荐指数
2
解决办法
2万
查看次数