Download presentation
Presentation is loading. Please wait.
1
CS/COE 0449 (term 2174) Jarrett Billingsley
OS – POSIX System Calls CS/COE 0449 (term 2174) Jarrett Billingsley
2
Class announcements Project 2 due tomorrow by midnight!
How many of you did program 2? Are you sure your password is right? Maybe you should check to be sure. Don’t forget, you can resubmit (_1, _2 etc.) So for project 3 (the next one, over spring break)… Due date of the 13th is too soon. So would you rather have it due… The same day as the exam (3/16)? Or the day after the exam (3/17)? Yeah I thought so 2/28/2017 CS/COE term 2174
3
System Calls, continued
2/28/2017 CS/COE term 2174
4
Language Runtime (libc, msvcrt, JRE)
Layer cake User-space applications usually look like this: These layers in the middle do a good bit of work trying to make as few system calls as possible. Remember stdio buffering? The public OS API can do some fun user-space stuff itself, but is mostly a bunch of wrapper functions: functions which perform the system call in a platform-independent way. User Space Kernel Program code Language Runtime (libc, msvcrt, JRE) Public OS API (POSIX, Win32) OS Kernel Syscall Handler 2/28/2017 CS/COE term 2174
5
The POSIX API 2/28/2017 CS/COE term 2174
6
The POSIX API POSIX stands for Portable Operating System Interface.
(the X just looks cool.) POSIX was defined as a universal API for all the flavors of UNIX, which Linux (mostly) implements as well. This is why it was needed. 2/28/2017 CS/COE term 2174
7
Everything’s a file! One of the most UNIX-y concepts that POSIX inherits is that pretty much every computer/OS resource is represented as a file. Hard drives? Files. Displays? Files. Keyboards? Files. Processes? Files. Files? Files. Let’s look at /proc, /dev, and /sys! 2/28/2017 CS/COE term 2174
8
The big unifying concept
Everything on the system is represented in this big tree. The root of this tree is is /, which is called… the root directory. The internal nodes are directories, and the leaves are files. Directory names always end in a /. We can also have symbolic links. These are “fake” files which serve as an alias for another file or directory. For example, when we access /bin/sh on thoth, it’s really accessing /bin/bash. / dev/ bin/ usr/ sh bash tar 2/28/2017 CS/COE term 2174
9
Accessing the “file” system
Unsurprisingly – considering C and UNIX’s relationship – the POSIX system calls for accessing files look a lot like C’s. int open(const char* filename, int flags); int read(int fd, void* buffer, size_t size); int write(int fd, const void* buffer, size_t size); int close(int fd); Things to note: There is no mention of FILE*. FILE* is an abstraction that the C stdio library gives us on top of these functions. Instead, open returns an int, and read/write/close all take an int to specify which file to operate on. This is called a file descriptor, but it’s just a number. 2/28/2017 CS/COE term 2174
10
Watching it go We can see all the system calls a program makes as it runs using the strace command. Let’s strace a simple “hello world” program. execve is the call the actually runs the program. (sorta.) brk is one way to dynamically allocate heap memory. Here it seems to be using it to ask where the heap begins. mmap (memory map) is a more modern way to dynamically allocate memory, and can also be used to read/write files. It’s part of the virtual memory system. mprotect lets you change the access rights on memory areas. munmap gives the mapped memory pages back to the OS. fstat gets information about an open file. 2/28/2017 CS/COE term 2174
11
The standard input/output/error files
Every process on a POSIX system is given these three files when it starts. By default, stdin is hooked up to the keyboard and stdout/err are hooked up to the terminal display. But you can change that! On the shell, you can redirect stdout to a file like so: ./command > output stderr can be redirected like this (notice the 2!): ./command 2> output You can output BOTH to the same file like this: ./command &> output And you can redirect stdin like this: ./command < file_to_read Name Descriptor stdin stdout 1 stderr 2 POSIX calls close(); dup() dup2() 2/28/2017 CS/COE term 2174
12
wordswordswordswordswordswordswordswordswordswords
World 7 You can even chain programs together with the pipe | character. This is Shift+\ on American English keyboards. Let’s try ps aux | less Here’s what’s happening: pipe() stdout stdin ps aux less wordswordswordswordswordswordswordswordswordswords 2/28/2017 CS/COE term 2174
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.