LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   kill pid.... won't work with 'pid' variable given.. (https://www.linuxquestions.org/questions/programming-9/kill-pid-wont-work-with-pid-variable-given-421732/)

sachitha 03-04-2006 11:01 PM

kill pid.... won't work with 'pid' variable given..
 
in my program i capture the pid of a child process by using getpid() and assign that to a variable called pid_R

but when i give :
kill pid_R

this doesnt do anything??
but if i give the exact pid of the child from the terminal kill 3445 this works..
why is that?

sachitha 03-04-2006 11:04 PM

The coding that i wrote for it...
 
#include<stdio.h>
#include<signal.h>
main(void)
{
int pid;
int pidchild;
pid=fork();
if (pid==0)
pidchild=getpid();

printf("sac%d\n",getpid());

kill pidchild;

sleep(10);
printf("parent prints %d\n",getpid()); //this line is printed by the child as well????
return 0;
}

paulsm4 03-04-2006 11:47 PM

I'm not sure if the code you posted will even compile ("kill pidchild"?)

But this should work:
Code:

#include <stdio.h>
#include <signal.h>
int
main(int argc, char *argv[])
{
  int pidparent = getpid ();
  int pidchild = fork();
  if (pidchild==0)
  {
    printf ("I'm the child of pid %d, my pid is %d!\n", pidparent, getpid ());
    /* Wait indefinitely... */
    getchar ();
  }
  else
  {
    printf ("I'm the parent of pid %d, my pid is %d!\n", pidchild, getpid ());
    /* Wait for the child to start... */
    sleep (5);
    kill (pidchild, 9);
  }
  printf ("Pid %d: exiting gracefully...\n", getpid ());
  return 0;
}

Please use "[code]" blocks in your posts - it definitely helps!

sachitha 03-05-2006 06:52 AM

isn't there a way to make the child terminate 'itself'???
can i use the coding
kill (pidchild, 9);
inside the child's coding??

paulsm4 03-05-2006 08:47 AM

Hi, sachitha -

Do you have a copy of Linux and a compiler?

If so, please try it yourself:
1. Run the code yourself. Verify that each process starts, and verify that the parent kills the child.

2. Modify the code so that both processes start up (and identify themselves with a "printf"), and
then the child terminates itself.

3. How many ways can you think of for this "hello world" child to terminate itself (hint: there are at least three)? Which is "better"? Why?

4. Can you think of a better way for the parent and child to synchronize themselves with each other besides my (dopey, ham-fisted) "sleep()" and "getchar()"?

Please post back what you find (or any questions you encounter while you're looking).

Thanx in advance .. PSM

Intimidator 03-05-2006 01:47 PM

This link will help

paulsm4 03-06-2006 07:48 PM

Satchita - did you ever get this working?

My answer to your last question really meant "Yes, of *course* you can have the child terminate itself. And yes, you can do it with a "kill (getpid(), 9)".

You cannot do it with a "kill (pidchild, 9)" ... because pidchild will be "0" in the child process (and a legal PID value only in the parent process).

And you probably *shouldn't* do a "kill" to terminate yourself; using "exit()" is probably a better choice. If you're in "main()", then simply doing a "return" is arguably the best choice of all.

Finally, Intimidator was making the very good point that sometimes processes can't be killed at all, and gave you a link explaining some of the circumstances this might occur.

Please let us know how it goes!

Sincerely .. PSM


All times are GMT -5. The time now is 08:25 PM.