I have a program that consists of a parent and a number of child processes. Basically parent writes to a file, then first child reads and edits it, and writes to another file that is read and edited by second file. This goes on. Also last child writes to the file of first child. The idea is similar to a chain.
However, I am having problem with rewriting. Generally the chain idea results in a number loops for children so that for each turn child needs to read and write. Reading is OK, but writing to same file again is problem. How can I solve that?
This the part for child processes:
Code:// Run child processes if(getpid() != pid) { if(cid <= N%K) turn++; int myturn = 1; while(myturn <= turn) { int in; // Open pipes if(myturn == 1) { mypipe[cid-1].streamr = fdopen (mypipe[cid-1].fd[READEND], "r"); mypipe[cid%K].streamw = fdopen (mypipe[cid%K].fd[WRITEEND], "w"); } // First element is read fscanf(mypipe[cid-1].streamr, "%d", &in); if(in == -1) { myturn = turn + 1; } else { printf("%d\n", in); int out = in; while(out != -1) { if(fscanf(mypipe[cid-1].streamr, "%d", &out)) printf("I am %d, read %d-%d from mypipe[%d]\n", getpid(), out, in, cid-1); if(out % in != 0) { // !!! - Problem is here - !!! // Cannot write for the second time fprintf(mypipe[cid%K].streamw, "%d\n", out); } } // Write -1 to the end fprintf(mypipe[cid%K].streamw, "%d\n", -1); fclose(mypipe[cid%K].streamw); myturn++; } } exit(0); } return 0;