0% found this document useful (0 votes)
3 views

Unix System Interface

The document provides an overview of the C programming language and its Unix system interface, covering topics such as file descriptors, low-level I/O operations, and file management functions like open, create, close, and unlink. It includes a quiz section to test knowledge on data structures and file handling. Additionally, it discusses random access in files using lseek() and provides an example implementation of fopen() and getc().

Uploaded by

maariaam.gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unix System Interface

The document provides an overview of the C programming language and its Unix system interface, covering topics such as file descriptors, low-level I/O operations, and file management functions like open, create, close, and unlink. It includes a quiz section to test knowledge on data structures and file handling. Additionally, it discusses random access in files using lseek() and provides an example implementation of fopen() and getc().

Uploaded by

maariaam.gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

The C Programming Language

Unix System Interface


Agenda


Quiz

File Descriptors

Low Level I/O

Open, Create, Close, Unlink

Random Access - lseek()

Example fopen() and getc() Implementation

Homework
Quiz

What linear data structures do you know ? What are their main
differences ? What operations are common for them ?

What hierarchical data structures do you know ? What
operations are common for them ? What advantages do they
have ?

What is the lookup table ? How it operates ? What is it's
advantage ?
File Descriptors

In the Unix system all input and output is done by reading or writing a
files, because all peripheral devices are files in the file system.

A file descriptor is an integer value which is returned by the Unix
system after successful file opening process. Whenever input or
output is to be done on the file, the file descriptor is used to identify a
file.

A file descriptor is analogous to the file pointer used by the standard
library.

File descriptor 0 is used for standard input (from keyboard)

File descriptor 1 is used for standard output (to screen)

File descriptor 2 is used for standard error output (to screen)

When “shell” runs a program three files are open with 0, 1, 2 file
descriptors described above.

The I/O can be redirected to and from file with < and > when
executing a program:
Low Level I/O

Input and output uses the read and write functions:


Each call return a count of the number of bytes transferred.

A return value of 0 bytes implies end of file.

-1 indicates some kind of error.

Any number of bytes can be read or written in one call.

Larger sizes will be more efficient because a fewer system calls will
be made.
Low Level I/O
Open, Create, Close, Unlink

Other than standard input, output and error, you must explicitly open
files in order to read or write them.


The open function is like fopen() library function, just instead of File
pointer it returns a file descriptor.

It returns -1 if an error occurres.

name – a character string containing filename.

flags – specifies how the file is to be opened. The main values are:


perms – specifies permissions of the file if O_CREAT is set as flag,
applications should use only above flags and set perms as 0.

The constants are defined in the <fcntl.h> header file.

The following example opens file for reading:
Open, Create, Close, Unlink

The system call creat is provided to create new files, or rewrite old
ones.


It returns -1 if an error occurres.

name – a character string containing filename.

perms – specifies permissions of the file.

There are nine bits of permission information associated with a file
that control read, write and execute access for the owner of the file,
for the owner’s group, and for all others.

The following example opens file for writing:


The function close breaks the connection between file descriptor and
and an open file, frees the file descriptor for use with some other file.


The function unlink removes the file name from the file system.
Random Access - lseek()

Input and output sequential: each read() or write() takes place at a
position in the file right after the previous one.

The system call lseek() provides a way to move around in a file
without reading or writing any data.


It return -1 if error occurs or the new position in the file.

fd – the file’s file descriptor where current position will be set

offset – location relative to origin

origin – can be 0, 1 or 2
– 0: Measure offset from the begging
– 1: Measure offset from the current position
– 2: Measure offset from the end


With lseek(), it is possible to treat files more or less like large arrays,
at a price of slower access.
Example fopen() and getc() Implementation
Example fopen() and getc() Implementation
Example fopen() and getc() Implementation
Example fopen() and getc() Implementation
Homework

Exercise 8-3
Thank You !

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy