的shmat()返回一个不同的" shmaddr所"对于相同的" shmkey" [英] shmat() is returning a different "shmaddr" for same "shmkey"

查看:597
本文介绍了的shmat()返回一个不同的" shmaddr所"对于相同的" shmkey"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的设置...

Here's my setup...

/* Bounded Buffer item structure */
struct item {
    int  id;  /* string index value */
    char str[80];  /* string value */
};

/* Structure for the shared memory region */
typedef struct {
    int    debug;           /* debug flag */
    int    in;              /* index of next empty slot */    
    int    out;             /* index of next full slot  */
    char   MUTEXname[32];   /* name of the MUTEX semaphore */
    char   EMPTYname[32];   /* name of the EMPTY semaphore */
    char   FULLname[32];    /* name of the FULL semaphore  */
    struct item buff[BUFFSIZE];  /* circular buffer for producer/consumer items*/
    char   strarray[MAX_STRINGS][80]; /* shared array of strings for consumers */
} shr_mem_t;

/* Values for obtaining a shmid key via ftok() */
#define KEYPATH "."
#define KEYPROJ 4520

主要:(会fork()的生产者和消费者的过程)

Main: (will fork() the "Producer" and "Consumer" processes)

/* Use ftok() to get a value for a key to identify a shared memory segment */
shm_key = ftok(KEYPATH, KEYPROJ);

/* Create the shared memory segment */
shmid = shmget(shm_key, sizeof(shr_mem_t), IPC_CREAT | IPC_EXCL | 0660);

/* Attach shared memory segment to the parent process */
shmptr = shmat(shmid, NULL, 0);

制作人:

/* Use ftok() to get value for the key to identify the shared memory segment */
shm_key = ftok(KEYPATH, KEYPROJ);

/* Get the ID of the existing shared memory segment */
shmid = shmget(shm_key, sizeof(shr_mem_t), 0660);

/* Attach the shared memory segment */
shmptr = shmat(shmid, NULL, 0);

消费者:

/* Use ftok() to get value for the key to identify the shared memory segment */
shm_key = ftok(KEYPATH, KEYPROJ);

/* Get the ID of the existing shared memory segment */
shmid = shmget(shm_key, sizeof(shr_mem_t), 0660);   

/* Attach the shared memory segment */
shmptr = shmat(shmid, NULL, 0);

错误检测:(为简便起见...)

Error testing: (for the sake of brevity...)

if (shmid == -1) {
    perror("shmget failed in __________");
    exit(1);
} else {
    sprintf(errString, "<*> __________ shared memory id: %i ", shmid);
    perror(errString);
}

if(shmptr == (void *)(-1)) {
    perror("shmat failed in __________");
    exit(1);
} else {
    sprintf(errString, "<*> __________ attaching to shared memory address: %p ", shmptr);
    perror(errString);
}

输出:

<*> Main creating shared memory id: 101376 : No such file or directory
<*> Main attaching to shared memory address: 16000 : No such file or directory
Creating the producer and consumer processes...
<*> Producer located shared memory id: 101376 : Successful
<*> Consumer located shared memory id: 101376 : Successful
<*> Producer attaching to shared memory address: 10000 : Successful
<*> Consumer attaching to shared memory address: 10000 : Successful

这是怎么回事与共享内存地址?如何主要和督促/缺点处理得到不同shmaddr所时,他们十分重视,当他们有相同的的shmid?

What is going on with the shared memory address? How are Main and the Prod/Cons processes getting different shmaddr when they attach when they have the same shmid?

由于提前,

以Z @ķ!

推荐答案

有两种参与组地址。 芯片的RAM地址,又名硬件,物理的,真实的,或主管地址,在开始你的第一个RAM芯片在0和向上移动。然而,在所有的真正的多任务的操作系统,每个进程都有自己的虚拟内存。 CPU和OS合作,给每个过程中的幻觉只有它是它自己的机器上,用自己的地址空间,以表(在内核和CPU,这取决于架构)的映射从虚拟(每-Process)地址真正/硬件/主管的地址。

There are two sets of addresses involved. The "Chip RAM addresses," aka hardware, physical, real, or supervisor addresses, start at your first RAM chip at 0 and move upwards. However, on all "true multi-tasking" operating systems, each process gets its own "virtual memory." The CPU and OS collaborate to give each process the "illusion" that it is alone on its own machine, with its own address space, with a table (in the kernel and CPU, depending on architecture) mapping from the "virtual" (per-process) addresses to the "real/hardware/supervisor" addresses.

共用存储器是一种特殊情况,其中,相同的真实存储器是来自一个以上的过程处理。虚拟地址的的shmat收益是局部的每个呼叫者。

Shared memory is a special case, where the same "real memory" is addressed from more than one process. The virtual addresses that "shmat" returns are local to each caller.

同样地,当加载的共享对象(的.so)库,它可以被映射到在每个进程不同的地址空间。

Likewise, when you load a shared object (.so) library, it may be mapped into a different address space in each process.

这篇关于的shmat()返回一个不同的&QUOT; shmaddr所&QUOT;对于相同的&QUOT; shmkey&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