0

Can you help me with the following task:

Write a C program that interprets command line parameters 1 and 2 as pid and signal number and sends the corresponding signal number to the process pid. To interpret command line parameters as integers, you can use the C function atoi. The user is to be informed via standard output how successful the process was. In addition, suitable exit codes are to be used.

Here my code:

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

// Taking argument as command line
int main(int argc, char *argv[])
{
    pid_t pid, int signo;
  
    // Checking if number of argument is
    // equal to 3 or not.
    if (argc != 3) 
    {
        printf("Call with %s <pid> <signo>.\n",argv[0]);
        return EXIT_FAILURE;
    }
    
    // Converting string type to integer type
    // using function "atoi( argument)"
    pid = atoi(argv[1]); 
    signo = atoi(argv[2]);
    
    // Checking if all the numbers are positive of not
    if (pid<1 || signo < 1)
    {
        printf("Pid and Signo must both be greater than zero");
        return EXIT_FAILURE;
    }
    if (kill(pid,signo)<0) {
        printf("Sending sig# %d to %d failed.\n",signo,pid);
        return EXIT_FAILURE;
    }
    printf("Succesfully sent sig# %d to %d.\n",signo,pid);
    return EXIT_FAILURE;
}

Is that right? Or what can be improved and simplified?

0 Answers0