The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?

Slides:



Advertisements
Similar presentations
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Advertisements

Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
The Environment of a UNIX Process. Introduction How is main() called? How are arguments passed? Memory layout? Memory allocation? Environment variables.
Daemon Processes Long lived utility processes Often started at system boot and ended when system shuts down Run in the background with no controlling terminal.
Creating a device-file node An introduction to some privileged Linux system-calls (needed for an upcoming programming exercise)
The ‘system-call’ interface We see how an application program can invoke privileged kernel services.
Sleeping and waking An introduction to character-mode device-driver modules for Linux.
Ways to read data from disk to memory Tan Li. read, write read, write -- low level file access, it's an operation between two file discriptors. SYNOPSIS.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
UNIX’s “grand illusion” How Linux makes a hardware device appear to be a ‘file’
Character Driver Issues Implementing ‘/dev/physmem’
Standard C Libraries Application Programmming Interface to System-Calls.
OS Project #2:Random File Access  Random file access: using the lseek() system call to access files in UNIX. zTake arguments from the command line. 
How to make a pseudo-file As a follow-up to our first lab, we examine the steps needed to create our own ‘/proc’ file.
Standard C Library Application Programming Interface to System-Calls.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
Design and Implementation of the Log-based File I/O Library for Sandboxing CSE 542 Operating Systems Fall 2005 Qi Liao and Xuwen Yu.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
The ‘wait4()’ function Looking at how the various ‘wait()’ system-calls rely on ‘wait4()’ in the x86_64 Linux kernel.
Accessing Files in CCS-2303, C-Term Accessing Files in C CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
CSE 451 Section 4 Project 2 Design Considerations.
Pointers Applications
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
NCHU System & Network Lab Lab 13 File I/O & Standard I/O.
Copyright Arshi Khan1 System Programming Instructor Arshi Khan.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
System Calls 1.
Offline Performance Monitoring for Linux Abhishek Shukla.
ANDROID Presented By Mastan Vali.SK. © artesis 2008 | 2 1. Introduction 2. Platform 3. Software development 4. Advantages Main topics.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 5 Acknowledgements: The syllabus and power point presentations are modified versions.
Chapter 2: Operating-System Structures. 2.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 2: Operating-System Structures Operating.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
File System Review bottomupcs.com J. Kubiatowicz, UC Berkeley.
Operating Systems Recitation 1, March th, 2002.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
UNIX Files File organization and a few primitives.
1 Shared Memory. 2  Introduction  Creating a Shared Memory Segment  Shared Memory Control  Shared Memory Operations  Using a File as Shared Memory.
Implementing System Calls CS552 Kartik Gopalan. CS552/BU/Spring2008 Steps in writing a system call 1.Create an entry for the system call in the kernel’s.
System Calls. The Linux we use is: Linux-Mandrake 7.0. In this project, you are going to change some kernel files and recompile the kernel. After you.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
Project 6 Unix File System. Administrative No Design Review – A design document instead 2-3 pages max No collaboration with peers – Piazza is for clarifications.
CS 6560 Operating System Design Lecture 5: System Calls Interrupts.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Linux File system Implementations
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
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.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
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.
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.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
Error handling I/O Man pages
CS/COE 0449 (term 2174) Jarrett Billingsley
Lecture 31: Introduction to File System
Advanced UNIX progamming
C Basics.
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
Advanced UNIX progamming
CSE 333 – Section 3 POSIX I/O Functions.
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?

A Modern OS Design Hardware Application Shared Runtime Libraries user-mode supervisor-mode System Call Interface Device Driver Components memory manager process manager file manager network manager OS Kernel

Role of ‘runtime libraries’ To understand what role is played by the kernel’s interface with ‘runtime libraries,’ let’s see how we could go around them. We can create a demo-program that does not need to use the standard C library – it will perform all their work on its own. If you fire your janitor, you will very quickly come to appreciate all that he did for you!

Apps can call kernel directly Hardware C applicationC++ application Assembly Language application Shared Runtime Libraries user-mode supervisor-mode System Call Interface Device Driver Components memory manager process manager file manager network manager OS Kernel

