CSCE 510 - Systems Programming Lecture 02 - System Calls CSCE 510 Jan 16, 2013.

Slides:



Advertisements
Similar presentations
CSCI 1730 April 1 st, Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.
Advertisements

© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: and st Ed: and
1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
1 Processes Professor Jennifer Rexford
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
1 UNIX 1 History of UNIX 2 Overview of UNIX 3 Processes in UNIX 4 Memory management in UNIX 5 The UNIX file system 6 Input/output in UNIX.
1 Processes and Pipes. 2 "He was below me. I saw his markings, manoeuvred myself behind him and shot him down. If I had known it was Saint-Exupery, I.
1 Case Study 1: UNIX and LINUX Chapter History of unix 10.2 Overview of unix 10.3 Processes in unix 10.4 Memory management in unix 10.5 Input/output.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 1 of 60.
I/O April 16, 2002 Topics Files Unix I/O Standard I/O Reading: (Beta) or (New) Problems: 12.9 (Beta) or 12.4 (New) class24.ppt
Linux Operating System
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105 “Tour of the Black Holes of Computing”
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
CSCE Systems Programming Lecture 03 - The Stat System Call CSCE 510 Jan 23, 2013.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
1 Logging in to a UNIX System init ( Process ID 1 created by the kernel at bootstrap ) spawns getty for every terminal device invokes our login shell terminal.
CS 590 Programming Environments with UNIX. Computer Lab Account Course Homepage
CSCE Systems Programming Lec 28 Finale – Wrap-Up CSCE 510 April 29, 2013.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
30 October Agenda for Today Introduction and purpose of the course Introduction and purpose of the course Organization of a computer system Organization.
Florida State UniversityCOP5570 – Advanced Unix Programming Today’s topics System V Interprocess communication (IPC) mechanisms –Message Queues –Semaphores.
UNIX UTILITY PACKAGE Shell Simulator Added Shell Commands File System Explorer ( LNC ) System Information Process Tree Viewer.
LINUX System : Lecture 7 Bong-Soo Sohn Lecture notes acknowledgement : The design of UNIX Operating System.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
Files and Directories File types stat functions for file information
Navigating Directories
Operating Systems Process Creation
Interprocess Communication Anonymous Pipes Named Pipes (FIFOs) popen() / pclose()
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing CS 105 “Tour of the Black Holes of Computing”
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
CS 105 “Tour of the Black Holes of Computing”
NCHU System & Network Lab Lab 14 File and Directory.
CSCI 330 UNIX and Network Programming
Byoung-Jo CHOI Fall 2007 SW Project II Advanced Linux Programming.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
Library Functions The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
CSCE Systems Programming Lecture 07 – Make, Stdio, I/O Buffering CSCE 510 Jan 23, 2013.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 3.
Week 12 - Thursday CS222.
Case Study 1: UNIX and LINUX
CASE STUDY 1: Linux and Android
Linux Processes & Threads
UNIX System Overview.
IT 344: Operating Systems Module 4 Processes
Operating System Hebrew University Spring 2004
LINUX System Programming with Processes (additional)
“The course that gives CMU its Zip!”
File Structure Related system calls
Programming Assignment # 2 – Supplementary Discussion
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
CSE 333 – Section 3 POSIX I/O Functions.
LINUX System : Lecture 7 Lecture notes acknowledgement : The design of UNIX Operating System.
CSE 451: Operating Systems Autumn 2009 Module 4 Processes
Chien-Chung Shen CIS, UD
IT 344: Operating Systems Winter 2008 Module 4 Processes
CSE 451: Operating Systems Winter 2007 Module 4 Processes
CS 105 “Tour of the Black Holes of Computing”
Presentation transcript:

CSCE Systems Programming Lecture 02 - System Calls CSCE 510 Jan 16, 2013

Overview Last Time  Outback Readings for today  Text Chapter 2, 3, 18.8, 15.1  /class/csce /Code/TLPI Prologue  Fundamental Unix/systems programming (Chap 2) continued Interlude - Course Pragmatics, CS man of the day, Quiz Epilogue  System Calls  Directories  Stat system call for information on files

Overview Continued – Chapter 2 CSCE 510 Jan 16, Slide - 3 System Calls  Shell revisited  2.6 Programs  2.7 Processes  2.8 Memory Mappings  2.9 Static and Shared Libraries  2.10 Interprocess Communication and Synchronization  2.11 Signals  2.12 Threads  Other Topics

Shell revisited CSCE 510 Jan 16, Slide - 4 System Calls Print prompt  Read command  Substitutions  Fork/Exec  Wait - get return (exit) status Repeat  Substitutions  Wildcards, Filename completion, alias subs., history, cmd substitution  I/O redirection  Filter a program that reads stdin writes stdout  ls –l > listing  grep Unix | wc

