LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-04-2006, 11:01 PM   #1
sachitha
Member
 
Registered: Aug 2003
Location: Sri Lanka
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
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?
 
Old 03-04-2006, 11:04 PM   #2
sachitha
Member
 
Registered: Aug 2003
Location: Sri Lanka
Distribution: Redhat 9.0
Posts: 104

Original Poster
Rep: Reputation: 15
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;
}
 
Old 03-04-2006, 11:47 PM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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!

Last edited by paulsm4; 03-04-2006 at 11:53 PM.
 
Old 03-05-2006, 06:52 AM   #4
sachitha
Member
 
Registered: Aug 2003
Location: Sri Lanka
Distribution: Redhat 9.0
Posts: 104

Original Poster
Rep: Reputation: 15
isn't there a way to make the child terminate 'itself'???
can i use the coding
kill (pidchild, 9);
inside the child's coding??
 
Old 03-05-2006, 08:47 AM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 03-05-2006, 01:47 PM   #6
Intimidator
Member
 
Registered: Mar 2005
Distribution: FC4
Posts: 83

Rep: Reputation: 15
This link will help
 
Old 03-06-2006, 07:48 PM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
kill <pid> does not do anything moskito01 Linux - General 9 08-25-2009 12:20 PM
when kill -9 PID wont work bleunuit Linux - Software 2 08-06-2004 11:46 AM
/var/run/[XXX].pid - Tcl pid code liguorir Linux - Software 1 05-20-2004 10:32 PM
question about kill(pid, sig) THETEZ Programming 3 04-18-2004 05:37 AM
ERROR: Couldn't write pid to pid file lawrencegoodman Linux - Newbie 2 02-13-2004 08:05 PM


All times are GMT -5. The time now is 04:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration