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 compile and run C 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
Dereferencing 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?
top can be used to show an interactive list of running
processes
dmesgWhat if thread A is waiting for thread B and thread B is waiting for thread A?
read, write)load and storeWhat if the system goes down during a write?
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]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 issues with cooperative multitasking?
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 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 kernel| Register | 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 convention| Register | 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)synchronized
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?
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
processptable to find an entry
where the state is P_FREEkalloc_pagetablevmiterforkreg_rax to 0P_FREE to
P_RUNNABLE> 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
---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 filelseek 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