1
votes

I'm working on a programming assignment for my Operating Systems class and I'm running into some issues with the way my output is showing up in the terminal.

The purpose of the program is to take input from a file or the STDIN and essentially pass it through 4 pipes to get the frequency of the words. I'm using calls to fork(),execlp(), and pipe() to complete this and am not having any issues with my program, however as the last child executes and prints to the terminal it looks like this:

os-class ~/cs344/Homework3 190% myword text.txt  
os-class ~/cs344/Homework3 191%       1 aaaaa        
1 aaaaaa      
  1 aaaaaaa  
      8 it  
      5 s  
      5 sa  

(Here I could enter command for 191%, something like 'ls'..)

myword is my executable and text.txt is the file I'm using for testing. I'm mainly confused because it seems like my pipes are lined up correctly with STDIN and STDOUT. Do I need to insert a wait() somewhere?

Thanks for any suggestions and sorry if there's already an answer to this somewhere! I wasn't exactly sure what to search for.

1
Yes, you probably do need to use a wait() (or perhaps several wait() calls, maybe in a loop) to ensure that all the output appears before myword exits.Jonathan Leffler

1 Answers

0
votes

Each parent that does fork() will need to wait() on its children or the children will continue to run after the parent has exited.

The shell (if you wrote it) also needs to wait on the programs it starts.