01 Introduction
01 Introduction
Chris Kauffman
Last Updated:
Wed Jan 20 03:58:53 PM CST 2021
1
CSCI 4061 Lec 001 - Logistics
2
Logistics Issues
Course Materials
https://www.cs.umn.edu/~kauffman/4061/schedule
▶ Will usually post slides prior to meetings
▶ Open today’s slides so you can see the following questions
▶ As students go into Breakout rooms, won’t be able to see MY
video feed: examine the slides yourself
Breakout Rooms
▶ During meetings, will use Zoom breakout rooms for
discussions
▶ Breakout Rooms will be semi-permanent: folks you meet
today are your discussion teammates, get to know them
▶ During discussions, write down your answers so you can
share your screen later
▶ Designate a team member that has this capability (e.g.
computer with video sharing)
3
Exercise: OS Ice Breaker
Write your answers in a text file Korra
so 1-2 teams can screen-share - I use OS K most of the time
- I like that it has a fancy GUI but
their answers HATE updates are always installing
- I can bend anything
1. Who is writing answers
down so they can share? Mako
- FireOS on my tablet!
2. What Operating System(s) - It’s runs fast but also hot, literally
do you use frequently? - Sometimes I think I brood too much
5
Answers: Responsibilities of the OS?
▶ Interact with hardware like CPU, hard drives, mice, monitors,
DRAM, network interface card etc. is tedious / difficult
▶ Electrical signals / voltage changes / timing / protocols
▶ Some hardware follows standard protocols, others are special
6
Answers: Responsibilities of the OS?
OSs play many parts but primarily they glue Hardware and
Software together
Create a "virtual machine" on top of hardware
▶ OS creates an abstraction layer over hardware
▶ Similar programming interface regardless of underlying
hardware environment:
▶ Phones, Laptops, Cars, Planes, Nuclear Reactors
▶ all see Processes, Memory, Files, Network
9
The Unix "Virtual" Machine
Unix Kernel provides basic facilities to manage its high level
abstractions of hardware, translate to actual hardware
▶ Link: Interactive Map of the Linux Kernel
▶ Examples Below
Processes: Executing Code File System: Storage / Devices
▶ Create new processes ▶ Create / Destroy Files
▶ Status of other processes ▶ read() / write()
▶ Pause until events occur ▶ Special files for
▶ Create/Manage threads communication, system
within process manipulation
Process Communication Networking
▶ Messages between processes ▶ Open sockets which connect
▶ Share memory / resources to other machines
▶ Coordinate resource use ▶ send()/recv() data over
connections
10
Outsides vs Insides of the OS
▶ Operating Systems are CSCI 4061
layered like everything else ▶ Systems Programming
in computer science
▶ Use functionality provided
▶ 4061: outer layer
by kernel
▶ 5103: inner layers
▶ Gain some knowledge of
▶ EE Degree: bottom layer internals but focus on
external practicalities
CSCI 5103
▶ Creation of a kernel / OS
internals
▶ Theory and practice of
writing / improving
operating systems
▶ Implement system calls
11
System Calls : The OS’s Privilege
▶ User programs will never actually read data from a file
▶ Instead, will make a request to the OS to read data from a file
▶ Usually done with a C function like in
int nbytes_read = read(file_des, in_buf, max_bytes);
▶ After a little setup, OS takes over
▶ Elevates the CPU’s privilege level to allow access to resources
not normally accessible using assembly instructions
▶ Modern CPUs have security models with normal / super status
▶ Like sudo make me a sandwhich for hardware
▶ At completion of read() CPU drops back to normal level
▶ User program now has stuff in in_buf or an error to deal with
▶ Same framework for process creation, communication, I/O,
memory management, etc.: make a system call to request an
OS service
12
Details of System Calls
1 ## 32-bit write linux system call in assembly (hello32.s)
2 _start:
3 movl $4, %eax # system call number for write: 4,
4 movl $1, %ebx # first arg: file descriptor, stdout = 1
5 movl $msg,%ecx # second arg: address of message to write
6 movl $13, %edx # third arg: message length, 13 bytes
7 int $0x80 # interrupt to call kernel
8 # write(1, message, 13) // equivalent C call in hello.c
9
10 ## 64-bit write linux system call in assembly (hello64.s)
11 _start:
12 movq $1, %rax # system call number for write: 1
13 movq $1, %rdi # first arg: file descriptor, stdout = 1
14 movq $msg,%rsi # second arg: address of message to write
15 movq $13, %rdx # third arg: message length, 13 bytes
16 syscall # make a system call, x86-64 convention
17 # write(1, message, 13) // equivalent C call in hello.c
Call x86_64 i386 ▶ Linux has ~300+ system
read rax = 0 eax = 3
calls provided by the kernel
write 1 4
open 2 5 ▶ C/Assembly calls for each
close 3 6
stat 4 106 Why system calls like this?
fork 57 2
Write down answers with team 13
Answers: Why System Calls like this? 1 / 2
Broad Reason 1: Control + Safety
▶ System call allows OS to control access to shared/sensitive
resources
▶ If user programs could directly access/modify such resources,
bad stuff can happen such as. . .
▶ Read other users’ files and process memory (security)
▶ Steal CPU / memory / disk space from other users (resource
management)
▶ Mess up hardware like printers or network by sending them bad
data, screw up OS by clobbering critical files/memory (safety /
stability)
▶ Shut down a machine terminating other user programs
(fairness)
▶ The OS layer enforces discipline for the above
▶ Notice some properties pertain to any system while others are
relevant to shared computer systems
14
Answers: Why System Calls like this? 2 / 2
15
Distinction of Application vs Systems Programming
16
General Topics Associated with Systems Programming
Concurrency Multiple things can happen, order is unpredictable
Asynchrony An event can happen at any point
Coordination Multiple parties must avoid deadlock / starvation
Communication Between close entities (threads/processes) or
distant entities (network connection)
Security Access to info is restricted
File Storage Layout of data on permanent devices, algorithms for
efficient read/write, buffering
Memory Maintain illusion of a massive hunk of RAM for each
process (pages, virtual memory)
Robustness Handle unexpected events gracefully
Efficiency Use CPU, Memory, Disk to their fullest potential as
other programs are built from here
In our projects, we’ll hit on most of these.
17
Assumption: You know some C
▶ CSCI 2021 is a prereq, covers some hardware, basic C
programming and interaction with hardware
▶ Assume that you know C syntax, basic semantics
▶ Why C vs other languages?
19
Answers: Recall these C things
20
Exercise: Actual C Code
#include <stdio.h>
#include <stdlib.h>
int main(){
long n = 1;
void *mem = NULL;
while( (mem = malloc(n)) != NULL){
printf("%12ld bytes: Success\n",n);
free(mem); 1. Describe at a high level what
n *= 2; this C program does
}
printf("%12ld bytes: Fail\n",n); 2. Explain the line
n /= 2; while( (mem = malloc(n)) != NULL){
long kb = n / 1024; in some detail
long mb = kb / 1024;
long gb = mb / 1024; 3. What kind of output would you
expect on your own computer?
printf("\n");
printf("%12ld b limit\n",n);
printf("%12ld KB limit\n",kb);
printf("%12ld MB limit\n",mb);
printf("%12ld GB limit\n",gb);
return 0;
}
21
Answers: Actual C Code
1 // max_memory.c: test the total memory available in a single malloc by
2 // repeatedly increasing the limit of the request
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 int main(){
8 long n = 1; // int cannot hold large enough numbes
9 void *mem = NULL; // Pointer to memory
10 while( (mem = malloc(n)) != NULL){ // allocate and check result
11 printf("%12ld bytes: Success\n",n); // %ld to print a long, %d for int
12 free(mem); // free last allocation
13 n *= 2; // double size of next request
14 } //
15 printf("%12ld bytes: Fail\n",n); // failed last allocation, no need to free
16 n /= 2; // back up one step for max success
17
18 long kb = n / 1024; // sizes of allocations
19 long mb = kb / 1024;
20 long gb = mb / 1024;
21
22 printf("\n");
23 printf("%12ld b limit\n",n); // Output human readable sizes
24 printf("%12ld KB limit\n",kb);
25 printf("%12ld MB limit\n",mb);
26 printf("%12ld GB limit\n",gb);
27 return 0; // return 0 to indicate succesful completion
28 }
22
Exercise: C Program with Input ▶ What data structure
typedef struct int_node_struct {
int data; is being used?
struct int_node_struct *next;
} int_node; ▶ Are there any global
int_node* head = NULL; variables?
int main(int argc, char **argv){ ▶ What’s going on here:
int x;
FILE *input = fopen(argv[1], "r"); new->data = x;
while(fscanf(input,"%d",&x) != EOF){ new->next = head;
int_node *new = malloc(sizeof(int_node));
new->data = x; ▶ Where do input
new->next = head;
head = new;
numbers come from?
} ▶ In what order will
int_node *ptr = head;
int i=0; input numbers be
printf("\nEntire list\n"); printed back?
while(ptr != NULL){
printf("list(%d) = %d\n",i,ptr->data); ▶ Does the program
ptr = ptr->next; have a memory leak?
i++;
} (What is a memory
fclose(input); leak?)
return 0;
} 23
Answers: C Program with Input
1 // read_all_numbers_file.c: simple demonstration of reading input from
2 // a file using a linked list with dynamic memory allocation.
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 typedef struct int_node_struct { // Definition of a node
7 int data; // integer data
8 struct int_node_struct *next; // link to another node
9 } int_node;
10
11 int_node* head = NULL; // global variable, front of list
12
13 int main(int argc, char **argv){
14 int x;
15 FILE *input = fopen(argv[1], "r"); // open input file named on command line
16 while(fscanf(input,"%d",&x) != EOF){ // read a number, check for end of input
17 int_node *new = malloc(sizeof(int_node)); // allocate space for a node
18 new->data = x; // set data, -> derefernces and sets
19 new->next = head; // point at previous front of list
20 head = new; // make this node the new front
21 }
22 int_node *ptr = head; // prepare to iterate through list
23 int i=0;
24 printf("\nEntire list\n");
25 while(ptr != NULL){ // iterate until out of nodes
26 printf("list(%d) = %d\n",i,ptr->data); // print data for one node
27 ptr = ptr->next; // move pointer forward one node
28 i++;
29 }
30 fclose(input); // close the input file
31 return 0; // Should free list but program is ending
32 } // so memory will automatically return to system
24
Slides, Code, Videos On the Course Site
25
Course Mechanics
26