Can anyone help me with the Kill command in C please?!!!

This is a discussion on Can anyone help me with the Kill command in C please?!!! within the Linux Programming forums, part of the Platform Specific Boards category; This code is supposed to list the signals mentioned in the code if I put -l as an argument - ...

  1. #1
    jon20
    Guest

    Can anyone help me with the Kill command in C please?!!!

    This code is supposed to list the signals mentioned in the code if I put -l as an argument - but whats happening is is that its displaying the process ID - whats am I doing wrong here?!!! plus when I provide a signal name as an argument alongside with the PID - it doesn't work!! Your help would be appreciated!

    include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <signal.h>

    int main (int argc, char *argv[])
    {
    int pid;
    int sig;


    printf ("In process: %d\n", getpid () );

    if (argc == 3){
    sig = atoi(argv[1]);
    pid = atoi(argv[2]);
    kill (pid, sig);
    }

    if (argc == 2){
    pid = atoi(argv[1]);
    }
    ///this is where the problem lies i Think!
    if (argc == 2 && argv[2] == '-l'){
    printf("SIGHUP, 1\n");
    printf("SIGTERM, 15\n");
    printf("SIGKILL, 9\n");
    }


    if(argv[1] == NULL){
    printf( "Here1\n");
    kill(pid, SIGTERM);
    printf("Killed.\n");
    system("ps");
    return(0);
    }
    else{

    if((sig == SIGTERM) || (sig == 15)){
    kill(pid, SIGTERM);
    return(0);

    }
    else{
    if((sig == SIGHUP) || (sig == 1)){
    kill(pid, SIGHUP);
    printf("Killed.\n");
    system("ps");
    return(0);
    }
    else{
    if((sig == SIGKILL) || (sig == 9)){
    kill(pid, SIGKILL);
    }
    }
    }
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    You need to be more specific about your use of argc and argv

    Valid arguments are in argv[0] to argv[argc-1]
    argv[argc] is a NULL pointer

    So tests like this are always broken
    if (argc == 2 && argv[2] == '-l'){
    because you're using a NULL pointer at this point


    In addition, argv[n] is a string, so the correct way to test it is using strcmp
    if ( strcmp(argv[2],"-l") == 0 ) {
    // do -l processing
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    U wouldn't be from Bristol UWE would ya???? Cos what your trying to do here is very very similar to an assignment we have on at the moment. I've written this code and it is extremly easy, I will post it but only if your not from UWE cos I don't want to get done for plageurism.
    PuterPaul.co.uk - Portfolio site

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Also why are you looking in argv[2], the -l would be in argv[1]
    PuterPaul.co.uk - Portfolio site

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to kill a dragon with various programming languages
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-01-2007, 01:13 PM
  2. Kill Signal Number? - Help !
    By brett in forum C Programming
    Replies: 5
    Last Post: 06-20-2007, 04:39 AM
  3. need code to kill running application
    By Jinxed in forum Windows Programming
    Replies: 1
    Last Post: 01-31-2002, 09:06 PM

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