2014-11-22 78 views
1

我試圖在兩個進程之間共享未命名的mach信號量。 我可以創建一個並在同一個進程中等待它。在Mac OS中的進程之間共享未命名的信號燈

semaphore_t semaphore = 0; 
mach_error_t err = semaphore_create(mach_task_self(), &semaphore, SYNC_POLICY_FIFO, 0); 
... 
semaphore_wait(semaphore); 

但我想將它發送到另一個進程(其中我只有mach_port_t),然後讓它semaphore_signal我自己的過程。

我已經嘗試過的東西,如:

mach_port_allocate(target, MACH_PORT_RIGHT_RECEIVE, targetSemaphore) 
mach_port_insert_right(target, targetSemaphore, semaphore, MACH_MSG_TYPE_COPY_SEND) 

因爲端口名稱已在目標進程或「未知失敗」如果我沒有在目標進程分配存在這將產生一個錯誤。

甚至:

mach_msg_send 
mach_msg_receive 

但我不能連一個端口正確的形式一個進程花葯送任何東西。

我做錯了什麼,它甚至有可能嗎?

回答

1

我想通了:

mach_port_extract_right 

是正確的方法,而不是:

mach_port_insert_right 

然後這樣做,會做的工作:

semaphore_t semaphore = 0; 
mach_error_t err = semaphore_create(mach_task_self(), &semaphore, SYNC_POLICY_FIFO, 0); 

err = mach_port_allocate(target, MACH_PORT_RIGHT_RECEIVE, &receivePort); 

mach_msg_type_name_t type; 
semaphore_t sendPort = 0; 
err = mach_port_extract_right(target, receivePort, MACH_MSG_TYPE_MAKE_SEND, &sendPort, &type); 

//Send semaphore using port 
mach_msg_send(&msg.header); 
相關問題