当前位置: 技术问答>linux和unix
关于共享内存的建立和返回的大小
来源: 互联网 发布时间:2016-07-12
本文导语: 我写了个小程序创建共享内存.但创建完后ipcs -t显示如下: m 3777551 0x0012d687 -C--------- indbwork neuapp 请问-C是什么意思啊? 然后程序返回的创建共享内存的大小是0呢 程序如下: #include "bank_console.h" int main(int a...
我写了个小程序创建共享内存.但创建完后ipcs -t显示如下:
m 3777551 0x0012d687 -C--------- indbwork neuapp
请问-C是什么意思啊?
然后程序返回的创建共享内存的大小是0呢
程序如下:
#include "bank_console.h"
int main(int argc,char* argv[])
{
int i,shm_size=0;
printf("okn");
shm_size=init_system(1234567);
printf("share_mem size is %dn",shm_size);
return 0;
}
int init_system(key_t key)
{
int shm_size=0;
struct shmid_ds shm_buff;
OPER_NODE* pOper_Node;
shm_id=shmget(key,sizeof(OPER_NODE)*5,IPC_CREAT|IPC_EXCL);
if (shm_id == -1)
fprintf(stderr, "Create Share Memory Error:%sna", strerror(errno));
pOper_Node=(OPER_NODE*)shmat(shm_id,0,0);
shmctl(shm_id,IPC_STAT,&shm_buff);
return shm_buff.shm_segsz;
}
为什么返回的内存大小是0呢?而且查看共享内存显示是什么-C就更迷糊了
m 3777551 0x0012d687 -C--------- indbwork neuapp
请问-C是什么意思啊?
然后程序返回的创建共享内存的大小是0呢
程序如下:
#include "bank_console.h"
int main(int argc,char* argv[])
{
int i,shm_size=0;
printf("okn");
shm_size=init_system(1234567);
printf("share_mem size is %dn",shm_size);
return 0;
}
int init_system(key_t key)
{
int shm_size=0;
struct shmid_ds shm_buff;
OPER_NODE* pOper_Node;
shm_id=shmget(key,sizeof(OPER_NODE)*5,IPC_CREAT|IPC_EXCL);
if (shm_id == -1)
fprintf(stderr, "Create Share Memory Error:%sna", strerror(errno));
pOper_Node=(OPER_NODE*)shmat(shm_id,0,0);
shmctl(shm_id,IPC_STAT,&shm_buff);
return shm_buff.shm_segsz;
}
为什么返回的内存大小是0呢?而且查看共享内存显示是什么-C就更迷糊了
|
|
-C--------- 表示你的共享内存区在第一次attach时被清空,并且你没有访问权限。
返回0很可能就是因为你没有访问权限。
MODE
(all) the facility access modes and flags. The mode consists of 11 characters that are interpreted as follows:
The first two characters can be the following:
R
If a process is waiting on a msgrcv system call.
S
If a process is waiting on a msgsnd system call.
D
If the associated shared memory segment has been removed. It disappears when the last process attached
to the segment detaches it.
C
If the associated shared memory segment is to be cleared when the first attach is run.
-
If the corresponding special flag is not set.
The next nine characters are interpreted as three sets of 3 bits each. The first set refers to the owner's
permissions; the next to permissions of others in the user group of the facility entry; and the last to all
others. Within each set, the first character indicates permission to read, the second character indicates
permission to write or alter the facility entry, and the last character is currently unused.
The permissions are indicated as follows:
r
If read permission is granted.
w
If write permission is granted.
a
If alter permission is granted.
-
If the indicated permission is not granted.
返回0很可能就是因为你没有访问权限。
MODE
(all) the facility access modes and flags. The mode consists of 11 characters that are interpreted as follows:
The first two characters can be the following:
R
If a process is waiting on a msgrcv system call.
S
If a process is waiting on a msgsnd system call.
D
If the associated shared memory segment has been removed. It disappears when the last process attached
to the segment detaches it.
C
If the associated shared memory segment is to be cleared when the first attach is run.
-
If the corresponding special flag is not set.
The next nine characters are interpreted as three sets of 3 bits each. The first set refers to the owner's
permissions; the next to permissions of others in the user group of the facility entry; and the last to all
others. Within each set, the first character indicates permission to read, the second character indicates
permission to write or alter the facility entry, and the last character is currently unused.
The permissions are indicated as follows:
r
If read permission is granted.
w
If write permission is granted.
a
If alter permission is granted.
-
If the indicated permission is not granted.