2013-04-08 56 views
0

我正試圖獲得信號量的懸念。我寫了一個程序,它與信號量交易和中,我有這個功能來創建一個單一的信號:爲什麼信號量被獲取而未被創建?

int semcreate() 
{ 
    semp = semget(ftok("./output.txt", 'a'),1,IPC_CREAT|IPC_EXCL|0664); 

    if(semp == -1) /*Semaphore exists, fetch*/ 
    { 
     printf("Semaphore fetched\n"); 
     semp = semget(ftok("./output.txt", 'a'), 1, 0); 
    } 
    else if(semp>=0) /*Semaphore created here*/ 
    { 
     printf("Semaphore created\n"); 
     seminit(); /* Initialize semaphore*/ 
    } 
    return 1; 
} 

當我運行兩個單獨的終端這個程序,這兩個實例中的打印Semaphore fetched。這是正確的行爲?爲什麼?

enter image description here

回答

0

也許是信號系統上存在。使用命令ipcs列出系統中的所有信號量,並使用ipcrm刪除程序創建的信號量。