Thread: Killing A Process

  1. #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!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #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?

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    It depends on what you're trying to do. This:
    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.

  5. #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?

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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.

  7. #7
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    _exit will hopefully never execute. execl _replaces_ the process image.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I said hopefully. Else you're right. But I think it'd be better to print out an error message and then quit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Killing a process.
    By Brian in forum Windows Programming
    Replies: 7
    Last Post: 01-19-2003, 02:36 PM