Professor Craton
Anything you want to know?
Can we have a computer without an operating system?
sh, tcsh, bashKDE, GNOME, Explorergcc source.c --output executable./executabletcc can run C compile and run in one step:
tcc -run source.cint *a defines pointer a*b dereferences b&c returns the address of cDisplaying the address of a value
Dereferening a pointer
#include <stdio.h>
int increment(int* value) {
*value += 1;
}
int main() {
int a = 1;
increment(&a);
printf("%d", a);
}Passing pointers
//dev, /sys, /proc,
/runcat /proc/loadavgopen, read, write,
close)open file to get a file descriptor for the open
fileread or write using the file
descriptorclose to tell the kernel we are done with the filechmod - change file mode bitschmod 775 myfile.c
chown - change file owner and groupchown user:group myfile.c
chgrp - change group ownershipchgrp group myfile.c
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
void main(int argc, char ** argv) {
if (argc != 3) exit(1);
int in = open(argv[1], O_RDONLY);
int out = open(argv[2], O_WRONLY|O_CREAT, 0644);
char buf[1024];
int bytes_read;
while (bytes_read = read(in, buf, 1024)) {
write(out, buf, bytes_read);
}
close(in);
close(out);
}int, char, floatunsigned int, long intint*, char*[1, 2, 3] (essentially just pointers)structenumprintf, fprintf,
scanf, and others/usr/bin/gcc in the shell. If the location of gcc
is in your PATH, you can simply type gcc and
the OS will resolve the full path.Compile source files into executable
> gcc src1.c src2.c --output executable
hello.c:
Preprocess, compile, assemble, and link our program to the file
hello
We now have a new executable file. We can run it in the shell as:
Compile source code into Assembler instructions:
Compile source code without linking:
push and popIf we allow C++, we can use a proper pass by reference mode.
printf in CWhere does the first instruction come from?
What if thread A is waiting for thread B and thread B is waiting for thread A?
Threads share memory, so they may interact unintentionally
This can be mitigated on some systems using virtual memory
read, write)load and storeWhat if the system goes down during a write?
Threads exist from the time their first instruction begins executing until the time of last instruction execution
Threads with overlapping lifetimes are running concurrently
A key feature of most operating systems is allow threads to run concurrently
Why is it desirable for the computer to execute multiple threads concurrently, rather than waiting for one to finish before starting another?
make -jkill -STOP [pid]kill -CONT [pid]Thread A Thread B
Instruction A1 Instruction A2 switchFromTo(A, B) Instruction B1 Instruction B2 switchFromTo(B, A) Instruction A3 Instruction A4
What does it mean to switch threads?
Consider:
Push registers to outgoing thread's stack
Store stack pointer in outgoing->sp
Load stack pointer from next->sp
Store `restore` address into outgoing->ip
Jump to next->ip
restore:
pop registers from the now resumed outgoing thread
switchFromTooutgoing = current
next = chooseNextThread()
current = next // Maintain global variable
switchFromTo(outgoing, next)
n running
threads on our n processorsfork of anotherfork system call can be called from a thread to
create a new processfork sees the process ID of the
child as a return valueWhat are some issue with cooperative multitasking?
CPUs generally execute the next instruction
Devices have the ability to interrupt this flow and redirect execution to an interrupt handler
Using timer and interrupt handlers, the OS can build pre-emptive multitasking
If not programmed carefully, threads can lead to undefined and unexpected behavior
This behavior can sometimes be exploitable by an attacker
yield()How does the OS determine which thread to switch to?
What are some weaknesses of fixed-priority scheduling?
If the threads will meet their deadlines under any fixed priority assignment, then they will do so under an assignment that prioritizes threads with shorter periods over those with longer periods.
To check that deadlines are met, it suffices to consider the worst-case situation, which is that all the threads’ periods start at the same moment.
Can this application be serviced on a single CPU using fixed-priority scheduling?
What impact does the cache hierarchy have on context switching?
How does this effect change in multiprocessor systems?
Process Execution Time Period
P1 1 8 P2 2 5 P3 4 10
nice values feed into schedulerrenice can adjust nicenesscall, ret,
syscallx86, x86_64,
ARM, and ARM64syscall instruction triggers kernelRegister Purpose
%rax System call number %rdi 1st parameter %rsi 2nd parameter %rdx 3rd parameter %r10 4th parameter %r8 5th parameter %r9 6th parameter
call and ret perform jumps and adjust
statepush and pop access stackrax register is used for return value in x64
calling conventionRegister Purpose
%rax 1st return register %rdi used to pass 1st argument to functions %rsi used to pass 2nd argument to functions %rdx used to pass 3rd argument to functions %rcx used to pass 4th argument to functions %rsp stack pointer
What if threads need to share data?
What behaviors do we need to avoid if we have shared memory?
Can the example sell more tickets than are available?
What if it is running in multiple threads?
class monitor)syncronized
keyword provides object locking at the method levelswap
to exchange a register value with memory is sufficient1 is unlocked, 0 is
locked10 with the mutex value
1 back1 backdu | sort -npthread_join, but does not require
threads to terminateWhat problems can be caused by synchronization?
Integrate the mutex wait queue with the scheduler and avoid simple FIFO behavior
Allow a high-priority thread to relock a mutex it gives up even if other threads are waiting on it
Persistent storage is typically cheaper than RAM
Virtual memory provides the tools to move rarely used memory pages to persistent storage
Valid Page Frame
1 1 1 0 0 X 0 X 0 X 0 X 1 3 0 X
mlockfork of anotherfork system call can be called from a thread to
create a new processfork sees the process ID of the
child as a return valueexec can be used to cause a process to begin running a
new programfork creates a copied processexec can be used to cause only the child to run a new
programkill can be used to send a signal to a specified
processEach process has its own virtual address space
Used by OS X, Windows, Linux, etc
Memory mappings are used to control permissions and access
> getfacl /bin/ls
# file: bin/ls
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
r - Readw - Writex - Executer - Listw - Create/rename/delete files in directoryx - Traverse a directory (access files if name already
known)rwx, r-x, and
---Hierarchical directories mapping names to objects
Indexes providing access based on contents
8.2 Storage Technology
open system callstdout, sockets, and hardware
devicesmmapread and write can be used to copy file
sections to memorypread and pwrite work similarly, but
require a positionread and write can be used to copy data
sequentiallyread and write update a stored position
in a file
lseek can be used to adjust this stored position
> dd if=/dev/zero of=sparsefile count=0 bs=4k seek=1000000000
> ls -s sparsefile
0 -rw-rw-r-- 1 jncraton 3.8T Apr 6 09:11 sparsefile
File size
Modified, written, accessed times
Count of names referencing this file
Commonly used for directory storage
Used by NTFS (Microsoft), HFS (Apple), and XFS (open source)