Advertisement
Geralt1001

Untitled

Feb 13th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <errno.h>
  6.  
  7. int getPidd();
  8.  
  9. int main(int argc, char* argv[])
  10. {
  11.     printf("\tCommand Center\n");
  12.    
  13.     if(argc!=1)
  14.     {
  15.         printf("Program is working without parameters.\n");
  16.         exit(1);
  17.     }
  18.     int target_pid;
  19.    
  20.    
  21.     int flag=1;
  22.     while(flag)
  23.     {
  24.         target_pid=getPidd();
  25.         switch(kill(target_pid,SIGRTMIN+6))
  26.         {
  27.             case 0:
  28.                 printf("Signal send succesfully!\n");
  29.                 break;
  30.             default:
  31.                 switch(errno)
  32.                 {
  33.                     case EINVAL:
  34.                         printf("The value of the sig argument is an invalid or unsupported signal number\n");
  35.                         break;
  36.                     case EPERM:
  37.                         printf("The process does not have permission to send the signal to any receiving process\n");
  38.                         break;
  39.                     case ESRCH:
  40.                         printf("No process or process group can be found corresponding to that specified by pid\n");
  41.                         break;
  42.                 }
  43.                 break;
  44.         }
  45.         printf("Send signal once again? (y/n): ");
  46.         char c[1];
  47.         scanf("%1c",c);
  48.         switch(c[0])
  49.         {
  50.             case 'y':
  51.                 continue;
  52.                 break;
  53.             case 'n':
  54.                 //exit(0);
  55.                 flag=0;
  56.                 break;
  57.         }
  58.     }
  59.     return 0;
  60. }
  61. //komunikaty do kill -> errno wziete z: http://linux.die.net/man/3/kill
  62.  
  63. int getPidd()
  64. {
  65.     char target_pid[6];
  66.     char *endint;
  67.     int pid;
  68.     while(1)
  69.     {
  70.         printf("Provide a PID:\n");
  71.         scanf("%6s",target_pid);
  72.  
  73.         pid=strtol(target_pid,&endint,10);
  74.         if(*endint || pid<2 || pid==getpid())
  75.         {
  76.             printf("Incorrect PID!\n");
  77.             continue;
  78.         }
  79.         else
  80.         {
  81.             printf("PID: %d\n",pid);
  82.             break;
  83.         }
  84.     }
  85.     return pid;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement