├── C_programming_help.C ├── README.md ├── first_C_programming_assignment.C └── objectives ├── boot-process └── README.md ├── kernel-proc └── README.md ├── mm-drv-fs └── README.md └── os-overview └── README.md /C_programming_help.C: -------------------------------------------------------------------------------- 1 | 2 | // Every C program is an operating system process/executable 3 | 4 | // Specified the same kind of comments you are used to with Javascript 5 | 6 | /* 7 | * Multi line comments 8 | */ 9 | 10 | // #includes allow us to combine different files into a single program 11 | // #including can be pointed at a file that you wrote, or it can be4 pointed 12 | // at a file somebody else wrote. You have to know where the file is, and 13 | // whether or not it is the one you need. 14 | 15 | #include 16 | 17 | // examples of return values and strictly typed function definitions 18 | int integerFunction(int input) { 19 | return 0; 20 | } 21 | float floatFunction(float input) { 22 | return 0.0; 23 | } 24 | float mixedFunction(char* input) { 25 | return 0; 26 | } 27 | // I don't really return a float, but gcc/clang will compile it because it 28 | // knows of a way to automatically convert the char type to a float (because a 29 | // char type is just an integer). 30 | float mixedFunction(int input) { 31 | return 'a'; 32 | } 33 | 34 | struct lat_lon { 35 | float lat; 36 | float lon; 37 | }; 38 | struct latLon { 39 | float lat; 40 | float lon; 41 | }; 42 | 43 | int main(int argc, char** argv) { 44 | // char** means one of two things: someone is doing a complicated 45 | // algorithm with this object (it is a pointer to a pointer) 46 | // or it means it is a two dimensional array like this char[][] 47 | // 48 | // argv = [ 49 | // ['g','c','c'], 50 | // ['c','f','i','l','e'], 51 | // ['H','i'] 52 | // ] 53 | // 54 | // In C, a string is "an array of characters" 55 | // A string is in "double quotes" 56 | // A character (char) is in 'single quotes' 57 | 58 | // simplest possible C program 59 | //return 0; 60 | 61 | // what is argc? 62 | printf("argc is: "); 63 | printf("%d\n", argc); 64 | 65 | for(int i = 0; i < argc; ++i) { 66 | printf("%s\n",argv[i]); 67 | } 68 | 69 | printf("%c\n",(int)mixedFunction(0.0)); 70 | printf("%f\n",mixedFunction(0.0)); 71 | 72 | lat_lon myLocation; 73 | myLocation.lat = 22.5; 74 | myLocation.lon = 21.3; 75 | printf("%p\n", &myLocation); 76 | 77 | } 78 | 79 | 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Module: Operating Systems One 2 | 3 | ## Objectives 4 | 5 | * [Operating Systems Overview](objectives/os-overview) 6 | * [Boot Process](objectives/boot-process) 7 | * [Kernel, Processes, Threads](objectives/kernel-proc) 8 | * [Memory Management, Drivers, File Systems](objectives/mm-drv-fs) 9 | 10 | ## Guided Demo 11 | 12 | ### File System 13 | 14 | Outline in writing (no coding--unless you really want to) the parts you 15 | would have to code up to implement a simple filesystem. The filesystem 16 | is what keeps track of and organizes files on disk. When you want to 17 | read a file, the OS must go retrieve the blocks of the file until it has 18 | them all loaded. 19 | 20 | The filesystem would need to store file names and the sizes of the file 21 | in bytes. Maybe a "created" date. 22 | 23 | It would also need to store the data in blocks; a file might be made up 24 | of multiple blocks somehow chained together. The blocks might be spread 25 | all over the disk (fragmented). 26 | 27 | When new files are written, they have to go in blocks that aren't 28 | currently in-use by other files. How are free blocks tracked? 29 | 30 | (Assume no subdirectories in this filesystem, and that all files are in 31 | the same directory.) 32 | 33 | How would you organize this data, both the information about the file 34 | (the _metadata_) and the data of the file itself? 35 | 36 | There's no right answer to this problem; there are many kinds of 37 | filesystems that use many different techniques to get the work done. 38 | 39 | This is a challenging problem. Mentally experiment with different 40 | approaches of keeping track of the data and figure what works and what 41 | breaks. 42 | 43 | **Stretch challenge**: How would you support subdirectories? 44 | -------------------------------------------------------------------------------- /first_C_programming_assignment.C: -------------------------------------------------------------------------------- 1 | /** 2 | * primitives (int char float double) 3 | * loops, break, continue 4 | * branches if else else if, while 5 | * arrays 6 | * structs (to become classes) 7 | * ^^^ ^^^ ^^^ ^^^ ^^^ 8 | * Operating Systems C Lesson Assignment 1 9 | * 10 | * Operating Systems and C and Theory and Algorithms Lessons 11 | * \/\/\/\/ 12 | * extended types 13 | * pointers, references, dereferencers 14 | * memory management malloc/free 15 | * const 16 | * meta programming (#ifdef) 17 | * declaration 18 | * definition 19 | * initialization 20 | * faults - SIGSEGV, SIG, et 21 | * header files 22 | * file scope 23 | * extern 24 | * includes 25 | * libraries 26 | * objects 27 | * compilation 28 | * compilation arguments 29 | * files within directories, linked and built 30 | * linking 31 | * printf formatting 32 | * comparisons && || ! 33 | * bit shifting >> << 34 | * binary operators &, |, ~ 35 | */ 36 | 37 | // In order to run a program, you must: 38 | // Write this C file 39 | // Make it syntactically correct 40 | // Compile the program with gcc my_first_C_program.C -o my_output 41 | // ./my_output 42 | 43 | #include 44 | 45 | int main(int argc, char** argv) { 46 | printf("Hello world!"); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /objectives/boot-process/README.md: -------------------------------------------------------------------------------- 1 | # The Process of Booting the Computer 2 | 3 | ## Basic Input/Output System (BIOS) 4 | 5 | When your computer first boots, it provides power to various electronics 6 | components according to the design of the hardware. As each component 7 | powers up it executes a Power On Self Test (_POST_). If POST succeeds, a very 8 | small operating system is activated with low level drivers to read and 9 | write memory, hard disks, simple display output, and potentially network 10 | cards. 11 | 12 | The BIOS attempts to read the first sector of the boot disc. In the 13 | first sector is the Boot Loader, which shows the BIOS which hard disk 14 | sector to execute first in order to activate the real operating system. 15 | 16 | In Windows, the Boot Loader is called the Master Boot Record. In Linux, 17 | it is GRUB or LILO. OS Xs Boot Loader is entirely proprietary. 18 | 19 | 20 | ## Boot-loader 21 | 22 | The first-stage boot loader is stored in the BIOS and generally reads 23 | the first sector from the boot disk which contains a small program for 24 | continuing to load. The small program (usually just a few hundred bytes 25 | of machine code) loads and executes the second-state boot loader. 26 | 27 | The second-stage, more-complicated boot loader (e.g. GRUB) then does the 28 | heavy lifting of loading the rest of the OS. Once the kernel is loaded, 29 | the boot loader transfer control to it to continue the boot process. 30 | 31 | ## Kernel Boot Process 32 | 33 | Once the kernel has been loaded, it sets up the execution environment 34 | for future processes, initializes hardware drivers, and then switches to 35 | _multiuser_ mode. 36 | 37 | At this point, it is free to initialize other services that the owner 38 | has configured, e.g. MongoDB, sshd, etc. 39 | 40 | And finally a login prompt is presented. 41 | 42 | 43 | ## Exercises 44 | 45 | Explain the following to someone in class, or in your house, or on the 46 | phone. If no one is available, any house plant will do. 47 | 48 | (The answers to the below questions aren't necessarily in the above text.) 49 | 50 | * Why is a first-stage boot loaded necessary? Why not just load the 51 | entire secondary bootloader from the start? 52 | 53 | * Why is a boot loader necessary at all? Why not just load the kernel directly? 54 | -------------------------------------------------------------------------------- /objectives/kernel-proc/README.md: -------------------------------------------------------------------------------- 1 | # The Kernel 2 | 3 | The Kernel is a compiled program that runs the CPU and every piece of 4 | fundamental hardware and software in your computer. The Windows and OS X 5 | kernels are completely secret and proprietary, but the Linux kernel is 6 | available on github (btw, Linus Torvalds also built Git)! 7 | 8 | [Linux Kernel](https://github.com/torvalds/linux) 9 | 10 | This enables curious developers to build and install a new kernel to 11 | their own computer. This is the only opportunity for this outside of 12 | private employment with Microsoft or Apple. 13 | 14 | ## Processes 15 | 16 | Every operating system provides the fundamental concept of a process. 17 | Processes are applications that run in user space. 18 | 19 | An application might spawn many processes over the course of its run. 20 | 21 | Processes in modern operating systems run in their own _address spaces_. 22 | That is, each process thinks it has its own copy of RAM all to itself. 23 | Variables declared in one process are not visible from other processes 24 | unless both processes explicitly agree to communicate. (Contrast to 25 | threads, below.) 26 | 27 | ### Process table 28 | 29 | The operating system allocates a list of processes called the process 30 | table. The process table is an array data about each process, such as 31 | where in memory its stack is located, what local data is needed for the 32 | currently executing stack frame, the ID of the process (_pid_), and much 33 | more. 34 | 35 | ### Process Lifecycle 36 | 37 | In Unix, a process is created with the `fork()` system call made by the 38 | _parent process_. 39 | 40 | A process exits when it's `main()` returns, when it calls `exit()`, when 41 | it is killed, or when it crashes. 42 | 43 | After the process exits normally, it exists in a _zombie_ state until 44 | the parent calls `wait()` to get exit status information from the child 45 | process. 46 | 47 | 48 | ## Threads 49 | 50 | At the OS level, threads are processes that share an address space. 51 | 52 | In a typical use case, a single process will spawn a number of threads 53 | to handle various tasks. Each thread has access to all the global data 54 | in the process. 55 | 56 | Synchronizing access to shared data across multiple threads (so they 57 | don't step on each others toes) is a tricky problem. 58 | 59 | 60 | ## Scheduling 61 | 62 | The kernel is responsible for sharing resources on a system between 63 | multiple processes. In the early days of large mainframes, operating 64 | systems did not support multi-tasking. Booting a computer might load the 65 | basic resources necessary to access registers, memory, and I/O. Once 66 | that boot process was complete, the system would load a single program 67 | which would run until completion. 68 | 69 | Modern operating systems support mult-tasking, which means that the 70 | operating system runs multiple applications at the same time. Because a 71 | CPU in general can only perform one activity at a time, the operating 72 | system loads processes, their stack frames, and their stack resources in 73 | a round-robin fashion. 74 | 75 | > If your computer has 4 cores, it can only actually run 4 programs at 76 | > once. You might have hundreds of processes that need to run, though. 77 | > The kernel decides which process gets some run time next. In any given 78 | > second, a large number of processes might be switched into and out of 79 | > a core. 80 | 81 | Each time a process is paused, its entire execution state is saved into 82 | main memory. The next processes stack frame is loaded from main memory, 83 | and execution is resumed. 84 | 85 | 86 | ## Exercises 87 | 88 | Write a program that spawns a child process with `fork()`. Have the 89 | parent print "I am the parent!". Have the child print "I am the child!". 90 | Then both processes should exit. 91 | 92 | Hint: `man fork` or `man 2 fork`. Pay attention to the return value from 93 | `fork()`. It's a brain-bender. 94 | 95 | Warning: don't run `fork()` in a loop unless you're careful. You can 96 | easily spawn so many processes the system is brought to its knees. For 97 | the above assignment, there's no need for a loop. 98 | -------------------------------------------------------------------------------- /objectives/mm-drv-fs/README.md: -------------------------------------------------------------------------------- 1 | # Memory Management, Drivers, and File Systems, Oh my! 2 | 3 | ## Memory Management 4 | 5 | The operating system is responsible for providing user programs access 6 | to memory. The operating system creates a stack frame at the beginning 7 | of a program's execution and allocates a section of available free 8 | memory for it to utilize. The program's memory space begins essentially 9 | at 0, and it has no knowledge of any memory outside of the memory that 10 | has been allocated to it by the operating system. 11 | 12 | This is one of the fundamental attack vectors used in attempting to take 13 | malicious control of a computer. The operating system is responsible for 14 | perfectly isolating each process from one another - if a process is able 15 | to gain access to memory other than what has been assigned to it it will 16 | be able to hijack the system. 17 | 18 | 19 | ## Drivers 20 | 21 | Drivers combine, assembly language, C code, and interrupt-driven 22 | programming to provide software control of physical devices. The drivers 23 | hide the dirty details of talking to the hardware from software 24 | developers. Then they present a consistent, easy-to-use programming 25 | interface. For example, a driver might know the details about how to 26 | talk specifically to an NVIDIA graphics card. Normal programmers don't 27 | need or want to know those details. They just want to be able to tell 28 | any 3D card from any manufacturer how to draw a scene. For that purpose, 29 | the driver exposes an OpenGL or Direct3D interface that the programmers 30 | can use. 31 | 32 | On general purpose computers like Kaby Lake machines, all drivers are 33 | written to control hardware on the other side of the PCIx bus. Hardware 34 | is built with a PCIx bus controlller, and driver commands configure 35 | binary messages to be sent and retrieved from the hardware over that 36 | bus. 37 | 38 | In smaller systems like IoT and firmware devices, the bus may be a true 39 | parallel bus that is shared between the CPU and the device. These buses 40 | are synchronized by the system clock. Drivers on these systems have to 41 | know how to take control of the hardware device at the hardware level 42 | and can be written to any degree of specificity. 43 | 44 | How to write drivers on the PCIx bus or on a parallel bus is a lesson in 45 | Computer Engineering, not covered here at Lambda School. 46 | 47 | 48 | ## File System 49 | 50 | [File systems at The Linux Documentation Project.org](http://www.tldp.org/LDP/sag/html/filesystems.html) 51 | 52 | Files are read and written from internal storage using PCIx drivers 53 | dedicated to controlling hard disks. The operating system keeps a record 54 | of all of the files in the system, called the File Table. 55 | 56 | You've probably heard of the FAT and vFAT filesystems. Linux users are 57 | more familiar with the _ext4_ filesystem. 58 | 59 | 60 | ### File table features: 61 | 62 | * Contiguity 63 | * Redundancy 64 | * Small overhead 65 | 66 | Dozens of specific file systems exist for specialized tasks, as well as dozens of general-purpose filesystems. 67 | 68 | 69 | ## Exercises 70 | 71 | Explain the following to someone in class, or in your house, or on the 72 | phone. If no one is available, any house plant will do. 73 | 74 | (The answers to the below questions aren't necessarily in the above text.) 75 | 76 | * If a malicious process had access to the data of another process 77 | without permission, what are some bad things that could happen? 78 | 79 | * If we didn't have drivers, what would programmers have to do if they 80 | wanted to communicate with different brands of USB cards? 81 | 82 | * What are some of the book-keeping tasks you can imagine a filesystem 83 | needs to keep track of when it comes to storing files? 84 | -------------------------------------------------------------------------------- /objectives/os-overview/README.md: -------------------------------------------------------------------------------- 1 | # Operating Systems Overview 2 | 3 | There are three operating systems that are primarily popular on general 4 | purpose computers - Windows, Mac OS X, and Linux. 5 | 6 | There are two operating systems that are essential for popular 7 | handhelds--Android and iOS. 8 | 9 | There are dozens of UNIX variants for simpler processors available. 10 | Typically when working with a small dedicated cpu (like an ARM CPU on an 11 | IoT device), the operating system supported for that device has been 12 | defined by the manufacturer of the device and is a subset of Linux. 13 | 14 | ## Windows 15 | 16 | Original popular OS for consumers, no permissions enabled fast and easy 17 | development, high performance for games and other user applications. 18 | Became a difficult model to maintain with the rise of the internet, 19 | leading to many revisions. 20 | 21 | Backwards compatibility is the hallmark of Windows, which causes endless 22 | headaches for modern developers. 23 | 24 | ## Linux 25 | 26 | Started by Linus Torvalds, who was at the time a student of Andrew 27 | Tanenbaum's, the Linux kernel is a copy of the MINIX kernel, which had 28 | been created by his instructor. MINIX is copy of the POSIX, an industry 29 | standardized system interface that was originally released in 1988. At 30 | the time there were many variants of UNIX, all limited to mainframe 31 | computers like VAX and PDP systems. The core of UNIX at this time is 32 | POSIX, which is the basis for essentially every successful operating 33 | system except Windows. 34 | 35 | There are dozens of Linux forks at this time, called distros. Ubuntu is 36 | the most famous and successful of these, and unless you want to build up 37 | a career in system administration it is not necessary to learn any 38 | others. The basics of the Linux operating system depend on a few basic 39 | philosophies - processes that accept streams of text as input and 40 | produce streams of text as output (pipes), file descriptors as 41 | references to device drivers and other hardware (including individual 42 | files stored on a permanent storage medium), and security enforced by 43 | basic access controls applied to every component of the system. 44 | 45 | Development paradigms: 46 | 47 | * C and C++ native development. 48 | 49 | * Management of symbolic and dynamic links via /usr/lib and 50 | /usr/local/lib and /usr/include and many more. Dependencies in linux 51 | are stored in files with specific paths, and can either be built 52 | natively using `gcc` or loaded using package management. 53 | 54 | Technologies you should familiar with: 55 | 56 | * Bash and shell scripting 57 | * SSH 58 | * System calls 59 | 60 | Technologies to be secondarily familiar with 61 | 62 | * Network programming 63 | 64 | 65 | ## macOS (previously Mac OS X) 66 | 67 | macOS is another fork of UNIX, now called Darwin but previously FreeBSD, 68 | which was created in the 90s by Berkely (Berkeley Software 69 | Distribution). OS X features a kernel and basic system libraries that 70 | are nearly identical to Linux, with many small differences. OS X's 71 | graphics layer, Cocoa, differs substantially from Linux graphics layers, 72 | features much greater reliability and consistency, and uniquely supports 73 | development in Objective-C and Swift. 74 | 75 | > Infographic: [Unix variant family tree](https://en.wikipedia.org/wiki/History_of_Unix#/media/File:Unix_history-simple.svg) 76 | 77 | Technologies you need to be familiar with: 78 | 79 | * XCode 80 | * Objective-C 81 | * Interface builder 82 | 83 | 84 | ## Exercises 85 | 86 | Explain the following to someone in class, or in your house, or on the 87 | phone. If no one is available, any house plant will do. 88 | 89 | (The answers to the below questions aren't necessarily in the above text.) 90 | 91 | * Which of the Big Three is your favorite OS? Why? 92 | 93 | * What did the Macintosh computer run as an OS before it ran macOS/OS X? 94 | * What were some of the drawbacks to this old OS? 95 | 96 | * Why is C a popular language for writing operating systems? 97 | --------------------------------------------------------------------------------