process - C Programming: Fork() and IPC with Pipes -


so have problem, need create 3 processes (each handle different task). first process sends information on second (the first waits acknowledgement second). second sends information third (the second waits acknowledgement third). third processes final information... process supposed loop on , on until process 1 analyzes entire text file. far, tried writing communication between 3 processes pipes. i'm not sure how send acknowledgment process 2 process 1 , process 3 process 2. i'm not entirely sure how loop it. thanks!

i have use stop , wait protocol... i'm not sure how done.

#include <stdio.h> #include <string.h> #include <sys/wait.h> #include <stdlib.h> #include <unistd.h>  int main(int argc, char *argv[]) {      int c = 0, t = 0;      int fd1[2], fd2[2];       char *thefile = "/users/desktop/thefile";      file *file = fopen (thefile, "r");      if (file == null) {         perror("file not exist");         exit(1);     }      while (c == 0) {          int status;          char readbuffer[80];         char readbuffer2[80];         int ttemp = 0;          pipe(fd1);         pipe(fd2);          pid_t pid = fork();          if (pid < 0) {             perror("pipe error");             exit(1);         }          if (pid == 0) {             //child 1             close(fd1[0]);             close(fd2[0]);             close(fd2[1]);              char line [80];             int c2 = 0;             file = fopen (thefile, "r");             while (fgets(line, sizeof(line), file) != null){                 if (c2 == t) {                     printf("line: %s\n", line);                     break;                 }                 c2++;             }             if (t != c2) {                 c = 1;             } else {                 write(fd1[1], line, (strlen(line)+1));             }              t++;              exit(1);         }          pid_t pid2 = fork();          if (pid2 < 0) {             perror("pipe error");             exit(1);         }          if (pid2 == 0) {             //child 2             close(fd1[1]);             close(fd2[0]);             read(fd1[0], readbuffer, sizeof(readbuffer));             printf("2nd child string: %s\n", readbuffer);             char string2[80] = "asdfasdf";             write(fd2[1], string2, (strlen(string2)+1));             exit(1);         }          pid_t pid3 = fork();          if (pid3 < 0) {             perror("pipe error");             exit(1);         }          if (pid3 == 0) {             //child 3             close(fd2[1]);             close(fd1[0]);             close(fd1[1]);             read(fd2[0], readbuffer2, sizeof(readbuffer2));             exit(1);         }           waitpid(pid, &status, 0);          waitpid(pid2, &status, 0);          waitpid(pid3, &status, 0);       }      fclose(file);     return 0; } 

as have come understand problem via comments, asking 2 distinct requirements:

  1. implementing "stop & wait" protocol between each pair of processes, and
  2. using waitpid() collect child processes finish

the latter pretty straightforward; have seems fine. former seem stuck on.

there couple of things here. 1 question of semantics: stop & wait, in form discussing it, requires recipient of message acknowledge sender message received before sender proceeds. there significant difference between characterizing acknowledgment way, , characterizing signal receiver of acknowledgment perform particular action. receiver in response acknowledgment own concern, not inherent in acknowledgment itself.

as communications, then, recommend establishing two pipes between each pair of processes, 1 communication in each direction. wait acknowledgment, then, process performs blocking read on appropriate pipe.

as looping, each process must loop separately, loops take same form:

  1. read next line (process 1 gets lines file; others lines pipes connected previous process)
  2. terminate if no line available
  3. except process 1, write one-byte acknowledgment message on pipe previous process
  4. process line
  5. write line appropriate terminus (process 3 writes lines unspecified terminus -- maybe stdout -- others write lines pipes connected next process).
  6. except process 3, perform blocking read receive acknowledgment next process.
  7. go (1)

be sure check result codes of functions provide them. since that's library functions , syscalls can tedious, suggest macro out there.

for clarity , readability, suggest writing work of each of 3 processes separate function. after fork() create each process handle file descriptor mangling needed , call appropriate function.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -