Operating Systems
Eric Lo
1. Overview of an Operating System
What is an OS?
• An OS
– Includes a program
• called “kernel” (e.g., kernel.exe)
– which manages all the physical devices (e.g., CPU, RAM and hard disk)
– exposes some functions as system calls for others to configure the kernel
or build things (e.g., C library) on top
– Includes some more programs
• called “drivers”
– which handles the interaction between the kernel and the external devices
(e.g., keyboard)
• called a “shell”
– Which renders a simple command-line user interface with a full set of
commands
• …
– Includes some “optional” programs
• GUI, Browser, Paintbrush, …
2
What is a process?
• A process is an execution instance of a program.
– More than one process can execute the same program code
Let’s consider the following two commands
Recursively print the directory entries,
Command A ls -R /
starting from the directory ‘/’
Recursively print the directory entries,
Command B ls -R /home
starting from the directory ‘/home’
They are 2 different processes
3
A process is more than a program
• A process has states concerning the execution.
E.g.,
• which line of codes it is running;
• how much time left before returning the CPU to the others
• Commands about processes
– e.g., ps & top.
4
Process-Related Tools
• The tool “ps” can report a vast amount of
information about every process in the system
– Try “ps -ef”.
This column shows the unique
identification number of a process,
called Process ID, or PID for short.
Hint: you can treat ps as the short- $ ps
form of “process status” PID TTY TIME CMD
1200 ... 00:00:00 bash
1234 ... 00:00:00 ps
$ _
By the way, this is called shell.
5
What is a Shell?
• A shell is a program
• You open a “terminal”, which actually launches a “shell” process
• E.g., bash in Linux
• Written in C
• use getchar() (to get your command “ps”)
• syntax checking
• invoke a function fork() (a system call) to create a new process
– i.e., becoming a child process of the shell.
• Ask the the child process to exec() the program “ps”.
Shell – the
$ ps
parent process
PID TTY TIME CMD
1200 ... 00:00:00 bash
Parent-child 1234 ... 00:00:00 ps
relationship $ _
ps – the child
process
6
Process hierarchy
• Process relationship:
– A parent process will have its child processes.
– Also, a child process will have its child processes.
– This forms a tree hierarchy.
Process A
Process B
Process C
Process D
Process E Process F
E.g., “Process E” is the shell and “Process F” is “ps”.
7
What is a system call?
• System call
– Is a function call.
– Exposed by the kernel.
– Abstract away most low-level details.
• Do you know how to read an input from keyboard?
Function
int add_function(int a, int b) { implementation.
return (a + b);
}
This is a
int main(void) { function call.
int result;
result = add_function(a,b);
return 0;
}
// this is a dummy example…
8
Interacting with the OS
int main(void) { //somewhere in the kernel.
time(NULL); Invoke & return int time ( time_t * t ) {
return 0; ......
} }
./program Here contains codes that
access the hardware clock!
Process OS Kernel
Process
9
System calls
• Categorizing system calls as follows:
Process File System Memory
Security Device Information
10
System calls
• How can we know if a “function” is a system call?
– Read the man page “syscalls” under Linux.
• Without reading the man pages, guess which of the
following is/are system call(s)?
Who are
Name Yes/No? they?
printf() & scanf() No
malloc() & free() No
fopen() & fclose() No
mkdir() & rmdir() Yes
chown() & chmod() Yes
11
System calls VS Library function calls
• System call vs Library call
• Take fopen() as an example.
– fopen() invokes the system call open().
– So, why people invented fopen()?
– Because open() is too primitive and is not programmer-
friendly!
Library call fopen(“hello.txt”, “w”);
System call open(“hello.txt”, O_WRONLY | O_CREAT | O_TRUNC, 0666);
12
System calls VS Library function calls
http://www.thegeekstuff.com/2012/07/system-calls-library-functions/comment-page-1/ 13
What will we learn about processes?
• System calls
– How to program a simple, bare-bone shell?
• Lifecycle and Scheduling
– How to create processes?
– How to handle the death of the processes?
– Which process shall get the core next?
• Signals
– How to suspend a process?
– A virus? We can make a program to play a song whenever you type
Ctrl+C?
• Synchronization
– How processes can cooperate to do useful work together?
14
Introduction to Operating System
Components
Memory
15
Process’ Memory
BTW, this arrangement is called s_ _ _ _ _ _ _ _ _ _ _!
Local variables Stack
Dynamically
Allocated Heap
Memory Loading
Global variables
program Data segment
Execute
Constants Constants
Text segment
Program code
C program layout Memory Space
16
What will we learn about memory?
• Virtual memory
– 1 process virtually owns all
• 232 RAM (=4GB) for 32-bit CPU
• 264 RAM (=16EB) for 64-bit CPU
– Practically OS set it to be 256TB or 1PB
• Memory-related functions
– E.g., how to write “malloc()”?
• Stack overflow techtarget
– Why & when?
• RAM = 256MB
➢malloc(16MB)
➢How much free memory left?
17
Introduction to Operating System
Components
File System
18
What is a File System?
• Have you heard of…
– FAT16, FAT32, NTFS, Ext3, Ext4,
BtrFS, Juliet?
– They are all file systems.
– It is about how to organize your
files in the storage device.
19
What is a File System?
• If a FS just lays your files one-by-one, consecutively,
tightly, in your hard disk, is it good?
– What if you increase the size of your file?
– What’s the performance of searching for a file? O(?)
– BTW, how to deal with directories?
Index
Files / Data
Metadata
20
What we will learn about FS?
• How to deal with directories?
• Implementation of some famous FS-es.
• Why does a file system perform badly?
• How to undelete a file?
21
More… from System Programming to
Programming Operating System
• Multi-threading
• Booting
• Architectural Conscious OS Programming
• Lock-free Programming
• I/O
• Virtualization
22