Categories
Linux Tutorial Series

Linux Tutorial Series – 115 – File modes and permissions

Here is the video version, if you prefer it:

First of all, let’s talk about why are file modes and permissions important. Well, if you don’t have the right permissions, you might not be able to modify certain files (either read, write or execute them). That’s why sometimes you get the error saying “Permission denied”. That is exactly what happens – you don’t have the right permissions.

Every Linux file has a set of permissions that determine whether you have the rights to read, write or execute the file. (Ward, 2014)⁠ Let’s employ ls -l to find out permissions of a certain file:

mislav@mislavovo-racunalo:~/Linux_folder$ ls -l

total 36

-rw-r--r-- 1 mislav mislav 8 Feb 5 23:09 1264.txt

...

Let’s focus on the 1264.txt file. The permissions part is the following:

-rw-r--r--

Let’s break it down, looking from left to right:

  • - - this indicates the file type; - indicates a regular file, d indicates a directory and there’s some other file types as well (like links (l), which we will cover later; just know that they exist)
  • rw- – this indicates the user permissions – this file was created by the user mislav and this user can both read and write the file
  • r-- – this indicates the group permissions – this file belongs to a group called mislav (the 4th column in the ls -l output) and it can only read the file; it can’t write to it
  • r-- – this indicates the world permissions – the permissions of everyone else who is neither the user nor the group member

- means that that option is not supported and a letter in place of - means that that operation is supported. If in the last position x is used as a letter, that means that that the file is able to be executed.

A note on groups: If only you use your computer, you most likely won’t have to mangle with groups. Groups are sets of users – a convenient way to modify permissions for a set of users within a group (you can add users to groups and remove users from groups and change the group a file belongs to), but I never had to mangle with groups myself.

Hope you learned something useful!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 10; 33-34

Categories
Linux Tutorial Series

Linux Tutorial Series – 113 – Review – revisiting top’s columns

Here is the video version, if you prefer it:

Let’s look at the output produced by top and explain what does it all mean:

mislav@mislavovo-racunalo:~/Linux_folder$ top

top - 13:15:58 up 101 days, 19:37, 1 user, load average: 0.44, 0.42, 0.44

Tasks: 241 total, 1 running, 239 sleeping, 0 stopped, 1 zombie

%Cpu(s): 0.2 us, 0.1 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

MiB Mem : 7853.8 total, 127.8 free, 4300.2 used, 3425.8 buff/cache

MiB Swap: 8066.0 total, 6694.0 free, 1372.0 used. 2891.4 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

8770 mislav 20 0 3002016 320192 111444 S 1.0 4.0 5:46.85 Web Content

8357 mislav 20 0 3524552 369556 151704 S 0.3 4.6 5:26.81 firefox-esr

14417 mislav 20 0 4222052 321880 168376 S 0.3 4.0 0:31.67 anki

...

  • First we have some information about current time, how long my machine has been running and how many users are logged in (Shotts, 2019)⁠
  • load average – how many processes is your computer executing in the last minute, last 5 minutes, last 15 minutes
  • Tasks – how many tasks are there on the computer – 1 is running (meaning being actively executed), 239 are sleeping (meaning waiting for something to happen (such as data from a device) to resume their execution), 0 stopped (meaning no processes whose execution was paused manually) and 1 zombie process (zombie process is a process whose parent process doesn’t exist anymore)
  • %Cpu(s) tells us what percentage of the CPU is being used on what kinds of processes: us is for user processes, sy is for system (kernel) processes, ni is for nice (low-priority) processes, id is for the percentage of the CPU that is idle, wa is for percentage of the CPU waiting for some input/output tasks, hi is the time spent processing hardware interrupts, si is the time processing software interrupts and st is relevant to virtual environments – if you don’t know what virtual environments are, that needn’t concern you (“Linux ‘top’ command: What are us, sy, ni, id, wa, hi, si and st (for CPU usage)?,” n.d.)⁠
  • MiB Mem and MiB swap tell you how much RAM and how much swap space is being used (measured in mebibytes (“Mebibyte,” n.d.)⁠)

Let’s now look at the columns available for each process: (“A Guide to the Linux ‘Top’ Command,” n.d.)⁠

  • PID – proces ID
  • USER – user who owns the process
  • PR – process priority
  • NI – niceness (nice value) of a process
  • VIRT – total amount of memory consumed by the process (permanent storage device + RAM – the basic idea is that if the operating system runs out of RAM, it can use some memory available on the permanent storage device in addition to the RAM; this concept is called virtual memory – look it up on Google if you are interested)
  • RES – memory consumed by the process in RAM
  • SHR – amount of memory shared with other processes (processes can share memory)
  • S – process state (is the process running, is it sleeping, …)
  • %CPU – how much CPU is the process using (in percentages)
  • %MEM – how much memory is the process using (in percentages)
  • TIME+ – total time used by the process since it started
  • COMMAND – the name of the process

