PDA

View Full Version : Can anyone help me with the Kill command in C please?!!!



jon20
04-06-2002, 05:11 AM
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);
}
}
}
}
}

Salem
04-06-2002, 05:34 AM
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
}

pdstatha
04-07-2002, 10:35 AM
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.

pdstatha
04-07-2002, 10:37 AM
Also why are you looking in argv[2], the -l would be in argv[1]