The Processes - LINUX: The Ultimate Beginner’s Guide! (2015)

LINUX: The Ultimate Beginner’s Guide! (2015)

Chapter 4: The Processes

You should also understand processes if you want to be a Linux user. In this chapter, you’ll learn about the different computer processes performed in Linux operating systems.

The Basics

Multi-users and Multi-tasking

Now that you know how to interact with the system, you may study computer processes in more detail. Some commands cannot be executed using a single process. There are commands (e.g. “mozilla”) that trigger a group or series of processes.

Additionally, Linux is based on the Unix system, where it is natural to have different users running different commands simultaneously. Obviously, you need to make sure that the computer’s processor can handle all the active processes. You should also provide a functionality that allows users to switch between different processes. In certain cases, you have to continue running a process even if the user who initiated it already logged out. You should also give users the capability to resume interrupted processes.

Different Types of Processes

Interactive Processes

You have to use a terminal session to start and control interactive processes. That means you can only start an interactive process if you are already connected to the system. Basically, the system doesn’t start these processes automatically as part of its basic functions. An interactive process runs in the foreground, taking over the terminal that started it. When this happens, you can’t initiate other programs as long as that particular process is active. As an alternative, you may run them in the background, so that the terminal you used can receive other commands.

Linux shells provide a feature called “job control.” This feature allows you to handle various processes easily. Job control can switch processes between the background and the foreground. With this, you may quickly start processes in the background.

Important Note: You can only run processes in the background for programs that don’t require user inputs. In general, tasks are placed in the background if they take a long time to complete.

The following list shows the common control applications for Linux systems:

· “bg” – This is used to reactivate an interrupted process in the background.

· “fg” – This returns the process onto the foreground.

· “jobs” – It shows the commands being run in the background.

· “kill” – This ends a task.

· “regular_command” – This runs the command in the system’s foreground.

· “command &” – This runs the command in the system’s background. It releases the terminal: you can run other programs while the previous command is still active.

· Ctrl + Z – This stops (suspend, but not terminate) a process that runs in the system’s foreground.

· Ctrl + C – This interrupts (stops and terminates) a process that runs in the foreground.

· “%n” – The system assigns a number to each process that runs in the background. Using the “%” symbol, you can refer to a process through its number (e.g. bg %2).

Automatic Processes

Automatic (also known as batch) processes are not linked to any terminal. Instead, these are jobs that can be lined up in the spooler area, where they will be performed on a first-in first-out basis. These processes can be executed using one of these methods:

· At a specific time and date – You must use the “at” command to accomplish this.

· At times when the overall system load is sufficiently low to receive additional tasks. You can do this through the “batch” command. In general, processes are lined up so they can be executed once the system load is below 0.8. In huge systems, the administrator may utilize batch processing when huge amounts of data need to be processed or when tasks requiring many resources need to be performed on an already busy system. You can also use batch processing to optimize system performance.

Daemons

These are server processes that continuously run. Often, they are activated during startup and wait in the system’s background until their services are needed. A common example is “xinetd”, the networking process, which is triggered in each boot procedure. After the booting process, xinetd waits until a program (e.g. a POP3 client) needs to connect to the internet.

The Boot Process

One of the popular features of Linux is its method of initiating and suspending the OS, where it gets specific programs through their ideal settings, allows you to modify the settings and the boot procedure, and stops in an organized and graceful way.

Beyond manipulating the boot and shutdown processes, the “openness” of Linux systems helps you to determine the causes of problems related to shutting down or booting up your machine.

The Procedure

When a computer is started up, the CPU looks for the Basic Input/Output System (BIOS) and executes it. This program is coded into the computer’s permanent ROM (read-only memory) and is available for use anytime. The computer’s BIOS provides peripheral devices with a low-level interface and manipulates the first phase of the startup procedure.

The BIOS checks the entire system, searches for and tests peripheral devices, and searches for a drive that can be used to start the system. Often, BIOS checks the CD-ROM drive (for newer OS) or the floppy drive (for older ones) for usable media. The sequence of the drives utilized for the booting process is commonly managed by a certain BIOS configuration on the operating system. Once a Linux system is installed onto the computer’s hard drive, the BIOS searches for an MBR (i.e. master boot record) starting at the initial part of the first hard disk, puts the data into the memory, and gives control to it.

