Title: Operating Systems: Practice 1
1Operating Systems Practice 1
2Overview
- Administration
- Introduction
- File I/O
- Tips for Windows OS.
- Kernel and User modes
3Administration
- TA Alex Shulman
- E-mail shulmana_at_post.tau.ac.il
- Office Hours Thursday 1330 1430
- Location Shrieber 10
- Website
- http//www.cs.tau.ac.il/shulmana/
- http//www.cs.tau.ac.il/shulmana/courses/OS_Summe
r_04/
4Exercises
- Weekly exercises
- Programming language C
- Singleton submission
- Assignments are 20 of total grade
- Final exam usually contains 1-2 question
regarding the exercises. - http//www.cs.tau.ac.il/shulmana/courses/ OS_Su
mmer_04/Assignments.php
5Submission Guidelines
- Submit
- A printout of the exercise (to me or to box 251).
- An electronic version.
- Consult
- http//www.cs.tau.ac.il/shulmana/courses/ OS_S
ummer_04/subgd.php
6Electronic Submission
- Location
- Directory os04c under your home directory.
- Subdirectory assign/
- E.g. Exercise 1 /os04c/assign1/
- E.g. Exercise 2 /os04c/assign2/
- Submit all .c files, .h files if and makefile.
- Do not touch or change the files you submit after
the deadline due.
7Electronic Submission
- Permissionschmod 705 chmod 705 /software1
chmod 705 /software1/assign1 chmod 705
/software1/assign1/
8Electronic Submission
- File Headers
- / .....
- Full Name your-full-name
- Id No your-id
- User Name your-user-name
- Assignment No the-assignment-number
- ..... /
9References
- 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)
10Introduction
11Programs and processes
- A program is an executable file residing on disk.
- An executing instance of a program is called a
process. - Every process has a unique identifier, a
nonnegative integer.
12Operating System
Operating System - coordinates access to physical
resources CPU, disk, I/O devices etc.
Services Memory management Protection Scheduling
File System and more
13Operating Systems
- Unix/Linux
- Windows
- Real-Time Operating Systems (e.g VxWorks,
PSOS). - Mainframes (e.g. VM of IBM)
Practices Scope
14File I/O
15Disk Access
Application Code
User Process
ltstdiogt etc.
C Library Functions
File System
File management
Segments
Kernel
Storage
Disk block
Driver
Disk1
Disk2
Disk3
16System Calls
- System Call is a request to the operating
system to perform some activity. - System calls are expensive. A system call
requires the computer to - save its state
- the operating system take control of the CPU
- the operating system performs some function
- the operating system save its state
- the operating system give control of the CPU back
to you.
17System Calls
- File Manipulation
- Process Control
- Device Manipulation
- Information
Application Code
C Library Functions
User Process
System Calls
Kernel
18System Calls and Library Functions
Application Code
fopen,fclose, printf, fgetc, getchar,
User Process
C Library Functions
open, close, read, write, seek
File System
Kernel
. . .
19File descriptors
- Each file is referred to by file descriptors
non negative integers. - The kernel returns a file descriptor to the
process, upon open or create commands. - The file descriptor is used by other operation
that refer to the file (e.g. read, write)
20Open/Close System Calls
- Open system call - is used to convert a pathname
into a file descriptor. - int open(const char pathname, int flags, int
mode) - Close system call - closes a file descriptor,
so that it may be reused. Any locks held on
the file it was associated with, and owned by the
process, are removed. - int close(int filedes)
21Unix Help
- man ltcommandgt
- man 1 General System Info.
- gt man 1 grep
- man 2 System Calls
- gt man 1 open
- man 3 Linux Programmer's Manual
- gt man 3 printf
22xman
23Read System Call
- Read system call- attempts to read up to
count - bytes from file descriptor fd into the buffer
- starting at buf.
- int read(int fd, void buf, int count)
- Return Value
- On success, the number of bytes read is
returned. - The file position is advanced by this number.
- The returned number may be smaller than the
number of bytes requested .
24Write System Call
- Write system call-writes up to count bytes to
the file - referenced by the file descriptor fd from the
buffer - starting at buf.
- int write(int fd, void buf, int count)
- Return Value
- On success, the number of bytes written is
returned. The returned number may be smaller than
the number of bytes requested .
25Standard Input, Output and Error
Unix shells associate file descriptor0 standard
input, 1 standard output, 2 standard error
- include ltfcntl.hgt
- int main(void)
- char c
- while (read(0, c, 1) 1) write(1, c, 1)
return 0 -
26Lseek System Call
- Reposition the file offset.
- File 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.
27lseek
- 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
28Potential Problems
Process 1
Process 2
1. Seek 3. Read
2. Seek 4. Read
File
29File system calls exercise 1
- Implementing a Windows CopyFile procedure.
- The program should use the system calls described
except for lseek. - Copying should be performed using a buffer size
of n bytes.
30CopyFile Function
lpExistingFileName - the name of an existing
file. lpNewFileName - the name of the new file.
bFailIfExists - If TRUE and the new file
exists, the function fails. If FALSE and the new
file exists, the function overwrites the existing
file and succeeds.
31MSDN Help
http//msdn.microsoft.com/
32SDK
- Microsoft Windows Software Development Kit
(SDK). - An Application Programming Interface (API)
designed for anyone who wants to develop a
Windows Media. - Allows to programmatically manage a Windows Media
server to send digital media content to clients
on both unicast and multicast-enabled networks.
33Windows Tips
- include ltwindows.hgt
- include ltstdio.hgt
- int main(int argc, LPTSTR argv)
- .
34Windows Data Types
- LPVOID a pointer to void.
- LPTSTR - a 32-bit pointer to a character string.
- DWORD - a 32-bit unsigned integer
- CHAR - an 8-bit Windows (ANSI) character.
- All Win32 objects are identified by variables of
type - HANDLE, and a single generic CloseHandle
- function applies to most objects.
35Error Codes
- Windows System error codes, as DWORDs, can be
obtained at any point using GetLastError. - Unix Global variable errno provides information
about the Unix function call failure. - gt man errno
36Kernel Monitoring