Preamble
The Linux Programming Interface – Part 1: Basic concepts
This chapter introduces concepts related to Linux system programming. It focuses on those who are using other operating systems, or have little experience with Linux and other UNIX implementations.
Operating system core: The Kernel
Operating system (operating system) is used with two meanings:
- To indicate all the included packages of a central software that manages computer resources all the accompanying tools such as command line interpreters, graphical user interfaces, file utilities and editors
- Narrower meaning refers to the central management and licensing of computer resources (CPU, RAM and peripherals).
The term Kernel is often used as a synonym for the operating system that we refer to in this book.
Although it is possible to run programs on a computer without the kernel, the kernel’s presence simplifies writing and using other programs, and increases the robustness and portability available for the development. programmer. The kernel did this to provide a software layer to manage a computer’s resource limits
The Linux kernel excutable can be found at / boot / vmlinuz, or something similar. Historically, before UNIX, the kernel was called unix, After UNIX, which had virtual memory, was renamed vmunix , On Linux this name is a mapping of system name with z replacing x to say that the kernel is a compressed executable
Tasks executed by kernel:
- Process scheduling: A computer with multiple CPUs that executes instructions for programs. Like UNIX systems, Linux is preemptive multitasking, multitasking means that running programs can run on memory simultaneously and each can accept CPU usage. Preemptive means the rules governing which processes are being used by the CPU and for how long.
- Memory management: While the computer memory is very large compared to 1 or 2 centuries ago, the size of the software also grows accordingly, so the physical memory (RAM) is still limited and the kernel must be shared. them for processes as equitably and efficiently. Like most modern operating systems, Linux uses virtual memory management, a technique:
- Processes are independent of other processes and the kernel, so one process cannot read and edit the memory of other processes or in the kernel.
- Only part of the process needs to keep the memory, lowering the memory limit of each process and allowing multiple processes to hold in RAM together. This helps the CPU usage, at any time there is at least one process that the CPU can execute
- Providing a file system: The kernel provides a file system on disk that allows files to be created, removed, updated and modified on them.
- Create and destroy processes: The kernel can load a new program into memory, give it resources (such as CPU, memory, and access to files) that provide the order to run. Once the process is completed, the kernel will ensure that the resources it uses are released.
- Access to equipment: Devices such as mice, monitors, keyboards, hard disks, floppy disks attached to a computer allow communication between computers and other parts of the world, allowing access, or both. The kernel provides programs with interfaces to standardize and simplify access to devices, while adjusting access by multiple processes on a device.
- Network: The kernel transfers and receives packets. Tasks include routing packets to the target system
- Provide a system call application programming interface (API): The process may require the kernel to perform some task by using some of the kernel entry points referred to as system calls. The linux system call API is the main topic of this book. Section 3.1 will detail the implementation of bowsc when a process is executed.
In addition, a multi-user operating system such as Linux will provide some abstraction users as a virtual private computer; For example, each user has a home directory.
Kernel mode and user mode
Typical processor architectures allow the CPU to hold at least 2 modes: user mode and kernel mode (supervisor mode). Hardware directives allow switching between the 2. Accordingly, the virtual memory area can be marked as user space or kernel space. When running in user mode, the CPU can access only memory marked as user space; When running in kernel mode, the CPU has access to both user and kernel memory space.
Some actions can be executed in kernel mode. For example, execution stops the system, accesses memory management areas, and initiates I / O devices. By leveraging hardware designs to place the operating system in kernel space, operating system installs ensure that user processes are not allowed to access kernel instructions and data structures, or to activities that adversely affect the system.
Process vs kernel views of the system
In many programming jobs today, we are used to thinking of programs as a process-driven way. However, when looking at many topics in this book, it might be useful to have a different look, a look from the kernel. To clarify, we will now look at every letter from the process viewpoint view and then the kernel viewpoint.
A typical program has a lot of processes. With each process, many things appear asynchronously. Each execution process does not know when it will be continued, because it depends on the CPU scheduling. The transfer of signals and communication between processes immediately or at some point is due to the kernel. A process of not knowing where it is allocated in RAM.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | A running system typically has numerous processes. For a process, many things happen asynchronously. An executing process doesn’t know when it will next time out, which other processes will then be scheduled for the CPU (and in what order), or when it will next be scheduled. The delivery of signals and the occurrence of interprocess communication events are mediated by the kernel, and can occur at any time for a process. Many things happen transparently for a process. A process 24 Chapter 2 doesn’t know where it is located in RAM or, in general, whether a particular part of its memory space is currently resident in memory or held in the swap area (a reserved area of disk space used to supplement the computer’s RAM). Similarly, a process doesn’t know where on the disk drive the files it accesses are being held; it simply refers to the files by name. A process operates in isolation; it can’t directly communicate with another process. A process can’t itself create a new process or even end its own existence. Finally, a process can’t communicate directly with the input and output devices attached to the computer. By contrast, a running system has one kernel that knows and controls everything. The kernel facilitates the running of all processes on the system. The kernel decides which process will next obtain access to the CPU, when it will do so, and for how long. The kernel maintains data structures containing information about all running processes and updates these structures as processes are created, change state, and terminate. The kernel maintains all of the low-level data structures that enable the filenames used by programs to be translated into physical locations on the disk. The kernel also maintains data structures that map the virtual memory of each process into the physical memory of the computer and the swap area(s) on disk. All communication between processes is done via mechanisms provided by the kernel. In response to requests from processes, the kernel creates new processes and terminates existing processes. Lastly, the kernel (in particular, device drivers) performs all direct communication with input and output devices, transferring information to and from user processes as required. Later in this book we’ll say things such as “a process can create another process,” “a process can create a pipe,” “a process can write data to a file,” and “a process can terminate by calling exit().” Remember, however, that the kernel mediates all such actions, and these statements are just shorthand for “a process can request that the kernel create another process,” and so on. |
The Shell
A Shell is a special program designed to read commands entered from the user and execute appropriate programs and respond. Sometimes known as an interpreter.
The term “login shell” refers to the process created to run a shell when the user first signs in.
While some operating systems integrate the interpreter into the kernel, on UNIX, the shell is a user process. On the same computer, different users may use different shells. Some prominent shells:
- Bourne shell (sh): The oldest and most widely used shell, written by Steve Bourne. It is the standard shell of the 7th UNIX version. The Bourne shell includes many familiar features: IO redirection, pipelines, filename generation (globbing), variables, manipulation of environment variables, command substitution, background command execution, and functions . All subsequent UNIX versions have a Bourne shell.
- C shell (csh): This shell is written by Bill Jou at the University of California by Berkeley. This name comes from the similarity of many of the shell’s control flow structures to the C programming language. C shell provides several interactive features not available in the Bourne shell, including the command history, command line. editting, job control, and aliases. The C shell is not backward compatible with the Bourne shell. Although the interactive standards on BSD are C shell, shell scripts.
- Korn shell (ksh): This shell is written as a replacement for Bourne shell by David Korn at AT&T Bell Laboratories. While still being compatible with the Bourne shell, it also incorporates the same interactive features as the C shell.
- Bourne again shell (bash): This shell is an installation of the GNU project for Bourne shell. It provides interactive features with the available C and Korn shell. The main author of bash is Brian Fox and Chet Ramey. Bash is widely used in Linux.
Shells are designed not just for interaction, but can also be used to interpret shell scripts, which are text files that include shell commands. For this purpose each shell has utilities such as a programming language: variables, loops, conditions, IO commands, and functions.
Each shells perform the same task, possibly with different syntax. Most of the examples in this book require a shell to use bash, unless otherwise noted, the reader can assume that all examples work the same way as Bourne-type shells.
Users and groups
Each user of the system is unique and a user can belong to several groups.
User
Every user in the system has a login name (username) and can have a user ID (UID). Each user, defined by a line in the / etc / passwd password file. Include the following information:
- Group ID: is the id of the first group the user belongs to
- Home directory: the initialization directory where the user is placed after logging in
- Login shell: the name of the program that is executed to interpret user commands
Each password record may also include the user’s password, in encrypted form. However, for security reasons, passwords are usually stored in shadow password files, which are only read for authorized users.
Group
For administrative reasons, control access to files and system resources – it is useful to organize users into groups. For example, everyone on a team has a project, and shares files. In earlier UNIX versions. A user can only be a member of only 1 group. BSD allows a user to belong to multiple groups, an idea posted by other UNIX versions and in the POSIX.1-1990 standard. Each group is identified by a line in the / etc / group file group, each group containing the following information:
- Group name: is the unique name of the group
- Group ID: griup code
- User list: a list of usernames separated by commas, members of this group.
Superuser.
A user who is a superuser will have special rights in the system. The super user ID account is 0, and usually the username is root. In UNIX systems, superuser can bypass all the permissions required by the system. So super user can access any file, any file that requires what rights, can read the signals of any process in the system. System administrators use superuser accounts to perform various administrative tasks on the system.
Single Directory Hierarchy, Directories, Links, and files.
The Kernel has a single entry tree that organizes all the files in the system. (Different from Windows where each disk will have a directory structure). The root of this tree is the root named / (slash). All files and directories are descendants of it.
{% img [class names] /images/the-linux-command-line-interface/subset-of–the-linux-single-directory-hierarchy.png ‘subset-of – the-linux-single-directory- hierarchy.png ” subset-of – the-linux-single-directory-hierarchy.png ‘%}
File types
There are 2 commonly known file types, regular and plain. Other file types include devices, pipes, sockets, directories, symbolic links.
The term file is often used to refer to a file of any kind, not just the regular file.
Directories and links
A mail is a special file, its content is a table of file names with a referral to the corresponding file, file names + links linked together called links, files can have many links, so May have multiple names, may be the same or different directories.
Directories include links to other files and directories. Links between directories are established by the tree structure as shown above.
Each directory includes at least 2 inputs:. (period), which connects to the current directory and .. (dots) connect to the parent directory, the directory above it in the tree. Every directory except the root, has a parent.
Symbolic links.
Like a normal link, a symbolic link provides an alternate name for a file.
….
Filenames
Pathnames
Current working directory
File ownership and permissions
File I / O Model
Programs
Processes
Memory Mappings
Static and Shared Libraries
Interprocess Communication and Synchronization
Signals
Threads
Process Groups and Shell Job Control
Sessions, Controlling Terminals, and Controlling Processes
Pseudoterminals
Date and Time
Client-Server Architecture
Realtime
The / proc File System
summary
In this chapter, we explored a set of basic concepts related to Linux system programming. Viet understands the concepts that have been provided with inexperienced users as the foundation for starting to learn system programming.
End
This chapter is too long, I will translate tomorrow notes still in this article.
Writing this chapter for ease of understanding also requires a certain understanding. This article will also be edited as my understanding improves.
Thank you
Sea study is limitless. The word “boundless” itself implies that there is no destination. You roam on a ship year after year on the surface of knowledge. So where does the meaning of such a life lie? The beauty of the islands you visit, the ocean storm experiences, fateful encounters with other ships, races and freeways.
For the exercise, please continue to watch the 6th lesson of Arif Butt: https://www.youtube.com/watch?v=tuWDi53pomU&list=PL7B2bn3G_wfBuJ_WtHADcXC44piWLRzr8&index=7