Lab 6: Processes and signals

Exercise 1: Managing processes

Task 1: Process id and jobs

Starting full screen programs like “nano” or “top” in background are immediately stopped (by a SIGTTIN signal - when they try to read from the tty, or by a SIGTTOU signal when they try to change the tty parameters). Many programs handle that badly.
About tty and signals: http://www.linusakesson.net/programming/tty/index.php

CTRL+C – sends to a process by its controlling terminal (by the TTY driver) SIGINT signal to the current foreground job.

Task 2: The proc file system

The /proc/ directory — also called the proc file system — contains a hierarchy of special files which represent the current state of the kernel — allowing applications and users to peer into the kernel’s view of the system.
Within the /proc/ directory, one can find a wealth of information detailing the system hardware and any processes currently running. In addition, some of the files within the /proc/ directory tree can be manipulated by users and applications to communicate configuration changes to the kernel.

Task 3: top

This opens up a tool that shows the top processes running on your system. This tool can be used to kill processes, renice processes, sort and various other process management. Press the h command to get a list of help.
Read material:
https://www.guru99.com/managing-processes-in-linux.html

Uptime
Top’s first line, top, shows the same information as the uptime command. The first value is the system time. The second value represents how long the system has been up and running, while the third value indicates the current number of users on the system. The final values are the load average for the system.

The load average is broken down into three time increments. The first shows the load for the last one minute, the second for the last five minutes, and the final value for the last 15 minutes. The results are a percentage of CPU load between 0 and 1.0. The processor is likely overworked if 1.0 (or higher) is displayed.

top - 00:49:59 up 1 day, 12:12,  3 users,  load average: 0,63, 0,66, 0,64

Tasks
The second line is the Tasks output, and it’s broken down into five states. These five states display the status of processes on the system:

Tasks: 386 total,   1 running, 384 sleeping,   1 stopped,   0 zombie

%Cpu(s)
Values related to processor utilization are displayed on the third line. They provide insight into exactly what the CPUs are doing.

%Cpu(s):  4,1 us,  0,4 sy,  0,0 ni, 95,3 id,  0,0 wa,  0,0 hi,  0,1 si,  0,0 st

MiB Memory
The final two lines of top’s output provide information on memory utilization. The first line—MiB Mem—displays physical memory utilization. This value is based on the total amount of physical RAM installed on the system.

MiB Mem :  15967,8 total,    260,9 free,   2749,7 used,  12957,2 buff/cache

Note: The term mebibyte (and similar units, such as kibibytes and gibibytes) differs slightly from measurements such as megabytes. Mebibytes are based on 1024 units, and megabytes are based on 1000 units (decimal). Most users are familiar with the decimal measurement, but it is not as accurate as the binary form. The top utility reports memory consumption in decimal.

MiB Swap
Linux can take advantage of virtual memory when physical memory space is consumed by borrowing storage space from storage disks. The process of swapping data back and forth between physical RAM and storage drives is time-consuming and uses system resources, so it’s best to minimize the use of virtual memory.

MiB Swap:   2048,0 total,   2047,5 free,      0,5 used.  12739,8 avail Mem

In general, a high amount of swap utilization indicates the system does not have enough memory installed for its tasks. The solution is to either increase RAM or decrease the workload.

Task 4: free

free is a popular command used by system administrators on Unix/Linux platforms. It’s a powerful tool that gives insight into the memory usage in human-readable format.
The man page for this command states that free displays the total amount of free and used memory on the system, including physical and swap space, as well as the buffers and caches used by the kernel. The information is gathered by parsing /proc/meminfo.

Questions to answer

  1. What are zombie processes? How can you find and kill them?
  2. What are the differences between kill, killall, and pkill?
  3. Run the top command on your system and annotate the data in the Tasks and %Cpu(s) lines of your output. Provide single sentence explanations for each of the data presented in these two lines.
  4. Execute the following bash command:
    $ bash -c "exec -a fun${RANDOM}process sleep infinity" &
    
    • Assume that there are multiple of such processes. To simulate this, you can run the command more than once.
    • Write a bash script that will locate and kill all the processes created by this command.
    • Display status messages when one of such processes is found, and when the process is killed. Additionally, display a message when the process is not found.
    • Your script should work on any machine it is executed on.
    • Be extremely careful and be as accurate as possible when finding this process. You don’t want to kill the wrong process.

    Show test results in the form of screenshots.

  5. Write a bash script that loops infinitely and prints “Hello world!” every ten seconds. It should print “Interrupt received” when it receives SIGUSR1.
    • Show the script in your report, and show how you’re sending the signal to it.
  6. Write a bash script to monitor CPU usage, memory usage, and disk space usage.
    • For testing purposes, the check should execute every 15 seconds.
    • The usage statistics should be saved to a log file /var/log/system_utilization.log.
    • One line of log should contain the timestamp, the % of CPU in use, the % of memory in use, and the % of disk space used.
    • The log should contain descriptive information that will make it easy to understand.

    Show log samples created by this script in your report.