C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-31-2009, 08:49 AM   #1
Registered User
 
Join Date: May 2009
Posts: 11
Killing A Process

The purpose of the following code is to complete this goal: launch a process, get its status, and then kill it.

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;

}
Now, what the code does do it launch the process, gets its status, and then wait for the user to exit off the process manually and then kill the process id. I want it to automatically (sleep for 5 seconds) and kill the process without requiring user input to close the process.

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   Reply With Quote
Old 05-31-2009, 09:03 AM   #2
critical genius
 
MK27's Avatar
 
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 05-31-2009, 10:01 AM   #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   Reply With Quote
Old 05-31-2009, 10:29 AM   #4
cas
Registered User
 
Join Date: Sep 2007
Posts: 448
It depends on what you're trying to do. This:
Quote:
what the code does do it launch the process, gets its status, and then wait for the user to exit off the process manually and then kill the process id
leads me to believe that you're slightly confused about what wait() and kill() do.

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   Reply With Quote
Old 05-31-2009, 10:48 AM   #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   Reply With Quote
Old 05-31-2009, 12:41 PM   #6
cas
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");
The last bit there tries to figure out how the process died, but it's not necessarily accurate. If vlc catches SIGTERM and exits by itself, then WIFSIGNALED(s) will be false. You can circumvent this by always just sending SIGKILL but that's usually a bad idea.
cas is offline   Reply With Quote
Old 06-01-2009, 06:11 AM   #7
Making mistakes
 
Join Date: Dec 2008
Posts: 347
_exit will hopefully never execute. execl _replaces_ the process image.
__________________
Look at this: Community Project
And this: Ascent - Programmer needed
Brafil is offline   Reply With Quote
Old 06-01-2009, 06:16 AM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by Brafil View Post
_exit will hopefully never execute. execl _replaces_ the process image.
But it will exit if execl() can't find the executable (or some such).

--
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   Reply With Quote
Old 06-01-2009, 06:41 AM   #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.
__________________
Look at this: Community Project
And this: Ascent - Programmer needed
Brafil is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22