Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #include <signal.h>
  6. #include <sys/wait.h>
  7. int i=0;
  8. void handsofblue(int twobytwo)
  9. {
  10.     printf("Guy killed me, Mal. Killed me with a sword. How weird is that?\n");
  11.     _exit(1);
  12. }
  13. void main(){
  14.     signal(SIGTERM, handsofblue);
  15.     printf("I has a bukkit!\n");
  16.     char *s="Parent:"; int pid;
  17.     while(i<51)
  18.     {
  19.         if(i<26){
  20.             usleep(300000);
  21.             printf("%s globvar:%d, my pid:%d\n",s, i++,getpid());
  22.             if(i==9 ){
  23.                 s = (pid=fork())? "Parent:":"Child:";
  24.                 srand(time(NULL)+getpid());
  25.             }
  26.         }
  27.         else{
  28.               long vartime = (rand() % 400 +100)*1000;
  29.             printf("%s globvar:%d, waited for: %dms, my pid: %d\n", s, i, (int)vartime/1000, getpid());
  30.             i++;
  31.             usleep(vartime);
  32.         }
  33.     }
  34.     //if(pid){ //wait 4 child
  35.     //waitpid(pid, &i, 0);
  36.     //}
  37.    
  38.     if(pid) { // kill teh child
  39.         waitpid(pid, &i, 1);
  40.         if(!WIFEXITED(i)){kill(pid,SIGTERM);}
  41.     }
  42.     printf("%s HALP! They be stealing mah bukkit!\n", s);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement