Advertisement
Doragonroudo

[SysPro] SystemCall, Signal

Mar 28th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. // -------------------- System Call ------------
  2.  
  3. //mysys.c
  4.  
  5. #include <stdio.h>
  6. int main(int argc, char *argv[]) {
  7.     int status;
  8.     if (argc < 2) {
  9.         printf("\nCommand-line argument required!\n");
  10.         exit(0);
  11.     }
  12.     if ((status = system(argv[1])) < 0)
  13.         perror("system() error");
  14.     return 0;
  15. }
  16.  
  17. //showpid.c
  18. #include <stdio.h>
  19. int main(void) {
  20.     printf("Real UID = %d,
  21.         Eff UID = %d\n",
  22.         getuid(), geteuid());
  23.     return 0;
  24. }
  25.  
  26. //-------------------- Signal --------------
  27.  
  28. //signal1.c
  29.  
  30. #include <signal.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34.  
  35. void my_alarm(int sig) {
  36.     for (int i = 0; i < 5; i++)
  37.     printf("Alarm!!! \n");
  38. }
  39. int main(void) {
  40.     int pid;
  41.     printf("Alarm clock is starting...\n");
  42.     if ((pid = fork()) == 0) {
  43.         sleep(3);
  44.         kill(getppid(), SIGALRM);
  45.         exit(0);
  46.     }
  47.     printf("Waiting for alarm...\n");
  48.     (void) signal(SIGALRM, my_alarm);
  49.     pause();
  50.     printf("Done!\n");
  51.     exit(0);
  52. }
  53.  
  54. //signal2.c
  55.  
  56. #include <sys/types.h>
  57. #include <signal.h>
  58. #include <unistd.h>
  59. #include <stdio.h>
  60. /* one handler for both signals */
  61. static void sig_usr(int);
  62. int main(void) {
  63.     if (signal(SIGUSR1, sig_usr) == SIG_ERR)
  64.         perror("can't catch SIGUSR1");
  65.     if (signal(SIGUSR2, sig_usr) == SIG_ERR)
  66.         perror("can't catch SIGUSR2");
  67.     for ( ; ; )
  68.         pause();
  69. }
  70. /* argument is signal number */
  71. static void sig_usr(int signo) {
  72.     if (signo == SIGUSR1)
  73.         printf("received SIGUSR1\n");
  74.     else if (signo == SIGUSR2)
  75.         printf("received SIGUSR2\n");
  76.     else
  77.         printf("received signal %d\n", signo);
  78. }
  79.  
  80. //signal3.c
  81.  
  82. #include <sys/types.h>
  83. #include <sys/wait.h>
  84. #include <signal.h>
  85. #include <stdio.h>
  86. #include <stdlib.h>
  87. #include <unistd.h>
  88.  
  89. static void sig_cld(int);
  90. int main()
  91. {
  92.     pid_t pid;
  93.     if (signal(SIGCLD, sig_cld) == SIG_ERR)
  94.         perror("signal error");
  95.     if ((pid = fork()) < 0)
  96.         perror("fork error");
  97.     else if (pid == 0)
  98.     {
  99.         /* child */ sleep(2);
  100.         _exit(0);
  101.     }
  102.     pause(); /* parent */
  103.     exit(0);
  104. }
  105. static void sig_cld(int signo)
  106. /* interrupts pause() */
  107. {
  108.     pid_t pid;
  109.     int status;
  110.     printf("SIGCLD received\n");
  111.     /* reestablish handler */
  112.     if (signal(SIGCLD, sig_cld) == SIG_ERR)
  113.         perror("signal error");
  114.     /* fetch child status */
  115.     if ((pid = wait(&status)) < 0)
  116.         perror("wait error");
  117.     printf("pid = %d\n", pid);
  118.     return;
  119. }
  120.  
  121. //sigaction.c
  122.  
  123. #include <signal.h>
  124. #include <stdio.h>
  125. #include <unistd.h>
  126. void rx_int(int sig)
  127. {
  128.     printf("SIGINT - %d Received!!!\n", sig);
  129. }
  130. int main(void)
  131. {
  132.     struct sigaction act;
  133.     act.sa_handler = rx_int;
  134.     sigemptyset(&act.sa_mask);
  135.     act.sa_flags = 0;
  136.     sigaction(SIGINT, &act, 0);
  137.     while(1)
  138.     {
  139.         printf("Running…\n");
  140.         sleep(1);
  141.     }
  142. }
  143.  
  144. //killthatshit.c
  145.  
  146. #include <signal.h>
  147. #include <stdio.h>
  148. #include <unistd.h>
  149. #include <sys/types.h>
  150. #include <stdlib.h>
  151. #include <string.h>
  152.  
  153. int main(void)
  154. {
  155.     int total_length = 20;
  156.     char line[total_length];
  157.     FILE * command = popen("pidof sigaction","r");
  158.  
  159.     fgets(line,total_length,command);
  160.  
  161.     pid_t pid = strtoul(line,NULL,10);
  162.     pclose(command);
  163.    
  164.     printf("PID IS : %d\n", pid);
  165.     kill(pid, SIGTERM);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement