Professor Craton
Anything you want to know?
Can we have a computer without an operating system?
sh
, tcsh
, bash
KDE
, GNOME
, Explorer
gcc source.c --output executable
./executable
tcc
can run C compile and run in one step:
tcc -run source.c
int *a
defines pointer a
*b
dereferences b
&c
returns the address of c
Displaying 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
,
/run
cat /proc/loadavg
open
, 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
, float
unsigned int
, long int
int*
, char*
[1, 2, 3]
(essentially just pointers)struct
enum
printf
, 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 pop
If 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?
read
, write
)load
and store
What 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 -j
kill -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
switchFromTo
outgoing = 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?
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
,
syscall
x86
, x86_64
,
ARM
, and ARM64
syscall
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
locked1
0
with the mutex value
1
back1
backdu | sort -n
pthread_join
, but does not require
threads to terminateWhat problems can be caused by synchronization?
Valid | Page Frame |
---|---|
1 | 1 |
1 | 0 |
0 | X |
0 | X |
0 | X |
0 | X |
1 | 3 |
0 | X |
mlock
fork
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
process> 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
devicesmmap
read
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