The Master Boot Record holds the steps on how to use the LILO or GRUB boot-loader, through a predetermined OS. Afterward, the MBR activates the boot-loader, which controls the rest of the process (that is, if a boot-loader is installed in the Master Boot Record). In the standard configuration of the Red Hat OS, grub utilizes the settings in the master boot record to show available boot choices. Once grub gets the right instructions for the OS to boot, either from the configuration file or its own command line, it acquires the required boot file and passes the control of the computer to the OS.

The Different Features of GRUB

This method of booting up a computer is known as direct loading. This is because no transitional code exists between the boot-loader and the OS’s main files (e.g. the kernel). Thus, startup instructions are utilized to load the OS directly.

On the other hand, the boot procedure used by other systems may be different from the one given above. For instance, MS-DOS and Windows OS overwrite the MBR completely as soon as they are installed without using any part of the existing MBR’s settings. This deletes the data stored by other operating systems (e.g. Linux) inside the MBR. Microsoft’s operating systems, just like other commercial systems, are booted through a process called chain loading startup method. In this method, the MBR checks the initial segment of the partition that holds the OS, where it gets the files required to boot the operating system.

Grub can support both of these methods. That means you can use grub with almost all operating systems, common file structures, and hard disks that can be recognized by your BIOS.

GRUB has other useful features. Here are the most important ones:

· Grub provides x86 computers with a command-based environment (even if no OS has been installed yet). This allows computers to achieve maximum versatility – they can easily load operating systems or collect data about those systems.

· Grub is compatible with LBA (i.e. Logical Block Addressing) mode. This mode is required to access IDE and SCSI hard drives. Without LBA, hard disks may face a 1024-cylinder restriction, where the computer’s BIOS cannot get a file past that point.

· Each time the system is started, grub’s configuration files are loaded from the hard disk. That means you can change boot options without overwriting the data in the MBR.

The INIT

Once loaded, the kernel finds INIT in the “sbin” directory and runs it.

The INIT is the parent/grandparent of all the tasks that run automatically on a Linux OS. First, INIT reads its own initialization file (this file is located in the “etc/inittab” directory). The initialization file tells INIT to load a configuration script for the system, which arranges the paths, begins swapping, scans the system files, etc. Simply put, this stage completes everything your machine needs during system startup (i.e. starting serial ports, setting the computer’s clock, etc.).

Afterward, INIT keeps on reading the initialization file, which defines how the OS should be run in every operating level. This file also assigns a run level for the system. Run levels are the configurations of computer processes. Once a run level is assigned for the system, INIT begins the background tasks required by the system. INIT does this by checking the correct “rc” directory for the assigned run level. Lastly, INIT runs all of the kill scripts using a stop parameter.

Shutdown

Linux was not designed to be turned off. If you really need to, however, you may run the “shutdown” command. Once the shutdown process is completed, you may use –h to suspend the system or –r to restart it.

The –r and –h options are now capable of invoking the shutdown command if used while the OS is using run levels 1 to 5. This range of run levels ensure correct shutdown for the whole system. However, this is a bad habit to develop since not all Linux systems have this capability.

If the machine doesn’t turn itself off, you must not power off the machine until the screen shows a message indicating that the OS is suspended or completed shutting down. Waiting for this message gives your computer ample time to disconnect active partitions. Remember: impatience can result to data loss.

How to Manage Processes

The Admin’s Tasks

Although it is the Admin’s job to manage the system’s processes, it may be useful for common users to know some things about it. This knowledge becomes important if the users’ own tasks and efficiency are concerned.

This section will explain system performance using theories. Here, you’ll learn about:

1. The problems encountered by common users

2. The methods common users can use to optimize available resources

How Much Time Does a Process Require?

Bash provides a pre-installed time command that shows the amount of time required to execute processes. This tool offers accuracy and versatility – you can use it to get precise data about any command. To help you understand the time command, a screenshot is given below:

In this example, the time command is used to calculate the amount of time needed to create a certain .pdf file. The result shows that the process requires 1 minute and 41 seconds.

