Week 11 Day 2 - Syscalls, Interrupts and Memory Management

Channel:
Subscribers:
2,640
Published on ● Video Link: https://www.youtube.com/watch?v=0HJktfdjNXs



Duration: 2:00:20
108 views
1


Today we talked about syscalls, interrupts and related concepts like function pointers and callbacks.


System calls are the basic interface that the OS provides your program. If you can find a good reference for it (which can be surprisingly tricky) you can see each and every basic operation your program could do with the Linux kernel, from creating directories to making pipes to opening and closing file descriptors. Your next homework will have you doing no C or C++ at all, just assembly with syscalls for I/O. It's a pain to do so, as you'll see, which is why I typically tie assembly and C or C++ together.



An interrupt can take place in a variety of ways, from clicking a mouse to hitting ctrl-C on the keyboard to doing a SWI 0 command in ARM 32 assembly.


When an interrupt happens, it invokes the interrupt handler in the operating system (if multiple interrupts happen, there is a priority system to determine who gets to preempt who), the OS then looks up what number the interrupt is, and calls the function in something called a "jump table" which is an array of functions to call in response to interrupts. It can do this very quickly, with just one level of indirection needed.


We then went through what a function pointer looks like (it has a really weird looking syntax), and what a function pointer is: it is a variable that holds the memory address of a function, which is to say, it's just an int like everything else. When you call a function with a function pointer, it just does a BL to that memory address.


Function pointers are used in a lot of different ways, such as the third parameter to std::sort() which is used to determine how to sort a vector or whatever, or as a mouse handler so that every time you click on something it calls the function, or you can pass a function pointer to signal() to tell the OS what function to call when that signal takes place. There's lots of signals like SIGINT (when you hit ctrl-c), SIGSTOP (when you hit ctrl-s to pause the program, hit ctrl-q to resume), SIGHUP (when you close PuTTY by accident with the program running), and everyone's favorite, SIGSEGV - the segfault signal.


If you don't have a handler registered, it uses the default one, which for things like SIGSEGV will simply kill your program. However, there's all sorts of neat tricks you can do to speed up copying, such as the COW trick (copy on write) trick in which you "duplicate" something but don't actually duplicate it, instead marking the region of memory read-only. Then when you try writing to it, you catch the signal, detect what page of memory was trying to be written to, duplicating the page then, and then doing the write.


This trick is used by fork() to avoid needlessly copying all variables a program has, especially since exec() is going to happen right afterwards a lot of the time, and it'll all get unloaded anyway.







Tags:
csci 45
copy on write
syscalls
interrupts
jump table
function pointers
signals