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

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

CSE 333 SECTION 3. POSIX I/O Functions

CS240: Programming in C

CSE 410: Systems Programming

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

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

Operating systems. Lecture 7

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

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

Operating System Labs. Yuanbin Wu

Files. Eric McCreath

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

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

Naked C Lecture 6. File Operations and System Calls

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

UNIX System Calls. Sys Calls versus Library Func

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

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

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

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

UNIX System Programming

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

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

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

CSC209H Lecture 6. Dan Zingaro. February 11, 2015

File I/0. Advanced Programming in the UNIX Environment

File Descriptors and Piping

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

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

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

Operating System Labs. Yuanbin Wu

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

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

CSCI 171 Chapter Outlines

UNIX input and output

Process Management! Goals of this Lecture!

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

Lecture 21 Systems Programming in C

Process Creation in UNIX

Basic OS Progamming Abstrac7ons

Recitation 8: Tshlab + VM

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

Basic OS Progamming Abstrac2ons

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

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

Do not turn the page until 5:00.

The course that gives CMU its Zip! I/O Nov 15, 2001

Experiment 6 The Real World Interface

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

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

Chapter 10. The UNIX System Interface

Socket programming in C

CSC 271 Software I: Utilities and Internals

All the scoring jobs will be done by script

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

Lab # 4. Files & Queues in C

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Processes. Johan Montelius KTH

A process. the stack

ECEN 449 Microprocessor System Design. Review of C Programming

Data and File Structures Chapter 2. Basic File Processing Operations

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

Files and the Filesystems. Linux Files

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

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

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Jansson Documentation

Standards / Extensions C or C++ Dependencies POSIX.1 XPG4 XPG4.2 Single UNIX Specification, Version 3

we are here Page 1 Recall: How do we Hide I/O Latency? I/O & Storage Layers Recall: C Low level I/O

The Embedded I/O Company TIP700-SW-82 Linux Device Driver User Manual TEWS TECHNOLOGIES GmbH TEWS TECHNOLOGIES LLC

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

RCU. ò Walk through two system calls in some detail. ò Open and read. ò Too much code to cover all FS system calls. ò 3 Cases for a dentry:

VFS, Continued. Don Porter CSE 506

Files and Directories

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

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)

CS240: Programming in C

FILE SYSTEMS. Jo, Heeseung

structs as arguments

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Summer June 15, 2010

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

W4118: OS Overview. Junfeng Yang

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

TELE402. Internetworking. TELE402 Lecture 1 Protocol Layering 1

CSE 333 Final Exam June 6, 2017 Sample Solution

System calls. Reading from a file. Closing a file

File Systems Overview. Jin-Soo Kim ( Computer Systems Laboratory Sungkyunkwan University

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

CS167 Programming Assignment 1: Shell

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

CSE 333 Section 8 - Client-Side Networking

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Operating Systems II Systems Programming in C

Transcription:

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

POSIX Family of standards specified by the IEEE Maintains compatibility across variants of Unix-like OS Defines API and standards for basic I/O: file, terminal and network Also defines a standard threading library API

Basic File Operations Open the file Read from the file Write to the file Close the file / free up resources

System I/O Calls int open(char* filename, int flags, mode_t mode); Returns an integer which is the file descriptor. Returns -1 if there is a failure. filename: A string representing the name of the file. flags: An integer code describing the access. O_RDONLY -- opens file for read only O_WRONLY opens file for write only O_RDWR opens file for reading and writing O_APPEND --- opens the file for appending O_CREAT -- creates the file if it does not exist O_TRUNC -- overwrite the file if it exists mode: File protection mode. Ignored if O_CREAT is not specified. [man 2 open]

System I/O Calls ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); fd: file descriptor. buf: address of a memory area into which the data is read. count: the maximum amount of data to read from the stream. The return value is the actual amount of data read from the file. int close(int fd); Returns 0 on success, -1 on failure. [man 2 read] [man 2 write] [man 2 close]

