![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 11
| Killing A Process Code: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int g, s;
pid_t c;
if((c = fork()) == 0)
{
execl("/usr/bin/vlc", "vlc", NULL);
}
wait(&s);
kill(c, SIGKILL);
return 0;
}
My question is how can I get the status of my child process, wait 5 seconds, and then kill the process? I hope I explained what I need the code to do well, if not, ask me questions and I will answer them to the best of my ability. Thank you! |
| tommyb05 is offline | |
| | #2 |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,228
| Since you are not in control of the process, AFAICT the only way to get status on it would be with a system call (eg, "ps --pid XXXX"). sleep(5) will delay for 5 seconds. You should use SIGTERM before you use SIGKILL and only use SIGKILL if SIGTERM fails. SIGTERM is the actual default used by the *nix "kill" command. |
| MK27 is offline | |
| | #3 |
| Registered User Join Date: May 2009
Posts: 11
| I'm pretty sure I wait() takes in an int for the child process's status (s here), but that isn't my problem. I can sleep() all I want but it still kills the process AFTER I close it manually, not before. Any other ideas? |
| tommyb05 is offline | |
| | #4 | |
| Registered User Join Date: Sep 2007
Posts: 448
| It depends on what you're trying to do. This: Quote:
When you say "get its status", what do you mean? You appear to believe that wait() gets information on a running process, but it does not. You don't wait() on a process that's running, you wait on a process that's dead (or stopped, but that's not an issue here). Or, at least, when you wait() on a running process, it waits until the process dies; hence its name. Once a process exits, there's no point in killing it, either. It's already dead. So if you've successfully waited on a process, you shouldn't kill it, because you don't own that pid anymore. So when you're asking for the status of a process, what exactly are you looking for? Also: the NULL you pass to execl() should be cast to char*, because the compiler can't properly convert NULL in a variable argument list. One of the rare occasions where a cast is correct. | |
| cas is offline | |
| | #5 |
| Registered User Join Date: May 2009
Posts: 11
| Alright, I believe I understand a little better now. Heres what I see when I run this code: The program launches (and I want it to die 5 seconds after its launched) I have to manually click "X" on the program for it to close, and then lau quits. How can I make it automatically close after 5 seconds? |
| tommyb05 is offline | |
| | #6 |
| Registered User Join Date: Sep 2007
Posts: 448
| MK27's post has the relevant information, so: Code: pid_t pid;
int s;
pid = fork();
if(pid == -1) err(1, "fork");
if(pid == 0)
{
execl("/usr/bin/vlc", "vlc", (char *)NULL);
_exit(1);
}
sleep(5);
kill(pid, SIGTERM);
sleep(1);
kill(pid, SIGKILL); /* not strictly necessary, but SIGTERM won't necessarily kill off a process */
if(wait(&s) == -1) err(1, "wait");
if(WIFSIGNALED(s) && (WTERMSIG(s) == SIGTERM || WTERMSIG(s) == SIGKILL)) puts("killed by us");
else if(WIFEXITED(s) && WEXITSTATUS(s) == 0) puts("exited normally");
else puts("something else happened");
|
| cas is offline | |
| | #7 |
| Making mistakes Join Date: Dec 2008
Posts: 347
| _exit will hopefully never execute. execl _replaces_ the process image. |
| Brafil is offline | |
| | #8 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #9 |
| Making mistakes Join Date: Dec 2008
Posts: 347
| I said hopefully. Else you're right. But I think it'd be better to print out an error message and then quit. |
| Brafil is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| init adopts zombie process? | password636 | Linux Programming | 4 | 07-01-2009 10:05 AM |
| How can you make a parent process wait for a child? I'm gettin a seg fault. | mr_coffee | C Programming | 3 | 10-15-2008 09:24 AM |
| Problem with forking a process | Unitedroad | C Programming | 10 | 10-04-2007 01:43 AM |
| process programming | St0rM-MaN | Linux Programming | 2 | 09-15-2007 07:53 AM |
| Killing a process. | Brian | Windows Programming | 7 | 01-19-2003 02:36 PM |