Standard C library functions The functions ‘write()’ and ‘exit()’ were not actually defined within our program-code They are examples of ‘external’ functions Header-files let the compiler know how to generate assembler code that calls them Their actual definitions are part of the standard GNU/C runtime-library (‘glibc’) The linker connects our code to ‘glibc’

The function ‘prototypes’ ssize_t write( int fd, void * buf, size_t count ); void exit( int status ); The C/C++ compiler needs this information to correctly generate the machine-code for calls to these ‘external’ library-functions.

C/C++ type names Some data-types used in C and C++ have sizes that are dependent on the underlying CPU architecture and OS design, and are not identical for all platforms and compilers A few relevant examples: –‘long int’ –‘size_t’ –‘ssize_t’ –‘off_t’

POSIX The ‘Portable Operating System Interface’ Defines a conformance ‘standard’ that lets application programmers write code which is ‘portable’ across differing architectures Implemented with header-files and shared runtime libraries Documented as an ANSI standard Online reference available via ‘man’ pages

Frequently needed functions Here are a few of the POSIX standard functions which are most frequently used by applications: –open() –read() –write() –close() –lseek() –exit() It’s advisable to learn them! (will save you time)

Their C function-prototypes ssize_t read( int fd, void *buf, size_t count ); ssize_t write( int fd, void *buf, int count ); int open( char *fname, int flags, … ); int close( int fd ); off_t lseek( int fd, off_t offset, int whence ); void exit( int status ); NOTE: These POSIX functions correspond directly to Linux kernel system-calls

Linux x86_64 system-call IDs All the Linux system-calls are assigned ID- numbers in this header-file: // System-call examples for x86_64 Linux #define __NR_read0 #define __NR_write1 #define __NR_open2 #define __NR_close3 … #define __NR_lseek8 … #define __NR_exit60 …

function semantics We will give a rough general description of the actions taken by the kernel when these six frequently needed functions are called Additional details about each function can be found online, using the ‘man’ command Example: $ man 2 read

ssize_t write( int fd, void *buf, size_t count ) This function asks the kernel to transfer ‘count’ bytes from the array pointed to by ‘buf’ to the previously-opened file specified by the file-descriptor ‘fd’. If the kernel cannot transfer any bytes, it returns the value -1; otherwise it returns the number of bytes that the kernel was able to transfer successfully and advances the file-pointer accordingly.

ssize_t read( int fd, void *buf, size_t count ) This function asks the kernel to transfer ‘count’ bytes into the array pointed to by ‘buf’ from the previously-opened file specified by ‘fd’. If the kernel cannot transfer any bytes, it returns a value of unless the file-pointer is already at the end of the file; otherwise, it returns the number of bytes successfully transferred and advances the file-pointer accordingly.

int open( char *fname, int flags, … ) This function asks the kernel to set up an internel record establishing a connection between the file named ‘fname’ and an unused nonnegative integer known as a file-descriptor, provided the file’s access permissions allow the kind of access that is specified by the ‘flags’ argument The function returns -1 if it cannot perform this operation; otherwise, it returns the file-descriptor value. An additional argument when creating a new file

int close( int fd ) This function asks the kernel to remove the connection it had previously set up between the file-descriptor ‘fd’ and the internal kernel record of an associated file. The value -1 is returned if the kernel was not succesful in performing this operation (e.g., the file had already been closed); otherwise, this function returns 0.

off_t lseek( int fd, off_t offset, int whence) This function asks the kernel to reposition the offset of the open file associated with the file descriptor ‘fd’ to the position given as ‘offset’ according to the given ‘whence’ directive, as follows: –SEEK_SET (=0): position is set to ‘offset’ –SEEK_CUR (=1): position += ‘offset’ –SEEK_END (=2): position = EOF + ‘offset’

void exit( int status ) This function asks the kernel to terminate the current program, and place the value of ‘status’ into the internal record that the kernel had associated with this program when it was first launched. This function will not return any value (because it does not return at all!)