The file directory /usr/bin contains a time command for GNU (this is different from the pre-installed BASH tool). This command shows more data that can be used in various ways. Additionally, it displays the command’s exit status and total elapsed time. When used with the GNU time command, the example given above provides these results:

You may check the system’s Info pages if you want to learn more about these time commands.

Performance

For common users, “excellent performance” refers to quick and correct execution of processes. For system managers, however, these words have more meaning: the admin needs to maximize the performance of the entire system, including daemons, programs, and users. In general, system performance may depend on numerable “insignificant” things that are not evaluated by the time command. Here are some examples of these “insignificant” things:

· Access to drives, display, controllers, interfaces, etc.

· The program being run was poorly designed or doesn’t utilize the computer’s resources efficiently.

· The number of active users in the system.

· The time of day.

· The accessibility of remote networks.

Load

Basically, load relies on what is ordinary for your network. For instance, an old P133 that runs a firewall, file server, SSH server, route daemon, proxy server, sendmail server, and other devices can support up to 7 active users. In this kind of situation, the load is still zero. Some systems, particularly those that have several processors, can easily support up to 67 users.

There is just one way to identify your network’s load: check it on a regular basis. This way, you’ll know what’s ordinary for your network. If you won’t use this method, you can only gauge system load from the command line’s response time. The response time lacks accuracy – it cannot be reliable since various factors are affecting it.

Remember that different networks behave differently even if they have identical load averages. For instance, a network can render 3D images smoothly if it has a graphics card that supports hardware acceleration. A network that uses an old VGA card, however, will be as slow as a turtle when rendering images. An old system can be extremely slow when starting an X server, while modern ones won’t even “feel” the added load.

Priority

The importance (or priority) of a task is determined by its nice number. Tasks with a high nice number are “cooperative” (or nice) to other users, other tasks, and the network itself: they are low-priority tasks. Low nice numbers indicate that the tasks they belong to are important and will take more system resources.

You can make a task “nicer” by giving it a higher nice number. However, this technique is only effective for processes that require lots of CPU time (e.g. software compilers). Processes that utilize lots of I/O time consistently are provided with a low nice number (i.e. higher priority). For instance, keyboard inputs always receive the highest priority in a computer network.

You can set a program’s importance using the nice command.

Many systems offer a command called BSD renice. This command allows users to edit the “niceness” of an active program. You may check the manual of your OS to find out if this command is available.

CPU Resources

On each operating system, many processes need to use the computer’s CPU at the same time. This is true even if you’re the only active user. Each process requires a specific amount of CPU resource in order to run properly. Sometimes, computer programs don’t get sufficient resources since the CPU handles many requests. You need to know the available system resources if you want to optimize your computer. There are tools that you can use to obtain this data.

Uptime, one of the commands you can use in Linux, lacks accuracy (it can only show averages, you need to identify the normal number), but can be useful in some situations. Here are some of the things you can do to improve the efficiency of your CPU.

· Stop your system from performing unnecessary tasks. Stop programs and daemons that you aren’t using. You may use the locate command for this.

· Run resource-intensive processes when the system’s load is at a low level. Often, you should run “heavy” processes during nighttime.

· Run low-priority tasks first.

If these methods don’t solve your problem, you may need to get a better CPU. For Linux systems, this is the system admin’s job.

Users

In Linux systems, computer users can be segregated into different types, depending on how they use system resources. Here are the three types of Linux users:

· People who run lots of small processes. A good example for this would be inexperienced Linux users.

· People who run several (but complex) processes. Examples for this type are people running emulators, calculations, and simulations.

· People who run few processes that require lots of CPU time (e.g. game developers).

As you can see, system requirements differ for each type of user. Also, it is extremely difficult to meet the needs of all the users in a network. If you belong to a multiuser network, it is beneficial (and entertaining) to identify the habits of your fellow users. This technique will help you to maximize the available system resources.

Visual Tools

For graphical environments, you have a wide-range of options in terms of monitoring tools. Here’s a screenshot of a tool called Gnome System Monitor. This tool can find and show information about active tasks. Also, it can monitor system resources.

Most Linux systems have some useful icons (e.g. monitors for load, disk, memory, etc.) that you may install onto your taskbar.