Errors When an error occurs, the error number is stored in errno, which is defined under <errno.h> View/Print details of the error using perror() and errno. POSIX functions have a variety of error codes to represent different errors. Some common error conditions: EBADF - fd is not a valid file descriptor or is not open for reading. EFAULT - buf is outside your accessible address space. EINTR - The call was interrupted by a signal before any data was read. EISDIR - fd refers to a directory. errno is shared by all library functions and overwritten frequently, so you must read it right after an error to be sure of getting the right code [man 3 errno] [man 3 perror]

Reading a file #include <errno.h> #include <unistd.h>... char *buf =...; // buffer has size n int bytes_left = n; // where n is the length of file in bytes int result = 0; while (bytes_left > 0) { result = read(fd, buf + (n-bytes_left), bytes_left); if (result == -1) { if (errno!= EINTR) { // a real error happened, return an error result } // EINTR happened, do nothing and loop back around continue; } bytes_left -= result; }

STDIO vs. POSIX Functions User mode vs. Kernel mode. STDIO library functions fopen, fread, fwrite, fclose, etc. use FILE* pointers. POSIX functions open, read, write, close, etc. use integer file descriptors.

JSON & Jannsson

JSON Data format to transmit objects in human readable text Not specific to JavaScript derived from javascript but any language can write and parse it In HW2 use it to serialize a 2D array or in general any complicated object Serialize -> create a one dimensional representation of this Will use the JSON output to test your input Not defining the interface for you so we can t run unit tests. Instead will compare against runtime data stored

JSON cont. Represents simple types like integer and string plus two complex types: arrays and maps Arrays using square brackets [1, 2, hello ] Maps using curly braces { key : 1, cat 2}

Jannsson Library we provide to help read and write JSON files. Use it serialize your 2D array by creating a Jansson object and populating with values from your 2D array, then use Jansson to write JSON to file json_t *array = json_array(); json_array_append_new(array, json_integer(42)); json_t *obj = json_object() Json_object_set_new(obj, foo, array);

Jansson cont. Deserialize JSON data into a Jansson object and fetch values from it to re-populate your 2D array // Loading json_t *root; json_error_t error; root = json_loads( data, 0, &error ); error checking // Extract functions json_object_get(root, field) json_array_get(root) Documentation provided in the library!

Generics

Using void pointers data next data next data next data next NULL struct struct struct struct Data is a void* - can be a pointer to anything Can also directly store primitive sizes like ints, floats (as long as < size of pointer) to avoid allocating extra memory Up to the programmer to keep track of types of elements in the list User must cast to the appropriate type to operate on the data

void*generics - callbacks Data structure can provide functions that apply user specified callback to elements User can explicitly cast void* pointers to desired type and preform an operation Custom free function frees pointers to malloc d data, does nothing for primitives Map function, etc. Implemented generic LinkedList in HW1 Free and sort functions that were type specific

Using the preprocessor Use the preprocessor to expand macros and generate type specific versions of the data structure. #define CREATE_LLIST_TYPE(t,s) \ typedef struct llist_node_t_ ## s { \ struct llist_node_t_ ## s *next; \ t data; \ } LList_node_ ## s; \ Each call to CREATE_LIST_TYPE(t,s) generates the appropriate code during preprocessing. You explicitly tell the preprocessor what code to create. Notice each version must have a different name to link -> name mangling

## concatenates with no spaces between them #define CREATE_LLIST_TYPE(t,s) \ typedef struct llist_node_t_ ## s { \ struct llist_node_t_ ## s *next; \ t data; \ } LList_node_ ## s; \ #define CREATE_LLIST_TYPE(int,int) #define CREATE_LLIST_TYPE(char*,string) typedef struct llist_node_t_int { struct llist_node_t_int *next; int data; } LList_node_int; typedef struct llist_node_t_string { struct llist_node_t_string *next; char* data; } LList_node_string;

Preprocessor caveats Can t hide any implementation from the user (no private headers) Source code written in the headers Hard to debug Will see something similar with how C++ implements generics using templates.