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. |
Like all system calls, if "kill" fails it returns -1 and errno is set.
Have a look at these values. |
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? |
Quote:
Quote:
Quote:
|
Quote:
Quote:
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.... |
From what library does this daemonize functions is taken ?
|
daemonize() is my function to create a daemon, but the pid that it returns is exact.
daemonize() contains fork(), getpid(), and setsid() functions. |
Quote:
If kill tell the process doesn't exists, the child process may have already died. |
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? |
daenonize() return the correct pid of process.
|
Quote:
|
Quote:
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!! |
Please post a compilable sample code exhibiting the issue. Use code tags for readability.
|
Quote:
|
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. |