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

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 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 12 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 12 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 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Copyright ©: Nahrstedt, Angrave, Abdelzaher 1Tarek AbdelzaherCS241 Systems ProgrammingSystem Calls and I/OCopyright ©: Nahrstedt, Angrave, Abdelzaher2Announcements Lecture slides posted (typos corrected) SMP0 due on Wednesday 10pm. Please start early. Pop quiz (end of class) Discussion sections  Cannot find a discussion section that fits your schedule? Please send e-mail to [email protected] ©: Nahrstedt, Angrave, Abdelzaher3This lecture Goals: Get you familiar with necessary basic system & I/O calls to do programming Things covered in this lecture Basic file system calls I/O calls Signals Note: we will come back later to discuss the above things at the concept levelCopyright ©: Nahrstedt, Angrave, Abdelzaher4System Calls versus Function Calls?3Copyright ©: Nahrstedt, Angrave, Abdelzaher5System Calls versus Function CallsfnCall()ProcessCaller and callee are in the sameProcess- Same user- Same “domain of trust”Function CallCopyright ©: Nahrstedt, Angrave, Abdelzaher6System Calls versus Function CallsfnCall()ProcessCaller and callee are in the sameProcess- Same user- Same “domain of trust”Function CallsysCall()ProcessSystem CallOS- OS is trusted; user is not.- OS has super-privileges; user does not- Must take measures to prevent abuse4Copyright ©: Nahrstedt, Angrave, Abdelzaher7System Calls System Calls A request to the operating system to perform some activity System calls are expensive The system needs to perform many things before executing a system call The computer (hardware) saves its state  The OS code takes control of the CPU, privileges are updated.  The OS examines the call parameters  The OS performs the requested function The OS saves its state (and call results)  The OS returns control of the CPU to the caller Copyright ©: Nahrstedt, Angrave, Abdelzaher8Steps for Making a System Call(Example: read call)5Copyright ©: Nahrstedt, Angrave, Abdelzaher9Examples of System Calls Example: getuid() //get the user ID fork() //create a child process exec() //executing a program Don’t mix system calls with standard library calls Differences? Is printf() a system call? Is rand() a system call?Copyright ©: Nahrstedt, Angrave, Abdelzaher10File System and I/O Related System Calls A file system:A hierarchical arrangement of directories. In Unix, the root file system starts with "/“6Copyright ©: Nahrstedt, Angrave, Abdelzaher11Why 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. Fairness Make sure other programs have a fair use of deviceCopyright ©: Nahrstedt, Angrave, Abdelzaher12System Calls for I/O There are 5 basic system calls that Unix provides for file I/O int open(char *path, int flags [ , int mode ] ); (check man –s 2 open) 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 but are different A 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 call (e.g. fopen() calls open())7Copyright ©: Nahrstedt, Angrave, Abdelzaher13Open int open(char *path, int flags [ , int mode ] )makes a request to the operating system to use a file.  The 'path' argument specifies the file you would like to use The 'flags' and 'mode' arguments specify how you would like to use it.  If the operating system approves your request, it will return a file descriptorto you. This is a non-negative integer. Any future accesses to this file needs to provide this file descriptor If it returns -1, then you have been denied access, and check the value of the variable "errno" to determine why (use perror()). Copyright ©: Nahrstedt, Angrave, Abdelzaher14Example 1#include <fcntl.h>#include <errno.h>extern int errno;main() {int fd;fd = open("foo.txt", O_RDONLY);printf("%d\n", fd);if (fd=-1) { printf ("Error Number %d\n", errno);perror("Program");}}8Copyright ©: Nahrstedt, Angrave, Abdelzaher15Example 1#include <fcntl.h>#include <errno.h>extern int errno;main() {int fd;fd = open("foo.txt", O_RDONLY);printf("%d\n", fd);if (fd==-1) { printf ("Error Number %d\n", errno);perror("Program");}}How to modify the example toprint the program name before the error message?Copyright ©: Nahrstedt, Angrave, Abdelzaher16Close int close(int fd)Tells the operating system 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?9Copyright ©: Nahrstedt, Angrave, Abdelzaher17read(…) 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 : at end of the file < size : fewer bytes are read to the buffer (why?) == size : read the specified # of bytes Things to be careful about buf needs to point to a valid memory location with length not smaller than the specified size  Otherwise, what could happen? fd should be a valid file descriptor returned from open() to perform read operation Otherwise, what could happen?Copyright ©: Nahrstedt, Angrave, Abdelzaher18Example 2#include <fcntl.h> main() { char *c;int fd, sz;c = (char *) malloc(100 * sizeof(char));fd = open(“foo.txt", O_RDONLY);if (fd < 0) { perror("r1"); exit(1); }sz = read(fd, c, 10);printf("called read(%d, c, 10). returned that %d bytes were read.\n",fd, sz);c[sz] = '\0';printf("Those bytes are as follows: %s\n", c);close(fd);}10Copyright ©: Nahrstedt, Angrave, Abdelzaher19write(…) int write(int fd, char *buf, int size)writes the bytes stored in buf to the file specified by fd It returns the number of bytes actually written, which is usually “size” unless there is an error Things to be careful about buf needs to be at least as long as specified by “size” The file needs to be opened for write


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?