Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include <sys/sem.h>
- #include <fcntl.h>
- #include <string.h>
- #include <wait.h>
- int main()
- {
- int P1, P2, P3;
- int optionMenu;
- FILE* fp;
- char buf[1024];
- mkfifo("FIFO12", 0777 | IPC_CREAT);
- mkfifo("FIFO23", 0777 | IPC_CREAT);
- switch (P1 = fork())
- {
- case 0:{
- switch(optionMenu){
- case 1 : {
- } break;
- case 2 : {
- if ((fp = fopen("/gutek/CLionProjects/SO_PROJEKT/tekst", "r")) == NULL)
- { /* Open source file. */
- printf("Nie mozna odnalezc pliku");
- return 1;
- }
- int fifo12 = open("FIFO12", O_WRONLY); //otwieranie kolejki fifo podanej nazwie na zapis
- while (fgets(buf, sizeof(buf), fp) != NULL)
- {
- buf[strlen(buf) - 1] = '\0';
- printf("%s\n", buf);
- write(fifo12,buf, sizeof(buf));
- }
- fclose(fp);
- } break;
- default: {
- printf("Wpisano nie prawidlowa liczbe\n");
- }
- }
- exit(0);
- }
- case -1:
- {
- printf("Blad utworzenia procesu\n");
- break;
- }
- default:
- {
- }
- }
- switch (P2 = fork())
- {
- case 0:
- {
- int fifo12 = open("FIFO12", O_RDONLY);
- int fifo23 = open("FIFO23", O_WRONLY);
- char dane[512] = {0};
- int dlugosclinii;
- read(fifo12, dane, sizeof(dane));
- printf("%d dostał odpowiedź: %s\n", getpid(), dane);
- dlugosclinii = (int)strlen(dane);
- sprintf(dane, "%d", dlugosclinii);
- write(fifo23, dane, sizeof(dane));
- exit(0);
- }
- case -1:
- {
- printf("Blad utworzenia procesu\n");
- break;
- }
- default:
- {
- }
- }
- switch (P3 = fork())
- {
- case 0:
- {
- int fifo23 = open("FIFO23", O_RDONLY);
- read(fifo23, buf, sizeof(buf));
- printf("%s\n", buf);
- exit(0);
- } case -1:
- {
- printf("Blad utworzenia procesu\n");
- break;
- }
- default:
- {
- }
- }
- wait(NULL);
- wait(NULL);
- wait(NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement