OS course – lecture 1 Nezer J. Zaidenberg
Announcements Lecturer : Prof. Amir Averbuch Tuturing assistant :Mr. Nezer Zaidenberg Ex. Grader : TBD The course form has changed since previous
years. It is recommended that you will attend lectures even if you taken this course previously
Grades 40% exercises 70% exam The exam will include multiple choice
questions. Most of which will be based on the homework!
References The main books for this course is Understand the Linux kernel 3rd edition (For
kernel space)
Advanced programming for the UNIX
environment 3rd edition by Richard W. Stevens (For user space)
Additional books (that will be noted on
specific lectures)
UNIX filesystems Beej Manual for network programming
Course goals The goal of this course is to gain practical
understanding of the OS
The course is very practical with lots of
homework and exam based on the homework
The course will focus on UNIX OS and
particular Linux
The exercises will be mainly coding work or
reviewing kernel and low-level user code.
What this course is not about Coding (to the extent of coding standards,
best coding practices or efficiency)
MPP (beyond simple algorithms) Network algorithms Encryption Etc. This code is on OS and OS related
programming only
What is OS Once upon a time there was no Operating systems Developers used to develop hardware and software,
together with everything needed to make the application run
As computers become more standard and tasks
become common several tasks stood out as needed by all (or practically all) software.
Those common tasks became “OS” and were
supplied by 3rd party vendors
Commercial software used to require specific OS.
This became one of the first usages of “code reusability”
Common tasks Memory management Hardware (driver) support Task scheduling on multi process environments File management User management (on multi-user systems)
Software providers developed a single software for all those components (and more) – the OS This software became the base for all further software development
What the OS includes
Core OS function Process management User management File management Memory management Hardware management Network management
Additional functions are added and removed according to political and marketing scenarios Initial UNIX implementation had built in C compiler (sold separately with almost all modern OS) Most UNIX OS comes with full range of servers not really part of “OS” such as ftp server, telnet server, daytime server, printing server and even HTTP server, J2EE platform, etc. Many OS also come with GUI (windows, OSX aqua, X Windows) and other user tools (calculator, Text processor etc.) Microsoft wished to add Internet Explorer, Windows media player etc. as part of the OS The European union prevented the inclusion of ms media player in all windows sold in europe… (anti trust reasons)
The course will deal with “CORE OS” functions only. Those services are usually managed by the OS “Kernel”
What is UNIX
UNIX is a general term for lots of things
UNIX may refer to standards such as POSIX that defines OS interface that many OS follow
UNIX is also general name for a family of many proprietary OS that were tested for standard completion. (however, even most modern OS does not fully implement all aspects of the standard)
Open source OS (such as Linux) that were not tested by standard committee (but tend to have the save API often referred to as UNIXlike
Most modern UNIX OS are brunched from either system V revision 4 or BSD 4.4 (both ancient versions of UNIX)
Today, for most people, UNIX means an OS with specific set of API and applications regardless of standards. So UNIX and UNIX-like OS’s are considered UNIX. (at least for this course)
UNIX history
UNIX (acr. for uniplexed information) was initially pun on multics (OS that is virtually extinct) and was the name of OS that was developed at bell labs The OS was initially distributed in source form so that anybody can modify and customize it.
UNIX was developed by Bell labs for their PDP11 computers.
The source was later brunched into two main trunks SYS-V and BSD code (with both brunches often borrowing code from each other)
Most modern Unices are usually based on either of those brunches.
Commercial UNIX today exist only in high end server and desktop market. (for example 2/3 of PC’s sold for over 1000$ are Macintosh – a UNIX brand)
Open source UNIX tends to rule several market segments (Linux and apache rule the http serving and Linux practically runs on all Wireless routers.) and is heavy favorite on several others (running database servers)
UNIX OS in the wild SVR4 Unices
Solaris – Unix by SUN Microsystems
HP-UX – Unix by HP
Tru64 – old unix by Digital and later Compaq, still maintained by HP
Linux – The most popular open source OS (sponsered by RedHat, IBM, Silicon graphics, Novell and more)
BSD Unices
OS X and Darwin – Apple OS (OS X is the proprietary version and darwin is the open source version)
AIX – Unix by IBM
FreeBSD, NetBSD, OpenBSD – Open source BSD distributions
NOT UNIX per-se
VxWorks – embedded OS, follows Posix standard. (but usually UNIX doesn’t refer to embedded system though it can)
GNU source and the FSF GNU – acr. For “GNU’s not UNIX” GNU – a public license granting permission to use,
modify and redistribute code provided it remains GNU FSF – acr. For “free software foundation” FSF – distribute the source for most of the free UNIX applications (The unix look and feel). Promote the GNU license Linux kernel – the “main functionality” of the OS developed by volunteers (not the FSF, but the group is connected) under the the GNU license
The many types of free MIT license – allow you to do what ever you want
with the code BSD license – allow modification and redistribution (modification may not be free). You must keep credits to original author GNU GPL – modifications must be free GNU LGPL – modification must be free, but linking with GNU source is not modification (license used by GNU libraries) Other free licenses - check on FSF website Commercial Open source – you may view the source but not use it commercially or you may view the source, but modification belong to specific company or modifications may not be redistributed. Are not considered “free” (such as Apple public license, Sun Public license, Netscape public license, MySQL)
Free as in beer – free as in speech Beer – when you get a free beer – you can drink it.
But not order the entire bar for a round of free beer! Speech – when you hear an idea you can repeat it and incorporate it in your own speeches. In neither case – free does not refer to distribution and support fees which may apply. The open source community looks for “free as in
speech” software. And only “free as in speech” is considered free. (GPL, LGPL, BSD, MIT are all “free” while commercial free licenses are not considered “free”.)
What is Linux distribution Linux distribution is a gathering of software
from several sources, compiling them and branding them by single company
All Linux distribution include the Linux kernel
and most GNU sources (thereby Linux distribution….)
Linux distributions by Red hat SuSe, debian,
Ubunto, slack ware have been popularized some gained commercial support.
Some Linux distribution include only free
content some include open source (but not free) as well and some include commercial content
On this course we will focus on fedora core 9
OS code and User code OS code – usually invoked in “the kernel” is
the code that “makes the computer work”. Including drivers, memory management etc.
User code – most code we write In this code we will use the terms Userland
and kernelspace to distinguish between them.
We will write both kinds of code in the
exercises
UNIX OS internals UNIX OS is built on top of two basic
principals Process File
All OS services are implemented as either
process or file. (for example the system logger is a “process” while a socket is a file.)
This is one of the first implementations of
the interface design pattern
Some unix file handling code Open(2) Close(2) Read(2) Write(2)
To learn about those file type “man 2 open” to get the function C info.
Library code relation with OS code Library functions, such as printf, are
invoking OS code to complete their work
For example printf Printf(3) (short for print formatted) parses
the input and format the text. Then it calls for write.
Printf is library function (and not OS
function)
The difference between system and library function OS function
Library function
Lowest level interface
Usually calls OS function
Section 2 of man
Section 3 of man
System dependent and may not exist on different systems (or used with different name or (parameters Almost always bug free
Programming language dependent (but not system (dependent
Almost always efficient
Almost always efficient
Usually – kernel space code
Usually user space code
Almost always bugfree
fopen(3) implementation FILE *fopen(char *name, char *mode) { int fd; FILE *fp; if (*mode != ‘r’ && *mode != ‘w’ && *mode != ‘a’) return NULL; for (fp = _iob; fp < _iob + OPEN_MAX; fp++) if ((fp->flag & (_READ | _WRITE)) == 0) break; /* found free slot */ if (fp >= _iob + OPEN_MAX) /* no free slots */ return NULL; if (*mode == ‘w’) fd = creat(name, PERMS); else if (*mode == ‘a’) { if ((fd = open(name, O_WRONLY, 0)) == -1) fd = creat(name, PERMS); lseek(fd, 0L, 2); } else fd = open(name, O_RDONLY, 0); if (fd == -1) /* couldn’t access name */ return NULL; fp->fd = fd; fp->cnt = 0; fp->base = NULL; fp->flag = (*mode == ‘r’) ? _READ : _WRITE; return fp; }
Conclusion So OS code is the underlying layer of the
library function Homework – (not for submission) Examine printf(3) code – find the OS function
call references. Follow all functions printf calls!