Operating System Architecture

An Introduction to UNIX/Linux

10 March 2020

Obed N Munoz

Cloud Software Engineer

A bit of UNIX/Linux History

2

The Linux Kernel

Do you pine for the nice days of Minix-1.1, when men were men
and wrote their own device drivers? Are you without a nice
project and just dying to cut your teeth on a OS you can try to
modify for your needs? Are you finding it frustrating when
everything works on Minix? No more all-nighters to get a nifty
program working? Then this post might be just for you. As I
mentioned a month ago, I’m working on a free version of a
Minix-look-alike for AT-386 computers. It has finally reached the
stage where it’s even usable (though may not be depending on
what you want), and I am willing to put out the sources for wider
distribution. It is just version 0.02 . . . but I’ve successfully run
bash, gcc, gnu-make, gnu-sed, compress, etc. under it

Linus Torvals - 1991

3

File System (Devices)

A device special file corresponts to a device on the system.

A device driver is a unit of kernel code that implements a set of operations that correspond to input and output actions on an associated device.

Devices can be divided in 2 types:

4

File System (disks and partitions)

5

File System (structure)

A file system is an organized collection of regular files and directories. Some examples of filesystem are:

6

File System (ext2 structure)

7

File System (File descriptors)

When openning files for write, you'll create or discard previous contents. The system requires to do some validations to verify that you have the proper rigthts to do so.

A File descriptor is a non-negative integer that is used as a reference to the file you're working with.

All information about the file is maintained by the operating system. User will only access it through the file descriptor.

8

Input / Output

All system calls for performing I/O refer to open files using a file descriptor, a (usually
small) nonnegative integer.

The following are the key required system calls for I/O operations.

fd = open(name, flags, mode)
numread = read(fd, buffer, count)
numwritten = write(fd, buffer, count)
status = close(fd)

Take a look on fileio/copy.c from The Linux Programming Interface.

9

Input / Output (Universality)

Same open(), read(), write() and close() are used to perform I/O in all types of files, including devices.

$ ./copy test test.old         # Copy a regular file
$ ./copy a.txt /dev/tty        # Copy a regular file to this terminal
$ ./copy /dev/tty b.txt        # Copy input from this terminal to a regular file
$ ./copy /dev/pts/16 /dev/tty  # Copy input from another terminal
10

System Calls

The UNIX-based operating systems provide their services through a set of system calls,
which will be function within the Operating System where the programs are running.

11

I/O System Calls - open

12

I/O System Calls - creat, read and write (1/2)

13

I/O System Calls - creat, read and write (2/2)

14

I/O System Calls - Example

int main(int argc, char *argv[])
{
    int f1, f2, n;
    char buf[BUFSIZ];

    if (argc != 3)
    error("usage: cp from to");
    if ((f1 = open(argv[1], O_RDONLY, 0)) == -1)
    error("cp: can't open %s", argv[1]);
    if ((f2 = creat(argv[2], PERMS)) == -1)
    error("cp: can't create %s, mode %03o", argv[2], PERMS);

    while ((n = read(f1, buf, BUFSIZ)) > 0)
    if (write(f2, buf, n) != n)
        error("cp: write error on file %s", argv[2]);

    return 0;
}
15

I/O System Calls - close and unlink

There's a limit (about 1024) on the number of opened files in a program. In order to open more, a program should be able to re-use file descriptors.

breaks the connection between the file descriptor and the opened file.
exit function call or return in a program will close all files.

removes the file name from the filesystem.

16

I/O System Calls - lseek

Take a look on fileio/seek_io.c from The Linux Programming Interface.

17

Programs and Processes

A process is an instance pf an executing program.

A program is a file containing a range of information that describes how to contruct a process at run time. Information includes:

18

Typical Program's memory layout

19

Processes in Linux

20

Errors Handling

The manual page for each system call documents the possible return values of the call, showing which value(s) indicate an error.

fd = open(pathname, flags, mode); /* system call to open a file */
if (fd == -1) {
   /* Code to handle the error */
}
...
if (close(fd) == -1) {
   /* Code to handle the error */
}
21

Errors reporting functions

#include <stdio.h>
void perror(const char *msg);

fd = open(pathname, flags, mode);
if (fd == -1) {
    perror("open");
    exit(EXIT_FAILURE);
}
char *strerror(int errnum); // Returns pointer to error string corresponding to errnum
22

Signals

A signal is a notification to a process that an event has occurred. Signals are sometimes described as software interrupts.

Signals may occur when:

Take a look on signals/ouch.c and signals/intquit.c from The Linux Programming Interface.

23

Let's Code: mycat

24

Resources and Credits

This material is genereated thanks to some extracts from following resources:

25

Thank you

Obed N Munoz

Cloud Software Engineer

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)