Hope you understand the output of top in detail now and hope you refreshed your memory!

References

A Guide to the Linux “Top” Command. (n.d.). Retrieved February 19, 2020, from https://www.booleanworld.com/guide-linux-top-command/#Understanding_top8217s_interface_the_task_area

Linux “top” command: What are us, sy, ni, id, wa, hi, si and st (for CPU usage)? (n.d.). Retrieved February 19, 2020, from https://unix.stackexchange.com/questions/18918/linux-top-command-what-are-us-sy-ni-id-wa-hi-si-and-st-for-cpu-usage

Mebibyte. (n.d.). Retrieved February 19, 2020, from https://en.wikipedia.org/wiki/Mebibyte

Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 137-139

Categories
Linux Tutorial Series

Linux Tutorial Series – 112 – Parent and child processes

Here is the video version, if you prefer it:

Let’s talk about parent and child processes.

Every process has to be instantiated (created) by some other process. The process which created the process is called the parent process and the created process is called the child process.

Keep this distinction in mind.

Thank you for reading!

Categories
Linux Tutorial Series

Linux Tutorial Series – 111 – Resource monitoring with vmstat and other commands

Here is the video version, if you prefer it:

In this short article I would like to mention a few commands that enable you to monitor your computer resource usage. They are: (Ward, 2014)⁠

  • vmstat
  • iostat
  • iotop
  • pidstat

I never used any of the above commands and so I am leaving it here for you to explore if you ever have the need. So far, I haven’t had the need.

A note: To understand the output of some of the commands above, you may need to understand how computer memory works in modern computers. In particular, you need to understand what is virtual memory and what are pages and what is a page fault. A Wikipedia article read on the topics mentioned in the previous sentence and further Googling on the topics that you don’t understand will be enough for you to gain a grasp of what is going on.

Thank you for reading!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 183-188

Categories
Linux Tutorial Series

Linux Tutorial Series – 110 – Load averages

Here is the video version, if you prefer it:

Let’s talk about load averages today. Load averages display how many processes (on average) are ready to run at this moment. You can display this information with uptime. (Ward, 2014)⁠

For example:

mislav@mislavovo-racunalo:~$ uptime

21:15:27 up 97 days, 3:36, 1 user, load average: 0.11, 0.14, 0.18

0.11 tells me that in the last minute my CPU (processor) has been dealing with 0.11 processes, in the last 5 minutes it has been dealing with 0.14 processes and in the last 15 minutes it has been dealing with 0.18 processes.

If you have multiple cores (I have 4), then a load average of 1 means that one core has been busy all the time, while other 3 have been “chilling out”. The point is that when considering the load averages, factor in the number of cores in your computer and if you are in fact dealing with a high load average, use top to find out what process is using up most resources (it will be at the top of the top’s list).

Thank you for reading!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 180-181

Categories
Linux Tutorial Series

Linux Tutorial Series – 109.1 – Difference between signals and interrupts

The question you may have is: “What is the difference between signals and interrupts?

The difference is as follows: Interrupts are the communication between the CPU (Central Processing Unit – your processor) and the operating system (the kernel), and signals are the communication between processes and the operating system (the kernel). (“Signals and interrupts a comparison,” n.d.)⁠

Let’s go into a bit more depth:

When an interrupt occurs (initiated by either hardware or software) it is actually managed by the CPU itself, which “interrupts” (pauses) the execution of the current process and tells the kernel to invoke the interrupt signal handler (which, to recap, is a program designed to handle interrupts). Signals, on the other hand, are used to communicate between processes. But, when the signal is traveling from the sending process to the receiving process, it is managed by the kernel, which invokes the action appropriate for the signal the process received.

I hope you gained some clarity on the difference between the two. This isn’t so that important and honestly I could have left out the part with the interrupts, but I just wanted for you to know about them since we were already talking about the operating system at such a low level. If you didn’t quite catch it, don’t worry – it won’t be that much of a hinderance.

Hope you learned something useful!

References

Signals and interrupts a comparison. (n.d.). Retrieved February 14, 2020, from https://stackoverflow.com/questions/13341870/signals-and-interrupts-a-comparison

Categories
Linux Tutorial Series

Linux Tutorial Series – 109 – Hardware and software interrupts

Here is the video version, if you prefer it:

This is for the curious souls out there. I hope there are some. Even if you are not as curious, you will benefit from reading this as it paints the bigger picture.

