当前位置: 技术问答>linux和unix
位运算的问题。
来源: 互联网 发布时间:2016-03-16
本文导语: #include #include #include #include int main(void) { key_t key; int msqid; int proj_id; key=IPC_PRIVATE; msqid=msgget(key,0777); if(msqid==-1) perror("cannot create message queue"); else printf("1. key=IPC_PRIVATE, message queue msqid= %dn",msqid); p...
#include
#include
#include
#include
int main(void)
{
key_t key;
int msqid;
int proj_id;
key=IPC_PRIVATE;
msqid=msgget(key,0777);
if(msqid==-1)
perror("cannot create message queue");
else
printf("1. key=IPC_PRIVATE, message queue msqid= %dn",msqid);
proj_id=1;
key=ftok("/home/program",proj_id);
if(key==-1)
perror("cannot generate messge queue key");
msqid=msgget(key,IPC_CREAT|0777);
if(msqid==-1)
perror("cannot create message queue");
else
printf("2. key=%d generated by ftok, message queue msqid= %dn",key,msqid);
msqid=msgget(key,IPC_CREAT |IPC_EXCL |0777);
if(msqid==-1)
perror("cannot create message queue");
msqid=msgget(key,IPC_CREAT |0777);
if(msqid==-1)
perror("cannot create message queue");
else
printf("Access the existing message queuen");
return 0;
}
-------------------------
msqid=msgget(key,IPC_CREAT |IPC_EXCL |0777);
这里的IPC_CREAT |IPC_EXCL |0777是什么意思?为什么都要用位运算连接起来?
#include
#include
#include
int main(void)
{
key_t key;
int msqid;
int proj_id;
key=IPC_PRIVATE;
msqid=msgget(key,0777);
if(msqid==-1)
perror("cannot create message queue");
else
printf("1. key=IPC_PRIVATE, message queue msqid= %dn",msqid);
proj_id=1;
key=ftok("/home/program",proj_id);
if(key==-1)
perror("cannot generate messge queue key");
msqid=msgget(key,IPC_CREAT|0777);
if(msqid==-1)
perror("cannot create message queue");
else
printf("2. key=%d generated by ftok, message queue msqid= %dn",key,msqid);
msqid=msgget(key,IPC_CREAT |IPC_EXCL |0777);
if(msqid==-1)
perror("cannot create message queue");
msqid=msgget(key,IPC_CREAT |0777);
if(msqid==-1)
perror("cannot create message queue");
else
printf("Access the existing message queuen");
return 0;
}
-------------------------
msqid=msgget(key,IPC_CREAT |IPC_EXCL |0777);
这里的IPC_CREAT |IPC_EXCL |0777是什么意思?为什么都要用位运算连接起来?
|
对楼主的程序
IPC_CREAT表示如果消息队列不存在就新建.
IPC_CREAT|IPC_EXCL表示建立新消息队列, 如果消息队列已经存在, 返回错误EEXIST.
0777表示消息队列的访问模式为.
用位或运算连接起来是为了把这些信息放在一个参数msgflg里.
看一下下面的定义, 可以注意到以下的宏IPC_CREAT等等, 只有一位二进制位是1, 就应该能理解了.
#define IPC_CREAT 001000 /* create entry if key does not exist */
#define IPC_EXCL 002000 /* fail if key exists */
#define IPC_NOWAIT 004000 /* error if request must wait */
#define S_IRUSR 0000400 /* R for owner */
#define S_IWUSR 0000200 /* W for owner */
#define S_IXUSR 0000100 /* X for owner */
#define S_IRGRP 0000040 /* R for group */
#define S_IWGRP 0000020 /* W for group */
#define S_IXGRP 0000010 /* X for group */
#define S_IROTH 0000004 /* R for other */
#define S_IWOTH 0000002 /* W for other */
#define S_IXOTH 0000001 /* X for other */
IPC_CREAT表示如果消息队列不存在就新建.
IPC_CREAT|IPC_EXCL表示建立新消息队列, 如果消息队列已经存在, 返回错误EEXIST.
0777表示消息队列的访问模式为.
用位或运算连接起来是为了把这些信息放在一个参数msgflg里.
看一下下面的定义, 可以注意到以下的宏IPC_CREAT等等, 只有一位二进制位是1, 就应该能理解了.
#define IPC_CREAT 001000 /* create entry if key does not exist */
#define IPC_EXCL 002000 /* fail if key exists */
#define IPC_NOWAIT 004000 /* error if request must wait */
#define S_IRUSR 0000400 /* R for owner */
#define S_IWUSR 0000200 /* W for owner */
#define S_IXUSR 0000100 /* X for owner */
#define S_IRGRP 0000040 /* R for group */
#define S_IWGRP 0000020 /* W for group */
#define S_IXGRP 0000010 /* X for group */
#define S_IROTH 0000004 /* R for other */
#define S_IWOTH 0000002 /* W for other */
#define S_IXOTH 0000001 /* X for other */
|
man msgget
[code=BatchFile]
msgget(2) System Calls msgget(2)
NAME
msgget - get message queue
SYNOPSIS
#include
int msgget(key_t key, int msgflg);
DESCRIPTION
The msgget() argument returns the message queue identifier associated
with key.
A message queue identifier and associated message queue and data struc-
ture (see intro(2)) are created for key if one of the following are
true:
o key is IPC_PRIVATE.
o key does not already have a message queue identifier associated
with it, and (msgflg&IPC_CREAT) is true.
On creation, the data structure associated with the new message queue
identifier is initialized as follows:
o msg_perm.cuid, msg_perm.uid, msg_perm.cgid, and msg_perm.gid are
set to the effective user ID and effective group ID, respec-
tively, of the calling process.
o The low-order 9 bits of msg_perm.mode are set to the low-order 9
bits of msgflg.
o msg_qnum, msg_lspid, msg_lrpid, msg_stime, and msg_rtime are set
to 0.
o msg_ctime is set to the current time.
o msg_qbytes is set to the system limit.
RETURN VALUES
Upon successful completion, a non-negative integer representing a mes-
sage queue identifier is returned. Otherwise, -1 is returned and errno
is set to indicate the error.
ERRORS
The msgget() function will fail if:
EACCES
A message queue identifier exists for key, but operation permis-
sion (see intro(2)) as specified by the low-order 9 bits of
msgflg would not be granted.
EEXIST
A message queue identifier exists for key but (msgflg&IPC_CREAT)
and (msgflg&IPC_EXCL) are both true.
ENOENT
A message queue identifier does not exist for key and
(msgflg&IPC_CREAT) is false.
ENOSPC
A message queue identifier is to be created but the system-
imposed limit on the maximum number of allowed message queue
identifiers system wide would be exceeded.
SEE ALSO
intro(2), msgctl(2), msgrcv(2), msgsnd(2), ftok(3C)
SunOS 5.9 5 Feb 1996 msgget(2)
[/code]
与int open(const char *path, int oflag, /* mode_t mode */...);
差不多, 只是msgget把oflag和mode合在一起成msgflg了.
[code=BatchFile]
msgget(2) System Calls msgget(2)
NAME
msgget - get message queue
SYNOPSIS
#include
int msgget(key_t key, int msgflg);
DESCRIPTION
The msgget() argument returns the message queue identifier associated
with key.
A message queue identifier and associated message queue and data struc-
ture (see intro(2)) are created for key if one of the following are
true:
o key is IPC_PRIVATE.
o key does not already have a message queue identifier associated
with it, and (msgflg&IPC_CREAT) is true.
On creation, the data structure associated with the new message queue
identifier is initialized as follows:
o msg_perm.cuid, msg_perm.uid, msg_perm.cgid, and msg_perm.gid are
set to the effective user ID and effective group ID, respec-
tively, of the calling process.
o The low-order 9 bits of msg_perm.mode are set to the low-order 9
bits of msgflg.
o msg_qnum, msg_lspid, msg_lrpid, msg_stime, and msg_rtime are set
to 0.
o msg_ctime is set to the current time.
o msg_qbytes is set to the system limit.
RETURN VALUES
Upon successful completion, a non-negative integer representing a mes-
sage queue identifier is returned. Otherwise, -1 is returned and errno
is set to indicate the error.
ERRORS
The msgget() function will fail if:
EACCES
A message queue identifier exists for key, but operation permis-
sion (see intro(2)) as specified by the low-order 9 bits of
msgflg would not be granted.
EEXIST
A message queue identifier exists for key but (msgflg&IPC_CREAT)
and (msgflg&IPC_EXCL) are both true.
ENOENT
A message queue identifier does not exist for key and
(msgflg&IPC_CREAT) is false.
ENOSPC
A message queue identifier is to be created but the system-
imposed limit on the maximum number of allowed message queue
identifiers system wide would be exceeded.
SEE ALSO
intro(2), msgctl(2), msgrcv(2), msgsnd(2), ftok(3C)
SunOS 5.9 5 Feb 1996 msgget(2)
[/code]
与int open(const char *path, int oflag, /* mode_t mode */...);
差不多, 只是msgget把oflag和mode合在一起成msgflg了.
|
这里的IPC_CREAT ¦IPC_EXCL ¦0777是什么意思?为什么都要用位运算连接起来
是因为msgflg是一个整形变量,转化作二进制的话,每一位都有相对应的含义。通过位操作可以对多个标志位进行置位
是因为msgflg是一个整形变量,转化作二进制的话,每一位都有相对应的含义。通过位操作可以对多个标志位进行置位