Week 12 - Wednesday CS222.

Slides:



Advertisements
Similar presentations
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,
Advertisements

CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
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.
Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files Special Topics in Comp. Eng. 2 Semester.
RjpSystem Level Programming Operating Systems 1 Having fun withy the Unix Operating System Praxis Week 7 Rob Pooley.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
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.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
Chapter 7 Files By C. Shing ITEC Dept Radford University.
Week 10 - Wednesday.  What did we talk about last time?  Linked list implementations  Started doubly linked lists.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
File Systems cs550 Operating Systems David Monismith.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
Week 4 - Monday.  What did we talk about last time?  Precedence  Selection statements  Loops  Lab 3.
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
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 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
By C. Shing ITEC Dept Radford University
Week 12 - Thursday CS222.
Error handling I/O Man pages
CS/COE 0449 (term 2174) Jarrett Billingsley
Hank Childs, University of Oregon
Practical Session 5.
Operating Systems Moti Geva
Loops BIS1523 – Lecture 10.
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.
Week 12 - Wednesday CS222.
CS1010 Programming Methodology
Chapter 18 I/O in C.
Week 4 - Monday CS222.
CS111 Computer Programming
Stack Data Structure, Reverse Polish Notation, Homework 7
Functions Inputs Output
Files I/O, Streams, I/O Redirection, Reading with fscanf
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Hank Childs, University of Oregon
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
LINUX System Programming with Processes (additional)
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSE 333 – Section 3 POSIX I/O Functions.
File Structure Related system calls
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Programming Assignment # 2 – Supplementary Discussion
File Input and Output.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Advanced UNIX progamming
CSE 333 – Section 3 POSIX I/O Functions.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
FILE I/O File Descriptors I/O Efficiency File Sharing
Module 12 Input and Output
Standard I/O Library Implementation
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Chapter 12: File-System Implementation CSS503 Systems Programming
Computer Architecture and System Programming Laboratory
I/O CS580U - Fall 2018.
Professor Jodi Neely-Ritz University of Florida
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

Week 12 - Wednesday CS222

Last time What did we talk about last time? Bit fields Unions

Questions?

Project 5

Exam 2 Post Mortem

Quotes Weeks of programming can save you hours of planning. Anonymous

Stack initialization Initializing the stack isn't hard We give it an initial capacity (perhaps 5) We allocate enough space to hold that capacity We set the size to 0 Stack stack; stack.capacity = 5; stack.values = (double*) malloc(sizeof(double)*stack.capacity ); stack.size = 0;

Push, pop, and top We can write simple methods that will do the operations of the stack ADT void push(Stack* stack, double value); double pop(Stack* stack); double top(Stack* stack);

Postfix notation You might recall postfix notation from CS221 It is an unambiguous way of writing mathematical expressions Whenever you see an operand, put it on the stack Whenever you see an operator, pop the last two things off the stack, perform the operation, then put the result back on the stack The last thing should be the result Example: 5 6 + 3 – gives (5 + 6) – 3 = 8

Evaluate postfix Finally, we have enough machinery to evaluate an array of postfix terms Write the following function that does the evaluation: We'll have to see if each term is an operator or an operand and interact appropriate with the stack double evaluate(Term terms[], int size);

Low Level File I/O

Low level I/O You just learned how to read and write files Why are we going to do it again? There is a set of Unix/Linux system commands that do the same thing Most of the higher level calls (fopen(), fprintf(), fgetc(), and even trusty printf()) are built on top of these low level I/O commands These give you direct access to the file system (including pipes) They are often more efficient You'll use the low-level file style for networking All low level I/O is binary

Includes To use low level I/O functions, include headers as follows: #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> You won't need all of these for every program, but you might as well throw them all in

File descriptors High level file I/O uses a FILE* variable for referring to a file Low level I/O uses an int value called a file descriptor These are small, nonnegative integers Each process has its own set of file descriptors Even the standard I/O streams have descriptors Stream Descriptor Defined Constant stdin STDIN_FILENO stdout 1 STDOUT_FILENO stderr 2 STDERR_FILENO

open() To open a file for reading or writing, use the open() function There used to be a creat() function that was used to create new files, but it's now obsolete The open() function takes the file name, an int for mode, and an (optional) int for permissions It returns a file descriptor int fd = open("input.dat", O_RDONLY);

