3 The free command just provides an overview of the memory for your Linux system. For a more detailed look, enter the command cat /proc/meminfo. You should see a long listing, similar to this:$ cat /proc/meminfo MemTotal: 2035504 kB MemFree: 1449632 kB MemAvailable: 1742352 kB Buffers: 25452 kB Cached: 386028 kB SwapCached: 0 kB Active: 166036 kB Inactive: 290704 kB Active(anon): 51796 kB Inactive(anon): 128 kB Active(file): 114240 kB Inactive(file): 290576 kB Unevictable: 18640 kB Mlocked: 18640 kB SwapTotal: 2097148 kB SwapFree: 2097148 kB Dirty: 156 kB Writeback: 0 kB AnonPages: 63940 kB Mapped: 63344 kB Shmem: 1048 kB KReclaimable: 38664 kB Slab: 74316 kB SReclaimable: 38664 kB SUnreclaim: 35652 kB KernelStack: 2044 kB PageTables: 1268 kB NFS_Unstable: 0 kB Bounce: 0 kB WritebackTmp: 0 kB CommitLimit: 3114900 kB Committed_AS: 376812 kB VmallocTotal: 34359738367 kB VmallocUsed: 27676 kB VmallocChunk: 0 kB Percpu: 516 kB HardwareCorrupted: 0 kB AnonHugePages: 0 kB ShmemHugePages: 0 kB ShmemPmdMapped: 0 kB FileHugePages: 0 kB FilePmdMapped: 0 kB CmaTotal: 0 kB CmaFree: 0 kB HugePages_Total: 0 HugePages_Free: 0 HugePages_Rsvd: 0 HugePages_Surp: 0 Hugepagesize: 2048 kB Hugetlb: 0 kB DirectMap4k: 90048 kB DirectMap2M: 2007040 kB $The kernel continually updates the meminfo file to show exactly what's going on in memory at that moment in time, so the file constantly changes.
SOFTWARE PROGRAM MANAGEMENT
The Linux operating system calls a running program a process . A process can run in the foreground, displaying output on a display, or it can run in background, behind the scenes. The kernel controls how the Linux system manages all the processes running on the system.
The kernel creates the first process, called the init process , to start all other processes on the system. When the kernel starts, it loads the init process into virtual memory. As the kernel starts each additional process, it gives it a unique area in virtual memory to store the data and code that the process uses.
There are a few different types of init process implementations available in Linux, but these days, the two most popular are as follows:
SysVinit—The SysVinit (SysV) initialization method was the original method used by Linux and was based on the Unix System V initialization method. Though it is not used by many Linux distributions these days, you still may find it around in older Linux distributions.
Systemd—The systemd initialization method was created in 2010 and has become the most popular initialization and process management system used by Linux distributions.
The SysVinit initialization method used a concept called runlevels to determine what processes to start. The runlevel defines the state of the running Linux system and what processes should run in each state. Table 1.1shows the different runlevels associated with the SysVinit initialization method.
TABLE 1.1: The SysVinit Runlevels
RUNLEVEL |
DESCRIPTION |
0 |
Shuts down the system |
1 |
Single‐user mode used for system maintenance |
2 |
Multiuser mode without networking services enabled |
3 |
Multiuser mode with networking services enabled |
4 |
Custom |
5 |
Multiuser mode with GUI available |
6 |
Reboots the system |
The /etc/inittab
file defines the default runlevel for a system. The processes that start for specific runlevels are defined in subdirectories of the /etc/rc.d
directory. You can view the current runlevel at any time using the runlevel
command, as shown here:
$ runlevel N 5 $
The systemd initialization method became popular because it has the ability to start processes based on different events such as these:
When the system boots
When a particular hardware device is connected
When a service is started
When a network connection is established
When a timer has expired
The systemd method determines what processes to run by linking events to unit files . Each unit file defines the programs to start when the specified event occurs. The systemctl
program allows you to start, stop, and list the unit files currently running on the system.
The systemd method groups unit files together into targets . A target defines a specific running state of the Linux system, similar to the SysVinit runlevel concept. At system startup, the default.target unit defines all the unit files to start. You can view the current default target using the systemctl
command.
$ systemctl get-default graphical.target $
The graphical.target
target defines the processes to start when a multiuser graphical environment is running, similar to the old SysVinit runlevel 5.
Real World Scenario
EXAMINING PROCESSES
In Chapter 14, “Working with Processes and Jobs,” you'll see how to use the ps
command to view the processes currently running on the Linux system. You can use it now to take a quick peek at what programs are currently running on your Linux system.
1 Log into your Linux system. (If you don't have a Linux system available yet, you can come back to here after going through either Chapter 2or Chapter 4.)
2 At the command prompt, enter the command psax. You should see something similar to this output:$ ps ax PID TTY STAT TIME COMMAND 1 ? Ss 0:00 /sbin/init maybe-ubiquity 2 ? S 0:00 [kthreadd] 3 ? I< 0:00 [rcu_gp] 4 ? I< 0:00 [rcu_par_gp] 5 ? I 0:00 [kworker/0:0-memcg_kmem_cache] 6 ? I< 0:00 [kworker/0:0H-kblockd] 7 ? I 0:00 [kworker/0:1-events] 8 ? I 0:00 [kworker/u2:0-events_power_efficient] . . . 1033 tty1 S 0:00 -bash 1054 tty1 R+ 0:00 ps ax $We've just shown the start of the listing, along with the last two lines, but you should see a long list of different programs running on your Linux system (including the ps command that you started). The kernel is keeping track of all those programs!
Still another responsibility for the kernel is hardware management. Any device that the Linux system must communicate with needs driver code inserted inside the kernel code. The driver code allows the kernel to pass data back and forth to the device, acting as a middleman between applications and the hardware. There are two methods used for inserting device driver code in the Linux kernel.
Drivers compiled in the kernel
Driver modules added to the kernel
Previously, the only way to insert device driver code was to recompile the kernel. Each time you added a new device to the system, you had to recompile the kernel code. This process became even more inefficient as Linux kernels supported more hardware. Fortunately, Linux developers devised a better method to insert driver code into the running kernel.
Programmers developed the concept of kernel modules to allow you to insert driver code into a running kernel without having to recompile the kernel. Also, a kernel module could be removed from the kernel when the device was finished being used. This greatly simplified and expanded using hardware with Linux.
The Linux system identifies hardware devices as special files, called device files . There are three different classifications of device files.
Character
Block
Network
Character device files are for devices that can only handle data one character at a time. Most types of modems and terminals are created as character files. Block files are for devices that can handle data in large blocks at a time, such as disk drives.
Читать дальше