File I/O Model CSCE 510 Jan 16, Slide - 5 System Calls  One of the distinguishing features of the I/O model on UNIX systems is the concept of universality of I/O.  This means that the same system calls:  open(), read(), write(), close(),  used to perform I/ O on all types of files, including devices.  File descriptors 0 – stdin, 1 – stdout, 2 – stderr unless remapped  Stdio Library  FILE *fp = fopen(“filename, “w+”) The Linux Programming Interface - Kerrisk

Programs CSCE 510 Jan 16, Slide - 6 System Calls  Source files .c,.cpp,.y,.py,.rb …  Object modules  Executables  Ar archives  Scripts  Shell scripts, perl, python, ruby,  gcc –c prog.c -lm  gcc –S prog.c  gcc prog.c

Processes CSCE 510 Jan 16, Slide - 7 System Calls  Memory Layout Memory Layout of a Unix Process Environment, command line args Stack Heap Shared Libraries Heap Bss: Global Data Text Segment  fork()- creates processes  Parent, Child  _exit(), exit(), wait()  init, scheduler  ps, kill

Memory Mappings CSCE 510 Jan 16, Slide - 8 System Calls

Static and Shared Libraries CSCE 510 Jan 16, Slide - 9 System Calls  Static Libraries  gcc –O2 -Wall prog.c –lm  ar t /usr/lib/libm.a | less  Shared Libraries .so

Interprocess Communication and Synchronization CSCE 510 Jan 16, Slide - 10 System Calls  Signals  Pipes  Sockets  File locks  IPC: semaphores, shared memory, msgqueues

Signals CSCE 510 Jan 16, Slide - 11 System Calls

Threads CSCE 510 Jan 16, Slide - 12 System Calls

/proc CSCE 510 Jan 16, Slide - 13 System Calls  “an interface to kernel data structures in a form that looks like files and directories”  /proc/pid - directory of info about running process  /proc/1 – information on init  /proc/cpuinfo

Other Topics from Chapter 2 CSCE 510 Jan 16, Slide - 14 System Calls  2.13 Process Groups and Shell Job Control  2.14 Sessions, Controlling Terminals, and Controlling Processes  2.15 Pseudoterminals  2.16 Date and Time  2.17 Client-Server Architecture  2.18 Realtime  2.19 The /proc File System

Intermission CSCE 510 Jan 16, Slide - 15 System Calls  Unix Jeopardy  System Calls  Directories 18.8  HW 1  Program 1

Unix for UC Berkeley Grad 2. “You can't trust code that you did not totally create yourself.” “You can't trust code that you did not totally create yourself. 3. “I wanted to avoid, special IO for terminals.” “I wanted to avoid, special IO for terminals. 4. B 5. the chess machine BelleBelle 6. Turing Award Unix 1/4/2013 CSCE 510 Sp 13 - Slide - 16

Lions Book CSCE 510 Jan 16, Slide - 17 System Calls dex.php Commentary on the 6 th Edition of Unix

HW -1 CSCE 510 Jan 16, Slide - 18 System Calls 1. More than a hint  Name the program args.c  Args.py 2. No hints

Args.py - CSCE Slide - 19 System Calls #!/usr/bin/python import sys print sys.argv[1:]

2nd Half CSCE 510 Jan 16, Slide - 20 System Calls  3.1 System Calls  3.2 Library Functions  3.3 The Standard C Library; The GNU C Library (glibc)  3.4 Handling Errors from System Calls and Library Functions  3.5 Notes on the Example Programs in This Book  Command-Line Options and Arguments  Common Functions and Header Files  3.6 Portability Issues  18.8 Directories  15.1 Stat system call to retrieve file information

3.1 System Calls CSCE 510 Jan 16, Slide - 21 System Calls  A controlled entry point into the kernel  Allows process to request the kernel to do something  Switches from user mode to kernel mode

Errors on System Calls CSCE 510 Jan 16, Slide - 22 System Calls fd = open( pathname, flags, mode); /* system call to open a file */ if (fd = = -1) { /* Code to handle the error */ }... if (close( fd) = = -1) { /* Code to handle the error */ } -1, NULL, errno, perror()

Fig 3.1 Execution of System Call CSCE 510 Jan 16, Slide - 23 System Calls ..

3.2 Library Functions CSCE 510 Jan 16, Slide - 24 System Calls

3.3 The Standard C Library CSCE 510 Jan 16, Slide - 25 System Calls

The GNU C Library (glibc) CSCE 510 Jan 16, Slide - 26 System Calls  software/ libc/  Determining the version of glibc on the system  /lib/ libc.so. 6  lld - list dynamic dependencies #include const char *gnu_get_libc_version( void);

