COMP 3000 Operating Systems
Facilities for Users/Programmers (part 2)
Lianying Zhao
Controlling Running Programs
• All processes have a parent process (except init) • As a result of a fork()-like system call
• The PPID of a process
• Hence forming the process tree
• The wait() System Call
• A process must be waited on (or it enters a zombie state after it exits)
• A zombie process can’t be killed
• Where the return code (exit status) is passed
COMP 3000 (Winter 2021) 2
Controlling Running Programs: Signals
• The user may want to notify one program of something • TheOSmay…
• Another program may …
• Signals: a limited form of IPC (inter-process communication)
• Asynchronous
• Predefined: e.g., SIGINT (interrupt, Ctrl-C), SIGCHLD (child process terminates)
• No new one can be defined. Use SIGUSR1 and SIGUSR2 for custom purposes
• An OS artifact
• Signals are again a POSIX thing. MS Windows: Messages • WM_CLOSE, WM_CREATE, etc.
COMP 3000 (Winter 2021) 3
Signal Handling
• The OS interrupts the signalled process, and calls the handler function
• All processes are listening to all signals
• Signal handlers are defined by the process (except for a few signals) • Just regular functions
• The C runtime (e.g., libc.so) registers default signal handlers
• Specified with sigaction() (or signal(), not recommended)
• Concurrency
• Can be invoked at any time by the OS kernel
• What if the handler function modifies shared data • Do as little as possible
COMP 3000 (Winter 2021) 4
RE: Tutorial 2
• What if the signal handler is triggered during a system call? • System call aborts and returns an error
• Signal handler waits until system call is finished
• System call is paused, signal handler runs, and then system call is resumed • SA_RESTART
COMP 3000 (Winter 2021) 5
Ways to send input and receive output from programs:
Computer <-> programs vs.
Human <-> computer
1. Command line arguments
2. Standard I/O
3. Files
4. Network
5. Combination (cf. IPC)
Pipeline and redirections
• Pipe: unidirectional data channel
• Command-1 | Command-2 | …| Command-N
(shell pipeline)
• We will leave programming with pipes to later discussions, e.g., pipe()
• | stdout -> stdin
• |& stdout+stderr -> stdin (shorthand for 2>&1 |)
• Redirecting: stdin, stdout or stderr • >,<,>>
• All these are only possible with the separation of exec() from fork()
COMP 3000 (Winter 2021) 6
Command Arguments and Options
• What’s passed to the program
int main(int argc, char *argv[]) {
• argc: number of arguments
• argv: the array of arguments, each being a string
• The first one (argv[0]) is always the command name (or… should be) • Subsequent ones are space-delimited strings
• Note: these are determined by the shell
• Example: ls -lais • Conventions
COMP 3000 (Winter 2021) 7
The File Abstraction and File Systems
• Recap:
• File: A linear array of bytes, stored persistently*
Ways of organizing data
• File system: Ways of organizing files
• The user’s perspective of files:
• Identifier: filename
• Further identified by: path + filename • Can be read from or written to
• Meaning of a filename
COMP 3000 (Winter 2021) 8
More on Pathnames
• Paths are hierarchical, and there is a root (“/”) • Absolute pathname
• E.g.,/home/student/comp3000
• Relative pathname
• (Current) working directory (CWD): per process • E.g.,tut1/abc
COMP 3000 (Winter 2021) 9
Operations on Files (POSIX)
• Create
• E.g., int fd = open(“/path/to/dir”,
O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR | S_IWUSR);
• Open
• For existing files, still a file descriptor (fd) is needed for any operations
• Read/Write/Close
• Seek: there exists a current location
• E.g.,lseek()
• There’s something “out-of-band” • E.g.,ioctl()
COMP 3000 (Winter 2021) 10
File Systems (Implementation)
• File system types are determined by purposes • Optical discs, Flash memory, Union/overlay, etc.
• Not just a choice: various implications • exFAT
• E.g., FAT32 has a file size limit of 4GB
• Special file systems (thanks to VFS) • devfs, udev
• configfs • sysfs
• procfs
• tmpfs
COMP 3000 (Winter 2021) 11
Mounting File Systems
• To be connected to the uniform file-system tree • Mount point – an existing directory
• Pathnames become relative
• Flexibility
COMP 3000 (Winter 2021) 12
procfs (/proc)
• Originally proposed in an academic paper in 1984
• As its name implies: “each member of which, /proc/nnnnn, corresponds to the
address space of the running process whose pid is nnnnn.”
• Gradually extended to a wide range of information about the system, e.g., • /proc/cpuinfo
• /proc/filesystems
• /proc/version
• But /proc/sys belongs to sysctl, to configure the kernel at run-time
COMP 3000 (Winter 2021) 13
sysfs (/sys)
• A way to interact with: kernel subsystems, hardware devices, and associated device drivers
• Exposing the kobject structures internally to kernel code and files externally to user space
• sysfs_create_file() to create entries
COMP 3000 (Winter 2021) 14