Tag: Linux shell

  • 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

  • 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!

  • 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

  • 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

  • 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

  • Linux Tutorial Series – 105 – A trip back down the OS lane – threads

    Here is the video version, if you prefer it:

    Today, let’s go back down the good ol’ OS lane and talk about something called threads. A side note: I tried to fit in “memory lane”, but I couldn’t; it wouldn’t fit the content.

    A thread is a sequence of commands that can be executed. A process always has at least one thread, but it can have multiple threads.

    “Why have multiple threads?”, you may ask. A fair question. If you divide up the task into multiple threads (and assuming you have multiple processor cores), each thread can execute in its own core and the job can be done faster. An example: Say you were multiplying two matrices – each was of dimensions 20 000 x 20 000. You could do it in one thread, or maybe you could create 4 threads, each dealing with a particular part of the matrix. Or you could have 8 threads, or 80 threads – doesn’t matter. The point is, the more threads you have, the faster the program will execute, if you have the appropriate processor (one that has multiple cores) or multiple processors in your system (as in servers).

    A thing I want to mention: Even if you have one processor core, you may have many apps open and they seem to be working flawlessly. In that case, what is actually happening is that you have an illusion of multitasking – your processor is just switching between programs very fast. You don’t even notice that switch, but again, it is an illusion of parallel execution. (Ward, 2014)⁠

    A thing I also want to mention: Dealing with threads is hard. Like, real hard. An example: Say you have two threads running concurrently (at the same time). Let’s call them thread A and thread B. Say both of them want to write in the same memory location. What can happen? Well, thread A can write its result first, then thread B can write its result and you have the result of the thread B. The reverse is also possible – thread B can write its result and that result can get overridden by thread A. Or, thread A can be writing its result, thread B starts writing its result in the middle of thread A writing its result… It is a mess. And again, to repeat, it is hard to manage threads. There are mechanisms to deal with thread management, but just know that dealing with threads is hard.

    Hope you learned something useful!

    References

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Page 5-6

  • Linux Tutorial Series – 104 – The pgrep and pkill commands

    Here is the video version, if you prefer it:

    Today, I will just throw a bug in your ear (this is a colloquialism in Croatia for “useful for you to know”) and tell you about two commands I have used – pgrep and pkill.

    You most likely won’t be needing those commands in most cases, but sometimes you know only the process name, but don’t know its ID and you want to end the process. Then you can use pgrep in combination with xargs (see here: (“Can I chain pgrep with kill?,” n.d.)⁠) or use pkill with the process name as the argument (“pkill(1) – Linux man page,” n.d.)⁠.

    Hope you learned something useful!

    References

    Can I chain pgrep with kill? (n.d.). Retrieved February 10, 2020, from https://unix.stackexchange.com/questions/138202/can-i-chain-pgrep-with-kill

    pkill(1) – Linux man page. (n.d.). Retrieved February 10, 2020, from https://linux.die.net/man/1/pkill

  • Linux Tutorial Series – 103 – The kill command

    Here is the video version, if you prefer it:

    Let’s talk about how to kill a process. That means to end a process; to terminate it. You can do this with the kill command.

    Usage of kill is this:

    kill <PID>

    where PID is the process ID of the process you want to kill (you can attain this ID using ps with its various options). By default, kill sends a TERM signal to the process. (Ward, 2014)⁠ You don’t really need to know any other signals. If the process doesn’t terminate after that, you can try using

    kill -KILL <PID>

    which sends the KILL signal, but use it as the last resort – it ends the process in an ugly way.

    Hope you learned something new!

    References

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

  • Linux Tutorial Series – 102 – Signals in operating systems – what are they used for?

    Here is the video version, if you prefer it:

    Today we will take a look at signals. They are very important in regard to operating systems and so they are relevant for Linux. Signals are a way for processes to communicate with one another. (“Signal (IPC),” n.d.)⁠

    Processes can’t access each other’s memory locations. So if Process A has memory locations 0 to 99 and Process B has memory locations 100 to 199, then if Process A tries to access memory location 129, operating system will not allow it.

    But, it is useful for processes to be able to communicate with each other. Let’s say Process A wants to somehow say to Process B “Stop! Stop!”. It can use a signal to do so.

    There are a lot of signals you can send to a process and they are listed here: (“POSIX signals,” n.d.)⁠. You don’t need to know their names. Just know what they are used for – communication between processes (also called inter-process communication (IPC)).

    Thank you for reading!

    References

    POSIX signals. (n.d.). Retrieved February 10, 2020, from https://en.wikipedia.org/wiki/Signal_(IPC)#POSIX_signals

    Signal (IPC). (n.d.). Retrieved February 28, 2020, from https://en.wikipedia.org/wiki/Signal_(IPC)

  • Linux Tutorial Series – 101 – The jobs command

    Here is the video version, if you prefer it:

    The jobs command displays status of jobs in your current Terminal session. (Barrett, 2016)⁠ Jobs are programs you are running in your current shell. The difference between jobs and processes is that processes are a operation systems term, while jobs are shell specific term – job is any program you start in the shell (whether in the foreground or in the background). (“What is the difference between a job and a process?,” n.d.)⁠

    I never used this command so far, so just mentioning it here.

    Thank you for reading!

    References

    Barrett, D. J. (2016). Linux pocket guide (3rd ed.). O’Reilly Media. Page 40

    What is the difference between a job and a process? (n.d.). Retrieved February 28, 2020, from https://unix.stackexchange.com/questions/4214/what-is-the-difference-between-a-job-and-a-process