Upload
dinhkhue
View
214
Download
0
Embed Size (px)
Citation preview
Titolo presentazionesottotitolo
Milano, XX mese 20XX
Piattaforme Software per la Rete
System Administration
A.A. 2016/17Federico Reghenzani, Alessandro Barenghi
Federico Reghenzani 2/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Outline
1) Linux boot sequence and BASH forking
2) Process Management
3) Scheduling
4) Process Inspection
Federico Reghenzani 4/24
Dipartimento di Elettronica, Informazione e Bioingegneria
The machine boot
Power on
BIOS
The system is powered on, the firmware (BIOS/EFI) is loaded into main memory
The BIOS or EFI firmware performs all the necessary checks and launch the bootloader
Bootloader
Linuxkernel
The kernel starts running
The bootloader mounts the bootloader filesystem and loads the correct kernel image (usually /boot/vmlinuz-*)
Federico Reghenzani 5/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Tyipical bootloaders
● GRUB: GRand Unified Bootloader
– A complete bootloader
● LILO: LInux Loader
– It does not support GPT partitions
– Unmaintained since Jan. 2016
● Syslinux
– Minimalistic
– Great for removable drives (e.g. USB sticks)
Federico Reghenzani 6/24
Dipartimento di Elettronica, Informazione e Bioingegneria
The Linux boot
Low-levelinitialization
Kernel imagedecompression
The first code initializes the CPU, programs the MMU,transistion to 64-bit mode, etc.
The kernel image is decompressed
start_kernel() Several non-architecture specific initializations: setup interrupts, memory configuration, scheduler, load initrd, ...
Start the initprocess
The init process is started, usually /sbin/init
Federico Reghenzani 7/24
Dipartimento di Elettronica, Informazione e Bioingegneria
The `init` process
● The init process is the first user-space process
– It is the ancestor of all user-space processes in the system
– It starts all enabled services during startup (e.g. DHCP, web server, graphic server, ntp, ...)
● Most common implementations:
– SysVinit (legacy systems)● Load services from /etc/init.d/, /etc/rc.d/
– systemd● Load services from /etc/systemd/
Federico Reghenzani 8/24
Dipartimento di Elettronica, Informazione e Bioingegneria
The process tree structure
● In Linux every process has exactly one parent and may have
● An arbitrary number of children
● init process is the only exception: it has no parent
● Every process has:
– PID: Process IDentifier
– PPID: Parent Process IDentifier
● For init PID=1, PPID=0
● PID is of pid_t type, implemented as 32-bit integer, but limited by /proc/sys/kernel/pid_max special file
Federico Reghenzani 9/24
Dipartimento di Elettronica, Informazione e Bioingegneria
The login phase
● At the end of the startup, the init daemon executes the getty command
● getty with the help of login command is in charge of authenticating the user
● After a successfull authentication the default shell for the user is started (see /etc/passwd)
Federico Reghenzani 10/24
Dipartimento di Elettronica, Informazione e Bioingegneria
BASH forking
bash
bash bash
bash: wait() ./my_prog
bash
fork()
exec()
user@machine:/path/to/dir$ ./myprog
user@machine:/path/to/dir$
Federico Reghenzani 12/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Seeing processes
● : show the current processes
– Common attributes: : show every process : show process running by a certain user : show the number of threads : show details of every process … tons of other attributes, check the man page
– Ideal for scripting
● : show processes in a tree format
● : return all the PIDs of <command>
ps
-e
-u <user>
-Lf
pstree
u
pidof <command>
Federico Reghenzani 13/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Seeing processes (interactively)
● System administrators use the command to check interactively the running processes and their attributes
● Often, an enhanced version is used:
top
htop
Federico Reghenzani 14/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Seeing processes – under the hood
● The information read by , , comes from the proc filesystem, mounted under /proc directory
● It’s a virtual filesystem: nothing is present on the disk
● When a process try to read/write files in the proc filesystem, the kernel runs specific routines to reply to these I/O requests
● Instead of exposing tons of system calls, the Linux kernel provide several information and knobs via file-based interfaces
ps pstree [h]top
Federico Reghenzani 15/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Running processes in background
● Running a command from the shell results in the shell waiting for its completion: this is known as running in foreground
● Appending a & at the end of a command starts the execution in background
● CTRL+Z sends a SIGSTOP signal to the process, freezing the execution
– and commands resume the execution of the frozen process, respectively in foreground and in backgroundfg bg
Federico Reghenzani 17/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Schedulers
● Normal scheduler
– CFS: Completely Fair Scheduler
– The key idea is to maximize the CPU allocation without sacrifice the interactive applications
● Real-Time schedulers
– Priority-based Round-Robin
– Priority-based FIFO
– EDF: Earliest Deadline First
Federico Reghenzani 18/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Priority and Nice value
● Linux implements two separate concepts of priority:
– The nice value Range [-20;19], higher values mean lower priority
(default 0) It represents the proportion of system’s processor
assigned to the process
– The real-time priority Range [0;99], higher values mean higher priority
(default 0) 0 → non real-time process > 0 → real-time process
Federico Reghenzani 19/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Changing priority
● allows the user to start a program with a different nice value:
● To change an already running process can be used:
● is used to change the real-time priority:
–
–
–
nice
nice -n <nice> command
renice
renice -n <nice> -p <pid>
chrt
chrt -f -p <priority> <pid>
chrt -r -p <priority> <pid>
chrt -o -p 0 <pid>
FIFO
RR
Return to non real-time priority
Federico Reghenzani 21/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Process Inspection
● How to analyze a live process execution?
– Debugging (via gdb)
– Tracing (via strace or ltrace)
– Monitoring the interacting files
Federico Reghenzani 22/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Debugging
● The standard Linux debugger is the GNU Debugger (gdb)
● It provides several functions to inspect the inner working of a process
● The debugging is performed via the ptrace system call
● Syntax:
–
–
gdb <progam>
gdb -p <pid>
Start a program with the debugger
Attach the debugger to a running process
Federico Reghenzani 23/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Tracing
● A process doing something meaningful needs to interact with the operating system and libraries
● The command intercepts and records system calls and signals.
– Common usage:
– Groups can be used to log only certain calls:● ● <group> = process, network, file or signal
● The command intercepts and records dynamic library calls. It shares most of the options with
strace
strace -o <filename> <command>
-e=<group>
ltrace
strace
Federico Reghenzani 24/24
Dipartimento di Elettronica, Informazione e Bioingegneria
Monitoring the interacting files
● The command lists all the open files on a system, including special files (e.g. sockets, libraries, etc.)
● Common options:
– prints all the files opened by any command starting with <string>
– prints all open files in a directory
– prints all open files of a certain user
– prints all open files by a certain PID
lsof
-c <string>
+D <dir>
-u <user>
-p <pid>