hello everybody!
i would like to post a question. If i embed in my C code the command kill(9,pid) inside an if command. Is this command(kill) executed in any way. Both if the if is true and false. Does kill have greater priority than the if command.
first of all, i would like to thank u for your interest to answer another question of mine!
i appriciate that.
in my code i have a kill command (which send a signal to a process) which is inside an if and when the if is true i want the kill to be executed. what i am asking is: the kill command is executed always despite the result of if condition?
the snippet of code is posted below!!!any help would be apriciated!!!
....
pid = fork();
errno=0;
if(pid == 0) //Target Program-Child Process
{
dup2(fd2,STDOUT_FILENO);
dup2(fd2,STDERR_FILENO);
close(fd2);close(fd1);
execl(argv[2],argv[3],argv[4],NULL);
//--------------------------------------------pipe -->return here only in the case of execve failure
close(mypipe[0]);
write(mypipe[1],strerror(errno),(strlen(strerror(errno))+1));
//-------------------------------------------end of pipe-----
exit(EXIT_FAILURE);
}
else if (pid>0)//Fault Injector Process-Parent Process
{
sprintf(proc_status,"/proc/%d/status",pid);
timer(0.100);
f12=fopen(proc_status,"r");fseek(f12,20,SEEK_SET);str=fgetc(f12);
if(str!='Z'){shoot++;kill(pid,9);}
waitpid(pid,&status,0);//wait for child to finish or interrupt
....
void timer(float cycle)//timer in microsec precision
{
float count=0.0;
int loop=1;
while(loop)
{
usleep(1000);
count+=0.001;
if(count>=cycle)
{loop=0;}
}
}
I assume that your problem is that the SIGKILL is being triggered irrespective of whether the character at offset 20 is "Z' or not.
Is 'str' declared as a char or an int?
Also note that if fopen(), fseek() or fgetc() fail the if statement will still be executed with str probably set to some unknown/random value and hence SIGKILL will be triggered. You need to add error checking to avoid this scenario.