Reputation: 3
But the issue is the PID that I print before sending to through the method and the PID that I print after receiving it in the method are totally different.I can't figure this out.
void killbyPIDprocess(struct process** ptr,char* p)
{
int i=0;
printf("hi");
while(ptr[i]!=NULL)
{
printf("Inside while loop");
printf("%d\n",ptr[i]->pid);
printf("%d\n",*p);
if(strcmp(ptr[i]->pid,p)==0)
{
printf("Kill process of PID %d\n",p);
}
else
{
i++;
}
}
}
In the loop method, my condition is
void loop(char *input)
{
bool flag=true;
char **tokens;
struct process **ptr=(struct process*) malloc (BUFFERSIZE);//Is the array that contains pointers to all the processes created.
int ci=0;int i=0;
while(flag==true)
{
input=getInp(input);
tokens=tokenize(input);
if(strcasecmp(*tokens,"kill")==0)
{
strtok(tokens[1],"\n");
char* pid=(char*)malloc (BUFFERSIZE);
pid=tokens[1];
printf("%s",pid);
killbyPIDprocess(ptr, pid);
}
}
The input method just takes input from the user. The tokenize method, uses strtok method to tokenize the input. If I input kill (PID), it goes to the method killbyPIDprocess(ptr,pid) where ptr is the double pointer containing all the pointers of struct processes. I store process info whn i create one. The pid that I print in the loop method is the same as that of the input that I give it ie the one pid I want to kill my process through, but when I pass this pid through the killbyPIDprocess method, it shows some other value. I haven't started to actually work on the kill code yet because it kept giving me errors. I used print statements to keep track of how much of my code was working. I'm relatively new to C and self taught so please point out the mistakes.
Upvotes: 0
Views: 296
Reputation: 5920
printf("%d\n",*p);
will print a numeric code for the first char in a buffer, so you have to use %s
format specifier - printf("%s\n", p);
to get the same results.
This code if(strcmp(ptr[i]->pid,p)==0)
is also incorrect. process::pid
member has an pid_t
type, which is an signed integer. Using it in the string comparison routines is an undefined behavior (not sure it will even compile). To compare PIDs you have to convert your string data to the integer, using for example an atoi
function. Then you could directly compare them with the ==
operator.
Upvotes: 1