Operating Systems 2017F Lecture 2

From Soma-notes
Jump to navigation Jump to search

Video

Video from the lecture given on September 12, 2017 is now available.

Code

Code and files from the lecture (captured as they were at the end) are available here.

Notes

What is a file?

 - A hierarchical name with a value mapping, accessed using the API "open, read, close, seek" (roughly).  Open and close are used for efficiency; read and write do all of the work.

Why is Unix and Linux interchangeable?

 - Linux is a variant of the Unix, it contains the Unix kernel with custom built layers on top.

What is the architecture of the Unix kernel?

 - A system of hierarchical files that represent all of the different hardware devices
   for example: writing to stdout or reading from stdin.  These are the computer monitor and keyborad file descriptors.

How do we add new devices to the system?

 - We tell the operating system to mount the new device.  The operating system responds with a file descriptor for the new device.  Accessing the device means writing to its file.
 - There are many files that represent a device.  We can trace these files by calling the command "lspci" which gets the PCI bus hardware device info from the kernel.  Using the   
   command "strace lspci" we can trace all of the system calls performed by lspci.  Now we know exactly which files make up each device on the PCI bus. 
 - file descriptors like /dev or /sys are conduits to the kernel.  The kernel then performs operations on the hardware.

The key point(s): The kernel abstracts the hardware by assigning file descriptors for every device. A program can gain information about the device using system calls to the kernel.

Virtualizing Memory

 - Hard disks file descriptors appear as:
   - /dev/sda -> unencrypted drive
   - /dev/mapper… -> encrypted drive
 - We can see the filesystem using the "df" command or view the size of files using "df."
 - Why are some of the files showing a size of 0 bytes?
   - When memory is virtualized, the kernel builds the file system as it goes.

Processes

 - What is a Process?
   - A running program
   - Just like the devices, every process gets a directory in the files system "/proc"
   - To view running process use the command "ps"
     - To stop a running process use the command "Kill <process id>"
 - What happens when multiple processes are created?
   - The operating system shares the resources between the processes using time sharing -- also called virtualizing the resources.
 - What is a shell in Linux?
   - Just another program that can access the operating system and run other programs
   - A shell can run multiple programs (processes), only control returns to the shell after the process has finished.  To give the shell priority use the "&" to send the current process to 
     the background.  This idea demonstrates how the CPU is virtualized because it is being shared by the shell and the running process.  The concept was demonstrated using xclock.
     - Use the "jobs" command to see which processes are running
     - Use the "ps aux" command to view the full command line of all running processes.
     - Use the "getpid()" function call in a c program to find the process id for the current process.
     - Use the "getppid()" function call in a c program to find the parents process id for the current process.
 - How do I make a process?
   - by copying the current process
   - fork(): a system call the clones the currently running process.  The new process (child) begins executing from the statement that contained the call to fork().
   - fork() returns zero it means the child process is running.
   - fork() returns > 0 it means the parent process is running.
   - fork() returns < 0 it means the fork() failed.

Using fork() in a c program

/* hello-world.c */

#include <stdio.h>
#include <unistd.h>

int main() {
    fork();
    fork();

    printf("Hello world! I'm %d.  My parent is %d\n", getpid(), getppid());
    return 42;
}

What does the program do?

 - The first call to fork() clones a child process running the hello-world program.  The child begins operating as if it just called fork().  Only the value returned from fork() 
   is different for the child than the parent.  The child process returns a zero.
 - Now there are two processes running concurrently, executing the same code.  A parent process and a child process.
 - The parent process calls fork() again creating another child process, and the first child process also creates a child process.  Now there are 4 processes running concurrently.
 - All 4 processes will print their process id and their parent's process id.
 - finally a return value of 42 is delivered to the parent of each process as it exits.
 - Important: The order of execution for each process is not deterministic.  We do not know which process will run next because the operating system decides.