How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

Similar documents
Summer June 15, 2010

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

UNIX input and output

CMPSC 311- Introduction to Systems Programming Module: Input/Output

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

UNIX System Programming

CSE 410: Systems Programming

Operating System Labs. Yuanbin Wu

System Calls and I/O Appendix. Copyright : University of Illinois CS 241 Staff 1

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

Chapter 10. The UNIX System Interface

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

Data and File Structures Chapter 2. Basic File Processing Operations

CS240: Programming in C

File Access. FILE * fopen(const char *name, const char * mode);

Input and Output System Calls

Physical Files and Logical Files. Opening Files. Chap 2. Fundamental File Processing Operations. File Structures. Physical file.

HIGH LEVEL FILE PROCESSING

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Process Management! Goals of this Lecture!

CSC 271 Software I: Utilities and Internals

System Calls and I/O. CS 241 January 27, Copyright : University of Illinois CS 241 Staff 1

Lecture 21 Systems Programming in C

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Input / Output Functions

Computer Architecture and Assembly Language. Practical Session 5

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Standard File Pointers

CSE 333 SECTION 3. POSIX I/O Functions

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

2009 S2 COMP File Operations

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Course organization. Course introduction ( Week 1)

Stream Model of I/O. Basic I/O in C

Section 3: File I/O, JSON, Generics. Meghan Cowan

Standard I/O in C, Computer System and programming in C

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

EM108 Software Development for Engineers

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING SOFTWARE ENGINEERING DEPARTMENT

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

CS240: Programming in C

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Operating System Labs. Yuanbin Wu

Computer Architecture and System Programming Laboratory. TA Session 5

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

File I/O. Arash Rafiey. November 7, 2017

PROGRAMMAZIONE I A.A. 2017/2018

Naked C Lecture 6. File Operations and System Calls

Lecture files in /home/hwang/cs375/lecture05 on csserver.

25.2 Opening and Closing a File

File and Console I/O. CS449 Spring 2016

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

Computer Programming Unit v

Process Creation in UNIX

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

UNIX I/O. Computer Systems: A Programmer's Perspective, Randal E. Bryant and David R. O'Hallaron Prentice Hall, 3 rd edition, 2016, Chapter 10

File Descriptors and Piping

CSE 333 SECTION 3. POSIX I/O Functions

Files. Programs and data are stored on disk in structures called files Examples. a.out binary file lab1.c - text file term-paper.

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

Operating systems. Lecture 7

File IO and command line input CSE 2451

Inter-Process Communication

CSI 402 Lecture 2 Working with Files (Text and Binary)

CS 33. Shells and Files. CS33 Intro to Computer Systems XX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

System Software Experiment 1 Lecture 7

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Processes COMPSCI 386

Processes often need to communicate. CSCB09: Software Tools and Systems Programming. Solution: Pipes. Recall: I/O mechanisms in C

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

Unix System Calls. Gwan-Hwan Hwang Dept. CSIE National Taiwan Normal University

CSCI-E28 Lecture 3 Outline. Directories, File Attributes, Bits, File Operations. Write our own versions of Unix programs

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

Files and Directories

The link() System Call. Preview. The link() System Call. The link() System Call. The unlink() System Call 9/25/2017

CS 201. Files and I/O. Gerson Robboy Portland State University

Input/Output and the Operating Systems

Topics. Unix Pipes (CSE 422S) In A Nutshell. Ken Wong Washington University. Pipe (Unnamed FIFO) in the Shell.

POSIX Shared Memory. Linux/UNIX IPC Programming. Outline. Michael Kerrisk, man7.org c 2017 November 2017

C-Refresher: Session 10 Disk IO

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

Exercise Session 3 Systems Programming and Computer Architecture

211: Computer Architecture Summer 2016

Lecture 8: Structs & File I/O

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

Transcription:

, 1 / 33, Summer 2010 Department of Computer Science and Engineering York University Toronto June 15, 2010 Table of contents, 2 / 33 1 2 3 Exam, 4 / 33 You did well Standard input processing Testing Debugging Weak points Memory allocation (especially to functions)

