Sunteți pe pagina 1din 5

Operating System Services

„ Loading and Running program?


■ Process Execution, I/O Operation
„ How disks are used? Opening, storing files?
■ File System Manipulation
OS: System calls „ How CPU/memory/IO device is used?
■ Resource Management
„ How programs talk with other programs on the
Based on slides © OS Concepts by Silberschatz, Galvin and Gagne, 2008 same or other computers?
Additional material by Diana Palsetia
■ Communication
„ Who logs in ? has access? What went wrong?
■ Security, protection and error detection
„ How does the user interact ?
CIT 595
■ User Interface 2

A view of OS Services System Call


„ Programming interface to the services provided by
the OS
„ Typically written in a high-level language
„ Mostly accessed by programs via a high-level
Application Program Interface (API) rather than direct
system call use
„ Three most common APIs are
■ Win32 API for Windows,
■ POSIX API for POSIX-based systems
y (including
( g virtually
y all
versions of UNIX, Linux, and Mac OS X),
■ Java API for the Java virtual machine (JVM)
„ Why use APIs rather than system calls?

CIT 595 3 CIT 595 4

1
API over System Call API over System Call
„ Are system dependent
■ Not be a good idea to directly use system calls „ The caller know nothing about how the
when portability cannot be neglected system call is implemented
„ S t
System calls
ll are also
l quite
it complex
l ■ JJustt needs
d to
t obey
b API and d understand
d t d
■ Involves the duo of TRAP and RET (or some what OS will do as a result call
variations of those two)
■ Most details of OS interface hidden from
„ To implement system call programmer by API
■ One would need specialized knowledge of I/O
registers ¾Managed by run-time support library (set of
■ O d off operations
Order i needed
d d to use them
h functions built into libraries included with
■ Implement enough protection because I/O compiler)
resources are generally shared among multiple
users and/or processes

CIT 595 5 CIT 595 6

Abstraction and Portability Unix System Calls for File I/O


If you want to call a C function to draw a pixel, „ There are 5 basic system calls that Unix
you … provides for file I/O:
■ Call a library function written in assembly
assembly, which
¾ Moves args from stack to regs and …
■ Calls a TRAP function written in assembly, which 1. int open(char *path, int flags , mode_t mode);
¾ Moves args from regs to stack and …
2. int close(int fd);
■ Calls an OS function written in C, which …
¾ Finally draws the pixel and … 3. ssize_t read(int fd, void *buf, int size);
■ Returns
R t tto the
th TRAP and d… 4 ssize
4. ssize_tt write(int fd
fd, void *buf
buf, int size);
■ Returns to the library function and … 5. off_t lseek(int fd, off_t offset, int whence);
■ Returns to you

CIT 595 7 CIT 595 8

2
File Descriptor int open(char *path, int flags , mode_t mode)
„ An index for an entry in a os/kernel data structure (file „ Open makes a request to the operating system to use
descriptor table) containing the details of all open a file
files
„ path argument specifies what file you would like to use
„ User application passes the abstract key to the
os/kernel through a system call „ flags and
fl d mode
d arguments
t specify
if how
h you would
ld lik
like
to use it
„ OS will access the file on behalf of the application, ■ E.g. flags: O_APPEND, O_CREAT, O_WRONLY(defined in
based on the key fcntl.h )
■ E.g. modes: S_IRWXU, S_IRUSR (defined in stats.h)
■ The application itself cannot read or write the file descriptor
table directly ¾ Use mode when creating file for first time

„ On success, it will return a file descriptor to you


„ Th
Three file
fil ddescriptors
i t predefined:
d fi d ■ This is a non-negative integer
■ File descriptor 0 is standard input ■ If it returns -1, then you have been denied access
■ File descriptor 1 is standard output ■ You can open the same file more than once
■ File descriptor 2 is standard error ¾ a different fd each time. If you open the same file for writing more
than once at a time, you may get bizarre results

CIT 595 9 CIT 595 10

int close(int fd) ssize_t read(int fd, void *buf, int size);

„ close() tells the operating system that you „ read size bytes from the file opened in file
are done with a file descriptor descriptor fd, and to put those bytes into the
location pointed to by buf
„ There is a limit on how many files a program
can open „ Returns how many bytes were actually read
■ because it takes resources to store all information
needed to correctly handle an opened file
„ write syscall writes data and returns the
number
b b bytes
t written
itt
„ So, close all files you don't currently need to
save some resources

CIT 595 11 CIT 595 12

3
Mechanics to open a file: Streams vs. File
off_t lseek(int fd, off_t offset, int whence)
Descriptors
„ File descriptors are represented as „ Streams are represented as
„ All open files have a file pointer associated objects of type int FILE * objects
with them
„ Provide a primitive, low-level „ Streams provide a higher-level
p and output
interface to input p interface, layered on top of the
„ Can move the file pointer manually operations primitive file descriptor facilities

■ How much to move is provided by offset „ Interface provides only simple „ Implemented in the C run-time
■ The whence variable specifies how the seek is to functions for transferring blocks of library
characters ■ API is provided in <stdio.h>
be done
¾ from the beginning of the file „ Can extract the file descriptor from a „ Provides powerful formatted
¾ from the current value of the pointer stream and perform low-level input and output functions (printf
operations directly on the file and scanf) as well as functions
¾ from the end of the file for character-
character and line-oriented
line oriented
descriptor
input and output
„ Upon successful completion, returns the „ Can initially open a connection as a
file descriptor and then make a
resulting offset location as measured in stream associated with that file
bytes from the beginning of the file descriptor (using fdopen())

CIT 595 13 CIT 595 14

Standard C Library Example Usage of streams and file descriptor


„ Can also represent a connection to a device or a pipe
or socket for communicating with another process
■ Operations that are specific to a particular kind of device -
use a file descriptor

„ Use streams rather than file descriptors


■ Unless there is some specific operation you want to perform
that can only be done on a file descriptor

„ Portability of your programs to systems other than


GNU systems
■ Be aware that file descriptors are not as portable as streams
■ You can expect any system running ISO C to support
streams
■ But non-GNU systems may not support file descriptors at all,
or may only implement a subset of the GNU functions that
operate on file descriptors

CIT 595 15 CIT 595 16

4
perror How do I know what .h files to include ?
„ Usually when an error occurs in a system or „ Man pages are you best friend
library call, a special return value comes „ man –s# functionname
back, and a global variable "errno" is set to ■ 2: System calls
say what the error is
■ 3: Library calls

„ Search functions but don’t know there name:


„ For example, suppose you try to open a file
■ Keyword search:
that does not exist:
man -k regexp
fd = open("in1", O_RDONLY);
if (fd < 0) { ■ Examples:
¾ man -k “memory”
perror(“open syscall failed because");
¾ man -k “locate.*string”
exit(1);
¾ Unix Specification:
} http://www.opengroup.org/onlinepubs/009695399/
CIT 595 17 CIT 595 18

S-ar putea să vă placă și