You may have pondered something along the lines of: “OK, there exist processes. OK, processes have priorities. But what is the connection between hardware and software? That is, how does the operating system know that we, for example, moved our mouse? Are we dealing with signals?” Not quite, but the concept is very similar.

Here is where the concept of hardware and software interrupts comes in. Basically, hardware and software interrupts tell the operating system (the kernel): “Hey, deal with me!”. For example, pressing a key on your keyboard triggers a hardware interrupt and your operating system processes it. Interrupts also have priorities, because multiple interrupts can occur at the same time and they need to be handled according to their urgency. It is also important to note that there are programs called interrupt handlers that are executed when an interrupt occurs.

An important caveat: This is not exactly how it works, but it paints the picture. In the next post, I will clarify the details, but they are minor and don’t impact your understanding that much.

Hope you learned something new!

Categories
Linux Tutorial Series

Linux Tutorial Series – 108 – Process priorities

Here is the video version, if you prefer it:

Let’s talk about process priorities today. Why do processes even have priorities?

Let’s say that the world within your computer is like the real world. Let’s further imagine you are going about your day, doing your thing, driving your car, when all of a sudden you hear an ambulance. Uh-oh. You know you have to move yourself out of the ambulance’s way, because it has priority.

By the same token, processes in your operating system have priorities, depending on how important they are. In Linux, processes have priorities which range from -20 to 20, with -20 being the most important. Yes, you read that right: -20 is the highest priority. (Ward, 2014)⁠

There is also something called a nice value, which is added to the process priority. By default, it is 0. This makes sense – as we learned in the previous paragraph, the higher the priority (in terms of its number), the lower it actually is. So if a processes nice factor is 10, then its real priority is whatever priority it has + 10. The higher the nice value, the more nice the process, since it effectively lowers its own priority. You will most likely never have to meddle with the nice level of a process.

Hope you learned something useful!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 179-180

Categories
Linux Tutorial Series

Linux Tutorial Series – 107 – Measuring how much time it takes for your program to execute

Here is the video version, if you prefer it:

If you are a software developer, you sometimes want to know how much time does your program need to execute. Or, if you are a “regular” Linux user, maybe you want to know how much time a command takes to execute (albeit most likely not). Here is how to measure it: (Ward, 2014)⁠

time programName

An example:

mislav@mislavovo-racunalo:~$ time ls

...

real 0m0.004s

user 0m0.000s

sys 0m0.004s

There are 3 relevant times: (“What do ‘real’, ‘user’ and ‘sys’ mean in the output of time(1)?,” n.d.)⁠

  • real time – the amount of CPU time from starting the call to finishing the call of the process; this includes the time your process waited for some resource and the time the processor was executing other processes (your processor can switch to another process and execute a fraction of that process, then get back to the execution of your process)
  • user time – the amount of CPU time spent within the process
  • sys time – the amount of CPU time spent in the kernel within the process; if you wanted to do some stuff that only the kernel can do (remember that a regular user can’t do everything), you call the kernel function to do that and then this gets added up to sys time

The CPU time your process takes up is user + sys time.

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 178-179

What do “real”, “user” and “sys” mean in the output of time(1)? (n.d.). Retrieved February 14, 2020, from https://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1

Categories
Linux Tutorial Series

Linux Tutorial Series – 106 – Viewing threads

Here is the video version, if you prefer it:

To view threads you are currently running, you can either execute ps m or execute top, then press H to see a list of all the threads. (Ward, 2014)⁠ Examples for both:

mislav@mislavovo-racunalo:~$ ps m

PID TTY STAT TIME COMMAND

5008 pts/1 - 0:00 bash

- - Ss 0:00 -

6695 tty2 - 0:00 /usr/lib/gdm3/gdm-wayland-session /usr/bin/gnome-sess

- - Ssl+ 0:00 -

- - Ssl+ 0:00 -

- - Ssl+ 0:00 -

Where there is a dash instead of a PID, that’s a thread.

top - 20:44:14 up 96 days, 3:05, 1 user, load average: 0.58, 0.62, 0.62

Threads: 864 total, 1 running, 862 sleeping, 0 stopped, 1 zombie

%Cpu(s): 3.5 us, 4.1 sy, 0.0 ni, 92.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st

MiB Mem : 7853.8 total, 1109.6 free, 4105.2 used, 2639.0 buff/cache

MiB Swap: 8066.0 total, 6836.9 free, 1229.1 used. 3052.6 avail Mem

I got to this by pressing H after I ran top.

Side note: I don’t think you will ever have to mangle with threads (I haven’t), but pays to know what they are and how to view them.

Hope you learned something useful!

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 177-178