Core of the operating system--core kernel responsibilities
进程调度
: Linux is a preemptive multitasking operating system where multiple processes can reside in memory at the same time, and each process can gain access to the CPU. Which processes get CPU usage, and how long each process can be used, is determined by the kernel process scheduler.
内存管理
: Linux uses a virtual internal management mechanism.
- Provides a
文件系统
- Create and Terminate
进程
: The kernel can load new programs into memory, providing them with the resources they need to run. Once the program executes, the kernel also ensures that it frees up resources for subsequent reuse.
设备
Access: The kernel provides a simplified version of the standard interface for program access to devices, while also quorum for multiple processes accessing each device.
联网
: The kernel is sending and receiving network messages (packets) in the name of the user process. This task involves routing network packets to the target system.
- Provide system calls
应用编程接口(API)
: The process can request the kernel to perform various tasks with the kernel entry point.
Kernel State and User state
? When running in a user state, the CPU can only access memory that is marked as user space, and attempting to access memory that is part of the kernel space can cause a hardware exception. When running in the core State, the CPU can access both the user space memory and the kernel space memory.
? Certain operations can be performed only when the processor is running in a kernel-like mindset. Such as: Execution of the outage (Halt) command to shut down the system, access to memory management hardware, and the initialization of device I/O operations. The implementations use this hardware design to place the operating system in kernel space. Ensures that user processes cannot access kernel directives and data structures or perform operations that are not conducive to system operation.
Shell
- Bourne Shell (SH)
- C Shell (CSH)
- Korn Shell (Ksh)
- Bourne again shell (bash)
? The purpose of designing the shell is not only for human-computer interaction, but also for explaining shell scripts. To achieve this, each shell has a number of features that are commonly associated with programming languages, including variables, loops and conditional statements, I/O commands, and functions.
User and group Users
? Each user of the system has a unique login name and an integer user ID (UID) corresponding to it. The system Password file/etc/passwd defines a row of records for each user, in addition to the above two information, the record contains the following information.
- Group ID: the integer group ID of the first group to which the user belongs.
- Home directory: The initial directory in which the user is logged on.
- Login Shell: Executes the program name to interpret the user command.
? The record can also save the user's password in encrypted form. However, for security reasons, user passwords are often stored in separate shadow password files for privileged users to read.
Group
? Each user corresponds to a row of records in the system group file/etc/group that contains the following information.
- Group name: (unique) Group names
- Group ID (GID): The integer ID associated with the group.
- User list: The user login name that is part of the group (the Group ID field that is logged by the password file could not be identified by another member of the set, also in this column), separated by commas.
Super User
? Super users have privileges in the system. The user ID of the Superuser account is 0, and the login name is usually root.
Single directory hierarchy, directories, links, and files
? The kernel maintains a single directory structure to place all the files of the system. This directory hierarchy is rooted in the root directory named "/". All files and directories are "descendants" of the root directory.
Paths and Links
? 每个目录至少包含两条记录
:.. And., the former is a link to the directory itself, and the latter is a link to its parent directory.对根目录而言,..是指向根目录自身的。
Symbolic Links
? Divided into hard links and soft links.
Ownership and permissions for files
? Each file has a user ID and group ID associated with it, defining the owner and group of the file, respectively. The system determines the user's access to the file based on the ownership of the file.
File I/O model file descriptor
? I/O system calls use file descriptors--nonnegative integers--to refer to open files. Typically, a shell-initiated process inherits 3 Open file descriptors: descriptor 0 is a standard input, a file that provides input to the process, and descriptor 1 is a standard output that refers to a file for the process to write output to, and a descriptor 2 as a standard error that refers to a file that is used by the process to write an error message or an exception advertisement. in the Stdio function library, these 描述符分别与文件流stdin、stdout和stderr
correspond.
stdio function Library
? The C programming language often invokes the I/O functions of the C standard library when performing file I/O operations. This set of I/O functions is also called the Stdio function library, which includes fopen (), fclose (), scanf (), printf (), fgets (), fputs (), and so on. The Stdio function is located above the I/O system call Layer (open (), close (), write (), and so on.
Memory layout of process processes
? Logically dividing a process into the following sections (also known as segments)
- Text: Instructions for the program.
- Data: Static variables used by the program.
- Heap: Programs can dynamically allocate additional memory from this area.
- Stack: A piece of memory that increases or decreases as a function is called, returned, to allocate storage space for local variables and function call link information.
Creating processes and executing programs
? A process can use System call fork () to create a new process. The process called fork () is called the parent process, and the newly created process is called a child process. The kernel creates child processes by copying the parent process. After a child process inherits a copy of the data segment, stack segment, and heap segment from the parent process, it can modify the content without affecting the parent process's "original" content. (a program text segment that is marked as read-only in memory code is shared by the parent, child process.) )
? The child process then either goes to a different set of functions that share the code snippet with the parent process, or, more commonly, uses the system call EXECVE () to load and execute a completely new program. Execve () Destroys existing text segments, data segments, stack segments, and heap segments, and replaces them with new ones, based on the code of the newly created program.
? Based on Execve (), the C language library also provides several related functions, although the interfaces are slightly different, but the functions are all the same. The names of all the library functions above begin with the string "exec", and in the case of differences between functions irrelevant, this book will use the symbolic exec () as a general term for these library functions. However, there is virtually no library function named EXEC ().
Process ID and parent process ID
? Each process has a unique integer-type process identifier (PID). Each process also has a parent process identifier (PPID) attribute that identifies the request kernel to create its own process.
Init process
? When the system boots, the kernel creates a special process called INIT, the "parent of all Processes", and the corresponding program file for the process is/sbin/init. All processes of the system are not created by Init (using fork ()) "Personally", or by their descendant processes. The process number of the INIT process is always 1 and always runs with superuser privileges.
Daemon process
? Daemons are special-purpose processes that create and process such processes the same way as other processes, but the following characteristics are unique:
- "Immortality." The daemon usually starts at system boot time until the system shuts down.
- The daemon runs in the background and has no control terminal for it to read or write data.
Examples of daemons are SYSLOGD (logging messages in the System log) and httpd (using HTTP to distribute Web pages).
Environment List
? Each process has a list of environments, which is a set of environment variables maintained in the process user space memory. Each element of this list consists of a name and its associated values. A new process, created by fork (), inherits the environment copy of the parent process. This also provides a mechanism for inter-parent-child interprocess communication. When the process calls exec () to replace the currently running program, the new program either inherits the environment of the old program, or specifies the new environment in the parameters of the exec () call and receives it.
? In most shells, you can use the Export command to create environment variables (C shell uses the setenv command),
? Such as$ export MYVAR=‘Hello world‘
? The C language program can use an external variable (char **environ) to access the environment, and the library function allows the process to get or modify values in its own environment.
? Environment variables are used in a variety of ways. For example, the shell defines and uses a series of variables that are accessible to scripts and programs executed by the shell. These include the variable home (which explicitly defines the path name of the user's login directory), and the variable path, which indicates the list of directories that the shell searches for when the user enters the command and the corresponding program.
Resource limits
? Each process consumes resources such as open files, memory, and CPU time. Using the system call Setrlimit (), the process can set an upper limit for each type of resource that it consumes. There are two related values for each of these resource restrictions: Soft limits limit the amount of resources a process can consume, and hard limit the adjustment limit for soft limits. When a non-privileged process adjusts the soft limit value for a resource, it can be set to any value between 0 and the corresponding hard limit value, but the hard limit value can only be lowered and cannot be increased.
? a new process created by fork () inherits the settings of its parent process for resource throttling.
? Use the Ulimit command (limit in C shell) to adjust the shell's resource limits. The child processes created by the shell to execute the command inherit the above resource settings.
Inter-process communication and synchronization
Linux provides a rich inter-process communication (IPC) mechanism.
- Signal (signal), used to indicate the occurrence of an event.
- Pipelines and FIFO, which are used to pass data between processes.
- Sockets to pass data between processes running on the same host or on different hosts that are networked.
- File locking, which allows a process to lock parts of a file to prevent other processes from reading or updating the contents of the file.
- Message Queuing, which is used to exchange messages (packets) between processes.
- Semaphore, used to synchronize process actions.
- Shared memory, allowing two or more processes to share a piece of memory. When a process changes the contents of shared memory, all other processes are immediately aware of the change.
Thread
? Each process can execute multiple threads. You can imagine a thread as a process that shares the same virtual memory and worked other properties. Each thread will execute the same program code, sharing the same data region and heap. However, each thread has its own stack, which is used to load local variables and function call link information.
? Threads can communicate through shared global variables. With the condition variables and mutex mechanisms provided by the threading API, the threads that the process belongs to can communicate with each other and synchronize behavior-especially with regard to the use of shared variables. In addition, the IPC and synchronization mechanisms enable threads to communicate with each other.
? The main advantage of threading is that it is easier to share data between threads, and for some algorithms it is more natural to implement multi-threading than to implement multiple processes. Furthermore, it is obvious that multithreaded applications can benefit from parallel processing of multiprocessor hardware.
/proc File System
? Linux provides a/proc file system consisting of a set of directories and files assembled (mount) in the/proc directory.
? The/proc file system is a virtual file system that provides an interface to the kernel data structure in the form of file system directories and files. This opens the door to viewing and changing various system properties. In addition, you can view information about the processes running in the system through a set of directories named/proc/pid (PID, Process ID).
? Typically, the contents of a file in the/proc directory are taken in human-readable form, and shell scripts can parse it. Programs can open, read, and write to established files in the/proc directory. In most cases, only privileged-level processes can modify the contents of files in the/proc directory.
"Linux System Programming Handbook" Reading notes-the 2nd Chapter basic Concepts