如何选择“密钥"?在Linux中进行进程间通信? [英] How to choose the "Key" for inter-processes communication in Linux?

查看:82
本文介绍了如何选择“密钥"?在Linux中进行进程间通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天...

我正在做一个作业,其中指出我有5个流程;服务器,其余为客户端. 每个过程都应从不同的可执行文件中触发.我将实现一种双向消息传递解决方案,但问题不在于消息传递本身.有没有一种优雅的方式可以在这些不同的可执行文件之间通信 key .即当我调用以下函数时:

I am doing a homework which states that I have 5 processes; a server and the rest are clients. Each process is supposed to be sparked from a different executable. I am going to implement a two-way message passing solution, but the question is not about message passing per se. Is there an elegant way to communicate the key between those different executables. i.e. when I call the following function:

int msgget(key_t key, int msgflg);

其他进程应该如何知道密钥?

我的作业可以使用预定的键,但是我想知道如何在实际程序中完成它.因为我了解"的内容,如果不相关的进程在某些用户的计算机上要求输入我的密钥,则可能会发生冲突.

It is OK for my homework to use a predetermined key, but I would like to know how it could be done in a real program. Because What "I understand" is there could happen a conflict if an unrelated process asks for the my key in some user's machine.

推荐答案

一种约定是使用 ftok () man

ftok()函数使用身份 给定名称的文件 路径名(必须引用一个 现有的可访问文件)和 proj_id的最低有效8位 (必须为非零)以生成一个 key_t类型系统V IPC密钥,适用 用于msgget(2),semget(2)或 shmget(2).

The ftok() function uses the identity of the file named by the given pathname (which must refer to an existing, accessible file) and the least significant 8 bits of proj_id (which must be non-zero) to generate a key_t type System V IPC key, suitable for use with msgget(2), semget(2), or shmget(2).

对于 命名同一个文件的所有路径名, 当相同的proj_id值是 用过的.返回的值应为 当(同时 现有)文件或项目ID 不同.

The resulting value is the same for all pathnames that name the same file, when the same value of proj_id is used. The value returned should be different when the (simultaneously existing) files or the project IDs differ.

这篇关于如何选择“密钥"?在Linux中进行进程间通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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