Search code examples
clinuxsignalsbackground-process

How can I terminate a background process with ctrlz in C?


I'm writing a script in C and I execute it with the & in order to run it on the background. I need to terminate the program with ctrl-z (SIGTSTP)

There is a main loop that intentionally prevents the script from terminating. I need to terminate it with ctrl-z and ctrl-z will not respond while the process is running the background.

How can I send the signal(from terminal) to the background process and how can I manipulate it?


Solution

  • You have to handle the signal and terminate the program when you receive the signal SIGTSTP.

    To understand what I'm talking about: C signal handling - Wikipedia

    Any guide/manual about handling signals in C should help you.

    P.S.: you can terminate the program in any moment with function exit() from <stdlib.h>.