uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

lab2_fifo.c (1075B)


      1 #include <sys/types.h>
      2 #include <sys/stat.h>
      3 #include <sys/wait.h>
      4 
      5 #include <err.h>
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include <unistd.h>
     10 
     11 int
     12 main(int argc, char *argv[])
     13 {
     14         FILE *cfp, *pfp;
     15         char coolfifo[] = "/tmp/coolfifo";
     16         pid_t pid;
     17         int ret, val;
     18 
     19         if ((ret = mkfifo(coolfifo, 0600)) < 0)
     20                 err(1, "mkfifo");
     21         switch (pid = fork()) {
     22         case -1:
     23                 err(1, "fork");
     24         case 0:
     25                 if ((cfp = fopen(coolfifo, "w")) == NULL)
     26                         err(1, "fopen");
     27                 ret = fprintf(cfp, "%d", 1000);
     28                 fflush(cfp);
     29                 exit(0);
     30         default:
     31                 if ((pfp = fopen(coolfifo, "r")) == NULL)
     32                         err(1, "fopen");
     33                 if ((ret = fscanf(pfp, "%d", &val)) < 0)
     34                         err(1, "fscanf");
     35                 fclose(pfp);
     36                 printf("parent: recv: %d\n", val);
     37                 unlink(coolfifo);
     38                 exit(0);
     39         }
     40 
     41         return 0;
     42 }