19
CS 470 CS 470 Lab 5 Lab 5 Comment your code

CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Embed Size (px)

Citation preview

Page 1: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

CS 470CS 470Lab 5Lab 5

Comment your code

Page 2: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Description of Lab5Description of Lab5

Create Four projects– Driver– Producer– Filter– Consumer

Page 3: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Description Cont.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.

Page 4: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Description Cont.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.

Page 5: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

DriverDriver

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

Page 6: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Driver Cont.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

Page 7: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

ProducerProducer

Say starting (stderr)Get the filename from the command lineOpen the source fileError check file openedLoop- read the source file get 40 chars at a time

Simulate work (0…500) to exercise synch mechanismsEcho what is being sent to the pipeActually put it in the producer-filter pipe-char by char (stdout)

End LoopAdd an EOFCleanupSay done

Page 8: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

FilterFilter

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)

Page 9: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

ConsumerConsumer

Say starting (stderr)Get the filename from the command lineOpen the sink fileError check file openedLoop-get character from filter-consumer pipe (stdin)

Refilter – change the character case again, if alphabeticSimulate work (0…150) to exercise synch mechanismsEcho character (stdout or stderr)Write character to file

End LoopCleanupSay done (stdout or stderr)

Page 10: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

2006 TA notes2006 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.

Page 11: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

20062006

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.

Page 12: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Program 1 – Program 1 – "Producer" - pseudo-code"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

Page 13: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Program 1 notesProgram 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 ).

Page 14: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Program 2 – Program 2 – "Filter" - pseudo-code"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

Page 15: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Program 2 notesProgram 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.

Page 16: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Program 3 - "Consumer" - Program 3 - "Consumer" - pseudo-codepseudo-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

Page 17: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Program 3 notesProgram 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.

Page 18: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Program 4 – Program 4 – The driver - pseudo-codeThe 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

Page 19: CS 470 Lab 5 Comment your code. Description of Lab5 Create Four projects – Driver – Producer – Filter – Consumer

Program 4 notesProgram 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