What are the most important things an Operating System Does ?

The book divides the duties of an OS into 3 broad categories, That's why it's called "Three Easy Pieces". Over this semester we will study each piece, One at time.

The THREE pieces ...

  • virtualization
  • concurrency
  • persistence

If you don't know what these are now, that is completely ok, you will know by the time we finish the course. When you are studying and coding projects it is important to keep in mind which area we are working on and what problem we are trying solve.

Crux of the problem

Each of the three main topics cover a series of sub problems that are solved by the techniques and algorithms, abstractions are created using policies and mechanisms that we will learn in this course.

Systems work over a timeline

Understanding the timeline of the subsystems is crucial to understanding operating systems in general. Ask yourself what steps happen and when, in reaction to an process or memory event occuring in ?

Abstractions, Mechanisms and Policies

The Operating system provides abstractions, for example the idea of a running process is an abstraction. To implement these abstractions an OS uses the concepts of Policies and Mechanisms. This course is about learning what abstractions the OS provides as well as what policies and mechanisms an os may use to implement the abstraction.

Your responabilities

  • attend the lectures and take notes. In each lecture I will introduce you to some material you should take notes.
  • read the relevant chapters from the book that I assign these will be related to my lectures
  • DO THE HOMEWORK PROJECTS, DO NOT CHEAT by using chat gpt to write it for you. this is where you solidfiy your knowledge.
  • By the end of this course you should be comfortable writing system code in c for UNIX systems.
  • https://github.com/remzi-arpacidusseau/ostep-projects

What do computers do?

If you break down a computer to it's fundamental operations it is simple and clear. A computer is like a good soldier. It goes and gets an instruction, it then reads the instruction, and then does what the instruction tells it to do. This is called the fetch, decode, and execute cycle. Lets play the fetch decode and execute game. who wants to be a cpu? come up to the front of class, grab an instruction off my desk, read the instruction, and then do what the instruction tells you to do. Do this for 3 instructions. On modern computers this happens billions of times per second. A computer program is just the set of instructions that the cpu operates on. A compiler takes the code you write and turns it into a set of instructions for the cpu.

If that's all that is going on why do we need an OS?

Back in the early days of computing, we didn't have operating systems. We had sysops. A sysop was a person who was in charge of operating the computer. When a programmer wanted to run their program on a computer they would write their program on punch cards. Each punch card would hold one instruction. The programmer would then hand the set of punch cards over to a the sysop who would load them into a tray. The sysop would put the computer into a mode that would suck in each card,read it and place the instruction on the card into memory locations. The cpu would then diligently,like a good soldier, go thru the fectch, decode and execute cycle for each instruction in memory. If there was output it would write the output to a screen or onto a printer. If it needed to write something to tape, it would write it to tape. This was called batch processing.

What if another programmer wanted to run a program ?

If another programmer wanted to run code, they would hand their punch cards(set of instructions) over to the sysop and wait. When the sysop noticed that the computer was free, that is done with batch processing They would load the next program into the tray and start the cycle over again.

Why do we need operating systems ?

As you can see from our little story, Computers were only able to run one program at a time. This was a big big problem because computers were very expensive to purchase and mantain. So any downtime on the machine was considered wasteful and a loss of valuable resources.

Operating Systems to the rescue!!!

The number one problem the OS is solving is allowing many programs to (APPARENTLY hmmm...?) use the hardware resources of the computer simultaneously. The hardware resources being the

  • cpu
  • memory
  • harddisk
  • screen or printer
  • network device.

This course is a study of "HOW" an OS achieves it's goals.

I will repeat the goal of the OS.

AN OS IS A PIECE OF SOFTWARE THAT ALLOWS MANY PROGRAMS TO USE A COMPUTER AND IT'S HARDWARE RESOURCES, IN WHAT *APPEARS* TO BE, SIMULTANEOUSLY. IN ADDITION THE GOAL OF THE OS IS TO MAKES IT EASY FOR PROGRAMS TO INTERACT WITH THE HARDWARE AND NOT GET IN EACH OTHERS WAY. IT MAKES THE COMPUTER EASY TO USE. I will repeat the goal of this class. We already know *WHY* and OS exists. The goal of this class is to understand how an OS accomplishes it's mission.

How in the world does an OS do this?

This class is all about answering this question. The numero uno method an OS uses to accomplish it's purpose, is VIRTUALIZATION, ahhhhhh (give me some ahhhh music).

ummm.... what is virtualization? huh?

What is virtual reality? what does virtual mean? Are we living in a simulation.... ? Virualization means (drum roll please... ) "MAKE BELIEVE". (laugh track) yes like playing... make believe (oohhhhhh)

Did you say make believe ?

YES!! An OS is a piece of software running on a computer that lies.. Lies.. LIES... to all the other programs that want to run. It makes them believe they have the whole computer to themselves. It makes them think that they have all the memory, complete control of the cpu and can write to the screen anytime they want. It makes them think the keyboard and mouse are just for them. and it does it's job so well, it is super efficient, nobody knows(but us) that it is faking it the whole time. It's lies lies lies. and this is called VIRTUALIZATION.

It's all... "make believe"

again please, what is Virtualization ?

