gimmeamilk gimmeamilk - 5 months ago 16
Linux Question

How can I generate system-wide unique IDs under Linux

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).

Answer

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

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

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;
}