How do we define?, to variables 1 i n t p i ; 2 f l o a t p f ; to 1 s t r u c t s t r p i ; to functions 1 r e t u r n e d t y p e ( pfoo ) ( t y p e s o f p a r a m s ) ; 2 f l o a t ( p f ) ( i n t, void ) ; 5 / 33, Memory allocation Functions void * malloc(int size); void * calloc(int n, int size); void * realloc(void * ptr, int size ); void free(void *ptr); 1 #d e f i n e SIZE 10 2 i n t main ( ) { 3 i n t i ; 4 char b u f f e r = ( char ) m a l l o c ( SIZE ) ; 5 i f ( b u f f e r==null) e x i t ( 1 ) ; 6 f o r ( i =0; i <SIZE ; i ++) 7 b u f f e r [ n]= rand ()%26+ a ; 8 b u f f e r [ SIZE]= \0 ; 9 p r i n t f ( Random s t r i n g : %s \n, b u f f e r ) ; 6 / 33 10 f r e e ( b u f f e r ) ; 11 return 0 ; 12 } Syntax, The syntax is the same as when defining variable (except typedef). Variable - x is variable of type int* 1 i n t x ; Type - x is equivalent type to int * 1 typedef i n t x ; 2 x i ; / e q u i v a l e n t to i n t i ; / 7 / 33

Defining new or renaming existing type, 1 / aaa i s new name f o r i n t / 2 typedef i n t aaa ; 3 / car100 i s new name f o r a r r a y o f 100 c h a r s / 4 typedef char car100 [ 1 0 0 ] ; 5 / func i s a f u n c t i o n t a k i n g 6 two i n t s and r e t u r n i n g i n t / 7 typedef i n t func ( int, i n t ) ; 8 / pfunc i s a p o i n t e r to f u n c t i o n 9 t a k i n g two i n t s and r e t u r n i n g i n t / 10 typedef i n t ( pfunc ) ( int, i n t ) ; 11 / t S t r i s a e q u i v a l e n t to sname, 12 t p S t r i s e q u i v a l e n t to sname / 13 typedef s t r u c t sname{ 14 member type1 member name1 ; 15... 8 / 33 16 } t S t r, t p S t r ;, How are defined? 1 s t r u c t sname{ 2 member type1 member name1 ; 3 member type2 member name2 ; 4... 5 member typen member namen ; 6 } s v a r 1, p s v a r 1 ; 9 / 33 Linked List, Definition List can be empty (NULL) or List has a head (list element) and tail (list) Each element has a pointer to the next element (last points to NULL) 1 s t r u c t l i s t N o d e { 2 i n t x ; 3 s t r u c t l i s t N o d e next ; 4 } head ; 5 typedef s t r u c t l i s t N o d e l i s t ; 10 / 33

Operations, Add to the end 1 l i s t addend ( l i s t head, i n t newval ){ 2 l i s t new = ( l i s t ) m a l l o c ( s i z e o f ( l i s t ) ) ; 3 i f ( head==null) 4 return new ; 5 while ( ( head >next )!=NULL) 6 head=head >next ; 7 head >next=new ; 8 return head ; 9 } 11 / 33 Operations, Remove head 1 l i s t f r e e F i r s t ( l i s t head ){ 2 l i s t tmp ; 3 i f ( head==null) 4 return NULL ; 5 tmp=head >next ; 6 f r e e ( head ) ; 7 return tmp ; 8 } 12 / 33, Binary tree Definition Tree can be empty or Tree has a root (tree node) and two children (trees) Each node has two to left and right child 13 / 33 1 s t r u c t treenode { 2 i n t x ; 3 s t r u c t treenode l c h i l d ; 4 s t r u c t treenode r c h i l d ; 5 } r o o t ; 6 typedef treenode t r e e ;