it's all lies made up by the OS. The OS virutalizes(that is creates a fake version) of the processor, the memory, (as wellas other resources). It provides what are known as SYSTEM CALLS (100's), these are a way for program to interact with the OS (BUT THEY THEY THINK THEY ARE INTERACTING WITH THE HARDWARE. The os virtualizes all the hardware. It pretends it is the hardware and then programs talk to the operating system thru system calls, the OS then does what the program asks. BUT.. it does so on it's own terms. The OS has policies that if follows to decide how to manage the resources for all the programs wanting to use them. How to schedule and how to allocate them, etc.. It makes it seem to the programs and more importantly to the user that many programs are using the resources simultaneously, but they not... We will learn how an OS does this in the remainder of the course.

CPU virtualaztion

One piece of Hardware that the OS virtualizes is the CPU. This makes it easy for a user to run many programs, and to the user it seems like they are running simultaneously.

CPU virutalization demo

We demonstrate this concept by writing, compiling, linking and running a program called Spin. All Spin does is print, the argument you pass it, to stdout every 1 second. We run the program by calling it from the shell

	    

			#include "stdio.h"
			#include "stdlib.h"
			#include  "time.h"

			void spin(int tm)
			{
			   time_t start = time(NULL);
			   while ((time(NULL) - start) < tm)
			   ;
			}

			
			int main (int argc, char **argv)
			{
			     if (argc != 2)
			     {
			        fprintf(stderr, "usage: cpu 'string'\n");
				exit(1);
			     }
			
			     char *str = argv[1];
			     while (1)
			     {
			          spin (1);
			          printf("%s\n", str);
			     }
			
	                     return 0;
			 }
	    
	  

Run Spin

run Spin and observe the ouput


			$ Spin  "A"

			$ Spin "A" & Spin " B" & Spin "  C" & Spin "    D" &

			the illusion of multiple
			processes running simultaneously
			
	  

How does the OS decide which program to run and when?

If multiple programs make requests to run simultaneously, how does the OS decide which program to run and when ?

The OS follows certain POLICIES to make these decision. They use POLICIES to make decisions and MECHANISMS to implement those policies. We will be learning about what are potential POLICIES and MECHANISMS that an OS can use an how they are implemented.

Memory Virtualization

Our model of physical memory is very simple. It is a sequenc of bytes arranged in linear fashion and addressed from 0 to x - 1, where x is the total number of bytes we have. We can write and read from memory. We write by specifing the address of the byte we want to write to and the value we want to write there. We read by just reading what is at any given address.

Program Memory

When a program is running, it keeps all it's data in memory. But not only that, all of it's instructions are also stored in memory. Every single fetch at the very least triggers a memory access to read the next instruction from memory.

Memory Virtualization demo

	    

			  #include unistd.h
			  #include stdio.h
			  #include stdlib.h
			  #include common.h
			  
			  int main(int argc, char *argv[])
			  {
			      int *p = malloc(sizeof(int)); // a1
			      assert(p != NULL);

			      printf("(%d) address pointed to by p: %p\n",
			                           getpid(), p); // a2

			      *p = 0; // a3

			      while (1)
			      {
				        Spin(1);
				        *p = *p + 1;
				        printf("(%d) p: %d\n", getpid(), *p); // a4
			      }
			  
			      return 0;
			   }
	    
	  

Address Space

When a program runs. The OS assigns a portion of physical memory to the program. The addresses that the program see's as it's own are not the same as the actual addresses of the physical memory. The virtual address space of the running program is mapped onto the physical address space of the actual memory. Two running programs may share the same virtual addresses, ie.. they may think that they own addresses 200 to 300, but be mapped onto different locations in actual physical memory. Each running program has what is known as it's address space or VIRTUAL ADDRESS SPACE

3 themes of our semester

  • Virualization

    We have began our preliminary discussion of Virtualization and will dig in further to understand what the policies and mechanisms are and how they are implemented for virtualization

  • Concurrency

    is the set of problems that arise when we context switch from program to program very quickly.

  • Persistence-"The File System"

    How an OS implements and manages a Long term Hard disk storage solution.

Design Goals for our OS

  • Make the Computer easy to use
  • Provide high performance
  • Minimize overhead
    • minimize extra instructions
    • minimize extra memory
  • Provide protection and security through isolation
  • reliabilty

C Programming

Operating Systems for the most part are implemented in C.

We are going to be doing all our projects in C.

C is a high level language, but low enough that we should know what instruction our assembly is compiling to.

That means we know what is actually happening on the computer

Some C basics you should already know.

What will the value of y be? why?

	    
		void inc(int x)
		{
		   x = x + 1;
		}

		int y = 10;
		inc(y);
		printf("%d", y); ???????????

	    
	  

Stack Frame

What is getting changed in the inc function?

There is some memory that is called a stack. When you call function, c creates a stack frame. All the local variables of the function, including the paramaters passed to the function are COPIED into the stack frame

The function just works on the variables that are on the stack frame

When the function is done the stack frame get removed from the stack.

Stack frame image

Parts of Memory You should know

You should already be familiar with the HEAP, the STACK, and the CODE SECTION of memory for a running program

Homework Projects

All projects need to pass all the required code tests

All project code should compile without errors

The projects are a fundamental part of this course. Projects always require a significant amount of time because we grade them only on success -- if your project doesn't work, you won't get credit for trying (sorry!). Do not procrastinate! It is likely they will take longer than you expect. Do not wait until the day before the assignment is due to start. These assignments should be started pretty much when they are handed out. All information necessary to complete the assignments will be available from the class web page.

project link