Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* basic FIFO operations, UNIX-native */
- /* pointer arithmetics:
- *
- * push(pointer, v) INDICATES *(pointer++) = v
- * pop(pointer) INDICATES *(--pointer)
- * (*++v)[0] INDICATES (**++v)
- * INDICATES first char in v
- * INDICATES name of string/vector v
- * likewise, *v[0] INDICATES **v
- * and *v[n] INDICATES **(v + n)
- * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
- * char **argv INDICATES pointer to char pointer
- * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
- * int *v[len] INDICATES vector "v" of "len" pointers to int elements
- * void *funct() INDICATES function "funct" that returns a pointer-to-void
- * void (*funct)() INDICATES pointer to a function "funct" that returns void
- *
- */
- /* useful characters: [] # */
- # include <stdio.h>
- # include <stdlib.h> // for exit
- # include <sys/ipc.h>
- # include <sys/shm.h> // for shmdet
- # include <sys/stat.h> // for message flags (O_*)
- # include <sys/types.h> // for shmdet
- # include <fcntl.h> // for O_RDWR
- # include <string.h> // for strcpy
- # define RSIZE 20 // reply size
- # define exception(x) { puts(x); exit(1); }
- typedef struct {
- long type;
- char reply[RSIZE];
- } request;
- int main(int argc, char * argv[])
- {
- /* using a FIFO */
- int pid, fifo, file, success;
- request req;
- char replymsg[RSIZE];
- printf("Type lowercase char: "); scanf("%s", req.reply); // scans the input and saves it
- // if(req.reply[0] > 'z' || req.reply[0] < 'a') exception("Invalid input char");
- req.reply[1] = '\0'; // now the char is a string
- success = mkfifo(req.reply, IPC_CREAT|0666);
- // if(success == -1) exception("Error in FIFO creation");
- file = open("secondalias.txt", 1);
- // if(file == -1) exception("Error in file opening");
- success = write(file, &req, sizeof(request)); // writes data
- success = close(file);
- file = open(req.reply, O_RDWR); // transfers the reply on the file
- success = read(file, replymsg, RSIZE); // reads the file
- printf("Reply: '%s'\n", replymsg); // prints it
- success = close(file);
- success = unlink(req.reply);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement