Lecture 02

advertisement
Lecture 02
Outline
• Basic UNIX Concepts
• Processes
• System Calls
1
July 24, 2016
Operating System (definition)
• “Operating System” has two meanings:
1. The entire package of central software that manages
resources and software for the command line, GUI windows,
files, editors, etc.
1. Central software that manages / allocates resources (CPU,
RAM, disks, devices, etc.)
• Referred to as “kernel”
2
July 24, 2016
Kernel
• Simplifies writing / using programs
• Manages limited resources
• Called vmunix on modern Linux
• Located in /boot/vmlinux
3
July 24, 2016
Kernel Tasks
•
•
•
•
•
•
•
Process scheduling
Memory management
Filesystem management
Process management
Device access
Networking
System call API
4
July 24, 2016
Process Scheduling
• Computer may have multiple CPUs
• Linux is preemptive multitasking
• preemptive- OS decides which process gets executed and in
what order
• multitasking- multiple programs can execute simultaneously
5
July 24, 2016
Memory Management
•
•
•
•
RAM is a limited resource
Linux uses virtual memory management
Processes are isolated from each other and kernel
Only part of a process needs to be kept in physical memory
• Allows more efficient memory utilization
6
July 24, 2016
Filesystem Management
• Kernel provides filesystem on disk
• Files created, deleted, updated, and retrieved
7
July 24, 2016
Process Management
•
•
•
•
•
Process – instance of a running program
Creation / termination of processes
Kernel loads new programs into memory
Kernel provides access to resources
When process completes, kernel frees resources
8
July 24, 2016
Device Access
•
•
•
•
Input / output
Mice, monitors, keyboards, disk drives, etc.
Kernel manages interface between processes / devices
Kernel manages / arbitrates access to multiple processes
dining philosophers…
9
July 24, 2016
Networking
• Transmits / receives packets
• Routes packets to target systems
• (Could be considered just another device)
10
July 24, 2016
System Call API
• Processes can request kernel services to perform various tasks
• TOPIC OF THIS COURSE
11
July 24, 2016
Kernel vs. User Mode
• CPU operates in two modes: kernel or user
• Portions of virtual memory loaded as “kernel” or “user”
• CPU can only access memory depending on mode
• “kernel” mode can access all memory
• “user” mode can only access user memory (Seg fault?)
12
July 24, 2016
Kernel vs. User Mode
process
function calls
system libraries
user mode
system calls
kernel mode
OS
hardware
13
July 24, 2016
Process vs. Kernel View
• Does NOT know which other processes are scheduled
• Does NOT know where it is on RAM or disk
• Cannot create new processes
• Cannot communicate directly with other processes
14
July 24, 2016
Process vs. Kernel View
•
•
•
•
•
•
•
•
Facilitates running ALL processes
Decides / schedules process access to CPU
Maintains data structures of running processes
Translates filenames to physical disk locations
Maintains data structures for virtual memory
Communicates between processes
Creates / terminates processes
Provides I/O access to devices
15
July 24, 2016
Shell
• Special purpose program
• AKA command line interpreter
• In UNIX, shell is a user process
• Many shell programs:
• sh- Bourne shell
• csh- C shell
• bash – Bourne-again shell
• Shell scripting is useful as well!!
16
July 24, 2016
File System (Basics)
• Kernel maintains single hierarchical directory structure
• Starts at “root” /
17
July 24, 2016
File Types
•
•
•
•
•
•
Regular file
Directory
Device
Pipe
Socket
Symbolic link
18
July 24, 2016
Regular File
• NOT a “special” file
• Special files: directory, socket, pipe, device, symbolic links, etc.
• Text files, pdfs, images, executables, etc.
19
July 24, 2016
Directory
•
•
•
•
Table of filenames with reference to them
Filename + reference = link
Can contain links to files and directories
Every directory contains . and ..
• . => current directory
• .. => parent directory
20
July 24, 2016
Symbolic Link
• Provides an alternate file name
• Basically a pointer
• “Dangling link” = symbolic link to nothing
//create symbolic link “target” pointing to “source”
UNIX> ln –s source target
21
July 24, 2016
File Types
• More on device, pipe, and socket later
• Filenames:
22
July 24, 2016
Filenames
• Up to 255 characters long
• Any character except / and \0 (NULL character)
• Advisable to use only letters, numbers, period, underscore,
and/or hyphen
• Can use the escape character \ for special shell characters
• Avoid starting filenames with -
23
July 24, 2016
Pathnames
• /this/is/a/path
• Read from left to right
• Absolute paths start with / (root)
• Relative paths start at CWD
24
July 24, 2016
Current Working Directory (CWD)
• A process’s “current location” with the directory hierarchy
• Where relative path begins
• Process inherits CWD from parent process
• Shell initiates process from a directory
25
July 24, 2016
File Ownership and Permissions
• UNIX has three types of ownership:
1. user- owner of file
1. group- users who are members of the file’s group
2. other- everyone else
26
July 24, 2016
File Ownership and Permissions
• UNIX has 3 types of permission:
1. read- allow contents to be read
1. write- allow contents to be modified
2. execute- allows file to be executed
27
July 24, 2016
File Ownership and Permissions
• Each file has 9 permission bits (3 per user type)
//print the permissions (only) of file.txt
UNIX> ls –l file.txt | awk ‘{print $1}’
-rw-r--r-other permission bits (read only)
group permission bits (read only)
user permission bits (read, write)
28
July 24, 2016
File Ownership and Permissions
• Use chmod command to modify permissions
//user: read, write, exe. group: read / write; other: read only
UNIX> chmod u=rwx file.txt
UNIX> chmod g=rw file.txt
UNIX> chmod o=r file.txt
UNIX> ls –l file.txt | awk ‘{print $1}’
-rwxrw-r--
29
July 24, 2016
File I/O Model
• In UNIX, everything is a file
• “Universality” of I/O
• System calls (open, read, write, close, etc.) used to perform I/O
on all file types
• Kernel provides single file “type”, just a stream of bytes
30
July 24, 2016
File Descriptors (FDs)
• I/O system calls refer to open files with FDs
• Small, nonnegative number
• FDs obtained from open() system call
• Processes (usually) inherit 3 FDs when started by shell:
• FD 0: standard input
• FD 1: standard output
• FD 2: standard error
31
July 24, 2016
File Descriptors (FDs)
• In C, use stdio library for I/O
• #include <stdio.h>
• FD 0 (standard input) = stdin
• FD 1 (stadnard output) = stdout
• FD 2 (standard error) = stderr
• Library contains fopen(), fclose(), scanf(), printf(), etc.
• Layered on top of system calls open(), close(), read(), write(), etc.
32
July 24, 2016
Command Line Arguments
• Programs often called with command line arguments
In C:
int main(int argc, char **argv)
argc: number of command line arguments
argv: pointer to C-style strings (char *)
e.g., argv[0] = program’s name
33
July 24, 2016
Outline
• Basic UNIX Concepts
• Processes
• System Calls
34
July 24, 2016
Processes
• Instance of a running program
• When program executed, the kernel:
•
•
•
•
Loads code into virtual memory
Allocates space
Manages resources
Sets up kernel bookkeeping
•
•
•
•
Process ID (PID)
User ID
Termination status
etc.
35
July 24, 2016
Processes: Memory Layout
• Process divided into segments:
•
•
•
•
Text: program’s instructions
Data: static variables used by program
Heap: area for dynamic memory allocation
Stack: grows / shrinks with function calls
stack
heap
data
text
36
July 24, 2016
Process Creation and Program Execution
• Process can create a new process with fork() system call
• Calling process: parent
• New process: child
• Kernel creates child process by duplicating parent process
• Child inherits copies of parent’s data, stack, and heap
• Program text (read-only) is shared between parent / child
37
July 24, 2016
Process Creation and Program Execution
• Use execve() system call to load / execute different program
• execve() destroys existing text, data, stack, and heap
• Replaces code with new segments
• More on this later…
38
July 24, 2016
Process Identification
• Each process has a unique process identifier (PID)
• Each process also has parent process identifier (PPID)
attribute
• Identifies the process that requested the kernel to create the child
process using fork() or exec()
39
July 24, 2016
Process Termination and Termination Status
• Process can terminate in one of two ways:
1.
2.
Requesting its own termination using _exit() system call
Being killed by the delivery of a signal
• Process then yields termination status
•
•
•
•
Small, nonnegative integer
Available for inspection by parent process using wait() system call…
If killed by signal, status set according to signal type
Or- value set during exit() system call
40
July 24, 2016
init Process
• When booting, kernel creates special process called init
• init is the parent of all processes
• all processes created using fork() either by init or one of init’s
descendants
•
•
•
•
init has PID 1 and runs with superuser privileges
init cannot be killed
init terminates when system shuts down
init creates and monitors a range of processes
41
July 24, 2016
Daemon Processes
• Special purpose process
• Special characteristics
• Long-lived: often starts on boot, stops at shutdown
• Runs in background: has no controlling terminal
42
July 24, 2016
Static vs. Dynamic Libraries
•
•
•
•
Statically linked
Object files copied into program’s code
Wastes disk space
If library changes, programs must be relinked
43
July 24, 2016
Static vs. Dynamic Libraries
• Dynamically linked at runtime, not compile time
• One copy of object file in memory
• If library changes, only needs to be reloaded into memory
44
July 24, 2016
Interprocess Communication (IPC)
•
•
•
•
•
•
•
Signals
Pipes
Sockets
File locking
Message queues
Semaphores
Shared memory
45
July 24, 2016
more later…
Signals
• “software interrupts”
• Informs process that some event or exception occurred
• Various types of signals (identified with number)
• Symbolic names: SIGxxx
46
July 24, 2016
Signals
• Kernel may send signal to process
• E.g., interrupt signal (ctrl-C)
• Process may send signal to another process (including self)
• E.g., kill from shell to process
• Process may establish signal handler to deal with signal
47
July 24, 2016
Threads
•
•
•
•
•
Each process can have multiple threads of execution
Each thread executes the same program code
Threads share data and heap sections
Each thread has its own stack
Threads can communicate with each other using shared global
variables
• Threading API provides condition variables and mutexes
• Allow threads to communicate and synchronize
• More on this later…
48
July 24, 2016
Shell Basics
• CTRL-C
• Sends interrupt signal to foreground process
• CTRL-Z
• Sends suspend signal to foreground process
• &
• Used to create a background process
• E.g., UNIX> ./some_program &
49
July 24, 2016
Time
• Real-time: seconds since January 1st, 1970
• Process time: (CPU time) total CPU time for process
• system time: kernel mode
• user time: user mode
50
July 24, 2016
time Command
// time how long it takes to execute ls program
// ignore standard output by redirecting to /dev/null
UNIX> time ls > /dev/null
real 0m0.005s
user 0m0.001s
sys
0m0.002s
• real- overall time of process
• user- time spent in user mode
• sys- time spent in kernel mode
51
July 24, 2016
Outline
• Basic UNIX Concepts
• Processes
• System Calls
52
July 24, 2016
System Call
• Controlled entry point to kernel
• Allows kernel to perform action for process
• Range of services available via API: e.g.,
• Creating a new process
• Performing I/O
• Creating a pipe for interprocess communication
53
July 24, 2016
System Calls
54
July 24, 2016
System Calls
• Changes CPU mode from user to kernel
• Set of system calls is fixed
• Each system call may have arguments
• Communicate between process / kernel
• In C, system calls look like ordinary C functions
• Program uses a C wrapper function
• Wrapper function handles arguments to system calls
• (Register settings, int 0x80 call in assemble)
55
July 24, 2016
System Call Errors
• If system call returns an error, wrapper function sets errno
global variable
• Wrapper function returns integer return value to caller
• Nonnegative number => success
• -1 => error; must check errno
56
July 24, 2016
Library Functions
• Many functions in standard C library
• Many functions rely on system calls
• fopen() uses open()
• fprintf() uses write()
• Library functions make things easier
• fprintf() for data formatting, write() writes chunks of memory
• malloc() and free() vs. brk() system call
58
July 24, 2016
Error Handling
• Almost every system call and library function returns a status
value
• Always check the return value
• Handling system call errors
• Usually, return value of -1 indicates an error
• man pages for each system call documents possible return values
59
July 24, 2016
Error Handling
• Always check the return value!!
fd = open(pathname, flags, mode);
if(fd == -1) { //error handling code };
if(fclose(fd) == -1) { //error handling code };
60
July 24, 2016
Error Handling
• When system call fails, errno set to positive value that
identifies the specific error
• errno is NOT modified after each system call
• Thus- check return value for error first!
• #include <errno.h> provides declarations of errnos
• Use perror() to print the current errno message
• Use strerror() to get string corresponding to error number
61
July 24, 2016
Live Coding Demonstration
• Command line arguments
• stdio.h
• scanf()
• printf()
• errno.h
• perror()
• CTRL-C, CTRL-Z, &
62
July 24, 2016
Download