Advertisement
heavenriver

fifo.c

Nov 27th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | None | 0 0
  1.  /* basic FIFO operations, UNIX-native */
  2.  
  3.  /* pointer arithmetics:
  4.   *
  5.   * push(pointer, v) INDICATES *(pointer++) = v
  6.   * pop(pointer) INDICATES *(--pointer)
  7.   * (*++v)[0] INDICATES (**++v)
  8.   *           INDICATES first char in v
  9.   *           INDICATES name of string/vector v
  10.   * likewise, *v[0] INDICATES **v
  11.   *       and *v[n] INDICATES **(v + n)
  12.   * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
  13.   * char **argv INDICATES pointer to char pointer
  14.   * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
  15.   * int *v[len] INDICATES vector "v" of "len" pointers to int elements
  16.   * void *funct() INDICATES function "funct" that returns a pointer-to-void
  17.   * void (*funct)() INDICATES pointer to a function "funct" that returns void
  18.   *
  19.   */
  20.  
  21.  /* useful characters: [] # */
  22.  
  23.  # include <stdio.h>
  24.  # include <stdlib.h> // for exit
  25.  # include <sys/ipc.h>
  26.  # include <sys/shm.h> // for shmdet
  27.  # include <sys/stat.h> // for message flags (O_*)
  28.  # include <sys/types.h> // for shmdet
  29.  # include <fcntl.h> // for O_RDWR
  30.  # include <string.h> // for strcpy
  31.  
  32.  # define RSIZE 20 // reply size
  33.  # define exception(x) { puts(x); exit(1); }
  34.  
  35.  typedef struct {
  36.    long type;
  37.    char reply[RSIZE];
  38.  } request;
  39.  
  40.  int main(int argc, char * argv[])
  41.     {
  42.      /* using a FIFO */
  43.      int pid, fifo, file, success;
  44.      request req;
  45.      char replymsg[RSIZE];
  46.      printf("Type lowercase char: "); scanf("%s", req.reply); // scans the input and saves it
  47.      // if(req.reply[0] > 'z' || req.reply[0] < 'a') exception("Invalid input char");
  48.      req.reply[1] = '\0'; // now the char is a string
  49.      success = mkfifo(req.reply, IPC_CREAT|0666);
  50.      // if(success == -1) exception("Error in FIFO creation");
  51.      file = open("secondalias.txt", 1);
  52.      // if(file == -1) exception("Error in file opening");
  53.      success = write(file, &req, sizeof(request)); // writes data
  54.      success = close(file);
  55.      file = open(req.reply, O_RDWR); // transfers the reply on the file
  56.      success = read(file, replymsg, RSIZE); // reads the file
  57.      printf("Reply: '%s'\n", replymsg); // prints it
  58.      success = close(file);
  59.      success = unlink(req.reply);
  60.      
  61.      return 0;
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement