03 - OS Concepts and StructureOutlineConcept OverviewProcessesAddress SpaceFilesAnnouncementsReading: MOS 1.5, 1.7Concept OverviewMost systems have 2 execution modes: - user mode (non-privileged) - kernel mode (privileged)User mode execution restricts allowed actions andinstructions.For example, in ARM:- Move to/from system register- Read/Write protected memory- Enabling/Disabling interruptsCan only be done in privileged mode.User applications and general libs run in user-mode.Libraries (e.g. libc) can provide a common user-modeinterface for system calls.The OS manages processes.An address space is a range of valid memoryaddresses for a given process.Address space contains:- Readable and writeable by the process- Text: program code- Data: initialized global vars- BSS: uninitialized global vars- Heap: dynamically allocated vars- Stack: local varsA process is a program in execution.Each process has associated with it:The OS represents a process as a structcontaining the associated state information, whichis stored in an array called the process table.- an address space- current registers- resources: - open files - outstanding alarms/timers - related processes - etc.A program is a sequence of instructions (e.g. executable files ELF, EXE)(see )man -s 2 syscalls- File system service- Process management serviceUser-mode code must make a system call (syscall)to the kernel to access devices on its behalf.System calls can be grouped together to provideservices. For example:ProcessesAddress SpaceFilesSystem CallsKernelMake sure to join the Discord!PA 1 is due Friday.Start today if you haven't started yet!1)2)3)4)5)6)Applications & System SoftwareNon-privilegedInstructionsPrivilegedInstructionsI/O DevicesLibrariesOS KernelUserLibrary callsSystem callsMachine inst.softwarehardwarestackheapbssdatatextProgram Memory0xFFFFFFFF0x00000000Each process has its own addressspace, controlled by the OS.The file system is an OS abstraction forinteracting with storage devices.UNIX example:File paths:Storage devices can be mounted in the file systemFrom PA 1: /dev/sda0 -> /From PA 1: /mnt/shared -> /home/rtsang1/sharedSpecial Files and PseudofilesPipesInput/OutputSystem CallsExample: "Hello World"In UNIX, "everything is a file".A pipe is a pseudofile for allowing multipleprocesses to communicate via FIFO channelmanaged by the kernel.All computers have physical devices for receivinginput and generating output.Kernel manages I/O devices with I/O subsystemKernel requires device drivers to support hardwareUser-mode applications use system calls (syscalls)to issue service requests.Syscall mechanism is OS and hardware specific.- Less portable across platformsSyscalls are wrapped in system and standardlibraries (e.g. libc) to mitigate this.- Usually 1 function per syscall- libc based on C POSIX standard- libc different implementation per platformPortable, since all functions are from C stdlib.printf internals are different based on OS.3 ways of writing "Hello World" of increasingplatform dependence.printf(char *str, ...);write(int fd, char *buf, size_t len);write is an OS-specific POSIX-compliant libraryfunction, not the actual syscall.It makes a CPU-dependent syscall.Syscall interface is completely platformdependent.It is necessary for certain actions, like creatinga new process or interfacing with hardware.The kernel is the portion of the OS thatexecutes in privileged mode.It manages everything that requires privileged-mode operations (processes, interrupts, scheduling,I/O, etc.) and possibly more.3 types of kernel architecture:In a monolithic kernel, the entire OS executes inprivileged mode as one giant executable.Examples:- Most UNIX variants- Linux- Most BSD variantsIn a microkernel, the minimum necessary functionsare included in the kernel, all other functionalityprovided as user-mode modules.A hybrid kernel is a mixed approach.Only moves modules out of kernel if doing so does notnegatively affect performance.Improves reliability while prioritizing performance.- Kernel functions can call every other function- Performant- A single bug will crash the entire kernel- Very complex- Modules cannot interfere with one another- More resilience against module bugs- More communication overhead between modules- Monolithic kernel- Microkernel- Hybrid kernelA device driver implements initialization andcommunication logic for a specific device.The I/O subsystem provides a uniform file-likeAPI for accessing I/O devices.(e.g. keyboard, monitor, mouse, printer, etc.)- Must be created in advance- Processes see it as just another fileExample:Example:This shows output lines of `ls -l` onlyif the line contains "D".`|` pipes stdout of first process tostdin of next process.Special files make I/O devices look like files:(macOS example)Symbolic Links point to and act like other files.Named/Anonymous FIFOs (sockets/pipes) areused for inter-process communication.Block special files model devices withrandomly addressable blocks (e.g. disks)Character special files model devices thatuse character streams (e.g. serial ports)/home/rtsang1/.bashrc../.bashrcAbsolute ( / ):Relative (Documents):(Every process has a current working directory)/|-- home| `-- rtsang1| |-- .bash_history| |-- .bashrc| |-- Documents| |-- Downloads| |-- .local| `-- .profile`-- tmp `-- dir `-- testA file is essentially a byte array.The OS provides syscalls to interact with files. (e.g. open, close, read, write files)Hierarchical file systems have directories.A directory is a special file that lists other files.The OS also provides syscalls for these./tmphomertsang1.localDocumentsdirDownloads.bashrctestbrw-r----- 1 root operator 1, 0 Jan 5 23:20 /dev/disk0crw-rw-rw- 1 root wheel 4, 48 Jan 5 23:20 /dev/ttys0printf(char *str, ...);write(int fd, char *buf, size_t len);syscall(int number, ...);KernelsMonolithic KernelMicrokernelHybrid Kernelshellshellfsfsdiskdiskprocprocipcipcttyttynetnetclockclocksyssysmakemakeotherotherusermodekernelmodekernelmodeprocessesuserprogramsserversdriversinterrupts, drivers,scheduling, etc.interrupts, drivers,scheduling, etc.usermodeExamples:- Mach- MINIX- L4Examples:- Windows- XNU (Darwin)- DragonFly BSDls -l | grep -e "D"