01 - OS, Process, Multitasking, and Virtual Memory

Class: CSCE-313


Notes:

Operating System

Pasted image 20260114135629.png|450

Three main hats

Linux

Some useful OS concepts

Processes in Linux

ps aux
> 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 

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 -

Multitasking

Virtual Memory

Pasted image 20260114142252.png|200

Example:

int a = 5;
int main()
	...
{

Crude analogy with an infinite ladder

Analogy with 2-D space

Virtual memory of multiple processes

Pasted image 20260114143518.png|400

Operating systems provide a separate page table, and thus a separate virtual address space, for each process.

Pasted image 20260114143713.png|450

Writing and Compiling your 1st program

hello.c

#include <stdio.h>
int main() {
	printf("hello, world\n");
	return 0;
}
$ gcc -o hello hello.c

Pasted image 20260114143823.png

Compilation System

  1. Preprocessor
    • Prepares the hello.c file for compilation by including the header files that the program requires
  2. Compile
    • Translates source code into assembly code and then into binary code understandable by the computer
  3. Assembler
    • Creates relocatable object programs (.o files)
  4. Linker
  5. Loader

Executing your first program

$ gcc –o hello hello.c
$ ./hello
hello, world
  1. After compilation, the hello.c program is transformed into an executable file that is stored on the computer disk.
  2. The command ./hello runs the executable file by loading the program from disk to main memory.
  3. The processor fetches the program from main memory, decodes, and executes it.
  4. The hello program prints its message to the screen and terminates.

Switching gears to Processor Architecture

Pasted image 20260116140338.png|500

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

Overflow & Underflow

Pasted image 20260116141836.png|400

2's complement arithmetic

Pasted image 20260116142446.png|450

Twos-complement.png|450

In 2's complement, u=u¯+1

u=2nuu=2n1u=u¯+1

What is 2𝑛1𝑢? Consider 4 bit numbers and u = 6.

1111011010001+0000110010

Generated Assembly Code

gcc -O0 -Wall -S hello.c

Pasted image 20260116143137.png|450


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