Standard C Library Application Programming Interface to System-Calls.

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
RT_FIFO, Device driver.
Another device-driver? Getting ready to program the network interface.
I/O Multiplexing The role of the ‘poll()’ method in Linux device-driver operations.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
January 13, Files – Chapter 2 Basic File Processing Operations.
CS 241 Section Week #5 2/23/12. 2 Topics This Section MP4 overview Function Pointers Pthreads File I/O.
I/o multiplexing On adding a ‘poll()’ method to our character-mode device-driver for an 82573L network controller.
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.
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’
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
04/14/2008CSCI 315 Operating Systems Design1 I/O Systems Notice: The slides for this lecture have been largely based on those accompanying the textbook.
Character Driver Issues Implementing ‘/dev/physmem’
Standard C Libraries Application Programmming Interface to System-Calls.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
1 System Calls & Stdio. 2 Two processes open the same file Both keep writing to it What happens?
A ‘ringbuffer’ application Introduction to process ‘blocking’ and the Linux kernel’s support for ‘sleeping’ and ‘waking’
Introduction to Kernel
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –
Posix Message Queues Courtesy of W. Richard Stevens Unix Network Programming Volume 2: Interprocess Communication.
The ‘mmap()’ method Adding the ‘mmap()’ capability to our ‘vram.c’ device-driver.
TCSS 342, Winter 2005 Lecture Notes
Guide To UNIX Using Linux Third Edition
Chapter 3 Buffer Cache TOPICS UNIX system Architecture Buffer Cache
FreeRTOS.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Data Types in the Kernel Ted Baker  Andy Wang CIS 4930 / COP 5641.
SIMULATED UNIX FILE SYSTEM Implementation in C Tarek Youssef Bipanjit Sihra.
C questions A great programmer codes excellent code in C and Java. The code does video decoding. Java code works faster then C on my computer. how come?
Operating Systems Recitation 1, March th, 2002.
System Commands and Interprocess Communication. chroot int chroot(const char *path); chroot changes the root directory to that specified in path. This.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
UNIX Files File organization and a few primitives.
Queues, Pipes and Sockets. QUEUE A structure with a series of data elements with the first element waiting for an operation Used when an element is not.
What Every Developer Should Know about the Kernel Dr. Michael L. Collard 1.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Linux File system Implementations
CSE 466 – Fall Introduction - 1 User / Kernel Space Physical Memory mem mapped I/O kernel code user pages user code GPLR virtual kernel C
 Wind River Systems, Inc Chapter - 7 Intertask Communication.
Named Pipes. Kinds of IPC u Mutexes/Conditional Variables/Semaphores u Pipes u Named pipes u Signals u Shared memory u Messages u Sockets.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
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.
ICOM Noack Linux I/O structure Device special files Device switch tables and fops How the kernel finds a device Parts of a device driver or module.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
OS Labs 2/25/08 Frans Kaashoek MIT
Silberschatz, Galvin, and Gagne  Applied Operating System Concepts Module 12: I/O Systems I/O hardwared Application I/O Interface Kernel I/O.
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.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
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.
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 -
Introduction to Kernel
Chapter 3 Buffer Cache TOPICS UNIX system Architecture Buffer Cache
An overview of the kernel structure
CSE 333 – Section 3 POSIX I/O Functions.
CSE 333 – Section 3 POSIX I/O Functions.
Intertask Communication
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Presentation transcript:

Standard C Library Application Programming Interface to System-Calls

Important File I/O Functions int open( char *pathname, int flags ); int read( int fd, void *buf, size_t count ); int write( int fd, void *buf, size_t count ); int close( int fd );

UNIX ‘man’ pages A convenient online guide to prototypes and semantics of the C Library Functions Example of usage: $ man 2 open

The ‘open’ function #include int open( const char *pathname, int flags ); Converts a pathname to a file-descriptor File-descriptor is a nonnegative integer Used as a file-ID in subsequent functions ‘flags’ is a symbolic constant: O_RDONLY, O_WRONLY, O_RDWR

The ‘close’ function #include int close( int fd ); Breaks link between file and file-descriptor Returns 0 on success, or -1 if an error

The ‘read’ function #include int read( int fd, void *buf, size_t count ); Attempts to read up to ‘count’ bytes Bytes are placed in ‘buf’ memory-buffer Returns the number of bytes read Or returns -1 if some error occurred Return-value 0 means ‘end-of-file’

The ‘write’ function #include int write( int fd, void *buf, size_t count ); Attempts to write up to ‘count’ bytes Bytes are taken from ‘buf’ memory-buffer Returns the number of bytes written Or returns -1 if some error occurred Return-value 0 means no data was written

Default is ‘Blocking’ Mode Special considerations for device-files The ‘read()’ function normally does not return 0 unless ‘end-of-file’ is reached Devices expected to have more data soon But on multitasking system: waiting is bad!

How system-calls work Application Program User-spaceKernel-space C Runtime Library Operating System Kernel Device Driver

How multitasking works Can be ‘cooperative’ or ‘preemptive’ ‘interrupted’ doesn’t mean ‘preempted’ ‘preempted’ implies a task was switched ‘task-switching’ implies a context-change

Tasks have various ‘states’ A task may be ‘running’ A task may be ‘ready-to-run’ A task may be ‘blocked’

Kernel manages tasks Kernel uses ‘queues’ to manage tasks A queue of tasks that are ‘ready-to-run’ Other queues for tasks that are ‘blocked’

Special ‘wait’ queues Need to avoid wasteful ‘busy waiting’ So Device-Drivers can put tasks to sleep And Drivers can ‘wake up’ sleeping tasks

How to use Linux wait-queues #include wait_queue_head_tmy_queue; init_wait_queue_head( &my_queue ); sleep_on( &wq ); wake_up( &wq ); But can’t unload driver if task stays asleep!

‘interruptible’ wait-queues Device-driver modules should use: interruptible_sleep_on( &my_queue ); wake_up_interruptible( &my_queue ); Then tasks can be awakened by interrupts

A convenient ‘macro’ DECLARE_WAIT_QUEUE_HEAD( wq ); This statement can be placed outside your module’s functions It combines declaration and initialization: wait_queue_head_t wq; init_wait_queue( &wq );

‘stash’: a character device Device works like a public ‘clipboard’ It uses kernel memory to store its data It allows ‘communication’ between tasks What one task writes, another can read!

Ringbuffer A first-in first-out data-structure (FIFO) Uses a storage array of finite length Uses two array-indices: ‘head’ and ‘tail’ Data is added at the current ‘tail’ position Data is removed from the ‘head’ position

Ringbuffer (continued) One array-position is always left unused Condition head == tail means “empty” Condition tail == head-1 means “full” Both ‘head’ and ‘tail’ will “wraparound” Calculation: next = ( next+1 )%RINGSIZE;

write-algorithm for ‘stash’ while ( ringbuffer_is_full ) { interruptible_sleep_on( &wq ); If ( signal_pending( current ) ) return –EINTR; } Insert byte from user-space into ringbuffer; wake_up_interruptible( &wq ); return 1;

read-algorithm for ‘stash’ while ( ringbuffer_is_empty ) { interruptible_sleep_on( &wq ); If ( signal_pending( current ) ) return –EINTR; } Remove byte from ringbuffer and store to user-space; wake_up_interruptible( &wq ); return 1;

Demonstration of ‘stash’ Quick demo: we can use I/O redirection For demonstrating ‘write’ to /dev/stash: $ echo “Hello” > /dev/stash For demonstrating ‘read’ from /dev/stash: $ cat /proc/stash