共享内存:密钥和ID有什么区别? [英] Shared memory: what's the difference between the key and the id?

查看:195
本文介绍了共享内存:密钥和ID有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用ipcs -a时,key列和id列之间有什么区别?

When invoking ipcs -a, what is the difference between the key column and the id column?

这是ipcs命令的示例输出:

ipcs -a

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      
0x00000000 0          ybaumes    600        393216     2          dest         
0x00000000 65537      ybaumes    700        8124648    2          dest         
0x00000000 3932163    ybaumes    700        169376     2          dest         
0x00000000 3604485    ybaumes    600        393216     2          dest         

推荐答案

首先,共享内存中的'id'列引用共享内存区域的特定处理程序.如果未获得共享存储区,它将返回负值.因此,基本上,"id"是由系统生成的,用户对此没有任何控制权.

Firstly, the 'id' column in shared memory refers to the specific handler to the shared memory area. If the shared memory area is not obtained, it will return a negative value. So basically, 'id' is generated by system and user doesn't have any control over it.

在ipcs命令中的键"列中引用的是参考进程间通信资源(如共享内存,消息队列和信号灯)而给出的值. 密钥只是key_t类型的整数".此外,key参数是与信号量ID关联的访问值.它可以是简单的整数,例如. 34562,可以在使用关联的get函数创建这些资源时将其传递. 需要密钥的位置接受特殊参数IPC_PRIVATE.在这种情况下,系统将生成唯一的密钥,并确保其他进程不会具有相同的密钥.

While 'key' column in the ipcs command refers to the value which is given in reference to inter process communication resources like shared memory, message queues and semaphores. 'A key is simply an integer of type key_t'. Moreover, the key argument is a access value associated with the semaphore ID. It can be simple integer number, eg. 34562, which can be passed at the time of creation of those resources using associated get functions. the place where a key is required accepts a special parameter, IPC_PRIVATE. In this case, the system will generate a unique key and guarantee that no other process will have the same key.

如果在需要密钥的地方使用IPC_PRIVATE请求资源,则该过程将收到该资源的唯一密钥.由于该资源是由外部人未知的唯一密钥标识的,因此其他进程将无法共享该资源,因此,可以确保请求进程专有地拥有和访问该资源.

If a resource is requested with IPC_PRIVATE in a place where a key is required, that process will receive a unique key for that resource. Since that resource is identified with a unique key unknown to the outsiders, other processes will not be able to share that resource and, as a result, the requesting process is guaranteed that it owns and accesses that resource exclusively.

从消息队列的角度使用此概念时,该概念会变得更加清晰,消息队列是使用特定的键值生成和发送消息的.仅当给定密钥在接收方匹配时,才能在接收方接收到相同的消息.因为,还有返回值给出了消息ID,该消息ID是根据相应的键值计算的,所以主要相关性是用于检查资源的唯一性.

This concept get more clear when it is used in terms of message queues, where a message is generated and sent with a specific key value. The same message can only be received at the receiving end when, the given key is matched at the receiver end. Because, there also return value gives the message id, which is calculated on the corresponding key value, main relevance is for checking for uniqueness of a resource.

这篇关于共享内存:密钥和ID有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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