# Lab 1: Introduction to Linux. Morozova Lada
## Exercise 1. Text processing
### What is `/etc/apt/sources.list.d` directory stands for? How can you use it?
The `/etc/apt/sources.list.d` directory allows to add and remove new directories without editing the `/etc/apt/sources.list` file. Check example with explanation in [Example](#example).
### How can you add/delete 3rd party repositories to install required software?
To add third party reposiory to `/etc/apt/sources.list.d` command `add-apt-repository ppa:<ppa_name>` can be used. This command will add the file with extension `.list` to the `/etc/apt/sources.list.d` and append the information about repository to the `/etc/apt/sources.list`.
To remove third party repository from `/etc/apr/sources.list.d` by using command `add-apt-repository --remove ppa:<ppa_name>`. The command will remove files related to repository from `/etc/apr/sources.list.d` and remove entry about the repository from `/etc/apr/sources.list`.
Check example with explanation in [Example](#example).
### When do you need to get public key for the usage of the remote repo?
The public keys allow to verify a signature of release files, e.g. whether files in repository belongs to claimed identity. Check example with explanation in [Example](#example).
### Example <a name="example"> </a>

*First image is present the content of `/etc/apt/sources.list.d` before installation of any third party repository.*

*Second image is present the extraction of public key for ppa repository by fingerprint with use of command `apt-key`. Also screenshot contains the log of execution of command `add-apt-repository` for [graphic drivers](https://launchpad.net/~graphics-drivers/+archive/ubuntu/ppa).*

*Third image contains the execution of `apt-get update` which updates the package lists after new packages were installed. After the update we can see that file with extenion `.list` is appered in the `/etc/apt/source.list.d`.*
## Exercise 2. Managing processes
### Describe how “top” works. Explain all important fields in its output based on your system resources.
The `top` provides dynamic real-time view of running system. `top` uses `/proc/` to quering the Linux kernel about its state. Each process have a directory under `/proc/` that describes it.

- Load average - measurment how busy sytem is; 3 number represent load over 1 minute, 5 minute, and 15 minutes.
- Tasks - describe number of processes running on machine and their status
- %Cpu(s) - percentage of CPU used to perform group of tasks:
- 1.6 us - 1.6% of CPU time used by user processes
- 5.3 sy - 5.3% of CPU time used by kernel processes
- 0.0 ni - no runnig niced processes
- 91.9 id - 91.9% of CPU time spent in idle handler
- 0.4 wa - 0.4% of CPU time spent for waiting I/O completion
- 0.0 hi - no time is spent on handling hardware interrupts
- 0.8 si - 0.8% of CPU time spent on handling software interrupts
- 0.0 st - no time was stolen from this vm by supervisor
**PID** - process ID.
**USER** - user to which process belong.
**PR** - scheduling priority of task used by Linux kernel.
**NI** - nice value of task that allow to control process priority.
**VIRT** - amount of virtual memory used by process.
**RES** - non-swapped physical memory a task is currently using.
**SHR** - shared memory size that another processes may use
**S** - process status
**%CPU** - task's share of the elapsed CPU time expressed percentage of total CPU time
**%MEM** - used percentage of memory
**TIME+** - total CPU time that process has used since it start
**COMMAND** - command line used to start a task or the name of the associated program
Let's take as example process of Firefox web brouser. It has the process ID = 8148. The process belong to "ladamoroz" user and have schedulig priority 20 seen by Linux kernel. Process have nice value 0 as user does not change it priority. Process uses 3523432 KiB of virtual memory which include all code, data, shared libraries, swapped pages, and mapped but not used pages. Process use 339252 KiB physical memory and 176304 KiB shared memory. Process' state is sleeping. Process used 5.6% of total CPU time and 8.4% of available physical memory. Process run for 0:11:49 and was created by firefox program.
### Explain briefly each process states from the output of the “top” command.
- **Running** - tasks which currently running or queued for run.
- **Sleeping** - tasks that waiting for some resource (e.g. I/O, disk, etc.).
- **Stopped** - tasks which was paused during running i.e. received suspend signal.
- **Zombie** - tasks which completed execution but did not removed from process table.
### What happens to a child process that dies and has no parent process to wait for it and what’s bad about this?
When child process have no parent process then child process become a child of init process. The init process now is wait for child process to terminate. If a lot of processes become zombie without parent then process table can overflow and system may not be able to create new processes.
### How to know which process ID used by application?
To check process id and other information about process used by aplication following command can be used
```
top -p $(pgrep -d , <program>)
```

*The following screenshot depictes the result of `top -p $(pgrep -d , firefox)`*
### What is a zombie process and what could be the cause of it? How to find and kill zombie process?
Zombie process results due to parent process that does not wait for child process. After child process terminated no other process is removed it from process table; the entry of terminated child process still present in process table which results in zombie state of process.
Shell pipes and shell scripts with background tasks which terminate before end of execution of background tasks can be considerd as examples of situations when zombie processes are created.
**Dealing with zombie process**
Determine the processes with Z in S field or "defunct" in COMMAND field
```
ps aux | egrep "Z|defunct"
```
Finding the parent process' PID
```
ps -o ppid= <child_pid>
```
Notifying the parent to attempt read the child process' status and remove it from process table
```
kill -s SIGCHLD <parent_pid>
```
If it does not remove zombie process when it necessary either to restart parent process or kill it
```
kill -9 <parent_pid>
```