Linux: Procesy
-
Upload
michal-sedlak -
Category
Documents
-
view
747 -
download
8
Transcript of Linux: Procesy
![Page 1: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/1.jpg)
L i n u x F E S T 1 1 . 1 0 . 2 0 0 8
GNU/LinuxProcesy
Autor: Michal SedlákEmail: michal6103 [at] gmail [dot] com
![Page 2: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/2.jpg)
11.10.2008 2/35
P r e z e n t á c i a j e z a l o ž e n á n a
The LBook:An Introductory Guide to Linux Basics
http://linuxbasics.org/course/book/index
Vizuálna téma prezentácie je odvodená témy vytvorenej:
Sakari Koivunen and Henrik OmmaReleased under the LGPL license.
![Page 3: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/3.jpg)
11.10.2008 3/35
O b s a h
● Všeobecne o procesoch– Typy, atribúty, životný cyklus
● Správa procesov● Plánovanie procesov
– Sleep, at, cron, crontab
![Page 4: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/4.jpg)
11.10.2008 4/35
P r o g r a m - P r o c e s
● Program je pasívny zhluk inštrukcií v strojovo zrozumiteľnej forme– ELF - Executable and Linking Format
● Jeden program môže spúšťať viac procesov
![Page 5: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/5.jpg)
11.10.2008 5/35
P r o c e s - V l á k n o
● Proces je vo všeobecnosti inštancia programu– Obraz strojového kódu (code)– Naalokovaná pamäť (heap, stack)– Tabuľka zdrojov (file deskriptory)– Bezpečnostné atribúty (EUID, RUID, EGUI...)– Stav procesoru (obsah registrov + PC, SP...)
● Vlákno je postupnosť inštrukcií– Zdieľa pamäť procesu– Program Counter, Stack Pointer, Data registers
![Page 6: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/6.jpg)
11.10.2008 6/35
P r o c e s - V l á k n o
– Každý proces obsahuje jedno, alebo viac vlákien
– Každé vlákno patrí niektorému procesu
![Page 7: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/7.jpg)
11.10.2008 7/35
P r o c e s v s . v l á k n o
michal@michaldesktop:~$ cat test.py import time, threading, processingfor cls in [threading.Thread, processing.Process]: start = time.time() for _ in range(1000): child = cls() child.start() child.join() print 'Spawning 1000 children with %s took %.2fs' % ( cls.__name__, time.time() start)
michal@ubuntu:~$ python test.py Spawning 1000 children with Thread took 0.19sSpawning 1000 children with Process took 4.76s
![Page 8: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/8.jpg)
11.10.2008 8/35
T y p y p r o c e s o v
● Interaktívne procesy● Automatické procesy● Démoni (daemons)
![Page 9: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/9.jpg)
11.10.2008 9/35
I n t e r a k t í v n e p r o c e s y
● Inicializované a ovládané cez terminál– Popredie
● Normálne spustenie procesu● Proces pripojený k terminálu● Terminál posiela príkazy procesu
– Pozadie● Spustenie so znakom & za príkazom: xterm &● Proces sa po inicializácií odpojí od terminálu
michal@michaldesktop:~$ xterm &[1] 29611michal@michaldesktop:~$ jobs[1]+ Running xterm &michal@michaldesktop:~$
![Page 10: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/10.jpg)
11.10.2008 10/35
P r á c a s i n t e r a k t í v n y m i p r o c e s m i
●prikaz Spustí príkaz v popredí
●prikaz & Spustí príkaz na pozadí a uvolní terminál
●jobs Zobrazí procesy bežiace na pozadí
●Ctrl+Z Pozastavenie procesu SIGSTOP (suspend)●Ctrl+C Prerušenie programu v popredí SIGINT●bg Reaktivácia pozastaveného procesu
●fg Vyvolanie programu do popredia
●kill ukončenie procesu
![Page 11: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/11.jpg)
11.10.2008 11/35
A u t o m a t i c k é p r o c e s y
● Nie sú pripojené k terminálu● Spúšťané na základe FIFO prístupu
– at● Spustenie procesu v konkrétnom čase
– batch● Spustenie procesu ak je zaťaženie systému nízke
![Page 12: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/12.jpg)
11.10.2008 12/35
D a e m o n
● Vo všeobecnosti inicializované pri štarte● Procesy spustené nepretržite● Serverové aplikácie (client-server)● Napr. httpd(Apache), inetd, ftpd
![Page 13: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/13.jpg)
11.10.2008 13/35
A t r i b ú t y p r o c e s o v
– Process ID (PID)– Parent Proces ID (PPID)– Nice– Terminal (TTY)– UID
● Real User Id (RUID)● Effective User Id (EUID)
– GID● Real Group Id (RGID)● Effective Group Id (EGID)
![Page 14: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/14.jpg)
11.10.2008 14/35
P a r a m e t r e p r o c e s o v
michal@michaldesktop:~$ passwd &[1] 6129[1]+ Stopped passwd
michal@michaldesktop:~$ ps axo pid,ppid\,nice,user,euid,ruid,cmd
PID PPID NI USER EUID RUID CMD 6106 5944 0 1000 1000 1000 bash 6129 6106 0 root 0 1000 passwd 6130 6106 0 1000 1000 1000 ps
![Page 15: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/15.jpg)
11.10.2008 15/35
I n f o r m á c i e o p r o c e s o c h
michal@ubuntu:~$ ps PID TTY TIME CMD 6133 pts/0 00:00:00 bash 6983 pts/0 00:00:00 ps
michal@ubuntu:~$ ps auxUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1 0.0 0.2 2844 1692 ? Ss 21:20 0:01 /sbin/initroot 2 0.0 0.0 0 0 ? S< 21:20 0:00 [kthreadd]root 3 0.0 0.0 0 0 ? S< 21:20 0:00 [ksoftirqd/0]root 4 0.0 0.0 0 0 ? S< 21:20 0:00 [watchdog/0]root 5 0.0 0.0 0 0 ? S< 21:20 0:00 [events/0]root 6 0.0 0.0 0 0 ? S< 21:20 0:00 [khelper].........
![Page 16: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/16.jpg)
11.10.2008 16/35
I n f o r m a c i e o p r o c e s o c h
michal@ubuntu:~$ pstreeinit NetworkManager {NetworkManager}─┬─ ─── ├─NetworkManagerD ├─acpid ├─atd ├─avahidaemon avahidaemon─── ├─bonoboactivati {bonoboactivati}─── ├─cron ├─cupsd ├─dhcdbd dhclient─── ├─firefox 5*[{firefox}]─── ├─gajim.py sh─── ├─gconfd2 ├─gdm gdm Xorg─── ─┬─ │ └─gnomesession bluetoothapple─┬─ │ ├─gnomepanel │ ├─metacity │ ├─nautilus {nautilus}─── │ ├─nmapplet │ ├─python
![Page 17: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/17.jpg)
11.10.2008 17/35
P r í k l a d y
michal@ubuntu:~$ pkill firefoxmichal@ubuntu:~$ killall firefoxfirefox: no process killed
michal@ubuntu:~$ pgrep u michal5749582458255828...
michal@ubuntu:~$ ps fp $(pgrep firefox)UID PID PPID C STIME TTY TIME CMDmichal 7096 1 8 22:27 ? 00:00:02 /usr/lib/firefox3.0.3/f...
michal@ubuntu:~$ renice +4 $(pgrep firefox)7096: old priority 0, new priority 4
![Page 18: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/18.jpg)
11.10.2008 18/35
![Page 19: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/19.jpg)
11.10.2008 19/35
![Page 20: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/20.jpg)
11.10.2008 20/35
V y t v o r e n i e p r o c e s u
● Fork– Vytvorenie kópie procesu– Zistenie PID (Dieťaťu sa PID zdá byť 0)– Vykonanie algoritmu na základe PID– Nový proces beží v rovnakom prostredí
● Nastavenie I/O, environment variables,...
● Exec– Nahradenie procesu zavolaným procesom– PID sa nemení– Nahradí sa stack, heap, data
![Page 21: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/21.jpg)
11.10.2008 21/35
D é m o n i
● Démon má ako PPID 1 (init)● Rodičia démonizujú svoje deti, aby deti
prežili po ich smrti ● Príklad - Window manager
– Dokážeme zmeniť WM bez reštartu ním spustených programov
![Page 22: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/22.jpg)
11.10.2008 22/35
U k o n č e n i e p r o c e s u a z o m b i e
● Pri normálnom ukončení vracia proces rodičovi exit status
● Zombie – rodič neprebral signál o ukončení procesu
● Exit status je číslo● Uvoľnenie zdrojov● Zabitie svojich zombie detí
![Page 23: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/23.jpg)
11.10.2008 23/35
P r í k l a d y e x i t k ó d o v
michal@michaldesktop:~$ ps |grep neexistuje;echo $?1
michal@michaldesktop:~$ ps |grep bash;echo $?18653 pts/0 00:00:00 bash0
michal@michaldesktop:~$ ls t*;echo $?test.py tree.DBF tv.jpg0
michal@michaldesktop:~$ ls neexistuje;echo $?ls: nie je možný prístup k asd: No such file or directory2
![Page 24: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/24.jpg)
11.10.2008 24/35
S i g n á l y
● Štandardne akýkoľvek signál ukončí proces● Medziprocesová komunikácia (IPC)● kill l; kill 9 1
● 15 SIGTERM – Ukončenie procesu● 2 SIGINT – Prerušenie (maskovateľný)● 9 SIGKILL – Zabitie (nemaskovateľný) ● 1 SIGHUP – Odpojenie terminálu - nohup ● man 7 signal
![Page 25: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/25.jpg)
11.10.2008 25/35
S p r á v a v ý k o n u p r o c e s o v
● Motivácia - Efektívne využitie prostriedkov● Úloha pre administrátora● Prvá informácia load
● Výkon– CPU– Pamäť– I/O
![Page 26: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/26.jpg)
11.10.2008 26/35
l o a d
● Počíta vážený priemer procesov v stave:– Running - bežiace– Runnable – čakajúce na CPU– Uninterruptible sleep – čakajúce na I/O
● 1, 5, 15 minút● 175% v poslednej minúte
– V priemere čakalo 0,75 procesu
michal@ubuntu:~$ uptime 19:51:48 up 1:24, 4 users, load average: 1.75, 0.86, 0.54
![Page 27: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/27.jpg)
11.10.2008 27/35
Č o m ô ž e o v p l y v n i ť u ž í v a t e ľ ?
● Nespúšťať nič čo nie je potrebné– Veľa premenných prostredia– Dlhé cesty na vyhľadávanie spustiteľných
súborov– Addony do window managerov atp.– Zbytočné služby (bluetooth, cupsd)
● Použiť programy s menšou záťažou– locate namiesto find
![Page 28: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/28.jpg)
11.10.2008 28/35
P r í k l a d
michal@ubuntu:~$ time locate *bash* > output
real 0m0.526suser 0m0.428ssys 0m0.024s
michal@ubuntu:~$ time find / name *bash* >output
real 1m11.150suser 0m1.152ssys 0m3.424s
![Page 29: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/29.jpg)
11.10.2008 29/35
B u ď t e m i l ý k i n ý m p r o c e s o m
● Upraviť prioritu programom– nice
– renice
● Default 0● Kladné hodnoty● Záporné hodnoty iba root● Indexácia beagle, trackerd● Nie je dobrý nápad znižovať prioritu
interaktívnej aplikácií
![Page 30: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/30.jpg)
11.10.2008 30/35
P a m ä ť a d i s k
michal@ubuntu:~$ free m total used free shared buffers cachedMem: 757 745 12 0 2 289/+ buffers/cache: 453 304Swap: 1027 24 1002
michal@ubuntu:~$ vmstat procs memory swap io system cpu r b swpd free buff cache si so bi bo in cs us sy id wa 2 0 25532 10756 1156 298216 1 4 282 147 321 1172 19 4 73 4
![Page 31: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/31.jpg)
11.10.2008 31/35
Č o s u š e t r e n ý m č a s o m
● sleep● at● batch● cron
$(sleep 1800; echo Skoncilo cviko) &
$(nohup sleep 5h; nice 30 moj_program) &
![Page 32: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/32.jpg)
11.10.2008 32/35
a t , a t r m , a t q
● Spustí program v danom časemichal@ubuntu:~$ at 22:00warning: commands will be executed using /bin/shat> echo Spustil som sa: $(date) > at.txtat> <EOT>job 1 at Fri Oct 10 22:00:00 2008
michal@ubuntu:~$ cat at.txt Spustil som sa: Pi okt 10 22:00:00 CEST 2008
michal@ubuntu:~$ batchwarning: commands will be executed using /bin/shat> echo Spustil som sa: $(date) > at.txtat> <EOT>job 2 at Fri Oct 10 22:08:00 2008
michal@ubuntu:~$ cat at.txt Spustil som sa: Pi okt 10 22:14:29 CEST 2008
![Page 33: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/33.jpg)
11.10.2008 33/35
c r o n
● Plánovač● /etc/cron.d● crontab e (editovať výhradne cez -e)● crontab l
● Uživateľské crontaby v /var/spool/cron
#/etc/crontab#m h dom mon dow user command17 * * * * root cd / && runparts report /etc/cron.hourly25 6 * * * root cd / && runparts report /etc/cron.daily47 6 * * 7 root cd / && runparts report /etc/cron.weekly52 6 1 * * root cd / && runparts report /etc/cron.monthly#
![Page 34: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/34.jpg)
11.10.2008 34/35
Z h r n u t i e
● Čo sú procesy a vlákna● Typy procesov● Životný cyklus procesu● Signály● Priority● Plánovanie
![Page 35: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/35.jpg)
11.10.2008 35/35
Ďakujem za pozornosťNa prípadné otázky rád odpoviem
michal6103 [at] gmail [dot] com
![Page 36: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/36.jpg)
S p o n z o r i
![Page 37: Linux: Procesy](https://reader033.fdocuments.us/reader033/viewer/2022051212/558c7b4bd8b42a8c4c8b457d/html5/thumbnails/37.jpg)
R e f e r e n c i e
http://en.wikipedia.org/wiki/Daemon_(computer_software)http://en.wikipedia.org/wiki/Demonhttp://www.cyberciti.biz/tips/linux-more-on-user-id-password-and-group-management.htmlhttp://www.faqs.org/faqs/unix-faq/faq/part4/section-10.htmlhttp://www.cs.berkeley.edu/~istoica/classes/cs194/05/notes/4-ProcThread.pdfhttp://en.wikipedia.org/wiki/Vforkhttp://en.wikipedia.org/wiki/Exec_(operating_system)http://en.wikipedia.org/wiki/Fork-exechttp://en.wikipedia.org/wiki/Load_(computing)