Operations, Add leaf (l p =r) 1 t r e e add ( t r e e root, t r e e new ){ 2 i f ( r o o t==null) 3 return new ; 4 i f ( root >x>new >x && root >l c h i l d!=null) 5 add ( root >l c h i l d, new ) ; 6 e l s e i f ( root >x>new >x && root >l c h i l d==null){ 7 root >l c h i l d=new ; 8 e l s e i f ( root >x<=new >x && root >r c h i l d!=null) 9 add ( root >r c h i l d, new ) ; 10 e l s e 11 root >r c h i l d=new ; 12 return r o o t ; 13 } 14 / 33 Operations, This kind of traverse can be used to print entire tree. In order traverse 1 void i n O r d e r ( t r e e r o o t ){ 2 3 i f ( r o o t==null) 4 return ; 5 i n O r d e r ( root >l c h i l d ) ; 6 p r i n t f ( %d,, root >x ) ; 7 i n O r d e r ( root >r c h i l d ) ; 8 return ; 9 } 15 / 33, Operations This kind of traverse can be used to free entire tree. Post order traverse 16 / 33 1 void p o s t O r d e r ( t r e e r o o t ){ 2 3 i f ( r o o t==null) 4 return ; 5 i n O r d e r ( root >l c h i l d ) ; 6 i n O r d e r ( root >r c h i l d ) ; 7 p r i n t f ( %d,, root >x ) ; / put f r e e ( r o o t ) to f r e e memory / 8 return ; 9 }

Files, stdio.h provides necessary declarations FILE is a structure holding all information about file 1 FILE f p ; / p o i n t e r to f i l e / 2 char name [ ] = t e s t. t x t ; / name o f f i l e / 3 char mode [ ] = r ; / mode read / 4 f p = fopen ( name, mode ) ; 18 / 33, 19 / 33 Possible modes r read w write (overwrites) a adds content to the end of the file b required for binary files in some cases If file does not exist and is opened in w or a mode it is created. Opening file that does not exist in r mode causes error (fopen returns NULL)., 20 / 33 How can we read or write file? Similarly to the standard input there are several possible ways of reading input from files: simplest one int getc(file *fp) reads next char from file, returns EOF for end of file or error int getc(int c, FILE *fp) writes a character c to the file and returns written char or EOF if error occurs formatted I/O, works like scanf and printf int fscanf(file *fp, char *format,...) int fprintf(file *fp, char *format,...)

Closing file!, 21 / 33 fclose(file *fp); closes a file pointed by fp brakes a connection between program and file Flushes a buffer where output of putc is collected (you can use int fflush(file *fp) to do it without closing file Example, 22 / 33 Let s write a program that will write an input to the file provided as a parameter. library, 24 / 33 interface UNIX allows us to use several services through a set of system, which are functions of operating system that may be called by our programs. Why system? It is to show you how previously described functions are implemented with functionality provided by UNIX OS.

File descriptors, 25 / 33 In UNIX every peripheral device (including screen and keyboard) is seen as a file. opens for you three standard files stdin, stdout and stderr. UNIX uses small non-negative ints (file descriptors) to identify all files. Standard files are identified by default by 0-stdin, 1-stdout and 2-stderr. On our systems (Prism lab) all required definitions are in header sys/file.h You have to include it to use system. Open vs. Create, Open 1 i n t f d ; 2 f d = open ( name, f l a g s, perms ) ; Create 26 / 33 1 i n t f d ; 2 f d = c r e a t e ( name, perms ) ; name is a char* containing a path to the file flags is an int that specifies how the file is to be opened O_RDONLY open for reading only O_WRONLY open for writing only O_RDWR open for both perms is an int containing information what permissions should be set on the file. We will use 0 as a default value Other options, 27 / 33 Other possible values of flags O_APPEND Append new information to the end of the file. O_TRUNC Initially clear all data from the file. O_CREAT If the file does not exist, create it. If the O_CREAT option is used, then you must include the third parameter. O_EXCL Combined with the O_CREAT option, it ensures that the caller must create the file. If the file already exists, the call will fail.

Permissions, 28 / 33 Values of perms S_IRUSR Set read rights for the owner to true. S_IWUSR Set write rights for the owner to true. S_IXUSR Set execution rights for the owner to true. S_IRGRP Set read rights for the group to true. S_IWGRP Set write rights for the group to true. S_IXGRP Set execution rights for the group to true. S_IROTH Set read rights for other users to true. S_IWOTH Set write rights for other users to true. S_IXOTH Set execution rights for other users to true. Close, 29 / 33 brakes connection between descriptor and file frees the file descriptor so it can be used for another file it is done by system on exit or return from main. Unlink, 30 / 33 Removes the file pointed by name from the file system! It corresponds to remove from standard library Look out there is no warning before removing!!!

- read and write, 1 i n t read ( i n t fd, char buf, i n t n ) ; 2 i n t w r i t e ( i n t fd, char buf, i n t n ) ; fd file descriptor buf an array of characters where the data is to go to or came from n number of bytes to be transfered Both return a number of bytes transfered (read or wrote) 31 / 33 Example, Let s write a program that will implement a copy functionality. It takes two paths and copy first into second. 32 / 33 Random access, Read and write are normally sequential. We can use, however, lseek function to move our cursor in the file into any place. 1 long l s e e k ( i n t fd, long o f f s e t, i n t o r i g i n ) ; sets the position in the file whose descriptor is fd to offset calculated relatively to the location specified by origin Origin can be: 0 means offset is calculated from the beginning of the file 1 means offset is calculated from current position 2 means offset is calculated from the end of the file 33 / 33 1 / go to the b e g i n n i n g o f the f i l e / 2 l s e e k ( fd, 0L, 0 ) ; 3 4 / go to the end o f the f i l e / 5 l s e e k ( fd, 0L, 2 ) ;