unix - Trying to print words in a file with C child processes -
the goal create child process each word in file, , have child process print word. file has following 5 words, each 1 on separate line:
aachen biscay capri dino ellis
the problem when try print file, 1 of words printing twice.
aachen ellis biscay capri ellis dino
here code. seems pretty straight forward, can't figure out why getting word.
int main (int argc, char *argv[]) { char word[50]; int i; pid_t p; while (fscanf(stdin, "%s", word) != eof) { = 0; while (i < sizeof(word)) { if (word[i] < 'a' || word[i] > 'z') { word[i] = '\0'; break; } i++; } p = fork(); if (p != 0) continue; break; } fprintf(stdout, "%s\n", word); return 0; }
i run program follows:
$ ./printwords < words2.txt > out.txt
the father printing last word in end. try instead of printing after loop:
if (p == 0) { fprintf(stdout, "%s\n", word); } else { continue; }
Comments
Post a Comment