Semaphore semget method return value confirmation

问题内容:

I want to get a same semId using same key.
Although, I called semget method using same key, and returned different semId.

Please answer me the reason why this problem happened.

Sample source :

int id1, id2;
int semflg = IPC_CREAT | 0666;
id1 = semget(0, 1, semflg);
id2 = semget(0, 1, semflg);

Result : id1 != id2

问题评论:

    
I think you need to add some tags to this question, since semget is not part of the C language.
    
@shlim what are the value of id1 and id2? Did you checked that none is having value -1?
    
id1, id2 value is some value which registed in IPC semaphore for example id1 = 786432, id2 = 819201
– shlim
9 hours ago

答案:

答案1:

Key id 0 is IPC_PRIVATE, and will always generate a new set of semaphores. Choose a valid key.

答案评论:

    
Thank you. I didn’t think that IPC_PRIVATE was 0. T.T
– shlim
9 hours ago

原文地址:

https://stackoverflow.com/questions/47747395/semaphore-semget-method-return-value-confirmation

添加评论