How do I kill every foreground process execpt my most recent one?

This is a discussion on How do I kill every foreground process execpt my most recent one? within the Linux Programming forums, part of the Platform Specific Boards category; Let's say that I have a bunch of foreground processes like in the following... [cdalten@localhost ~]$ w 16:14:06 up 49 ...

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    How do I kill every foreground process execpt my most recent one?

    Let's say that I have a bunch of foreground processes like in the following...
    [cdalten@localhost ~]$ w
    16:14:06 up 49 days, 23:37, 8 users, load average: 1.28, 1.64, 1.04
    USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
    cdalten pts/1 :0.0 16:14 0.00s 0.44s 0.06s w
    cdalten pts/2 :0.0 23Sep09 26days 0.40s 0.40s bash
    cdalten pts/3 :0.0 Sun18 43:48m 0.46s 0.46s bash
    cdalten pts/4 :0.0 27Sep09 20days 0.40s 0.40s bash
    cdalten pts/5 :0.0 Sun20 18:47m 5.61s 5.19s irssi
    cdalten pts/6 :0.0 Mon17 7:58 1.29s 1.29s bash
    cdalten pts/7 :0.0 Mon17 18:27m 0.42s 0.42s bash
    cdalten pts/9 :0.0 24Sep09 25days 1.25s 0.83s python
    [cdalten@localhost ~]$
    Now what I want to do is kill EVERY foreground process except my most recent one. I thought about maybe doing a sort and then kill every process except the most recent one. However, for some strange reason, I think there might be a better and/or saner way to do this. Here is what I've attempted so far..

    Code:
    #include <sys/types.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <utmp.h>
    #include <string.h>
    #include <pwd.h>
    #include <unistd.h>
    #include <errno.h>
    
    #ifdef LINUX
    #include <sys/stat.h>
    #endif
    
    #include <fcntl.h>
    
    #define MAX_TTY 20
    
    struct utmp    uinfo[MAX_TTY];
    
    int who_am_i(void);
    
    int who_am_i(void)
    {
      int fd, in = 0;
      struct passwd *pwd;
      struct utmp user;
    
      char login[UT_NAMESIZE+1];
    
      if ((pwd = getpwuid(geteuid())) == NULL)
        (void)strcpy(login, "unknown");
      else
        (void)strcpy(login, pwd->pw_name);
    
      if ((fd = open(_PATH_UTMP, O_RDONLY)) < 0) {
        perror(_PATH_UTMP);
        return -1;
      }
    
      (void)memset(&uinfo, 0, sizeof (uinfo));
    
      while(read(fd, &user, sizeof(user)) == sizeof(user)) 
        if (strncmp(user.ut_name, login, UT_NAMESIZE) == 0) {
          if (in == MAX_TTY) {
            return in;
          } else { 
            uinfo[in++] = user;
          }
        }
    
      close(fd);
    
      return 0;
    } 
    
    int main(void)
    {
      if (who_am_i() == -1) {
        exit(EXIT_FAILURE);
      }
    
      exit(EXIT_SUCCESS);
    }
    Ideas?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Your premise is a fallacy. Just because the number of the process is higher does NOT make it the latest. In fact, there is no good way to determine which process is the "newest" (unless the machine has only been up for a brief period). Keep in mind that PIDs are reused by the kernel. This means that when the process ID gets to be 32K, it wraps around to whatever the lowest unused PID is.

    The way you'll have to go about doing this is to look at the time stamps on the /proc/<pid> to see which process is actually the latest. Next, you'll need to make sure that you don't get any processes that are kernel processes or init (killing init is considered to be "bad").

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why isn't the execlp() function doing anything?
    By jsrig88 in forum C Programming
    Replies: 5
    Last Post: 10-12-2009, 11:09 AM
  2. sequenceing or queueing multiple process
    By sv_joshi_pune in forum Windows Programming
    Replies: 1
    Last Post: 08-14-2009, 10:43 AM
  3. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 02:43 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 08:53 AM

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