In this section, we’ll take a brief look at Linux.
Linux, like Windows, is an operating system that provides us with a platform to develop or execute certain programs. It can be said that Linux system is also a kind of software, do not think of him too mysterious. (Note: usually what we are talking about is kernel + software.)
2: Let’s take a brief look at the history of Linux
Linux is a free and open source Unix-like operating system. It was developed by Linus Torvalds, a Finnish student, and other enthusiasts on October 5, 1991. Linux is a multi-user, multi-task, multi-threaded and multi-CPU operating system based on POSIX and Unix. It runs major Unix tools, applications, and network protocols, and supports both 32-bit and 64-bit hardware. Linux inherits the design idea of Unix taking network as the core, and is a stable multi-user network operating system. There are many different versions of Linux, but they all use the Linux kernel. Linux can be installed on a variety of computer hardware devices, such as mobile phones, tablets, routers, video game consoles, desktop computers, mainframes, and supercomputers. Look it up for yourself if you want more informationCopy the code
3: Let’s talk about the Linux system structure here, so that we can understand the Linux system from the general direction.
A: As mentioned earlier, the Linux system can be used as a kind of software, providing an environment for programs to run. We call it the kernel. People who have learned microcontroller are to, we are at the bottom of a very simple variety of small procedures, for example, we use assembly language to write some small procedures to control the hardware of some behavior (that is, the high and low point flat, here do not do too much introduction). And our Linux system is the control of all our hardware resources, the allocation of these resources to the agreed function, our software is able to use these resources through some calls. B: In Linux, we write some functions that need to be done by calling the kernel. Here we will call the kernel to do this operation called system call. There are three ways to make system calls: shell, library functions (all based on the interface given by the system), and direct calls. (as shown in the figure below).Copy the code
4: Here we introduce the operation of logging in to Linux.
During login, a user needs to enter the user name and password respectively. The user name and password are usually stored in the /etc/passwd file. Each line of the file contains a user and the login items are separated by seven colons. These are the login name, encrypted password, user numerical ID, user numerical group ID, comment field, actual directory, and the SHELL program the user is using. Previously we said that the shell can be used to interact with Linux, so we can configure our own shell programs so that we can use them easily. Usually the default shell program is bash. I prefer ZSH. Please refer to the following connection for ZSH configurationCopy the code
Blog.csdn.net/towedjfiowa…
5: Next, a brief introduction to files and directories
In Linux or Unix it is possible to be everything files, so it is essential to know about files and directories in Linux. In Windows, we can divide disks into C, D, and so on. But in Linux, we only have one disk, and that disk starts from the/(root) directory, and everything else is mounted in one way or another. If you don't believe me, you can use vim dirname to open a directory, and you will find that there are many directory items/files stored in it. In Linux, files also contain file attributes, including file size, file type, file owner, file permission, and file modification time. These attributes are retrieved by stat or fstat. The following nine characters of the file type are owner: Group: other people's read/write permissions on the file. Common file (-), directory (d), pipe (P), link file (L) character device file (C), block device file (b), socket (S).Copy the code
File name: The characters that appear in file names on Linux are the exception for /, which is used to split a directory, and null, which is used to end a directory. Of course, under normal circumstances, try to use only the PRINTED character set, so as not to cause unnecessary trouble. And when we create a directory, we automatically create two. And.. Directories, which represent the current directory and the upper-level directory respectively. Path name: A path is classified into absolute paths and relative paths. Absolute paths start from the root directory, such as /etc/passwd. Relative directory is the directory starting from the current directory./.. / passwd. Here's how to get a path name: for example, the following small program is to read the contents of the specified directory. #include <dirent.h> #inlcude <stdio.h> int main(int argc, char* argv[]) { DIR *dp; struct dirent *dirp; if(argc ! = 2) { return 0; } if((dp = opendir(argv[1])) == NULL) { return 0; } while((dirp = readdir(dp)) ! = NULL) { printf("%s\n", dirp->d_name); }} There are only two things you need to understand about this program just two of these functions. Working directory: Each executing process has a working directory. All relative paths are relative to the current working directory. Start directory: the directory where we sit when we log in to the system.Copy the code
5: Let’s talk about input and output in Linux.
File descriptors, which in Linux return a non-negative integer when we open a file, are used to read and write files (note: file descriptors are finite resources). Standard input and output: normally, when we start a program that will automatically open three file descriptors, the three file descriptors are respectively the standard input (data read from the terminal), the standard output (can will some information to the terminal display), standard error (will display error messages to the terminal). IO with buffer and IO without buffer: IO with buffer and IO without buffer can be seen briefly in the following figure. IO without a buffer directly outputs data from the process to the terminal device. Buffered IO is to output data from the process to the kernel buffer, and then output to the terminal device under certain conditions (this condition can be newline, buffer full, direct output (and no buffer behavior is basically the same).Copy the code
We'll cover the impact of buffers on reading and writing files in more detail later.Copy the code
6: Procedures and processes:
A program is an executable file stored in a directory on disk. A process is a program in execution.Copy the code
7: Error processing.
When some Linux functions fail, it is common to return a negative value and copy the integer variable error as a message. We can use the perror/strerror function to return/print some of the error information. Of course, when we do something about a program error, we need to include the error.h header. Example: #include <stdio.h> #include <errno.h> #include <string.h> int main() { fprintf(stderr, "EACCES %s\n", strerror(EIO)); errno = ENOENT; perror(NULL); } The output content is shown in the following figureCopy the code
Of course, there are different kinds of mistakes. Errors can be classified as fatal and non-fatal. For non-fatal errors, we can handle them properly. For fatal errors, we may just print out a single message and exit the program.Copy the code
8: indicates the user ID
The group ID and user ID mentioned above are user representations, which are used to divide users. Because Linux is all about files, manipulation of files is crucial, and we can use group IDS and user ids and others to assign permissions to files, so that we can avoid some files being manipulated by mistake. User ID: the value that identifies the user ID in the password file, which is used to distinguish different login users. One special user is root. The root user has various computer rights, and only the root user has certain operating system (OS) functions, such as changing user IDS and group ids. Group ID: To facilitate work management, for example, a group can be divided into a group ID, so to a certain extent, the members of this group have more permissions. In addition, the ls-l command maps the ID to the user name, so the ls-l command displays the name. Example: The program gets the user ID and group ID. #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { printf("uid = %d, gid = %d", getuid(), getgid()); return 0; } Additional group ID: You can look in the /etc/group file for additional group IDS, which means that users can belong to not only one group but also other groups.Copy the code
9: signal
Signaling is a technique for notifying a process that something has happened. For example, a program that divides 0 will receive a SIGFPE signal. At the same time, there are three ways for a process to process a received signal: ignore it, follow the system default (see the man 7 Signal manual), or provide a function to process it. Details on signals will be given in a subsequent chapter.Copy the code
10: Time:
The various times in Linux are very important for us to develop programs. For example, we need to set the random seed (srand((unsigned int)time(0)) based on the current execution time. Calendar time: Calendar time is the number of seconds since midnight on January 1, 1970. This time can be used to record the last change to the file. Those of you who know makefiles will know that makefiles are compiled based on when the file was last modified. Typically, this type of time is stored as process time of type time_t: it is called CPU time, which measures how much CPU resources a process uses. Such time is typically stored as clock_t. A process has three types of time: clock time (the total elapsed time of the process), user CPU time (the time it takes to execute user instructions), and system CPU time (the time it takes to execute tasks in the kernel).Copy the code
11: System calls and library functions
The Linux architecture was introduced at the outset, and there are several ways to interact with the kernel. Such as shell, library functions. It also introduced the concept of system calls. System calls: My understanding is that the Linux kernel provides a portal to various functions, called system calls, like the open function. Library functions: A library function is a library that is wrapped on top of a system call to provide various functions.Copy the code
This article is published by OpenWrite, a blogging tool platform