Slide 1This lectureSystem Calls versus Function Calls?System Calls versus Function CallsSlide 5System CallsSteps for Making a System Call (Example: read call)Examples of System CallsSystem calls vs. libcFile System and I/O Related System CallsWhy does the OS control I/O?System Calls for I/OOpenStandard Input, Output and ErrorExample 1Slide 16Closeread(…)Example 2write(…)Example 3lseeklseek exampleCopyright ©: Nahrstedt, Angrave, Abdelzaher 1Tarek AbdelzaherVikram AdveCS241 Systems ProgrammingSystem Calls and I/OCopyright ©: Nahrstedt, Angrave, Abdelzaher2This lectureGoals:Get you familiar with necessary basic system & I/O calls to do programmingThings covered in this lectureBasic file system callsI/O callsSignalsNote: we will come back later to discuss the above things at the concept levelCopyright ©: Nahrstedt, Angrave, Abdelzaher3System Calls versus Function Calls?Copyright ©: Nahrstedt, Angrave, Abdelzaher4System Calls versus Function CallsfnCall()ProcessCaller and callee are in the sameProcess - Same user - Same “domain of trust”Function CallCopyright ©: Nahrstedt, Angrave, Abdelzaher5System 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 abuseCopyright ©: Nahrstedt, Angrave, Abdelzaher6System CallsSystem CallsA request to the operating system to perform some activitySystem calls are expensiveThe system needs to perform many things before executing a system callThe 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 functionThe OS saves its state (and call results) The OS returns control of the CPU to the callerCopyright ©: Nahrstedt, Angrave, Abdelzaher7Steps for Making a System Call(Example: read call)Copyright ©: Nahrstedt, Angrave, Abdelzaher8Examples of System CallsExample:getuid() //get the user IDfork() //create a child processexec() //executing a programDon’t confuse system calls with libc callsDifferences?Is printf() a system call?Is rand() a system call?System calls vs. libcCopyright ©: Nahrstedt, Angrave, Abdelzaher9Use man –s 2Use man –s 3Each I/O system call has corresponding procedure calls from the standard I/O library.Copyright ©: Nahrstedt, Angrave, Abdelzaher10File System and I/O Related System CallsA file system: A hierarchical arrangement of directories. In Unix, the root file system starts with "/“Copyright ©: 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 programs that may run at the same time or later.FairnessMake sure other programs have a fair use of deviceCopyright ©: Nahrstedt, Angrave, Abdelzaher12System Calls for I/OThere are 5 basic system calls that Unix provides for file I/Oint 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); Remember: these are different from regular procedure callsSome library calls themselves make a system call(e.g. fopen() calls open())Copyright ©: Nahrstedt, Angrave, Abdelzaher13Openint 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 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; check the value of global variable "errno" to determine why (or use perror() to print corresponding error message).Copyright ©: Nahrstedt, Angrave, Abdelzaher14Standard Input, Output and ErrorNow, every process in Unix starts out with three file descriptors predefined: File descriptor 0 is standard input. File descriptor 1 is standard output. File descriptor 2 is standard error. You can read from standard input, using read(0, ...), and write to standard output using write(1, ...) or using two library callsprintfscanfCopyright ©: Nahrstedt, Angrave, Abdelzaher15Example 1#include <fcntl.h>#include <errno.h>main(int argc, char** argv) { int fd; fd = open("foo.txt", O_RDONLY); printf("%d\n", fd); if (fd=-1) { fprintf (stderr, "Error Number %d\n", errno); perror("Program"); }}Copyright ©: Nahrstedt, Angrave, Abdelzaher16Example 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, Abdelzaher17Closeint 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(”foo.txt"); exit(1); } if (close(fd1) < 0) { perror(”foo.txt"); exit(1); }printf("closed the fd's\n");After close, can you still use thefile descriptor?Why do we need to close a file?Copyright ©: Nahrstedt, Angrave, Abdelzaher18read(…)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 how 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 bytesThings to be careful aboutbuf must point to valid memory not smaller than the specified size Otherwise, what could happen?fd should be a valid file descriptor returned from open() to perform read operationOtherwise, what could happen?Copyright ©: Nahrstedt, Angrave, Abdelzaher19Example 2#include <fcntl.h> main(int argc, char** argv) { char *c; int fd, sz; c = (char *) malloc(100 * sizeof(char)); fd = open(“foo.txt", O_RDONLY); if
View Full Document