LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-19-2007, 08:29 AM   #1
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Rep: Reputation: 30
how to use kill syscall


Hi all,
I do not know to use kill system call! that is to say I want to kill a process with the above syscall, but it do not results.

I writed

int pid (or also pid_t pid)

kill(pid,15), but the process owner of this pid do not stop.

Where is the problem?

Thank you very much for helping.
 
Old 05-19-2007, 11:40 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Like all system calls, if "kill" fails it returns -1 and errno is set.

Have a look at these values.
 
Old 05-21-2007, 12:39 PM   #3
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
The code of my program is the following:

pid_t pid;
int ret;

...
case 'a': ...
pid = daemonzize();
...

case 's': ...
ret = kill(pid,15);
...

this function return pid of daemon, but when I' d like stop daemon, then kill don't work and ret is not pid returned from daemonize().

The -s option must stop daemon, but pid in "case 's'" section do not remember the process id.

How can I solve this problem?
 
Old 05-22-2007, 04:01 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Quote:
Originally Posted by shifter
this function return pid of daemon, but when I' d like stop daemon, then kill don't work and ret is not pid returned from daemonize().
So what is ret value ?
Quote:
The -s option must stop daemon, but pid in "case 's'" section do not remember the process id.
Do you mean pid isn't correct ?
Quote:
How can I solve this problem?
By checking errno (or using perror) if ret is equal to -1.
 
Old 05-23-2007, 09:54 AM   #5
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Quote:
So what is ret value ?
ret value is -1.


Quote:
Do you mean pid isn't correct ?
I checked with errno, and errno is 3, that is to say process that I want to stop it do not exist...


So, I don't know how can obtaining process id of process. I tried with getpid, but process id it don't is equal at pid returned by daemonize().
How don't know what I can solve this problem....
 
Old 05-23-2007, 11:38 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
From what library does this daemonize functions is taken ?
 
Old 05-23-2007, 07:28 PM   #7
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
daemonize() is my function to create a daemon, but the pid that it returns is exact.
daemonize() contains fork(), getpid(), and setsid() functions.
 
Old 05-23-2007, 08:03 PM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Quote:
Originally Posted by shifter
daemonize() is my function to create a daemon, but the pid that it returns is exact.
It isn't as kill fails.
If kill tell the process doesn't exists, the child process may have already died.
 
Old 05-23-2007, 08:26 PM   #9
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Where does daemonize() get the value to return?

When you do the fork() you should be checking the return value of that. One of the processes is going to get a 0 (the child), the other is going to get the pid of the child (the parent). By storing the return value of fork and calling getpid() you can get both the child and parent pid's in the parent process. The child process can get its own pid with getpid() and it's parents pid with getppid().

Does that help?
 
Old 05-23-2007, 09:56 PM   #10
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
daenonize() return the correct pid of process.

Last edited by shifter; 05-23-2007 at 09:57 PM.
 
Old 05-23-2007, 10:01 PM   #11
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Quote:
Originally Posted by shifter
daenonize() return the correct pid of process.
Have you checked a process with this pid is still alive by running "ps" in a terminal ?
 
Old 05-23-2007, 10:56 PM   #12
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Quote:
Have you checked a process with this pid is still alive by running "ps" in a terminal ?
Yes

My code is the following:

...

pid_t daemonize() {

if (getppid() != 1) i = fork();
if (i < 0) exit(1);
if (i > 0) exit(0);
setsid();
return(getpid);

}

...

and in main:

pid_t pid;

case 'a': pid = damonize();
...
case 's': pid = getpid();
kill = (pid,15);


I don't find bug!!

Last edited by shifter; 05-23-2007 at 10:57 PM.
 
Old 05-24-2007, 04:51 AM   #13
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Please post a compilable sample code exhibiting the issue. Use code tags for readability.
 
Old 05-27-2007, 07:27 AM   #14
shifter
Member
 
Registered: May 2006
Distribution: Slackware, DragonFly
Posts: 233

Original Poster
Rep: Reputation: 30
Quote:
int daemonize() {

int i;

if (getppid() != 1) i = fork();
if (i < 0) exit(1);
if (i > 0) exit(0);
setsid();
return(getpid());

}

main(int argc, char** argv) {

pid_t pid; /*or int pid; */
...
...

while ((c = getopt(argc, argv, ":a:s")) != -1) {
switch (c) {
case 'a': ...
pid = daemonize();
...
break;
case 's': pid = getpid();
printf("pid = getpid() : %d \n",pid);
--pid;
printf("--pid : %d \n",pid);
kill(pid,15);
printf("PID %d \n",pid);
break;
default: messages(c);
break;
}
}

...
...

}
The code is that above. I section "case 's'" of switch statement I done some test of printf..., but it don't works...

Last edited by shifter; 05-27-2007 at 07:28 AM.
 
Old 05-27-2007, 08:44 AM   #15
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
This code still doesn't compile, and and is quite confusing.
getopt third argument is broken.

What parameters do you pass to the command ?
What is the intended purpose of the daemonize function ?
What process do you want to kill ?
Why don't you check and print the kill return value to exhibit the problem you are complaining of ?
 
  


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
syscall hook robertos Linux - Kernel 15 03-09-2007 08:33 PM
syscall hook robertos Programming 4 03-06-2007 12:48 AM
assembly execve (syscall 11) rblampain Programming 2 07-03-2005 10:23 AM
what is difference between syscall of linux and Unix sdamo Linux - General 1 06-12-2005 02:07 AM
Linux syscall 11 (execve) Genjix Programming 1 12-30-2004 12:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration