{"id":2802,"date":"2019-10-21T15:00:25","date_gmt":"2019-10-21T08:00:25","guid":{"rendered":"https:\/\/tino.vn\/?post_type=ht_kb&#038;p=2802"},"modified":"2019-10-21T15:01:54","modified_gmt":"2019-10-21T08:01:54","slug":"tim-hieu-ve-tien-trinh-trong-linux","status":"publish","type":"ht_kb","link":"https:\/\/tino.vn\/blog\/docs\/tim-hieu-ve-tien-trinh-trong-linux\/","title":{"rendered":"T\u00ecm hi\u1ec3u v\u1ec1 ti\u1ebfn tr\u00ecnh trong linux"},"content":{"rendered":"<p>Khi l\u00e0m vi\u1ec7c v\u1edbi h\u1ec7 \u0111i\u1ec1u h\u00e0nh, ta lu\u00f4n g\u1eb7p c\u1ee5m t\u1eeb process hay ti\u1ebfn tr\u00ecnh. V\u1eady ti\u1ebfn tr\u00ecnh l\u00e0 g\u00ec? B\u00e0i vi\u1ebft n\u00e0y s\u1ebd gi\u1ea3i th\u00edch cho c\u00e1c b\u1ea1n hi\u1ec3u, \u0111\u1ed3ng th\u1eddi gi\u00fap c\u00e1c b\u1ea1n ph\u00e2n bi\u1ec7t \u0111\u01b0\u1ee3c c\u00e1c lo\u1ea1i ti\u1ebfn tr\u00ecnh.<\/p>\n<h2 id=\"1._Ti\u1ebfn_tr\u00ecnh\">1. Ti\u1ebfn tr\u00ecnh<\/h2>\n<p>Ti\u1ebfn tr\u00ecnh (processes) \u0111\u01b0\u1ee3c hi\u1ec3u \u0111\u01a1n gi\u1ea3n l\u00e0 m\u1ed9t ch\u01b0\u01a1ng tr\u00ecnh \u0111ang ch\u1ea1y trong trong h\u1ec7 \u0111i\u1ec1u h\u00e0nh. M\u1ed9t ti\u1ebfn tr\u00ecnh c\u00f3 th\u1ec3 ph\u00e2n th\u00e0nh m\u1ed9t hay nhi\u1ec1u ti\u1ebfn tr\u00ecnh con kh\u00e1c.<\/p>\n<h2 id=\"\"><\/h2>\n<h2 id=\"2._Ph\u00e2n_lo\u1ea1i_ti\u1ebfn_tr\u00ecnh\">2. Ph\u00e2n lo\u1ea1i ti\u1ebfn tr\u00ecnh<\/h2>\n<h3 id=\"\"><\/h3>\n<h3 id=\"2.1._Init_process\">2.1. Init process<\/h3>\n<p>Init process l\u00e0 ti\u1ebfn tr\u00ecnh \u0111\u1ea7u ti\u00ean \u0111\u01b0\u1ee3c kh\u1edfi \u0111\u1ed9ng sau khi b\u1ea1n l\u1ef1a ch\u1ecdn h\u1ec7 \u0111i\u1ec1u h\u00e0nh trong boot loader. Trong c\u00e2y ti\u1ebfn tr\u00ecnh, init process l\u00e0 ti\u1ebfn tr\u00ecnh cha c\u1ee7a c\u00e1c ti\u1ebfn tr\u00ecnh kh\u00e1c. Init process c\u00f3 \u0111\u1eb7c \u0111i\u1ec3m sau:<\/p>\n<ul>\n<li>PID = 1<\/li>\n<li>Kh\u00f4ng th\u1ec3 kill init process<\/li>\n<\/ul>\n<h3 id=\"\"><\/h3>\n<h3 id=\"2.2._Parents_process_\u2013_Child_process\">2.2. Parents process \u2013 Child process<\/h3>\n<p>Trong h\u1ec7 \u0111i\u1ec1u h\u00e0nh linux c\u00e1c ti\u1ebfn tr\u00ecnh \u0111\u01b0\u1ee3c ph\u00e2n th\u00e0nh parents process v\u00e0 child process. M\u1ed9t ti\u1ebfn tr\u00ecnh khi th\u1ef1c hi\u1ec7n l\u1ec7nh fork() \u0111\u1ec3 t\u1ea1o ra m\u1ed9t ti\u1ebfn tr\u00ecnh m\u1edbi th\u00ec \u0111\u01b0\u1ecdc g\u1ecdi l\u00e0 parents process. Ti\u1ebfn tr\u00ecnh m\u1edbi t\u1ea1o \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 child process.<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/tino.vn\/blog\/wp-content\/uploads\/2019\/10\/process01.png\" alt=\"image\" title=\"\"><\/figure>\n<p>M\u1ed9t parents process c\u00f3 th\u1ec3 c\u00f3 nhi\u1ec1u child process nh\u01b0ng m\u1ed9t child process ch\u1ec9 c\u00f3 m\u1ed9t parents process. Khi quan s\u00e1t th\u00f4ng tin c\u1ee7a m\u1ed9t ti\u1ebfn tr\u00ecnh, ngo\u00e0i PID (Processes ID) ta c\u1ea7n \u0111\u1ec3 \u00fd t\u1edbi PPID (Parent Processes ID). N\u00f3 s\u1ebd cho ta th\u00f4ng tin v\u1ec1 parents process c\u1ee7a ti\u1ebfn tr\u00ecnh \u0111\u00f3:<\/p>\n<pre class=\"wp-block-code\"><code>ps -ef<\/code><\/pre>\n<figure class=\"wp-block-image\"><img decoding=\"async\" class=\"wp-image-2146\" src=\"https:\/\/tino.vn\/blog\/wp-content\/uploads\/2019\/10\/process02.png\" sizes=\"(max-width: 626px) 100vw, 626px\" srcset=\"https:\/\/tino.vn\/blog\/wp-content\/uploads\/2019\/10\/process02.png 626w, https:\/\/tino.vn\/blog\/wp-content\/uploads\/2019\/10\/1_process02-300x173.png 300w\" alt=\"\" title=\"\"><\/figure>\n<h3 id=\"\"><\/h3>\n<h3 id=\"2.3._Orphan_process_\u2013_Zombie_Process\">2.3. Orphan process \u2013 Zombie Process<\/h3>\n<p>Khi parents process \u2013 child process ho\u1ea1t \u0111\u1ed9ng s\u1ebd x\u1ea3y ra m\u1ed9t s\u1ed1 tr\u01b0\u1eddng h\u1ee3p \u0111\u1eb7c bi\u1ec7t. L\u00fac \u0111\u00f3 Orphan process \u2013 Zombie Process s\u1ebd \u0111\u01b0\u1ee3c h\u00ecnh th\u00e0nh.<\/p>\n<p>Khi m\u1ed9t parents process b\u1ecb t\u1eaft tr\u01b0\u1edbc khi child process \u0111\u01b0\u1ee3c t\u1eaft, ti\u1ebfn tr\u00ecnh con \u0111\u00f3 s\u1ebd tr\u1edf th\u00e0nh m\u1ed9t orphan process. L\u00fac n\u00e0y init process s\u1ebd tr\u1edf th\u00e0nh cha c\u1ee7a orphan processes v\u00e0 th\u1ef1c hi\u1ec7n t\u1eaft ch\u00fang.<\/p>\n<p>Khi m\u1ed9t child process \u0111\u01b0\u1ee3c k\u1ebft th\u00fac, m\u1ecdi tr\u1ea1ng th\u00e1i c\u1ee7a child process s\u1ebd \u0111\u01b0\u1ee3c th\u00f4ng b\u00e1o b\u1edfi l\u1eddi g\u1ecdi h\u00e0m wait() c\u1ee7a parents process. V\u00ec v\u1eady, kernel s\u1ebd \u0111\u1ee3i parents process tr\u1ea3 v\u1ec1 h\u00e0m wait() tr\u01b0\u1edbc khi t\u1eaft child process. Tuy nhi\u00ean v\u00ec m\u1ed9t v\u00e0i l\u00ed do m\u00e0 parents process kh\u00f4ng th\u1ec3 tr\u1ea3 v\u1ec1 h\u00e0m wait(), khi \u0111\u00f3 child process s\u1ebd tr\u1edf th\u00e0nh m\u1ed9t zombie process. Khi \u1edf tr\u1ea1ng th\u00e1i n\u00e0y, ti\u1ebfn tr\u00ecnh s\u1ebd g\u1ea7n nh\u01b0 gi\u1ea3i ph\u00f3ng b\u1ed9 nh\u1edb ho\u00e0n to\u00e0n, ch\u1ec9 l\u01b0u gi\u1eef m\u1ed9t s\u1ed1 th\u00f4ng tin nh\u01b0 PID, l\u01b0\u1ee3ng t\u1ea1i nguy\u00ean s\u1eed d\u1ee5ng,\u2026 tr\u00ean b\u1ea3ng danh s\u00e1ch ti\u1ebfn tr\u00ecnh.<\/p>\n<p>Tuy gi\u1ea3i ph\u00f3ng b\u1ed9 nh\u1edb ho\u00e0n to\u00e0n nh\u01b0ng c\u00e1c zombie process kh\u00f4ng b\u1ecb k\u1ebft th\u00fac. V\u00ec v\u1eady n\u1ebfu l\u01b0\u1ee3ng zombie process l\u1edbn s\u1ebd n\u1eafm gi\u1eef l\u01b0\u1ee3ng l\u1edbn c\u00e1c PID. N\u1ebfu l\u01b0\u1ee3ng PID \u0111\u1ea7y, s\u1ebd kh\u00f4ng c\u00f3 ti\u1ebfn tr\u00ecnh m\u1edbi \u0111\u01b0\u1ee3c t\u1ea1o th\u00eam. C\u00e1c zombie process s\u1ebd ch\u1ec9 b\u1ecb k\u1ebft th\u00fac n\u1ebfu nh\u01b0 parents process c\u1ee7a ch\u00fang b\u1ecb kill.<\/p>\n<p>\u0110\u1ec3 t\u00ecm c\u00e1c zombie process ta g\u00f5 ki\u1ec3m tra tr\u1ea1ng th\u00e1i c\u1ee7a ti\u1ebfn tr\u00ecnh theo l\u1ec7nh sau:<\/p>\n<pre class=\"wp-block-code\"><code>ps -lA | grep '^. Z'<\/code><\/pre>\n<h3 id=\"\"><\/h3>\n<h3 id=\"2.4_Daemon_Process\">2.4 Daemon Process<\/h3>\n<p>M\u1ed9t Daemon Process l\u00e0 m\u1ed9t ti\u1ebfn tr\u00ecnh ch\u1ea1y n\u1ec1n. N\u00f3 s\u1ebd lu\u00f4n trong tr\u1ea1ng th\u00e1i ho\u1ea1t \u0111\u1ed9ng v\u00e0 s\u1ebd \u0111\u01b0\u1ee3c k\u00edch ho\u1ea1t b\u1edfi m\u1ed9t \u0111i\u1ec1u ki\u1ec7n ho\u1eb7c c\u00e2u l\u1ec7nh n\u00e0o \u0111\u00f3. Trong Unix, c\u00e1c daemon th\u01b0\u1eddng \u0111\u01b0\u1ee3c k\u1ebft th\u00fac b\u1eb1ng \u201cd\u201d v\u00ed d\u1ee5 nh\u01b0 httpd, sshd, crond, mysqld,\u2026<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Khi l\u00e0m vi\u1ec7c v\u1edbi h\u1ec7 \u0111i\u1ec1u h\u00e0nh, ta lu\u00f4n g\u1eb7p c\u1ee5m t\u1eeb process hay ti\u1ebfn tr\u00ecnh. V\u1eady ti\u1ebfn tr\u00ecnh l\u00e0 g\u00ec? B\u00e0i vi\u1ebft n\u00e0y s\u1ebd gi\u1ea3i th\u00edch cho c\u00e1c b\u1ea1n hi\u1ec3u, \u0111\u1ed3ng th\u1eddi gi\u00fap c\u00e1c b\u1ea1n ph\u00e2n bi\u1ec7t \u0111\u01b0\u1ee3c c\u00e1c lo\u1ea1i ti\u1ebfn tr\u00ecnh. 1. Ti\u1ebfn tr\u00ecnh Ti\u1ebfn tr\u00ecnh (processes) \u0111\u01b0\u1ee3c hi\u1ec3u \u0111\u01a1n gi\u1ea3n l\u00e0 m\u1ed9t [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":16424,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"tags":[],"ht_kb_category":[17],"class_list":["post-2802","ht_kb","type-ht_kb","status-publish","format-standard","has-post-thumbnail","hentry","ht_kb_category-huong-dan-ve-cloud-vps"],"_links":{"self":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb\/2802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb"}],"about":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/comments?post=2802"}],"version-history":[{"count":0,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb\/2802\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/media\/16424"}],"wp:attachment":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/media?parent=2802"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/tags?post=2802"},{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb_category?post=2802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}