You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

424 lines
18 KiB

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  5. <head>
  6. <!-- 2023-03-09 Thu 19:12 -->
  7. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  8. <meta name="viewport" content="width=device-width, initial-scale=1" />
  9. <title>Lab5 Solution Amirlan Sharipov (BS21-CS-01)</title>
  10. <meta name="author" content="Amirlan Sharipov (BS21-CS-01)" />
  11. <meta name="generator" content="Org Mode" />
  12. <style>
  13. #content { max-width: 60em; margin: auto; }
  14. .title { text-align: center;
  15. margin-bottom: .2em; }
  16. .subtitle { text-align: center;
  17. font-size: medium;
  18. font-weight: bold;
  19. margin-top:0; }
  20. .todo { font-family: monospace; color: red; }
  21. .done { font-family: monospace; color: green; }
  22. .priority { font-family: monospace; color: orange; }
  23. .tag { background-color: #eee; font-family: monospace;
  24. padding: 2px; font-size: 80%; font-weight: normal; }
  25. .timestamp { color: #bebebe; }
  26. .timestamp-kwd { color: #5f9ea0; }
  27. .org-right { margin-left: auto; margin-right: 0px; text-align: right; }
  28. .org-left { margin-left: 0px; margin-right: auto; text-align: left; }
  29. .org-center { margin-left: auto; margin-right: auto; text-align: center; }
  30. .underline { text-decoration: underline; }
  31. #postamble p, #preamble p { font-size: 90%; margin: .2em; }
  32. p.verse { margin-left: 3%; }
  33. pre {
  34. border: 1px solid #e6e6e6;
  35. border-radius: 3px;
  36. background-color: #f2f2f2;
  37. padding: 8pt;
  38. font-family: monospace;
  39. overflow: auto;
  40. margin: 1.2em;
  41. }
  42. pre.src {
  43. position: relative;
  44. overflow: auto;
  45. }
  46. pre.src:before {
  47. display: none;
  48. position: absolute;
  49. top: -8px;
  50. right: 12px;
  51. padding: 3px;
  52. color: #555;
  53. background-color: #f2f2f299;
  54. }
  55. pre.src:hover:before { display: inline; margin-top: 14px;}
  56. /* Languages per Org manual */
  57. pre.src-asymptote:before { content: 'Asymptote'; }
  58. pre.src-awk:before { content: 'Awk'; }
  59. pre.src-authinfo::before { content: 'Authinfo'; }
  60. pre.src-C:before { content: 'C'; }
  61. /* pre.src-C++ doesn't work in CSS */
  62. pre.src-clojure:before { content: 'Clojure'; }
  63. pre.src-css:before { content: 'CSS'; }
  64. pre.src-D:before { content: 'D'; }
  65. pre.src-ditaa:before { content: 'ditaa'; }
  66. pre.src-dot:before { content: 'Graphviz'; }
  67. pre.src-calc:before { content: 'Emacs Calc'; }
  68. pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
  69. pre.src-fortran:before { content: 'Fortran'; }
  70. pre.src-gnuplot:before { content: 'gnuplot'; }
  71. pre.src-haskell:before { content: 'Haskell'; }
  72. pre.src-hledger:before { content: 'hledger'; }
  73. pre.src-java:before { content: 'Java'; }
  74. pre.src-js:before { content: 'Javascript'; }
  75. pre.src-latex:before { content: 'LaTeX'; }
  76. pre.src-ledger:before { content: 'Ledger'; }
  77. pre.src-lisp:before { content: 'Lisp'; }
  78. pre.src-lilypond:before { content: 'Lilypond'; }
  79. pre.src-lua:before { content: 'Lua'; }
  80. pre.src-matlab:before { content: 'MATLAB'; }
  81. pre.src-mscgen:before { content: 'Mscgen'; }
  82. pre.src-ocaml:before { content: 'Objective Caml'; }
  83. pre.src-octave:before { content: 'Octave'; }
  84. pre.src-org:before { content: 'Org mode'; }
  85. pre.src-oz:before { content: 'OZ'; }
  86. pre.src-plantuml:before { content: 'Plantuml'; }
  87. pre.src-processing:before { content: 'Processing.js'; }
  88. pre.src-python:before { content: 'Python'; }
  89. pre.src-R:before { content: 'R'; }
  90. pre.src-ruby:before { content: 'Ruby'; }
  91. pre.src-sass:before { content: 'Sass'; }
  92. pre.src-scheme:before { content: 'Scheme'; }
  93. pre.src-screen:before { content: 'Gnu Screen'; }
  94. pre.src-sed:before { content: 'Sed'; }
  95. pre.src-sh:before { content: 'shell'; }
  96. pre.src-sql:before { content: 'SQL'; }
  97. pre.src-sqlite:before { content: 'SQLite'; }
  98. /* additional languages in org.el's org-babel-load-languages alist */
  99. pre.src-forth:before { content: 'Forth'; }
  100. pre.src-io:before { content: 'IO'; }
  101. pre.src-J:before { content: 'J'; }
  102. pre.src-makefile:before { content: 'Makefile'; }
  103. pre.src-maxima:before { content: 'Maxima'; }
  104. pre.src-perl:before { content: 'Perl'; }
  105. pre.src-picolisp:before { content: 'Pico Lisp'; }
  106. pre.src-scala:before { content: 'Scala'; }
  107. pre.src-shell:before { content: 'Shell Script'; }
  108. pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
  109. /* additional language identifiers per "defun org-babel-execute"
  110. in ob-*.el */
  111. pre.src-cpp:before { content: 'C++'; }
  112. pre.src-abc:before { content: 'ABC'; }
  113. pre.src-coq:before { content: 'Coq'; }
  114. pre.src-groovy:before { content: 'Groovy'; }
  115. /* additional language identifiers from org-babel-shell-names in
  116. ob-shell.el: ob-shell is the only babel language using a lambda to put
  117. the execution function name together. */
  118. pre.src-bash:before { content: 'bash'; }
  119. pre.src-csh:before { content: 'csh'; }
  120. pre.src-ash:before { content: 'ash'; }
  121. pre.src-dash:before { content: 'dash'; }
  122. pre.src-ksh:before { content: 'ksh'; }
  123. pre.src-mksh:before { content: 'mksh'; }
  124. pre.src-posh:before { content: 'posh'; }
  125. /* Additional Emacs modes also supported by the LaTeX listings package */
  126. pre.src-ada:before { content: 'Ada'; }
  127. pre.src-asm:before { content: 'Assembler'; }
  128. pre.src-caml:before { content: 'Caml'; }
  129. pre.src-delphi:before { content: 'Delphi'; }
  130. pre.src-html:before { content: 'HTML'; }
  131. pre.src-idl:before { content: 'IDL'; }
  132. pre.src-mercury:before { content: 'Mercury'; }
  133. pre.src-metapost:before { content: 'MetaPost'; }
  134. pre.src-modula-2:before { content: 'Modula-2'; }
  135. pre.src-pascal:before { content: 'Pascal'; }
  136. pre.src-ps:before { content: 'PostScript'; }
  137. pre.src-prolog:before { content: 'Prolog'; }
  138. pre.src-simula:before { content: 'Simula'; }
  139. pre.src-tcl:before { content: 'tcl'; }
  140. pre.src-tex:before { content: 'TeX'; }
  141. pre.src-plain-tex:before { content: 'Plain TeX'; }
  142. pre.src-verilog:before { content: 'Verilog'; }
  143. pre.src-vhdl:before { content: 'VHDL'; }
  144. pre.src-xml:before { content: 'XML'; }
  145. pre.src-nxml:before { content: 'XML'; }
  146. /* add a generic configuration mode; LaTeX export needs an additional
  147. (add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
  148. pre.src-conf:before { content: 'Configuration File'; }
  149. table { border-collapse:collapse; }
  150. caption.t-above { caption-side: top; }
  151. caption.t-bottom { caption-side: bottom; }
  152. td, th { vertical-align:top; }
  153. th.org-right { text-align: center; }
  154. th.org-left { text-align: center; }
  155. th.org-center { text-align: center; }
  156. td.org-right { text-align: right; }
  157. td.org-left { text-align: left; }
  158. td.org-center { text-align: center; }
  159. dt { font-weight: bold; }
  160. .footpara { display: inline; }
  161. .footdef { margin-bottom: 1em; }
  162. .figure { padding: 1em; }
  163. .figure p { text-align: center; }
  164. .equation-container {
  165. display: table;
  166. text-align: center;
  167. width: 100%;
  168. }
  169. .equation {
  170. vertical-align: middle;
  171. }
  172. .equation-label {
  173. display: table-cell;
  174. text-align: right;
  175. vertical-align: middle;
  176. }
  177. .inlinetask {
  178. padding: 10px;
  179. border: 2px solid gray;
  180. margin: 10px;
  181. background: #ffffcc;
  182. }
  183. #org-div-home-and-up
  184. { text-align: right; font-size: 70%; white-space: nowrap; }
  185. textarea { overflow-x: auto; }
  186. .linenr { font-size: smaller }
  187. .code-highlighted { background-color: #ffff00; }
  188. .org-info-js_info-navigation { border-style: none; }
  189. #org-info-js_console-label
  190. { font-size: 10px; font-weight: bold; white-space: nowrap; }
  191. .org-info-js_search-highlight
  192. { background-color: #ffff00; color: #000000; font-weight: bold; }
  193. .org-svg { }
  194. </style>
  195. </head>
  196. <body>
  197. <div id="content" class="content">
  198. <h1 class="title">Lab5 Solution Amirlan Sharipov (BS21-CS-01)</h1>
  199. <div id="table-of-contents" role="doc-toc">
  200. <h2>Table of Contents</h2>
  201. <div id="text-table-of-contents" role="doc-toc">
  202. <ul>
  203. <li><a href="#orgb4a8d53">1. Question 1</a>
  204. <ul>
  205. <li><a href="#orgb9d88ac">1.1. What is a zombie process</a></li>
  206. <li><a href="#org266fff4">1.2. Finding zombie processes</a></li>
  207. <li><a href="#org459fc7a">1.3. kill vs killall vs pkill</a></li>
  208. <li><a href="#org5ff0fe7">1.4. top</a></li>
  209. <li><a href="#org83cfe45">1.5. kill fun processes script</a></li>
  210. <li><a href="#org48d69a8">1.6. Hello world</a></li>
  211. <li><a href="#org96feea4">1.7. System util</a></li>
  212. </ul>
  213. </li>
  214. </ul>
  215. </div>
  216. </div>
  217. <div id="outline-container-orgb4a8d53" class="outline-2">
  218. <h2 id="orgb4a8d53"><span class="section-number-2">1.</span> Question 1</h2>
  219. <div class="outline-text-2" id="text-1">
  220. </div>
  221. <div id="outline-container-orgb9d88ac" class="outline-3">
  222. <h3 id="orgb9d88ac"><span class="section-number-3">1.1.</span> What is a zombie process</h3>
  223. <div class="outline-text-3" id="text-1-1">
  224. <p>
  225. Zombie processes appear in parent-child process relationships. They are finished executing, but they are still in the process table. For example, when a child process finishes executing, but parent process didn&rsquo;t acknowledge it yet.
  226. </p>
  227. </div>
  228. </div>
  229. <div id="outline-container-org266fff4" class="outline-3">
  230. <h3 id="org266fff4"><span class="section-number-3">1.2.</span> Finding zombie processes</h3>
  231. <div class="outline-text-3" id="text-1-2">
  232. <p>
  233. I created a zombie process using C and then run this command:
  234. </p>
  235. <div class="org-src-container">
  236. <pre class="src src-bash">bat zombie.c
  237. gcc zombie.c
  238. ./a.out &amp;
  239. ps aux | <span style="color: #c792ea;">grep</span> <span style="color: #c3e88d;">"defunct"</span> | <span style="color: #c792ea;">grep</span> -v <span style="color: #c3e88d;">"</span><span style="color: #c3e88d;">grep</span><span style="color: #c3e88d;">"</span>
  240. </pre>
  241. </div>
  242. <pre class="example" id="org9746a26">
  243. #include &lt;stdio.h&gt;
  244. #include &lt;stdlib.h&gt;
  245. #include &lt;unistd.h&gt;
  246. int main() {
  247. pid_t p = fork();
  248. if (p == 0) {
  249. exit(0);
  250. } else {
  251. sleep(10);
  252. }
  253. }
  254. rinri 810248 0.0 0.0 0 0 ? Z 19:11 0:00 [a.out] &lt;defunct&gt;
  255. </pre>
  256. <p>
  257. So, using the last command I can find zombie processes. Let&rsquo;s say that I know that there is only one zombie process. To kill it, let&rsquo;s kill its parent process:
  258. </p>
  259. <div class="org-src-container">
  260. <pre class="src src-bash"><span style="color: #c792ea;">kill</span> -9 $<span style="color: #c792ea;">(</span>ps -o <span style="color: #ffcb6b;">ppid</span>= -p $<span style="color: #f78c6c;">(</span>ps aux | <span style="color: #c792ea;">grep</span> -m <span style="color: #f78c6c; font-weight: bold;">1</span> <span style="color: #c3e88d;">"defunct"</span> | <span style="color: #c792ea;">grep</span> -v <span style="color: #c3e88d;">"</span><span style="color: #c3e88d;">grep</span><span style="color: #c3e88d;">"</span> | awk <span style="color: #c3e88d;">'{print $2;}'</span><span style="color: #f78c6c;">)</span><span style="color: #c792ea;">)</span>
  261. </pre>
  262. </div>
  263. <p>
  264. By using this line, it&rsquo;s possible to create script to kill all the zombie processes.
  265. </p>
  266. </div>
  267. </div>
  268. <div id="outline-container-org459fc7a" class="outline-3">
  269. <h3 id="org459fc7a"><span class="section-number-3">1.3.</span> kill vs killall vs pkill</h3>
  270. <div class="outline-text-3" id="text-1-3">
  271. <p>
  272. kill sends the specified signal to the given pid.
  273. killall sends the specified signal to all the processes by the given process name.
  274. pkill sends the specified signal to all the processes matching the given pattern.
  275. </p>
  276. </div>
  277. </div>
  278. <div id="outline-container-org5ff0fe7" class="outline-3">
  279. <h3 id="org5ff0fe7"><span class="section-number-3">1.4.</span> top</h3>
  280. <div class="outline-text-3" id="text-1-4">
  281. <div class="org-src-container">
  282. <pre class="src src-bash">top -b -n <span style="color: #f78c6c; font-weight: bold;">1</span> | head -n <span style="color: #f78c6c; font-weight: bold;">5</span>
  283. </pre>
  284. </div>
  285. <pre class="example">
  286. top - 19:12:03 up 2:35, 1 user, load average: 2.29, 2.75, 2.98
  287. Tasks: 299 total, 1 running, 298 sleeping, 0 stopped, 0 zombie
  288. %Cpu(s): 17.4 us, 6.5 sy, 0.0 ni, 73.2 id, 0.0 wa, 0.0 hi, 2.9 si, 0.0 st
  289. MiB Mem : 14886.6 total, 6722.9 free, 4607.7 used, 3556.0 buff/cache
  290. MiB Swap: 8243.0 total, 8243.0 free, 0.0 used. 9791.8 avail Mem
  291. </pre>
  292. <p>
  293. (Explaining the first evaluation, results may differ after export)
  294. The Tasks line says, that there is 283 total processes, 1 of them is currently running, others are sleeping. No stopeed and no zombie processes.
  295. The %Cpu(s) line says about processor utilization.
  296. (Taken from the Lab6 file)
  297. us is the percent of time spent running user processes.
  298. sy is the percent of time spent running the kernel.
  299. ni is the percent of time spent running processes with manually configured nice values.
  300. id is the percent of time idle (if high, CPU may be overworked).
  301. wa is the percent of wait time (if high, CPU is waiting for I/O access).
  302. hi is the percent of time managing hardware interrupts.
  303. si is the percent of time managing software interrupts.
  304. st is the percent of virtual CPU time waiting for access to physical CPU.
  305. Values such as id, wa, and st help identify whether the system is overworked.
  306. </p>
  307. </div>
  308. </div>
  309. <div id="outline-container-org83cfe45" class="outline-3">
  310. <h3 id="org83cfe45"><span class="section-number-3">1.5.</span> kill fun processes script</h3>
  311. <div class="outline-text-3" id="text-1-5">
  312. <div class="org-src-container">
  313. <pre class="src src-bash">bash -c <span style="color: #c3e88d;">"exec -a fun</span><span style="color: #f78c6c;">$</span><span style="color: #ffcb6b;">{RANDOM}</span><span style="color: #c3e88d;">process </span><span style="color: #c3e88d;">sleep</span><span style="color: #c3e88d;"> infinity"</span> &amp;
  314. bash -c <span style="color: #c3e88d;">"exec -a fun</span><span style="color: #f78c6c;">$</span><span style="color: #ffcb6b;">{RANDOM}</span><span style="color: #c3e88d;">process </span><span style="color: #c3e88d;">sleep</span><span style="color: #c3e88d;"> infinity"</span> &amp;
  315. bash -c <span style="color: #c3e88d;">"exec -a fun</span><span style="color: #f78c6c;">$</span><span style="color: #ffcb6b;">{RANDOM}</span><span style="color: #c3e88d;">process </span><span style="color: #c3e88d;">sleep</span><span style="color: #c3e88d;"> infinity"</span> &amp;
  316. <span style="color: #ffcb6b;">FUNPROCS</span>=<span style="color: #c3e88d;">"</span><span style="color: #89DDFF; font-weight: bold;">$(</span><span style="color: #89DDFF; font-weight: bold;">pgrep</span><span style="color: #89DDFF; font-weight: bold;"> -f 'fun.*process.*infinity')</span><span style="color: #c3e88d;">"</span>
  317. <span style="color: #c792ea;">echo</span> <span style="color: #c3e88d;">"Found </span><span style="color: #89DDFF; font-weight: bold;">$(</span><span style="color: #89DDFF; font-weight: bold;">echo</span><span style="color: #89DDFF; font-weight: bold;"> "</span><span style="color: #89DDFF; font-weight: bold;">$</span><span style="color: #89DDFF; font-weight: bold;">FUNPROCS</span><span style="color: #89DDFF; font-weight: bold;">" | wc -l)</span><span style="color: #c3e88d;"> processes:"</span>
  318. <span style="color: #c792ea;">echo</span> <span style="color: #c3e88d;">"</span><span style="color: #f78c6c;">$</span><span style="color: #ffcb6b;">FUNPROCS</span><span style="color: #c3e88d;">"</span>
  319. <span style="color: #89DDFF;">for</span> FUNPROC<span style="color: #89DDFF;"> in</span> $<span style="color: #ffcb6b;">FUNPROCS</span>
  320. <span style="color: #89DDFF;">do</span>
  321. <span style="color: #c792ea;">kill</span> -9 <span style="color: #c3e88d;">"</span><span style="color: #f78c6c;">$</span><span style="color: #ffcb6b;">FUNPROC</span><span style="color: #c3e88d;">"</span>
  322. <span style="color: #c792ea;">echo</span> <span style="color: #c3e88d;">"killed </span><span style="color: #f78c6c;">$</span><span style="color: #ffcb6b;">FUNPROC</span><span style="color: #c3e88d;">"</span>
  323. <span style="color: #89DDFF;">done</span>
  324. </pre>
  325. </div>
  326. <pre class="example">
  327. Found 3 processes:
  328. 811145
  329. 811146
  330. 811147
  331. killed 811145
  332. killed 811146
  333. killed 811147
  334. </pre>
  335. </div>
  336. </div>
  337. <div id="outline-container-org48d69a8" class="outline-3">
  338. <h3 id="org48d69a8"><span class="section-number-3">1.6.</span> Hello world</h3>
  339. <div class="outline-text-3" id="text-1-6">
  340. <div class="org-src-container">
  341. <pre class="src src-bash">bat helloworld.sh
  342. </pre>
  343. </div>
  344. <pre class="example">
  345. #!/bin/bash
  346. trap 'echo "Interrupt received"; exit' SIGUSR1
  347. while :
  348. do
  349. echo "Hello World"
  350. sleep 10
  351. done
  352. </pre>
  353. <p>
  354. To kill:
  355. kill -s SIGUSR1 &ldquo;$(ps aux | grep &rdquo;helloworld.sh&ldquo; | grep -v &rdquo;grep&ldquo; | awk &rsquo;{print $2}&rsquo;)&rdquo;
  356. </p>
  357. </div>
  358. </div>
  359. <div id="outline-container-org96feea4" class="outline-3">
  360. <h3 id="org96feea4"><span class="section-number-3">1.7.</span> System util</h3>
  361. <div class="outline-text-3" id="text-1-7">
  362. <div class="org-src-container">
  363. <pre class="src src-bash">bat status.sh
  364. </pre>
  365. </div>
  366. <pre class="example" id="org928686a">
  367. #!/bin/bash
  368. while :
  369. do
  370. CPUUSAGE="$(top -b - n 1 | grep "Cpu" | awk '{print 100-$8}')%"
  371. MEMUSAGE="$(free -h | grep "Mem" | awk '{print $3/$2*100}')%"
  372. # disk usage for root directory
  373. DISKUSAGE="$(df -h | awk '{ if ($6 == "/")
  374. print $5;
  375. }')"
  376. echo "$(date) CPU: $CPUUSAGE Mem: $MEMUSAGE Disk:$DISKUSAGE" &gt;&gt; /var/log/system_utilization.log
  377. sleep 15
  378. done
  379. </pre>
  380. <div class="org-src-container">
  381. <pre class="src src-bash">bat /var/log/system_utilization.log
  382. </pre>
  383. </div>
  384. <pre class="example">
  385. Thu Mar 9 07:09:13 PM MSK 2023 CPU: 43.2% Mem: 32.1429% Disk:70%
  386. Thu Mar 9 07:09:29 PM MSK 2023 CPU: 45.7% Mem: 32.1429% Disk:70%
  387. Thu Mar 9 07:09:44 PM MSK 2023 CPU: 34.1% Mem: 32.1429% Disk:70%
  388. Thu Mar 9 07:09:59 PM MSK 2023 CPU: 28.5% Mem: 32.1429% Disk:70%
  389. Thu Mar 9 07:10:14 PM MSK 2023 CPU: 22.5% Mem: 32.1429% Disk:70%
  390. Thu Mar 9 07:10:30 PM MSK 2023 CPU: 34.8% Mem: 32.1429% Disk:70%
  391. Thu Mar 9 07:10:45 PM MSK 2023 CPU: 37.6% Mem: 32.1429% Disk:70%
  392. Thu Mar 9 07:11:00 PM MSK 2023 CPU: 29.8% Mem: 32.1429% Disk:70%
  393. </pre>
  394. </div>
  395. </div>
  396. </div>
  397. </div>
  398. <div id="postamble" class="status">
  399. <p class="author">Author: Amirlan Sharipov (BS21-CS-01)</p>
  400. <p class="date">Created: 2023-03-09 Thu 19:12</p>
  401. </div>
  402. </body>
  403. </html>