190 likes | 322 Views
CS 470 Lab 5. Comment your code. Description of Lab5. Create Four projects Driver Producer Filter Consumer. Description Cont. Create a txt file with random text in it all lower case. The Producer should read in the information from the file and send through a pipe to the Filter.
E N D
CS 470Lab 5 Comment your code
Description of Lab5 • Create Four projects • Driver • Producer • Filter • Consumer
Description Cont. • Create a txt file with random text in it all lower case. • The Producer should read in the information from the file and send through a pipe to the Filter. • The Filter should change the text from lower case to upper case then pass it through a second pipe to the Consumer.
Description Cont. • The Consumer should read in from the pipe then change the text back to lower case and write it out to a second text file.
Driver Will set up pipes and run producer, filter and consumer processes for a timed period then terminate them if they are still running. Error check the command line (should be “Driver jnk.txt 30”) Get the runtime from the command line Create and properly set a waitable timer Create the producer-filter pipe(error check see pg 367) Make sure SA are set so that the HANDLE can be inherited
Driver Cont. Create the filter-consumer pipe (same as producer-filter pipe) Create producer process Create filter process Create consumer process (note: when creating processes all usual parameters except startInfo) Wait until timer goes off Terminate the child processes Wait for producer, filter & consumer Clean up-close handles Say done
Producer Say starting (stderr) Get the filename from the command line Open the source file Error check file opened Loop- read the source file get 40 chars at a time Simulate work (0…500) to exercise synch mechanisms Echo what is being sent to the pipe Actually put it in the producer-filter pipe-char by char (stdout) End Loop Add an EOF Cleanup Say done
Filter Say starting (stderr) Loop- get character from producer-filter Pipe (stdin) Simulate work (0…100) to exercise synch mechanisms Filter-change character case if alphabetic Echo character to stderr Send character to filter-consumer pipe (stdout) EndLoop Add EOF to Pipe Say done (stderr)
Consumer Say starting (stderr) Get the filename from the command line Open the sink file Error check file opened Loop-get character from filter-consumer pipe (stdin) Refilter – change the character case again, if alphabetic Simulate work (0…150) to exercise synch mechanisms Echo character (stdout or stderr) Write character to file End Loop Cleanup Say done (stdout or stderr)
2006 TA notes • Lab 5 - Create 4 programs. The first takes input from a file and sends the contents of the file to standard output and standard error 40 characters at a time. The second program takes input from standard input and outputs the opposite case of the character to standard output. The third program is similar to the second, it takes input from standard input, changes the case of the character, and outputs to standard output. In addition, the third program also writes the processed input to a file. The fourth program creates processes to start the first three programs and then creates the pipes between them.
2006 • General notes: • The first three programs should work separately from the others. • Pipes in programming are like pipes in real life. They have two ends. Make sure the both ends are handled by your driver program. • Using different pipes for input and output streams just adds a few lines of code before creating the process. By default the pipe for output is standard output, the default pipe for input is standard input. In previous programs before you created a process you didn't reconfigure these pipes because you didn't need to, however in this one you do.
Program 1 – "Producer" - pseudo-code • main • initialize variables • start random number generator • say starting • get file name from command line, otherwise output error and exit • open file and if not successful, output error • while read from file 40 characters at a time • sleep for a random amount of time • for each character in buffer • echo character to standard error • echo character to standard output • flush standard output • send end of file to standard output • flush standard output • close handles • say done • end
Program 1 notes • This is similar to the first lab assignment. • Using a while( ReadFile( ... ) ) statement helps in reading the contents of the file until an end of file is reached. • Remember to flush standard output every time something is written to it. • An end of file character is a named constant, EOF. • Writing a character to a stream can be done in several ways, however since you can write only one character at a time in this assignment I suggest using void putc( char, stream ). • Flusing an output stream is done by using void fflush( stream ).
Program 2 – "Filter" - pseudo-code • main • initialize variables • start random number generator • say starting • while getting characters from standard input isn't an end of file • sleep for a random amount of time • if the character is alphabetical • if the character is upper case change to lower case • else change the character to upper case • echo character to standard error • echo character to standard output • flush standard output • send end of file to standard output • flush standard output • say done • end
Program 2 notes • Getting a character from standard input and making sure it's not an end of file can be done in one line. Use: while( ( c = getc( stdin ) ) != EOF ) where c is a char variable. • In stdio.h there are functions bool isalpha( char ), char toupper( char ), and char tolower( char ), use them wisely.
Program 3 - "Consumer" - pseudo-code • main • initialize variables • start random number generator • say starting • get file name from command line, otherwise output error and exit • open file and if not successful, output error • while getting characters from standard input isn't an end of file • sleep for a random amount of time • change the case of the character • echo the character to standard output • flush standard output • write the character to a file • close handles • say done • end
Program 3 notes • Opening the file is exactly how it's done in program 1. • Getting characters and swapping the case is exactly how it's done in program 2. • Writing to a file requires a • bool WriteFile( ... ) command.
Program 4 – The driver - pseudo-code • main • initialize variables • error check the command line argument • create a waitable timer • create the producer-filter pipe • create the filter-consumer pipe • set up the producer input, output, and error • create the producer process • set up the filter input, output, and error • create the filter process • set up the consumer input, output, and error • create the consumer process • wait for the timer • terminate the producer, filter, and consumer processes • close handles • end
Program 4 notes • There should be 5 handles; the timer, producer-filter read pipe, producer-filter write pipe, filter-consumer read pipe, filter-consumer write pipe. • Use separate STARTUPINFO and PROCESS_INFORMATION variables for each process. • Configure the STARTUPINFO variable before you execute the CreateProcess command for that variable. • There are 3 items of interest in the STARTUPINFO variable: • sInfo.hStdInput = pipeHandle -- assigning input to a pipe you specify • sInfo.hStdOutput = GetStdHandle( STD_OUTPUT_HANDLE ) -- assigning output to the default pipe • sInfo.hStdError = GetStdHandle( STD_ERROR_HANDLE ) -- assigning error to the default pipe • The output of one pipe should connect to the input of another. For example, the producer output pipe should connect to the filter input pipe. Illustrating this: • CreatePipe( &producerFilterReadPipe, &producerFilterWritePipe, &pipeAttributes, 0 ); // create the pipe • producerInfo.hStdOutput = producerFilterWritePipe; // set up output of the producer at one end of the pipe • filterInfo.hStdInput = producerFilterReadPipe; // set up input of the filter as the other end of the pipe