Documente Academic
Documente Profesional
Documente Cultură
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
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
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
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())
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