DOC PREVIEW
U of I CS 241 - System Calls and I/O

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS241 Systems Programming System Calls and I/OCS241 AdministrativeThis lectureMore C review URLsFile system from a user’s point of viewSlide 6File name & Path nameSystem CallsExamples of System CallsSystem Calls for I/OSteps for Making a System Call (review)Why does the OS control I/O?OpenCloseread(…)Examplewrite(…)Slide 18lseeklseek exampleStandard Input, Output and ErrorI/O Library CallsFiles in Cfopen() and fclose()File modes for fopen()fprintf() and fscanf()getc() and putc()Moving around in filesExample: file accessSignalsSummaryCS 241 Spring 2007System Programming 01/14/19 CS241 © 2006,2007 LA, RHC and YZ, All Rights Reserved1CS241 Systems ProgrammingSystem Calls and I/OLawrence AngraveLecture 42CS241 AdministrativeThis weekSMP0 due on Monday 9am morningDiscussion section Anyone cannot find a discussion section that fit his/her schedule? Anyone needs to change discussion section?Quizzes Self-assessment quiz on campus Pop quiz in lectureLecture & Book contentTypically selected from self-assessment quizWednesday or FridayMP quiz in lecture Test programming knowledge associated with current lecture topics & MPMonday after MP submission3This lectureGoals:Get you familiar with necessary basic system & I/O calls to do programmingGet basic things out of your way before moving to more complex thingsEnable you to work on your MP and any future MPsThings covered in this lectureBasic file systemI/O callsSignalsNote: we will come back later to discuss the above things at the concept level4More C review URLsC Pointers GuideC Tutorial - Lesson 8: An Introduction To Pointers - Dereferencing ...C/C++ Tutorial: An Introduction To PointersA Tutorial on Pointers and Arrays in CC GuideThe GNU C Programming Tutorial Programming in C (new, by Marshall) Programming in C: A Tutorial (old, by Kernighan) "The C programming language" book website5File system from a user’s point of viewA file system: A hierarchical arrangement of directories. In Unix, the root file system starts with "/“6File system from a user’s point of viewA file system: A hierarchical arrangement of directories. In Unix, the root file system starts with "/“To see the filesystems on your machine, type "df". yyzhou|carmen|~|[1]% dfFilesystem 1k-blocks Used Available Use% Mounted on/dev/sda1 5162796 3967004 933536 81% //dev/sda5 63322504 31773716 28332176 53% /mounts/carmen/disks/0/dev/sdb1 70557052 44202760 22770196 67% /mounts/carmen/disks/1/dev/sdc1 70557052 45750108 21222848 69% /mounts/carmen/disks/2/dev/sdd1 70557052 25866092 41106864 39% /mounts/carmen/disks/3none 1034536 305740 728796 30% /dev/shm/dev/sda3 1035692 489184 493896 50% /var7File name & Path nameFilename: The name of a file as it appears in a directory. Pathname: A sequence of zero or more filenames, separated by slashes. List all filenames in the current directory: ls –aChange directory: cd dirChange to home directory: cd Copy directory: cp -r dir1 dir2Create a directory: mkdir dirPrint the currrent directory path: pwd8System CallsSystem CallsA request to the operating system to perform some activityLooks like a procedure call, but it's differentSystem calls are expensive----Why?The system needs to perform many things before executing a system callThe computer (hardware) save its state (Why?)Let the operating system take control of the CPU (why?)The OS examines the parameters you passed (why?)Have the operating system perform some functionHave the operating system save its stateHave the operating system give control of the CPU back to you9Examples of System CallsCan anyone give me an example?getuid() //get the user IDexecv() //executing a programtimes() // get the execution timeDon’t mix system calls with standard library callsDifferences?Is printf( ) a system call?Is rand() a system call?10System Calls for I/OThere are 5 basic system calls that Unix provides for file I/O (check man –s 2 open)int open(char *path, int flags [ , int mode ] ); int close(int fd); int read(int fd, char *buf, int size); int write(int fd, char *buf, int size); off_t lseek(int fd, off_t offset, int whence); They look like regular procedure calls. They are differentA system call makes a request to the operating system. A procedure call just jumps to a procedure defined elsewhere in your program. Some library calls may themselves make a system callE.g. fopen() calls open())11Steps for Making a System Call (review)12Why does the OS control I/O?Safety The computer must ensure that if my program has a bug in it, then it doesn't crash or mess up the system, other people's programs that may be running at the same time or later.FairnessMake sure other programs have a fair use of deviceFor experienced studentsIn what types of systems, the OS does NOT control I/O?13Openint open(char *path, int flags [ , int mode ] ) makes a request to the operating system to use a file. The 'path' argument specifies what file you would like to useThe 'flags' and 'mode' arguments specify how you would like to use it. If the operating system approves your request, it will return a ``file descriptor'' to you. This is a non-negative integer. Any future accesses to this file needs to provide this file descriptorIf it returns -1, then you have been denied access, and check the value of the variable "errno" to determine why (use perror()). #include <fcntl.h>main(){ int fd; fd = open(“foo.txt", O_RDONLY); printf("%d\n", fd);}What could be the output?Why specify mode?What did I forget?14Closeint close(int fd) tells the operating system that you are done with a file descriptor. #include <fcntl.h>main(){ int fd1, fd2; if(( fd1 = open(“foo.txt", O_RDONLY)) < 0){ perror("c1"); exit(1); } if (close(fd1) < 0) { perror("c1"); exit(1); }printf("closed the fd's\n");After close, can you still use thefile descriptor?Why do we need to close a file?15read(…)int read(int fd, char *buf, int size) tells the operating system To read "size" bytes from the file specified by "fd“ into the memory location pointed to by "buf". It returns many bytes were actually read (why?)0 ---- end of the file < size ---- fewer bytes are read to the buffer (why?)== size ---- read the specified # of bytesNEVER > size (why?)Things to be carefulbuf needs to point to a valid memory location with length


View Full Document

U of I CS 241 - System Calls and I/O

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download System Calls and I/O
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view System Calls and I/O and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view System Calls and I/O 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?