1 Abstraction
Overview
- BIOS:Basic Input Output System
- PC(program counter) SP(stack pointer)
- CS:IP code segment : instruction pointer.
- .s source code files written in assembly
Compare of cs61c and cs162
- Processor -- Thread
- Memory -- Address Space
- Disks,SSDS,... -- Files
- Networks -- Sockets
- Machines -- Processes
Instruction Fetch/Decode/Execute
Four fundamental OS concepts
- Thread: execution context
- Address space: set of memory address accessible to program(r/o)
- Process: instance of a running program(protected address space+ >=1 threads)
- Dual Mode operation/Protection: only the "system" has the ability to access certain resources
Thread
Concept
3 states
- running
- ready – eligible to run, but not currently running
- blocked – ineligible to run (e.g.waiting for an I/O to finish)
API
- yield() — Current thread yields the CPU
- sleep() (e.g., sending to full buffer)
- wakeup() (e.g., buffer space becomes available)
Concurrency
Concurrency vs parallelism
- Concurrency is about handling multiple things at once(Networked servers must handle concurrent requests)
- Parallelism is about doing multiple things simultaneously(Parallel programs must parallelise for performance)
- Parallel => concurrent, but not the other way round!
Multiprocessing vs multithreading
- Multiprocessing: Multiple CPUs(cores)
- Multithreading: Multiple threads/processes
User/Kernel Threading Models
- One-to-one: All major operating systems: Windows, Linux (with the Native POSIX Thread Library), macOS
- Many-to-one: GNU Portable Threads (Pth)
- Many-to-many: Windows user-mode scheduling
Address Space
Base and bounds registers are visible/accessible only when processor is running in supervisor mode
Process
Thread encapsulate concurrency
Address spaces encapsulate protection
Process Management API
pthread: posix(portable operating system interface)
join(pid) - suspends execution of the calling thread until the target pid terminates
exit – terminate a process
fork – copy the current process
- : Returned to parent or caller,return value is pid of new child
- : Returned to the newly created child process
- : error creating,running in original process
exec – change the program being run by the current process
wait – pauses the parent until the child finishes,crashes, or is terminated
int main(int argc, char* argv[]){
pid_t cpid,tcpid;
cpid = fork();
int status;
pid_t mypid=getpid();
if (cpid > 0) { /* Parent Process */
tcpid = wait(&status);
printf("%d wait%d(%d)",mypid,tcpid,status);
} else if (cpid == 0) { /* Child Process */
mypid = getpid();
printf("[%d] child\n", mypid);
char *myargs[3];
myargs[0] = strdup("wc"); // program: "wc" (word count)
myargs[1] = strdup("test.cpp"); // argument: file to count
myargs[2] = NULL; // marks end of array
execvp(myargs[0], myargs); // runs word count
printf("this shouldn't print out.\n");
}
}output:
[8042] child
8041 wait8042(256)
kill – send a signal (interrupt-like notification) to another process
signal – system call to send a notification to another process
Main thread creates (forks) collection of sub-threads passing them args to work on, and then joins with them, collecting results
Internal Events--PCB
PCB is used for saving states in a context
For multi-threaded process, substitute process → thread and PCB → TCB
External Events--Interrupt
Example:web server
Dual Mode Operation
Unix structure
3 types of userkernel mode transfer
- syscall
- process requests a system service,e.g.,exit()
- like a function call, but "outside" the process
- interrupt
- external asychronous event triggers context switch
- e.g.,timer, I/O device
- trap/exception
- internal asychronous event in process triggers context switch
- e.g.,protection violation(segment fault),divide by zero