Example 3-1. Header file for TLPI programs - CSCE Slide - 27 System Calls lib/ tlpi_hdr.h #ifndef TLPI_HDR_H #define TLPI_HDR_H /* Prevent accidental double inclusion */ #include /*Type defs used by many programs*/ #include /* Standard I/ O functions */ #include /* Prototypes of commonly used library functions, plus EXIT_SUCCESS and EXIT_FAILURE constants */ #include /* Prototypes for many system calls */ #include /* Declares errno and defines err constnts*/ Kerrisk, Michael ( ). The Linux Programming Interface: A Linux and UNIX System Programming Handbook (Kindle Locations ). O'Reilly Distribution. Kindle Edition.

- CSCE Slide - 28 System Calls #include /*Common string-handling functions */ #include "get_num.h" /* Declares our functions for handling numeric arguments (getInt(), getLong()) */ #include "error_functions.h" /* Declares our error-handling functions */ typedef enum { FALSE, TRUE } Boolean; #define min( m, n) (( m) < (n) ? (m) : (n)) #define max( m, n) (( m) > (n) ? (m) : (n)) #endif lib/ tlpi_hdr.h Kerrisk, Michael ( ). The Linux Programming Interface: A Linux and UNIX System Programming Handbook (Kindle Locations ). O'Reilly Distribution. Kindle Edition.

3.5.1 Error-diagnostic functions CSCE 510 Jan 16, Slide - 29 System Calls Example 3-2. Decl for common error-handling funcs lib/ error_functions.h #ifndef ERROR_FUNCTIONS_H #define ERROR_FUNCTIONS_H void errMsg( const char *format,...); #ifdef __GNUC__ /* This macro stops 'gcc -Wall' complaining that "control reaches... */ #define NORETURN __attribute__ ((__noreturn__)) #else #define NORETURN #endif void errExit( const char *format,...) NORETURN ; void err_exit( const char *format,...) NORETURN ; void errExitEN( int errnum, const char *format,...) NORETURN ; void fatal( const char *format,...) NORETURN ; void usageErr( const char *format,...) NORETURN ; void cmdLineErr( const char *format,...) NORETURN ;

3.5 Notes on the Example Programs from Text CSCE 510 Jan 16, Slide - 30 System Calls

18.8 Reading Directories CSCE 510 Jan 16, Slide - 31 System Calls  opendir(), readdir() #include DIR *fdopendir( int fd); struct dirent *readdir( DIR *dirp); struct dirent { ino_t d_ino; /* File i-node number */ char d_name[]; /* name of file */ };

CSCE 510 Jan 16, Slide - 32 System Calls errno = 0; direntp = readdir( dirp); if (direntp = = NULL) { if (errno != 0) { /* Handle error */ } else { /* We reached end-of-directory */ }

/class/csce /Code/TLPI - CSCE Slide - 33 System Calls  tar xvfz tlpi dist.tar.gz // the code from text  hermes> ls tlpi dist.tar.gz tlpi-dist  cd tl  Ls hermes> ls acl getopt pmsg svmsg … daemons Makefile.inc.MacOSX pshm tty dirs_links Makefile.inc.Solaris pty users_groups … files pgsjc sockets filesys pipes svipc

TLPI/dirs_links - CSCE Slide - 34 System Calls  cd dir_links  ls bad_symlink.c list_files_readdir_r.c t_dirbasename.c file_type_stats.c Makefile t_unlink.c list_files.c nftw_dir_tree.c view_symlink.c

more list_files_readdir_r.c - CSCE Slide - 35 System Calls

Example Scanning a directory CSCE 510 Jan 16, Slide - 36 System Calls

Stat structure CSCE 510 Jan 16, Slide - 37 System Calls struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };

st_mode macros CSCE 510 Jan 16, Slide - 38 System Calls S_ISREG(m) is it a regular file? S_ISDIR(m) directory? S_ISCHR(m) character device? S_ISBLK(m) block device? S_ISFIFO(m) FIFO (named pipe)? S_ISLNK(m) symbolic link? (Not in POSIX ) S_ISSOCK(m) socket? (Not in POSIX )

st_mode Flags CSCE 510 Jan 16, Slide - 39 System Calls  ls /usr/include/sys …  less /usr/include/sys/stat.h … pages of stuff #define S_IRUSR __S_IREAD /* Read by owner. */ #define S_IWUSR __S_IWRITE /* Write by owner. */ #define S_IXUSR __S_IEXEC /* Execute by owner. */ … #define S_IRGRP (S_IRUSR >> 3) /* Read by group. */