Modes The main modes are O_RDONLY Open the file for reading only O_WRONLY Open the file for writing only O_RDWR Open the file for both There are many other optional flags that can be combined with the main modes A few are O_CREAT Create file if it doesn’t already exist O_DIRECTORY Fail if pathname is not a directory O_TRUNC Truncate existing file to zero length O_APPEND Writes are always to the end of the file These flags can be combined with the main modes (and each other) using bitwise OR int fd = open("output.dat", O_WRONLY | O_CREAT | O_APPEND );

Permissions Because this is Linux, we can also specify the permissions for a file we create The last value passed to open() can be any of the following permission flags bitwise ORed together S_IRUSR User read S_IWUSR User write S_IXUSR User execute S_IRGRP Group read S_IWGRP Group write S_IXGRP Group execute S_IROTH Other read S_IWOTH Other write S_IXOTH Other execute int fd = open("output.dat", O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IRGRP );

read() Opening the file is actually the hardest part Reading is straightforward with the read() function Its arguments are The file descriptor A pointer to the memory to read into The number of bytes to read Its return value is the number of bytes successfully read int fd = open("input.dat", O_RDONLY); int buffer[100]; read( fd, buffer, sizeof(int)*100 ); //fill with something

write() Writing to a file is almost the same as reading Arguments to the write() function are The file descriptor A pointer to the memory to write from The number of bytes to write Its return value is the number of bytes successfully written int fd = open("output.dat", O_WRONLY); int buffer[100]; int i = 0; for( i = 0; i < 100; i++ ) buffer[i] = i + 1; write( fd, buffer, sizeof(int)*100 );

close() To close a file descriptor, call the close() function Like always, it's a good idea to close files when you're done with them int fd = open("output.dat", O_WRONLY); //write some stuff close( fd );

lseek() It's possible to seek with low level I/O using the lseek() function Its arguments are The file descriptor The offset Location to seek from: SEEK_SET, SEEK_CUR, or SEEK_END int fd = open("input.dat", O_RDONLY); lseek( fd, 100, SEEK_SET );

Example Use low level I/O to write a hex dump program Print out the bytes in a program, 16 at a time, in hex, along with the current offset in the file, also in hex Sample output: 0x000000 : 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 0x000010 : 02 00 03 00 01 00 00 00 c0 83 04 08 34 00 00 00 0x000020 : e8 23 00 00 00 00 00 00 34 00 20 00 06 00 28 00 0x000030 : 1d 00 1a 00 06 00 00 00 34 00 00 00 34 80 04 08

File descriptors revisited A file descriptor is not necessarily unique Not even in the same process It's possible to duplicate file descriptors Thus, the output to one file descriptor also goes to the other Input is similar

Duplicating descriptors on the command line stderr usually prints to the screen, even if stdout is being redirected to a file What if you want stderr to get printed to that file as well? You can also redirect only stderr to a file ./program > output.txt ./program > output.txt 2>&1 ./program 2> errors.log

dup() and dup2() If you want a new file descriptor number that refers to an open file descriptor, you can use the dup() function It's often more useful to change an existing file descriptor to refer to another stream, which you can do with dup2() Now all writes to stderr will go to stdout int fd = dup(1); //makes a copy of stdout dup2(1, 2); //makes 2 (stderr) a copy of 1 (stdout)

I/O buffering in files Reading from and writing to files on a hard drive is expensive These operations are buffered so that one big read or write happens instead of lots of little ones If another program is reading from a file you've written to, it reads from the buffer, not the old file Even so, it is more efficient for your code to write larger amounts of data in one pass Each system call has overhead

Buffering in stdio To avoid having too many system calls, stdio uses this second kind of buffering This is an advantage of stdio functions rather than using low-level read() and write() directly The default buffer size is 8192 bytes The setvbuf(), setbuf(), and setbuffer() functions let you specify your own buffer

Flushing a buffer Stdio output buffers are generally flushed (sent to the system) when they hit a newline ('\n') or get full When debugging code that can crash, make sure you put a newline in your printf(), otherwise you might not see the output before the crash There is an fflush() function that can flush stdio buffers fflush(stdout); //flushes stdout //could be any FILE* fflush(NULL); //flushes all buffers

Quiz

Upcoming

Next time… Files and atomicity File systems Lab 12

Reminders Finish Project 5 Read LPI chapters 13, 14, and 15 Due Friday by midnight Read LPI chapters 13, 14, and 15