LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to use kill syscall (https://www.linuxquestions.org/questions/programming-9/how-to-use-kill-syscall-555125/)

shifter 05-19-2007 07:29 AM

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.

jlliagre 05-19-2007 10:40 AM

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

Have a look at these values.

shifter 05-21-2007 11:39 AM

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?

jlliagre 05-22-2007 03:01 PM

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.

shifter 05-23-2007 08:54 AM

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....

jlliagre 05-23-2007 10:38 AM

From what library does this daemonize functions is taken ?

shifter 05-23-2007 06:28 PM

daemonize() is my function to create a daemon, but the pid that it returns is exact.
daemonize() contains fork(), getpid(), and setsid() functions.

jlliagre 05-23-2007 07:03 PM

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.

jtshaw 05-23-2007 07:26 PM

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?

shifter 05-23-2007 08:56 PM

daenonize() return the correct pid of process.

jlliagre 05-23-2007 09:01 PM

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 ?

shifter 05-23-2007 09:56 PM

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!!

jlliagre 05-24-2007 03:51 AM

Please post a compilable sample code exhibiting the issue. Use code tags for readability.

shifter 05-27-2007 06:27 AM

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...

jlliagre 05-27-2007 07:44 AM

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 ?


All times are GMT -5. The time now is 09:46 PM.