为什么信号量被提取而不是被创建?

时间:2013-04-08 13:58:35

标签: c linux ipc semaphore

我正试图掌握信号量。我编写了一个处理信号量的程序,我用它来创建一个信号量:

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

1 个答案:

答案 0 :(得分:0)

系统上可能存在信号量。使用命令ipcs列出系统中的所有信号量,并使用ipcrm删除程序创建的信号量。

相关问题