(schematic image attached)

Hi,
I am programming in C.
My code has two threads, one for reading keyboard inputs and the other one for handling a TCP server.
The main and input thread code are in the same file (main.c) and the server thread is in another one (server.c).

I want to execute a function (which code is in another file, myfunction.c) in a new process, using fork. I have attached an schematic image to explain what my code does.

When I do that inside the input thread it works fine, but if I execute the same fork code in the server thread, when I receive the SIGCHLD signal and call the wait function to get the child's termination, the server's listening file descriptor gets corrupted and the server stops. If I comment the wait function the server doesn't get corrupted and keeps working, but then the child process become into a zombie process.

I don't understand why the same code works inside one thread and fails in the other. The only difference I see is that the server_thread is in a different file, but it shouldn't be important.

I am stuck, any help or suggestion will be appreciated!
Thanks

esquema_pregunta.jpg