Download presentation
Presentation is loading. Please wait.
1
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling system calls – File descriptor – open() and close() Lecture 09CS 311 - Operating Systems I1
2
Things you need to be prepared with! Throughout the rest of the course – We will program in C (make sure you are thorough with pointers, function calls, structures and other important C elements). – We will use data structures (refresh the concepts of important data structures). Not pretty sure whether system calls works in Windows platform (surely works in Linux and Mac). Lecture 09CS 311 - Operating Systems I2
3
Systems Programming System call – Collection of routines (library) – Mechanism to communicate with an OS to make use of its services. – Programmer’s functional interface to the Linux kernel. Any subroutine used from the system call library communicates directly to the heart of the kernel. Lecture 09CS 311 - Operating Systems I3
4
System call Categories Error management – handle errors in OS File management – deal with files and directories in the file system – Inter-process Communication – deal with communication between processes via pipes and sockets Process management – deal with processes and their states. Lecture 09CS 311 - Operating Systems I4
5
Error management hierarchy perror() – only system call for handling errors. Lecture 09CS 311 - Operating Systems I5 Error handling perror
6
File management hierarchy Lecture 09CS 311 - Operating Systems I6 Files open close read write lseek unlink chown dup2 special Directory chmod fcntl fstat ftruncate truncate stat sync dup link mknod ioctl pipe Sockets getdents Internet sockets accept bind connect listen socket gethostbyname gethostname htonl htons inet_addr inet_ntoa
7
Process Management hierarchy Lecture 09CS 311 - Operating Systems I7 Process management nice chdir wait fork exec exit Signals setgid setpgrp getpgrp getppid setuid getgid getrgid getuid getruid alarm signal kill pause
8
Error Management By convention, all system calls return -1 if failed. Such return value does not provide any information on the type of error. Global variable (integer) called “errno” is used – To get information about the type of “last” error occurred. – Every process contains this global variable. – errno is initialized to zero at the start of each process. – errno is set to the associated error code when a failure occurs. – Include to access this value from your program. perror() system call converts current “errno” to a text descriptor. Lecture 09CS 311 - Operating Systems I8
9
Error Management Prototype of perror(): void perror(char *str); – Prints the user message in ‘str’ followed by a brief system generated error message. – If no error occurred perror(), prints “Error 0”. Predefined error macros EPERM – 1 – Operation not permitted ENOENT – 2 – No such file or directory ESRCH – 3 – No such process EINTR – 4 – Interrupted system call EIO – 5 – I/O error Lecture 09CS 311 - Operating Systems I9
10
File Management Everything in UNIX/Linux is represented as files. File management system call allows you to manipulate the full collection of regular directory and special files including – Disk based files – Terminals – Printers – Interprocess communication facilities such as pipes and sockets Lecture 09CS 311 - Operating Systems I10
11
File Descriptor (fd) A small integer that acts as a pointer to a file. Properties in file descriptor – File pointer that points to the beginning of a file when it is created. – Flag indicating whether to close the fd when “exec” is called. – Flag indicating whether the contents written to the file should be appended at the end. – Flag indicating whether the process should block on input from file if the file is empty. (in case of pipes and socket) – Process ID. Any number of fds can point to a single file. Lecture 09CS 311 - Operating Systems I11
12
open() system call Fds are created using open() system call Prototype for open() int open (char *filename, int mode [,int permissions]); Filename – absolute or relative path to the file Mode – bitwise OR operation of different modes Permission – flags set for file permissions (octal representation). Applicable only when you create a file. Returns a non-negative fds when success, -1 when fails. Some of in-built file modes (include ‘fcntl.h’ to use these modes) – O_RDONLY - Open for read-only. – O_WRONLY - Open for write-only. – O_RDWR - Open for read and write. Lecture 09CS 311 - Operating Systems I12
13
File modes and close() Some more built-in modes – O_APPEND – Append contents to file – O_TRUNC - If the file exists, it is truncated to length zero – O_CREAT - If the file doesn't exist, create the file, and set the owner ID to the process's effective user ID. – O_EXCL - If O_CREAT is set and the file exists, then open () fails – O_NONBLOCK/O_NDELAY - only for named pipes. Default fd values – 0 – standard input, 1 – standard output, 2 – standard error Close a file using close() system call Prototype of close():int close (int fd); (returns 0 on success, -1 on failure) Lecture 09CS 311 - Operating Systems I13
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.