如何在Linux下生成系统范围的唯一ID [英] How can I generate system-wide unique IDs under Linux

查看:56
本文介绍了如何在Linux下生成系统范围的唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在多进程Linux系统上工作,需要生成唯一的ID.安全性不是考虑因素,因此从零开始并向上计数的 ID 生成器就可以了.而且它只是在本地计算机中,不涉及网络.显然,实现这一点并不难,但是我只是想知道是否已经提供了任何东西(最好是轻量级的).

I'm working on a multiprocess Linux system and need to generate unique IDs. Security is not a consideration, so an ID generator that starts at zero and counts up would be fine. Also it's just within a local machine, no network involved. Obviously it's not hard to implement this, but I was just wondering if there was anything already provided (preferably lightweight).

推荐答案

这听起来像是...的工作... uuidgen :

This sounds like a job for... ...uuidgen:

% uuidgen 
975DA04B-9A5A-4816-8780-C051E37D1414

如果要将其构建到自己的应用程序或服务中,则需要 libuuid :

If you want to build it into your own application or service, you'll need libuuid:

#include <uuid/uuid.h>
#include <iostream>

int main()
{
    uuid_t uu;
    uuid_generate(uu);
    char uuid[37];
    uuid_unparse(uu, uuid);
    std::cout << uuid << std::endl;
}

这篇关于如何在Linux下生成系统范围的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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