ron
ron

Reputation: 995

semaphore shmget() what does semflg = 384 do?

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

key_t  key    = IPC_PRIVATE;
int    id;
int    semflg = 384;

id = shmget( key, 8192, semflg );

Can someone tell me what affect the value of 384 has in semflg?

Upvotes: 1

Views: 200

Answers (1)

Patrick Trentin
Patrick Trentin

Reputation: 7342

The manpage of shmget says:

In addition to the above flags, the least significant 9 bits of shmflg specify the permissions granted to the owner, group, and others. These bits have the same format, and the same meaning, as the mode argument of open(2).

So let's check the manpage of open:

The following symbolic constants are provided for mode:
- S_IRWXU  00700 user (file owner) has read, write, and execute permission
- S_IRUSR  00400 user has read permission
- S_IWUSR  00200 user has write permission
- S_IXUSR  00100 user has execute permission
- S_IRWXG  00070 group has read, write, and execute permission
- S_IRGRP  00040 group has read permission
- S_IWGRP  00020 group has write permission
- S_IXGRP  00010 group has execute permission
- S_IRWXO  00007 others have read, write, and execute permission
- S_IROTH  00004 others have read permission
- S_IWOTH  00002 others have write permission
- S_IXOTH  00001 others have execute permission

The integer value 384 is encoded as 110000000 in binary, which corresponds to the flags 600, that is S_IRUSR|S_IWUSR:

  • user has write permission
  • user has read permission

Whoever wrote that piece of code spared 2 minutes of his life and wasted much more time of the lives of many others, me included.

Congratulations. :)


edit: thanks to @Fabio Turati for pointing out a major error ;)

Upvotes: 1

Related Questions