01 - OS, Process, Multitasking, and Virtual Memory
Class: CSCE-313
Notes:
Operating System
- An operating system is the layer of software that interfaces between (diverse) hardware resources and the (many) applications running on the machine
- An operating system implements a virtual machine for the application, whose interface is more convenient than that of the raw hardware interface (convenient = security, reliability, portability)
/CSCE-313/Visual%20Aids/Pasted%20image%2020260114135629.png)
Three main hats
- Illusionist
- Provide clean, easy-to-use abstractions of physical resources
- Your OS gives you a filesystem abstraction regardless of the layout of the disk
- Referee
- Manage protection, isolation, and sharing of resources
- "one process cannot affect another process"
- Glue
- Provides a set of common services
- filesystem, networking stack, etc.
Linux
- 1991
- Linus Torvalds started extending the MINIX OS
- An OS to teach students about OSs
- Linus Torvalds started extending the MINIX OS
- 1992
- Released under the GPL license. Available on the web.
- 2000 - 2010
- Takes over the server and HPC market
- 2012 - 2020
- Android (Linux-based) becomes the most popular mobile Operating System
- Open source, widely used
Some useful OS concepts
- Process
- Analogy: tabs in your browser
- Think of each browser tab of being a process
- They provide isolation from other services (they are units of isolation)
- A process in general cannot read or write, what is happening on another process
- Each process has a PID (a handle)
- Each process must running under a user
- Unix partitions trust by creating new users to run specific processes
- Analogy: tabs in your browser
- Multitasking
- The OS tries to be resource-fair
- Takes all the processes and runs each one a little bit
- Gives you the illusion that your processes are continuously running.
- A 10-cores processor can in principle run 10 processes at the same time
- Virtual Memory
- This is what gives you that unit of isolation
- As this process generates addresses, the OS very quickly maps physical pages to virtual pages and makes it look like you have all of that loaded instantly
- Each individual process thinks it has a lot of resources available, what the OS is doing is just executing each one of them for a little bit to make it look like it is continuously running
- There is no address that a process can generate that can refer to an address that refers to another process
Processes in Linux
ps aux
ps: is the process status command.a: display information about other users' processes + your own. Skip procs without controlling terminal.u: displays the processes belonging to the specified usernames. Also show more attributes.x: includes processes that do not have a controlling terminal.
> ps aux
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
macc 2768 10.0 0.9 440813328 143936 ?? R 8:18AM 20:54.03 /Applications
_windowserver 919 8.3 0.5 436586160 91840 ?? Ss 8:03AM 44:10.63 /System/Libra
macc 1574 4.9 11.9 1658986896 1999824 ?? S 8:04AM 19:46.28 /Applications
macc 45500 4.0 0.8 436002864 127216 ?? S 2:03PM 0:00.35 /Applications
root 575 2.8 0.2 435400960 29488 ?? Ss 8:03AM 5:54.40 /usr/sbin/blu
_driverkit 704 1.6 0.3 435308720 44224 ?? Ss 8:03AM 4:42.47 /System/Libra
root 703 1.2 0.2 435395040 31920 ?? Ss 8:03AM 1:36.23 /usr/libexec/
_locationd 558 1.1 0.2 435382208 27744 ?? Ss 8:03AM 3:06.48 /usr/libexec/
root 739 1.1 0.1 435362144 13680 ?? Ss 8:03AM 1:28.02 /usr/libexec/
macc 1066 1.1 0.3 435910832 52064 ?? S 8:03AM 1:57.17 /usr/libexec/
macc 45502 1.0 0.1 435314688 9008 s000 S 2:03PM 0:00.09 -/bin/zsh
macc 37374 1.0 0.6 1893963808 108720 ?? S 1:22PM 0:12.20 /Applications
root 539 0.7 0.2 435422560 25760 ?? Ss 8:03AM 0:54.40 /System/Libra
macc 1608 0.6 0.1 435523344 23488 ?? S 8:04AM 0:09.25
- USER: user behind the process
- PID: unique process ID
- %CPU: percentage of CPU usage
- %MEM: percentage memory usage
- VSZ: virtual size in Kbytes
- RSS: resident set size in KB
- TT: control terminal name
- Processes that do not have a controlling terminal will appear as ??
- STAT: symbolic process state
- STARTED: time started
- CMD: command that launched the process.
Note the TTY/TT field:
> ps
PID TTY TIME CMD
45502 ttys000 0:00.51 -/bin/zsh
45517 ttys000 0:00.00 -/bin/zsh
45549 ttys000 0:00.00 -/bin/zsh
45551 ttys000 0:00.01 -/bin/zsh
45552 ttys000 0:00.03 /Users/macc/.cache/gitstatus/gitstatusd-darwin-arm64 -G v1.5.4 -s -
- ttys000 is the name of a controlling terminal for some process
Multitasking
- Multitasking is the concurrent (not parallel) execution of multiple processes over a period.
- One of the jobs of the OS is to completely use its resources and try to be fair with all the processes
- The notion of context switching comes up here
- The CPU is running so fast that, to any user, it appears that the computer is running all the programs simultaneously.
Virtual Memory
- Virtual memory is an abstraction that provides each process with the illusion that it has exclusive use of main memory.
- A virtual address space is a set of ranges of virtual addresses that the operating system makes available to a process.
/CSCE-313/Visual%20Aids/Pasted%20image%2020260114142252.png)
- When you compile a program it determines which addresses needs to be used
- Every process will map half its address space to the kernel just for efficiency
- Stack usually grows down (grows towards lower addresses)
- Text is generally read only/executable
- Initialized data goes into Data
- As you do
newormallocthe OS gives you more memory in the Heap- The only way to get this is by asking your OS, and it will do that for you, it is the OS that is actually increasing the size of your address space
- Your program is being executed in your ELF file that contains all the instructions and information already understood by the computer
- Note all of these addresses are virtual addresses, they are just mapped to a physical address space
- 1 virtual page maps to 1 physical page, this is the granularity we have
- Page size is usually 4KB
- There are other architectures where you can increase this size but generally it has been probably found that 4Kb is a good size
- This is probably an engineering tradeoff
Example:
int a = 5;
int main()
...
{
- The compiler will place
ain your Data segment. - Your ELF file will have these segments declared:
- Text
- Static
- etc.
Crude analogy with an infinite ladder
- You climb and climb, the next step you take generates a page for it
- The OS takes the latter from below and place it above you so you can continue stepping
- You actually don't see the page fall/ladder fall
- Everytime you have a page fall, the OS will fix that for you and you will be able to continue
- This is a linear way of traversing your address space
Analogy with 2-D space
- Addresses are no longer sequential
- When you generate an address somewhere and that address is not mapped to real memory it will generate a fault
- The oS will patch this thing for you and you will be able to try it again
- Regardless of where you go, if there is a page fault the OS will notice this and fix it for you.
Virtual memory of multiple processes
/CSCE-313/Visual%20Aids/Pasted%20image%2020260114143518.png)
- Whatever the processor cannot put into virtual memory, ti swaps up with the disk
- This is called the swaps space and is the reason we need to allocate a partition for it when setting up the disk for our OS
Operating systems provide a separate page table, and thus a separate virtual address space, for each process.
/CSCE-313/Visual%20Aids/Pasted%20image%2020260114143713.png)
- This mapping is very carefully managed and controlled by the OS
Writing and Compiling your 1st program
hello.c
#include <stdio.h>
int main() {
printf("hello, world\n");
return 0;
}
- The hello program displays the phrase “hello, world” to the screen.
- This program is written in the C programming language.
- The computer stores the hello.c program in a file as a sequence of bytes.
$ gcc -o hello hello.c
/CSCE-313/Visual%20Aids/Pasted%20image%2020260114143823.png)
-
The first thing that happens is that the pre-processor runs: first the compiler looks for the
#signs in column 1 of the file- Literally just find the file to include and import it
- You can also define macros as part of this pre-processing step -> basically just a string-to-string type thing
-
The compiler generates assembly language code (then it gets closer to machine code)
-
Now for example a function like
printfcomes in the Linker step- Somewhere in
libcisprintf - In your
hello.cone of the instructions is a call toprintf - You need to find where
printfis defined, but also to find its address - You can find this in a standard place on a Linux system
- You need to point the
printffunction call in your program to point (to the address) to the actual address of thelibcoriginalprintffunction. - At this point you can take this binary anywhere in your program and run it easily
- The linker takes all of your files and "links" them into a single executable.
- It is the reason you do not have to worry about standard library functions and other functions external to your program
- Somewhere in
-
A static compilation would be putting the whole
libcinto the compilation of your program so it is available by your program -
The GCC compiler reads the file hello.c and translates the file into an executable file hello.
-
The figure is a logical view—a .s file may not actually be generated.
Compilation System
- Preprocessor
- Prepares the
hello.cfile for compilation by including the header files that the program requires
- Prepares the
- Compile
- Translates source code into assembly code and then into binary code understandable by the computer
- Assembler
- Creates relocatable object programs (.o files)
- Linker
- Loader
Executing your first program
$ gcc –o hello hello.c
$ ./hello
hello, world
- After compilation, the hello.c program is transformed into an executable file that is stored on the computer disk.
- The command ./hello runs the executable file by loading the program from disk to main memory.
- The processor fetches the program from main memory, decodes, and executes it.
- The hello program prints its message to the screen and terminates.
Switching gears to Processor Architecture
/CSCE-313/Visual%20Aids/Pasted%20image%2020260116140338.png)
- Your CPU has a set of registers
- As the CPU is executing instructions it sets things in condition codes
- Memory is very far away compared to the CPU cache
- You can get significance performance because you are able to store things efficiently in the cache and have a sense of locality
- !
Registers in x86-64
Registers are the fastest kind of memory available in the machine. x86-64 has 14 general-purpose registers and several special-purpose registers. You’ll notice different naming conventions, a side effect of the long history of the x86 architecture (the 8086 was first released in 1978).
| Full register (bits 0-63) |
32-bit (bits 0–31) |
16-bit (bits 0–15) |
8-bit low (bits 0–7) |
8-bit high (bits 8–15) |
Use in calling convention | Callee-saved? |
|---|---|---|---|---|---|---|
| General-purpose registers: | ||||||
| %rax | %eax | %ax | %al | %ah | Return value (accumulator) | No |
| %rbx | %ebx | %bx | %bl | %bh | – | Yes |
| %rcx | %ecx | %cx | %cl | %ch | 4th function parameter | No |
| %rdx | %edx | %dx | %dl | %dh | 3rd function parameter Second return register (for 9–16 byte return values) |
No |
| %rsi | %esi | %si | %sil | – | 2nd function parameter | No |
| %rdi | %edi | %di | %dil | – | 1st function parameter | No |
| %r8 | %r8d | %r8w | %r8b | – | 5th function argument | No |
| %r9 | %r9d | %r9w | %r9b | – | 6th function argument | No |
| %r10 | %r10d | %r10w | %r10b | – | – | No |
| %r11 | %r11d | %r11w | %r11b | – | – | No |
| %r12 | %r12d | %r12w | %r12b | – | – | Yes |
| %r13 | %r13d | %r13w | %r13b | – | – | Yes |
| %r14 | %r14d | %r14w | %r14b | – | – | Yes |
| %r15 | %r15d | %r15w | %r15b | – | – | Yes |
| Special-purpose registers: | ||||||
| %rsp | %esp | %sp | %spl | – | Stack pointer | Yes |
| %rbp | %ebp | %bp | %bpl | – | Base pointer (general-purpose in many compiler modes) |
Yes |
| %rip | %eip | %ip | – | – | Instruction pointer (Program counter; called $pc in GDB) |
* |
| %rflags | %eflags | %flags | – | – | Flags and condition codes | No |
- You can pass up to 6 function parameters through registers, if you need more parameters they will be passed through the stack
Overflow & Underflow
- Is
≥ 0? - What about 40000 * 40000? (groovyConsole/gdb)
- What about 50000 * 50000?
- It comes out to be a big negative number? how? -> overflow
- Interpreting a really big unsigned-represented number as a 2's complement signed integer will probably have a negative value
- Is (x + y) + z = x + (y + z)? (gdb)
- (1e20 + -1e20) + 3.14f → ?
- 1e20 + (-1e20 + 3.14f) → ?
/CSCE-313/Visual%20Aids/Pasted%20image%2020260116141836.png)
2's complement arithmetic
/CSCE-313/Visual%20Aids/Pasted%20image%2020260116142446.png)
/CSCE-312/Visual%20Aids/Twos-complement.png)
In 2's complement,
- That makes −𝑢 + 𝑢 ≡ 0 𝑚𝑜𝑑 2^𝑛
What is
Generated Assembly Code
gcc -O0 -Wall -S hello.c
- Use https://godbolt.org/ to see the assembly generated
/CSCE-313/Visual%20Aids/Pasted%20image%2020260116143137.png)
Terminal tips
Every process when it starts in unix has three file descriptors
For example: ls generates its output to file descriptor one which is the standard output, this file descriptor 1 is bound to the terminal so the output is displayed in the current terminal
> ls
Applications Documents Library Music Postman Xrams
Desktop Downloads Movies Pictures Public
You can redirect this output and bind it to a file instead of the standard file descriptor:
> ls -l > foo
Now the output of ls -l should be in the foo file:
> cat foo
total 0
drwxr-xr-x@ 9 macc staff 288 Jan 8 18:18 Applications
drwx------@ 3 macc staff 96 Dec 10 11:57 Desktop
drwx------@ 13 macc staff 416 Jan 16 00:44 Documents
drwx------@ 240 macc staff 7680 Jan 16 14:32 Downloads
-rw-r--r-- 1 macc staff 0 Jan 16 14:37 foo
drwx------@ 99 macc staff 3168 Sep 20 21:56 Library
drwx------ 7 macc staff 224 Dec 10 11:35 Movies
drwx------+ 5 macc staff 160 Jul 9 2025 Music
drwx------+ 5 macc staff 160 Aug 15 13:46 Pictures
drwxr-xr-x@ 4 macc staff 128 Jul 9 2025 Postman
drwxr-xr-x+ 5 macc staff 160 Jul 9 2025 Public
drwxr-xr-x 4 macc staff 128 Dec 19 14:33 Xrams
When we run du we get a bunch of error messages, we can separate the errors displayed from the output using:
du -h /etc > Foo 2-bar