Section 3 Syscalls, I/O, Signals February 3rd, 2017 Taught by Josh Don
Signals Signal: An asynchronous notification sent to a process to notify it that an event has occurred When a signal is sent, the OS interrupts the process’ normal flow of execution to deliver the signal Ex: Ctrl-C sends SIGINT
List of Signals
Run default signal handler for SIGINT! Signals Run default signal handler for SIGINT! Process A Process B SIGINT
func can be a custom function, or it can be: SIG_DFL: restore default Signal redirection Process B Process A SIGINT Run ! func signal(SIGINT, ) func func can be a custom function, or it can be: SIG_DFL: restore default SIG_IGN: ignore signal
Demo
Files vs File Descriptors File: A computer resource that allows a program to store information durably (persists even after the program finishes executing). File Descriptor (FD): An integer value that is used to reference a particular file. A process gets a file descriptor when it OPENs a file. FILE: A handy wrapper around a file descriptor, adding buffering and other features Get an FD → int open(char *filename,…) Get a FILE → FILE *fopen(char *filename,…) Get FD from FILE → int fileno(FILE *stream);
File descriptors When a process starts, it get FDs 0-2 opened Kernel File Table stdin stdout Process File Table stderr FD a.txt When a process starts, it get FDs 0-2 opened b.txt 1 2
File descriptors cont. >> open(“b.txt”, O_CREAT|O_WR) Kernel File Table stdin Process File Table stdout >> open(“b.txt”, O_CREAT|O_WR) stderr FD a.txt b.txt 1 2 3
dup The dup(int oldfd) system call creates a copy of the file descriptor oldfd, using the lowest-numbered unused file descriptor for the new descriptor.
Now we have another reference to stdout dup example Kernel File Table stdin >> dup(1) Now we have another reference to stdout Process File Table stdout stderr FD a.txt b.txt 1 2 3 4
dup2 The dup2(int oldfd, int newfd) system call performs the same task as dup(), but instead of using the lowest-numbered unused file descriptor, it uses the file descriptor number specified in newfd. If the file descriptor newfd was previously open, it is silently closed before being reused.
>> dup2(3, 1) dup2 example Kernel File Table stdin stdout Process File Table stderr FD a.txt b.txt 1 2 3 4