[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Semaphore lifecycle?



Hello!

I have a problem with a semaphore and I am not sure whats the problem. Its about the lifecycle of a semaphore after logout.



The workflow:
A program creates a semaphore and exits.
It is executed by a non-root user.
After execution the user quits the session (logout).



If I do the workflow on Debian 8 the semaphore is still here if I relogin.
Thats what I would expect. The semaphore should exists as long it is not deleted or the OS restarts.

If I do it on Debian 9 the semaphore is deleted after I relogin and check (ipcs).

If I do it on Debian 9 with root or sudo the behaviour the same as it is on Debian 8.



Heres a little programm in C++ for testing.
----sem.cpp------------------------------------
int main (void)
{
    printf ("\n*** SEM ***");
    long semkey = 0x99112233;
    printf ("\nkey=%x", semkey);
    long sem = semget (semkey, 1, IPC_CREAT | IPC_EXCL | 0666);
    if (sem == -1)
    {
        printf ("\nunable to create SEM (perhaps an old exists, try to delete)");
        sem = semget (semkey, 1, 0666);
        if (sem == -1)
        {
            printf ("\nunable to get old SEM");
        }
        else
        {
            if (semctl (sem, 0, IPC_RMID) == 0)
            {
                printf ("\nold SEM deleted");
            }
            else
            {
                printf ("\nunable to delete old SEM\n");
                return 1;
            }
        }

        printf ("\nretry to create SEM");
        sem = semget (semkey, 1, IPC_CREAT | IPC_EXCL | 0666);
        if (sem == -1)
        {
            printf ("\nunable to create SEM\n");
            return 1;
        }
    }
    if (semctl (sem, 0, SETVAL, 1) == -1)        // exactly 1 in critical-section
    {
        printf ("\nunable to init SEM\n");
        return 1;
    }

    printf ("\nOK\n");
    return 0;
}
---------------------------

Compile with g++ sem.cpp.


If the program also creates a shared memory that will stay after logout if executed by non-root.
Also if I start the program with nohup and & it will stay executed.
But the semaphore disappears.

???

Cheers,
CH


Reply to: