I have a project for school and I need some help. I'm working in C and I have a server and a client. In server I make a new process for each client with fork. My question is: if I close the server with CTRL+C, how do I kill all the remaining processes ? Thanks
Asked
Active
Viewed 1.1k times
4 Answers
2
You can store their pids after they are created and then, when quitting, signal sigterm
or sigkill
to them.
You can do it through shell - killall
is what you are searching for under linux.
You could use threads instead of processes (could you? what do the project details say?) and communicate through variables.

Dariusz
- 21,561
- 9
- 74
- 114
-
yes, I could use threads but I already finished the project.. a lot of work if i want to modify it for using threads. I have only this thing to do to finish it, and I know that my teacher will try it: CTRL+C on server, clients still running, that's no good.., thanks for your answer, i will try it – Jones Jan 28 '13 at 09:56
1
This will do it for you in the shell:
killall -15 <process name>
In C, you can try:
kill(0, SIGKILL)
in the SIGINT
signal handler for your main server process to kill all the processes in its process group, which should include its children.

sheu
- 5,653
- 17
- 32
0
When you kill a process alone, it will not kill the children.
You have to send the signal to the process group if you want all processes for a given group to receive the signal: kill -9 -parentpid
. Otherwise, orphans will be linked to init
,

sr01853
- 6,043
- 1
- 19
- 39