System calls. Reading from a file. Closing a file

Similar documents
CSE 333 SECTION 3. POSIX I/O Functions

CSE 333 SECTION 3. POSIX I/O Functions

A Fast Review of C Essentials Part II

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

Operating systems. Lecture 7

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

(T) x. Casts. A cast converts the value held in variable x to type T

UNIX System Calls. Sys Calls versus Library Func

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)

Recitation 8: Tshlab + VM

Operating System Labs. Yuanbin Wu

CSCE 455/855 Distributed Operating Systems

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Operating System Labs. Yuanbin Wu

TIP114-SW-72. LynxOS Device Driver. User Manual. The Embedded I/O Company. 10 Channel Absolute Encoder Interface (SSI) Version 1.0.

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

Chapter 10. The UNIX System Interface

CS 3013 Operating Systems WPI, A Term Assigned: Friday, August 31, 2007 Due: Monday, September 17, 2007

CSE 333 Midterm Exam 2/12/16. Name UW ID#

Naked C Lecture 6. File Operations and System Calls

Workshop on Inter Process Communication Solutions

PRINCIPLES OF OPERATING SYSTEMS

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Reviewing gcc, make, gdb, and Linux Editors 1

CSC209H Lecture 7. Dan Zingaro. February 25, 2015

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

CSC209 Fall Karen Reid 1

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

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

Static Vulnerability Analysis

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

Software Engineering /48

Creating, Compiling and Executing

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CSE 374 Programming Concepts & Tools

File Descriptors and Piping

CSE 333 Final Exam June 6, 2017 Sample Solution

DRIVER AND LIBRARY FOR DB67 DSP BOARD DRIVER AND LIBRARY FOR DB67 DSP BOARD

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

Discussion 6. HW4 and Libraries

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

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Programming Studio #9 ECE 190

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Chapter 11 Introduction to Programming in C

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

Computer Security. Robust and secure programming in C. Marius Minea. 12 October 2017

Operating Systems CS 217. Provides each process with a virtual machine. Promises each program the illusion of having whole machine to itself

COSC Operating Systems Design, Fall Lecture Note: Unnamed Pipe and Shared Memory. Unnamed Pipes

All the scoring jobs will be done by script

Figure 1 Ring Structures

Recitation Processes, Signals,UNIX error handling

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Makefiles SE 2XA3. Term I, 2018/19

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

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

CS 361 Computer Systems Fall 2017 Homework Assignment 1 Linking - From Source Code to Executable Binary

More on C programming

UNIX input and output

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

CS Operating system Summer Midterm I -- July 11, 2017 You have 115 min (10:00am-11:55pm). Good Luck!

Chapter 11 Introduction to Programming in C

CSC 271 Software I: Utilities and Internals

CSE 410: Systems Programming

CS2141 Software Development using C/C++ Compiling a C++ Program

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

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

Problem Set 1: Unix Commands 1

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview

CS240: Programming in C. Lecture 14: Errors

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

CSC258: Computer Organization. Functions and the Compiler Tool Chain

Chapter 11 Introduction to Programming in C

Project 3: Base64 Content-Transfer-Encoding

Socket programming in C

Chapter 11 Introduction to Programming in C

CS 25200: Systems Programming. Lecture 14: Files, Fork, and Pipes

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

Goals of this Lecture

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

IPC and Unix Special Files

A brief introduction to C programming for Java programmers

Structured programming

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

make and makefile CS 211

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

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

SOFTWARE ARCHITECTURE 2. FILE SYSTEM. Tatsuya Hagino lecture URL.

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Summer June 15, 2010

Chapter 11 Introduction to Programming in C

TIP675-SW-82. Linux Device Driver. 48 TTL I/O Lines with Interrupts Version 1.2.x. User Manual. Issue November 2013

Transcription:

System calls The C language and C compiler has no knowledge about system calls. They are just function calls to precompiled functions. Example: fd = open( test.c, O_RDONLY); This will open the file test.c in read-only mode. The function value is a descriptor number or -1 if open failed. Typical use of open: If((fd=open( test.c, O_RDONLY)) < 0), printf( Could not open the file test.c \n ); } Reading from a file The read system call looks like this: fd is a file descriptor (from open for example) char buf[4000]; n = read(fd, buf, 4000); n is the number of characters actually read. It is sometimes less than 4000. If n is 0 it means end of file If n < 0 it means that an error has occured. Closing a file To close a file the close system call is used. n = close(fd); n is -1 if there was an error otherwise 0. 1

Error handling for system calls Compile time statements Error returns for system calls are usually indicated by the function value. The function value is something impossible (often -1). Furthermore more information on the error is stored in the external (int) variable errno. You shouldn t declare errno but include <errno.h> insstead To get printed explanation use perror function: if ((n=read(..)) < 0) perror( Could not read input file ); This will print the text and an explanation of the current errno value. We have already seen the #include statement. There are two of them #include <filename.h> searches for the filename.h in /usr/include (in general) #include filename.h searches first in the directory where the source file is then as the alternative above. Compile time statements #define defines a macro. In its simplest form it looks like this: #define TABSIZE 100. int table[tabsize]; for (i = 0; i < TABSIZE; i++). table*i+.. gcc has an enormous number of flags. The man command for gcc generates about 170 pages. Usually we need no flags gcc test.c will compile the file and generate an a.out file gcc -o testprog test.c will compile test.c and give a testprog executable file 2

gcc -c test.c means that test.c should be compiled into assembly text which is assembled but the result is left in test.o No linking is done. gcc -S test.c means that test.c should be compiled into assembly text which is left in test.s gcc sends some flags to the linker. The relevant flag here is the -l flag. gcc test.c -lalib (note, no space after -l) The -l flag is sent to the linker asking it to first link the file test.o and then search alib for unresolved references and then the standard libraries. The placement of -l flag is important. gcc test.c lalib test2.c Pitfalls to avoid (1) Remember the difference between operators = and == The first is an assignment while the second is a test for equality. Since the C-language is rather forgiving the error is seldom detected by the compiler. if (i == j+1) or if (i = j+1) are both correct C but with different meanings. Pitfalls to avoid (2) Don t delete necessary unnecessary parenthesis! if ((fd = open( test.c, O_RDONLY)) < 0) perror( Unable to open test.c ); read(fd, buf, 2000); This is correct C but isn t it tempting to delete one of the paired parenthesis? 3

Pitfalls to avoid (2) Pitfalls to avoid (3) Then we get this. It is valid C but with very different result if (fd = open( test.c, O_RDONLY) < 0) perror( Unable to open test.c ); read(fd, buf, 2000); If open fails the program takes correctly the error path. If open succeeds fd will be set to 0 and the read will read from standard input (keyboard). Not expected! You must always test system calls for error returns and give appropriate errormessage! If for example an open fails we cannot do reads on the file! If we try we will probably use -1 as the descriptor number that will make read to fail. Pitfalls to avoid (4) Remember that arrays (both native arrays and arrays pointed to by pointers) are indexed from 0! Pitfalls to avoid (5) Remember the differens between the logical operators && ( ) and the bitwise operators & ( ). They are different and create errors if mixed that the compiler never will find and that can work in some special cases. 4

Debugging advice Debugging advice When you are debugging the program use plenty of printf statements to report the progress in the program and relevant intermediate results. Use fflush(null) after each printf statement to assure that the text is written out and not just be sitting in a buffer. When the program is working you delete these statements and test the resulting program again. Example: if ((fd = open( test.c, O_RDONLY)) < 0) perror( unable to open test.c ); printf( Open of test.c succeeded, fd = %d\n,fd); fflush(null); 5