chris01
chris01

Reputation: 12331

Linux Semaphore Lifecycle

I have an old C++ STL 11 program that uses a semaphore and a shared memory. It is running for many years like a charm (Debian 7). Its executed under root.

Now I migrated to a new server (Debian 9) and it runs under a non-root user.

The program is started in the background with nohup and &.

As soon I logout in the new environment the semaphore disappears (not anymore listed when ipcs). But the shared memory is still available and the process is also executed what I do not understand. Why are not all IPC-elements are handled the same way?

Any ideas for that??

EDIT If I start it with sudo the semaphore is owned by root and exists even if I logout.

Upvotes: 0

Views: 100

Answers (1)

bk2204
bk2204

Reputation: 76539

Any process that's started in the background by a shell will get SIGHUP when you log out. If your process doesn't handle that, it will be terminated.

If you want to preserve a process that you've started this way, you can either use the disown shell builtin to avoid that or start the process with nohup.

The reason this succeeds when you run it as root is because your unprivileged user cannot signal a root-owned process and therefore the shell's attempt to do so fails silently.

The semaphore is, in this case, a red herring.

Upvotes: 1

Related Questions