SDL2 fork/clone behaviour

I was wondering what is the behaviour of SDL toward POSIX fork/clone system calls, i.e what resources are share between the parent and the child?

For example, if process P forked process C, does each processes have their own events queue (SDL_PollEvent won’t be shared).

In the case that some resources are indeed shared between P and C, is it possible to use clone with certain flags to unshare those resources.

Just to be clear, I am not talking about thread when referring to clone, I know about SDL_CreateThread, and this is not what I’m interested in. I’m interested in complete different processes that will communicate through some POSIX IPC.

Hi Jim,

I was wondering what is the behaviour of SDL toward POSIX
fork/clone
system calls, i.e what resources are share between the
parent and the child?

fork:

File descriptors are shared between parent and child, including fds
for opened joystick devices. None of SDL’s internal structures are
shared.

If you fork() before calling SDL_JoystickOpen() things are as
if you had 2 completely independent programs. It doesn’t matter if you
have called SDL_Init() already or not.

If you fork() after calling SDL_JoystickOpen() both processes will
compete for the events from the joystick. Events will NOT be
duplicated. This means you will get inconsistent behaviour if one
process sees a button down and the other sees the corresponding button
up event. Note: I actually tested this behavior by putting a fork()
into sdl2-jstest and printing out the PID with the events received.

For example, if process P forked process C, does each
processes have their own events queue (SDL_PollEvent won’t be
shared).

Internal structures of libraries are never shared after fork(). That’s
a general rule for all libraries.

In the case that some resources are indeed shared between P and
C, is it possible to use clone with certain flags to unshare
those resources.

clone() is Linux-specific and allows you to control everything. But
there’s not need to use it. Just make sure you don’t fork() with any
joysticks open and you’ll be fine.

MSB

Thank you for the your reply Matt, it answers everything I needed to know.

Regards,
Jim.