In my parent process, I have created a child process which executes system("find / -print"). From inside the parent, when I try to kill this child process using kill(childProcPID, SIGTERM), it doesn't get terminated immediately. system command keeps on printing the output on console.
Here is the example code:
int main(void) {
pid_t childProc = fork();
switch (childProc) {
case -1:
perror("fork() error");
exit(EXIT_FAILURE);
case 0:
system("find / -print");
printf("if I use kill(pid, SIGTERM) control doesnt reach here");
exit(EXIT_SUCCESS);
default:
;
int i = 500000;
//No a great way to put sleep
//but its just temp
while (i != 0) {
--i;
}
kill(childProc, SIGTERM);
break;
}
printf("Exit!!!!!!");
return EXIT_SUCCESS;
}
Please let me know what I am doing wrong or is the right way to kill a child ?