vakho

OP sistemebi - KOLO KODI

Jan 7th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <sys/stat.h>
  6. #include <sys/wait.h>
  7. #include <sys/types.h>
  8.  
  9. /*
  10.  * mshobeli FIFO-shi wers KODS da elodeba shvils!
  11.  * shvili am KODS igebs da debs "test.c" failshi!
  12.  * amis mere mshobeli akompilirebs "test.c" fails!
  13.  */
  14.  
  15. int main()
  16. {
  17.   printf("[PROGRAM STARTED]\n");
  18.  
  19.   int pr, status, FIFO, fd;
  20.   char str[] = "#include <stdio.h> #include <stdlib.h> int main() { printf(\"Hello world!\\n\"); return 0; }";
  21.   char str2[92];
  22.   char path[] = "asdf.fifo";
  23.   (void)umask(0);
  24.   FIFO = mknod(path, S_IFIFO | 0754, 0);
  25.   if (FIFO == -1)
  26.   {
  27.     printf("FIFO was not created!\n");
  28.     exit(-1);
  29.   }
  30.  
  31.   pr = fork();
  32.   if (pr == -1) exit(-1);
  33.   if (pr != 0) // PARENT PROCESS
  34.   {
  35.     printf("[PARENT]:\n");
  36.    
  37.     fd = open(path, O_WRONLY);
  38.     if (fd == -1) exit(-1);
  39.     if (write(fd, str, sizeof(str)) != sizeof(str)) exit(-1);
  40.     close(fd);
  41.    
  42.     wait(&status);
  43.     execl("usr/bin/gcc", "gcc", "test.c", "-o", "test.out", NULL);
  44.    
  45.     printf("[END OF PARENT]\n");
  46.   }
  47.   else // CHILD PROCESS
  48.   {
  49.     printf("[CHILD]:\n");
  50.    
  51.     fd = open(path, O_RDONLY);
  52.     if (fd == -1)
  53.     {
  54.       printf("FD problem!");
  55.       exit(-1);
  56.     }
  57.     if (read(fd, str2, sizeof(str2)) != sizeof(str2)) exit(-1);
  58.     close(fd);
  59.    
  60.     (void)umask(0);
  61.     fd = open("test.c", O_WRONLY | O_CREAT, 0756);
  62.     if (fd == -1) exit(-1);
  63.     if (write(fd, str2, sizeof(str2)) != sizeof(str2)) exit(-1);
  64.     close(fd);
  65.    
  66.     printf("[END OF CHILD]\n");
  67.   }
  68.   printf("[PROGRAM ENDED]\n");
  69.   return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment