operating systems recitation 1, march 10-11 th, 2002

28
Operating Systems Recitation 1, March 10- 11 th , 2002.

Upload: elmer-mcdowell

Post on 02-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Operating Systems Recitation 1, March 10-11 th, 2002

Operating Systems

Recitation 1, March 10-11th, 2002.

Page 2: Operating Systems Recitation 1, March 10-11 th, 2002

Course staff

:מרצים 5285 ,[email protected], סיון טולדו 7439 ,[email protected], ענת ברמלר

:מתרגל 5396 ,[email protected], עדו דרורי

:בודקים mailbox 281 ,[email protected], ניר נוימרק

mailbox 380 ,[email protected], אלעד סרבר

Page 4: Operating Systems Recitation 1, March 10-11 th, 2002

Course structure

• Three elements– Lectures– Recitations– Programming exercises

Page 5: Operating Systems Recitation 1, March 10-11 th, 2002

Course structure• Lectures

Sunday, 16:00-19:00, Orenstein 103.Monday, 16:00-19:00, Dan David 001.

• RecitationsGroup 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.

• Webpagehttp://www.math.tau.ac.il/~stoledo/os/

Page 6: Operating Systems Recitation 1, March 10-11 th, 2002

Course requirementweekly 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.

Page 7: Operating Systems Recitation 1, March 10-11 th, 2002

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.

Page 8: Operating Systems Recitation 1, March 10-11 th, 2002

Introduction

Page 9: Operating Systems Recitation 1, March 10-11 th, 2002

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.

Page 10: Operating Systems Recitation 1, March 10-11 th, 2002

System calls and library functions

application code

C library functions

system calls

kernel

user process

Page 11: Operating Systems Recitation 1, March 10-11 th, 2002

System calls and library functions

application code

memory allocationfunction malloc

sbrk system call

kernel

user process

Page 12: Operating Systems Recitation 1, March 10-11 th, 2002

File I/O

technical details

Page 13: Operating Systems Recitation 1, March 10-11 th, 2002

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.

Page 14: Operating Systems Recitation 1, March 10-11 th, 2002

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 descriptor0 standard input, 1 standard output, 2 standard error

Page 15: Operating Systems Recitation 1, March 10-11 th, 2002

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 writingOptional constants include:O_APPEND append to the end of file on each writeO_CREAT create the file if it doesn’t existO_EXCL gives an error if O_CREAT is specified and the file exists

Page 16: Operating Systems Recitation 1, March 10-11 th, 2002

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.

Page 17: Operating Systems Recitation 1, March 10-11 th, 2002

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.

Page 18: Operating Systems Recitation 1, March 10-11 th, 2002

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.

Page 19: Operating Systems Recitation 1, March 10-11 th, 2002

lseek

• Every open file has an associated offset, a non-negative 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.

Page 20: Operating Systems Recitation 1, March 10-11 th, 2002

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 offset bytes from beginning of file

– SEEK_CUR current value + offset

– SEEK_END size of file + offset

Page 21: Operating Systems Recitation 1, March 10-11 th, 2002

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.

Page 22: Operating Systems Recitation 1, March 10-11 th, 2002

process table entry

fd 0:fd 1:fd 2:fd 3:

flags ptr

...

process table entry

fd 0:fd 1:fd 2:fd 3:fd 4:

flags ptr

...

file status flagcurrent file offsetv-node ptr

file table

file status flagcurrent file offsetv-node ptr

v-node table

information

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.

each process has itsown current offset for

the file

Page 23: Operating Systems Recitation 1, March 10-11 th, 2002

• 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.

Page 24: Operating Systems Recitation 1, March 10-11 th, 2002

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).

Page 25: Operating Systems Recitation 1, March 10-11 th, 2002

Exercise 1 - notes

• In order to turn the string that contains the buffer size into an integer, you can callsscanf(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);

Page 26: Operating Systems Recitation 1, March 10-11 th, 2002

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.

Page 27: Operating Systems Recitation 1, March 10-11 th, 2002

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.

Page 28: Operating Systems Recitation 1, March 10-11 th, 2002

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.cpermissions: chmod ugo+rx (to above)

• Hardcopy – submit ONLY what is required.name, ID, login, CID (upon return of 1st exercise)ex-rw.canswers to timing questions.submit in mailbox 281, Nir Neumark, Schreiber

Bldg.• Reference: chapter 3, pages 47-60, in Stevens book.