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

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
C Programming - Lecture 3 File handling in C - opening and closing. Reading from and writing to files. Special file streams stdin, stdout & stderr. How.
MPI Program Structure Self Test with solution. Self Test 1.How would you modify "Hello World" so that only even-numbered processors print the greeting.
CS1061 C Programming Lecture 17: Steams and Character I/O A. O’Riordan, 2004.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Linux+ Guide to Linux Certification, Second Edition
CS470 Lab 4 TA Notes. Objective Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer.
CS470 Lab3 TA Notes. Step 1 This is just a simple program that reads in from the command line the time the program should run.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
General Programming Introduction to Computing Science and Programming I.
Introduction to Bash Programming Ellen Zhang. Previous three classes What have we learnt so far ?
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
CPS120: Introduction to Computer Science Decision Making in Programs.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Chapter Four I/O Redirection1 System Programming Shell Operators.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
READING AND WRITING FILES. READING AND WRITING FILES SEQUENTIALLY  Two ways to read and write files  Sequentially and RA (Random Access  Sequential.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Input/output streams. Character Input / Output and Input Validation Input and output devices: keyboards, disk drives, mouse, monitors, printers, network.
File Systems cs550 Operating Systems David Monismith.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Linux+ Guide to Linux Certification, Second Edition
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
CSC 4630 Meeting 17 March 21, Exam/Quiz Schedule Due to ice, travel, research and other commitments that we all have: –Quiz 2, scheduled for Monday.
Files A collection of related data treated as a unit. Two types Text
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Linux Administration Working with the BASH Shell.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Input from STDIN STDIN, standard input, comes from the keyboard.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
The Selection Structure
The Linux Operating System
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Files I/O, Streams, I/O Redirection, Reading with fscanf
Fork and Exec Unix Model
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Programming Assignment # 2 – Supplementary Discussion
File Input and Output.
More advanced BASH usage
Writing Functions( ) (Part 4)
Line at a time I/O with fgets() and fputs()
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Module 12 Input and Output
CST8177 Scripting 2: What?.
Switch Case Structures
Presentation transcript:

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