* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Operating Systems
Survey
Document related concepts
Process management (computing) wikipedia , lookup
Library (computing) wikipedia , lookup
MTS system architecture wikipedia , lookup
Plan 9 from Bell Labs wikipedia , lookup
Windows NT startup process wikipedia , lookup
Commodore DOS wikipedia , lookup
Spring (operating system) wikipedia , lookup
Burroughs MCP wikipedia , lookup
Computer file wikipedia , lookup
Transcript
Operating Systems Recitation 1, March 10-11th, 2002. Course staff :מרצים 5285 ,[email protected], סיון טולדו 7439 ,[email protected], ענת ברמלר :מתרגל 5396 ,[email protected], עדו דרורי :בודקים mailbox 281 ,[email protected], ניר נוימרק mailbox 380 ,[email protected], אלעד סרבר References • Operating Systems Sivan Toledo (Akademon, 2001) • Operating System Concepts Abraham Silberschatz, Baer Peter Galvin, Greg Gagne (John Wiley & Sons, 2001) • Advanced Programming in the UNIX Environment W. Richard Stevens (Addison Wesley, 1992) • Understanding the Linux Kernel Daniel P. Bovet, Marco Cesati (O'Reilly, 2000) Course structure • Three elements – Lectures – Recitations – Programming exercises Course structure • Lectures Sunday, 16:00-19:00, Orenstein 103. Monday, 16:00-19:00, Dan David 001. • Recitations Group 11: Sunday, 19:00-20:00, Shenkar 222. Group 10: Monday, 12:00-13:00, Kaplun 118. Group 07: Monday, 15:00-16:00, Kaplun 118. Group 08: Monday, 19:00-20:00, Shreiber 06. Reception: Sunday, 11:00-12:00, Schreiber 20M. • Webpage http://www.math.tau.ac.il/~stoledo/os/ Course requirement weekly C programming exercises • Linux www.linux.org free Unix-type operating system several distributions (www.redhat.com) • Final exam includes 1-2 questions regarding the programming exercises. • Programming exercises are mandatory and grant 5-10 bonus points in final grade. Exercise submission guidelines • Software – Directories: under your home directory create a subdirectory called os02b. There, for each exercise create a subdirectory, with the same name as the exercise program file. There, submit the exercise. – Files: submit all .c files, .h files if any, makefile ONLY – Permissions: read and execution permissions to user, group and others, for both directories and files after exercise submission: chmod ugo+rx • Hardcopy – name, ID, login, CID (returned with 1st graded exercise) – .c files – Answers to additional questions. Introduction Programs and processes • A program is an executable file residing on disk. It is read into memory and executed by the kernel as a result of an exec function. • An executing instance of a program is called a process. • Every Unix process has a unique identifier, a nonnegative integer. System calls and library functions application code user process C library functions system calls kernel System calls and library functions application code user process memory allocation function malloc sbrk system call kernel File I/O technical details File I/O • Most Unix file I/O can be performed using only five functions: open, read, write, lseek, close. • We will examine the effect of different buffer sizes on the read and write functions. File descriptors • To the kernel all open files are referred to by file descriptors – non negative integers. • When we open an existing file or create a new file, the kernel returns a file descriptor to the process. • When we want to read or write a file, we identify the file with the file descriptor that was returned by open. • Convention, Unix shells associate file descriptor 0 standard input, 1 standard output, 2 standard error open • A file is opened or created by calling the open function #include <sys/types.h> primitive system data types #include <sys/stat.h> file status #include <fcntl.h> file control … int open(const char *pathname, int flags, int mode) Returns a file descriptor if OK, -1 on error. • pathname is the name of the file to open or create. • The options for this function are specified by the flags argument, which is formed by OR’ing together constants, one of the following must be specified: O_RDONLY open for reading only, O_WRONLY open for writing only, O_RDWR open for reading and writing Optional constants include: O_APPEND append to the end of file on each write O_CREAT create the file if it doesn’t exist O_EXCL gives an error if O_CREAT is specified and the file exists close • An open file is closed by #include <unistd.h> symbolic constants … int close(int filedes) Returns 0 if OK, -1 on error • Closing a file also releases any record locks that a process may have on the file. • When a process terminates, all open files are automatically closed by the kernel. read • Data is read from an open file with the read function #include <unistd.h> … ssize_t read(int filedes, void* buff, size_t nbytes) Returns number of bytes read, 0 if end of file, -1 on error. The data that is read is stored in the buffer whose address is specified in buff and size indicated by nbytes. write • Data is written to an open file with the write function #include <unistd.h> … ssize_t write(int filedes, const void *buff, size_t nbytes) Returns the number of bytes written if OK, -1 on error. • The return value is usually equal to the nbytes argument, otherwise an error has occurred. • A common cause for a write error is either filling up a disk or exceeding the file size limit for a given process. • Write start at the file’s current offset. If the O_APPEND option was specified in the open, the file’s offset is set to the current end of file before each write operation. • After a successful write, the file’s offset is incremented by the number of bytes actually written. lseek • Every open file has an associated offset, a nonnegative integer that measures the number of bytes from the beginning of the file. • Read and write operations start at the current file offset and increment it by the number of bytes read or written. • By default, this offset is initialized to 0 when a file is opened, unless the O_APPEND option is specified. lseek • An open file can be explicitly positioned #include <sys/types.h> #include <unistd.h> … off_t lseek(int filedes, off_t offset, int whence) Returns new file offset if OK, -1 on error. • The interpretation of the offset depends on the value of the whence argument – SEEK_SET – SEEK_CUR – SEEK_END offset bytes from beginning of file current value + offset size of file + offset File sharing • • Unix supports sharing of open files between different processes. Three data structures are used by the kernel for I/O: 1. Every process has an entry in the process table. Within each process table entry is a table of open file descriptors, a vector, with one entry per descriptor. 2. The kernel maintains a file table for all open files. Each file table entry contains: file status flag (read, write, append), current file offset, pointer to v-node table entry. 3. Each open file has a v-node structure: file type, pointers to functions that operate on the file. File owner, size, device, position on disk. process table entry ptr flags fd 0: fd 1: fd 2: fd 3: file table file status flag current file offset v-node ptr v-node table information ... each process has its own current offset for the file process table entry ptr flags fd 0: fd 1: fd 2: fd 3: fd 4: ... file status flag current file offset v-node ptr current file size • Two independent processes have the same file open • The first process has the file open on descriptor 3, and the 2nd process has the same file open on descriptor 4. • Each process that opens the file gets its own file table entry, but only a single v-node table entry for a given file. • After each write the current file offset in the file table entry is incremented by the number of bytes written. • lseek only modifies the current file offset in the file table entry, no I/O takes place. File system calls – exercise 1 • Write and run a simple C program on Linux, that copies a file, using the basic file system calls. • The program should print an error message if there are not enough arguments (printf and exit), if the input file does not exist or if the output file already exists (perror). • The program should use the system calls described except for lseek. • Copying should be performed using a buffer size of n bytes. • The program should accept three arguments, the input filename, output filename, and n (which is the buffer size). Exercise 1 - notes • In order to turn the string that contains the buffer size into an integer, you can call sscanf(argv[3], “%d”, &n) in stdio.h • Use malloc to allocate storage for the buffer #include <stdlib.h> utility functions … char* buffer; ... buffer = (char*) malloc(n); Time values • Clock time – the amount of time a process takes to run. This depends on the number of other processes being run on the system. Whenever you report the clock time, the measurements should be made with no other activities on the system. • User CPU time – attributed to user instructions. • System CPU time – attributed to the kernel when it executes on behalf of a process. For example, whenever a process executes a system service, such as read or write, the time spent within the kernel performing that system service is charged to the process. • To measure times for any process, execute the time command with the argument to the time command being the command to measure. Exercise 1 - timing • Measure the running time of your program using the time command, when copying a file of size 1MB: www.cs.tau.ac.il/~stoledo/Public/os/onemb • Measure the running time for buffer sizes (in bytes): 1, 64, 512, 1024, 8192, 65536 • Is there a considerable difference between the running times with different buffer sizes? If so, explain why. Exercise 1 • Chapter 2.8, pages 41-44, in Toledo’s book. • Submission deadline: Friday, March 22nd. • Software directory: ~username/os02b/ex-rw/ files: ex-rw.c permissions: chmod ugo+rx (to above) • Hardcopy – submit ONLY what is required. name, ID, login, CID (upon return of 1st exercise) ex-rw.c answers to timing questions. submit in mailbox 281, Nir Neumark, Schreiber Bldg. • Reference: chapter 3, pages 47-60, in Stevens book.