How to kill a background process from a system function call

How can I kill a background process that was executed by calling a system function in C. For example, I have a compiled request for the application "fooprocess". Then I want to write a program that will execute the fooprocess application in the background using the system function, please see the code below,

const char app[] = "fooprocess &";
system(app);

As you can see, there is a &. so that I can run the fooprocess application in the background. How can I kill this fooprocess?

Many thanks.

+5
source share
3 answers

, PID. , system, , fork + exec.

+5

system("foo &"); , /. gigantic: pid, , pid , , , pid .

( , , ), , - system fork exec posix_spawn. ( , init), wait/waitpid, wait pid , ..

+2


, :

ps -axc|grep -i myApp|awk '{print $1}' | tr -d '\n' | xargs -0 kill  

system() :

system("ps -axc|grep -i myApp|awk '{print $1}' | tr -d '\n' | xargs -0 kill");

.

0

All Articles