Linux concepts and Systems reading notes

Source: Internet
Author: User
Tags processing text semaphore

"Linux concept and system tutorial http://www.cnblogs.com/vamei/archive/2012/10/10/2718229.html"

1.Linux Boot (bootstrap)

Boot sequence: init process, kernel, boot loader, BIOS, MBR-

Bios:basic Input/output System

Mbr:master Boot Record

2.Linux File Management

(1) File attachment information (metadata)

The file itself contains only data. The file name is actually stored in the catalog file. In addition to these, there are file extensions maintained by the operating system, such as file type, file size, file permissions, file modification time, file read time, and so on. You can query the file information ($ls-l file.txt) with the LS command to get the following result:

-rw-r--r--1 Vamei Vamei 8445 Sep 8 07:33 file1.txt

The meanings of each part are as follows:

    • Let's start with the first one, which represents the file type, which indicates that File1.txt is a regular file (d should be shown if it is a catalog file).
    • Then there are nine characters, rw-r--r--, which are used to represent file permissions. These nine characters are divided into three groups,rw-, R--, r--, corresponding to the owner (owner), the owning group (owner), and all other people (other). Review Linux boot, login, I will have a user identity and a group identity, the equivalent of my business card. The first group indicates that if the user ID on my business card proves that I am the owner of the file, then I can have read (r) to the file, write (w) the permission to the file, but do not have permission to execute (-if you have execute permission, x) that file. The second group indicates that if the group on my business card proves that my group is a member of the owning group for that file, then I have permission to read from the file. The third group says that if my business card shows that I am neither an owner nor a member of a group, then I only have permission to read in. When I want to perform a read operation, Linux will first see if I am the owner. The following will further explain the owner and the owning group.
    • The following 1 is the number of hard links (link count).
    • The subsequent Vamei indicates that the user Vamei is the owner (owner) of the file and that the owner of the file has permission to change the file permissions (for example, rwxrwxrwx). The owning group of the later Vamei files is the group Vamei. The owner and the owning group of the file are appended to the file when the file is created (equivalent to locking the file, only the user with the appropriate business card can open the operation). Note that Linux has a superuser root (also known as the root user) and that the user owns all the files.
    • The subsequent 8445 represents the file size in bytes (byte).
    • Sep 8 07:33 represents the time of the last write of the file (modification times). In fact, the file additional information contains the last read time of the file, which is not displayed.

(2) Umask

When we create a file, such as using touch, it tries to create the new file as permission 666, which is rw-rw-rw-. But the operating system should refer to the permissions mask to see if the file is actually created as 666. The permission mask indicates that the operating system does not allow permission bits to be set, such as 037 (----wxrwx), which means that setting the WX bit and other rwx bits of the group is not allowed. If this is the permission mask, the final file permission is rw-r-----(the W bit and other RW bits of the group are mask). We can change the permissions mask by $umask 022 way.

3.Linux Architecture

4.Linux command Line and command

(1) Shell commands can be divided into the following categories

    • Executable (executable file)
    • Shell built-in functions (built-in function)
    • Aliases (alias).

(2) When a command runs, you can use CTRL + C when you want to stop it halfway. If you just want to stop temporarily, use CTRL + Z. The specific mechanism is related to the signal (signal).

(3) Add sudo before executing the command to temporarily execute a command as root

5.Linux file Management Related commands

(1) File permissions related

    • $chmod 755 A.txt,change Mode Changes the read, write, and execute permissions for A.txt
    • $sudo Chown Root A.txt,change owner changes the owner of the file to be the root user. This command requires Superuser privileges to execute, so we add sudo before the command.
    • $sudo chgrp root a.txt,change Group Change the file's owning group to root

6.Linux text Stream

(1) Text stream

Linux is used as a unit of data in bytes (byte), which means that the sequence is one unit per eight bits (bit) (eight-bit binary corresponds to a decimal range of 0 to 255), "Everything is a stream of bytes"

(2) standard input, standard output, standard error and redirection

    • REDIRECT Standard output: $ls > a.txt, the computer will create a new a.txt file and point the command line's standard output to this file; $ls >> a.txt, here >> The role of the standard output is also redirected. If a.txt already exists, the text stream generated by LS will be appended to the end of A.txt and will not create a new a.txt every time as >.
    • REDIRECT Standard error: $cd void 2> a.txt > B.txt, standard error corresponds to always 2nd number, so there is the above notation. Standard error output to a.txt, standard output output to b.txt.
    • Also redirect standard output and standard error: $cd void >& a.txt, error message is directed to A.txt.
    • Echo,cat

(3) piping (pipe)

Pipelines can direct the output of one command to the input of another, allowing two (or more commands) to work continuously like pipelining, continuously processing text streams. On the command line, we use | To represent pipes: $cat < A.txt | WC


7.Linux Process Basics

(1) You can use the $ps command to query a running process

(2) When the computer is powered on, the kernel (kernel) only establishes an INIT process. Linux kernel does not provide a system call to create a new process directly. All the remaining processes are created by the Init process through the fork mechanism. The new process has to replicate itself through the old process, which is the fork. The fork () system call is a system call to create a child process under UNIX with its own process, one call, two returns, and in the parent process, fork returns the process ID of the newly created child process, and in the child process, fork returns 0. At the call of Fork (), the entire parent process space is copied to the child process as is, including instructions, variable values, program call stacks, environment variables, buffers, and so on.

(3) When the parent process learns that the child process is terminated, it is the responsibility to use the wait system call for the child process. This wait function takes out the exit information of the child process from the kernel and empties the space occupied by the information in kernel.

8.Linux Signal Basics

(1) compared to other inter-process communication methods (interprocess communication, such as pipe, shared memory), the signal can be transmitted more coarse information, just an integer. But it is because of the small amount of information transmitted, the signal is also easy to manage and use. Signals are therefore frequently used for system management related tasks, such as notification process finalization, abort, or recovery, and so on.

(2) Common signals

    • SIGINT when the keyboard presses CTRL + C to signal from the shell, the signal is passed to the process running in the foreground of the shell, and the default action that should signal is to interrupt (INTERRUPT) the process.

    • Sigquit when the keyboard presses ctrl+\ to signal from the shell, the signal is passed to the process running in the foreground of the shell, and the default action that should be signaled is to exit (quit) the process.

    • SIGTSTP when the keyboard presses CTRL + Z to signal from the shell, the signal is passed to the process running in the foreground of the shell, and the default action that should be signaled is to pause (STOP) the process.

    • The Sigcont is used to notify the paused process to continue.

    • SIGALRM acts as a timer, usually after a certain period of time before the program generates the signal.

(3) using the signal in the shell

Let's actually use the signal below. We run ping in the shell:

$ping localhost

At this point we can pass the SIGTSTP to the process via CTRL + Z. Shown in the shell:

[1]+  Stopped                 ping localhost

We use $ps to query the PID of the ping process (PID is the room number of the ping process), which is 27397 in my machine

We can send a signal to a process in the shell through the $kill command:

$kill-sigcont 27397

To pass the sigcont signal to the ping process.

9.Linux Process Relationships

(1) Process Group

Each process belongs to a process group, and each process group can contain multiple processes. The process group will have a process group lead process (leader), and the PID of the lead process (PID see Linux Process Foundation) becomes the ID of the process group (the Process group ID, pgid) to identify processes groups.

(2) Under the premise of Shell support job control, multiple process groups can also form a session. Each process group in a session is referred to as a work (job). A session can have one process group as the foreground work for the session (foreground), while the other process groups are background work (background). Each session can be connected to a control terminal. The foreground process group that is passed to the session when the control terminal has input and output.

The meaning of the session is to include multiple tasks in a terminal and take one of them as the foreground to receive the input and output of the terminal and the terminal signal directly. Other work runs in the background.

A command can be run in the background by adding & to the end:

$ping localhost > Log &

At this point the terminal displays:

[1] 10141

1 in parentheses means work number, while 10141 is Pgid

We query for more detailed information in the following ways:

$PS-O Pid,pgid,ppid,sid,tty,comm

(TTY indicates control terminal)

The signal can be passed by kill

$kill-sigterm-10141

Or

$kill-sigterm%1

To send to the workgroup. The above two commands, one is sent to Pgid (by adding in front of pgid-to indicate is a pgid rather than PID), one is sent to work 1 (% 1), the two are equivalent.

A job can change from backstage work to foreground work by $FG:

$cat > Log &

$FG%1

When we run the first command, we cannot enter the command because we are working in the background, until we bring the work into the foreground before we can enter it into the cat command. When the input is complete, press Ctrl+d to tell the shell to end the input.

10.Linux Users

(1) Each process is maintained with the following 6 IDs:

Real identity: Real UID, Real GID

Valid identity: Effective UID, effective GID

Storage identity: Saved UID, saved GID

Where the real identity is the identity that we use to log in, the identity that is checked when the process really goes to manipulate the file, and the identity that is stored is another identity other than the true identity. Different stages of a process may require varying privileges. For example, the first valid identity of a process is the real identity, but when running to the middle, you need to read some configuration files as other users, and then do other things. To prevent other user identities from being abused, we need to make the process's valid identity changes back to the real identity before the operation. In this way, the process needs to change between two identities.

11.Linux from program to process

(1) Each process space is divided into different regions according to the following ways:

The text area is used to store instructions (instruction), indicating the operation of each step. Global data is used to hold the globals, which are used to hold local variables, and the heap (heap) is used to hold dynamic variables (the variable). The program uses the malloc system call to open space directly from memory for dynamic variable. Text and global data are determined at the beginning of the process and remain fixed throughout the process.

Stack (stack) is a frame (stack frame). When the program calls the function, the stack grows downward by one frame. The parameters and local variables of the function are stored in the frame, and the return address of the function is returned. The frame at the bottom of the stack, along with the global variables, forms the current environment (context). A typical programming language allows you to use only the frame at the bottom of the stack, rather than allowing you to invoke other frames (this also conforms to the "advanced out" feature of the stack structure.) But there are also some languages that allow you to call the rest of the stack, which is equivalent to allowing you to invoke the local variables declared in main (), such as Pascal, when you run the inner () function.

When malloc is used in a program, the heap grows upward, and its growing portion becomes the space that malloc allocates from memory. The space created by malloc will persist until we use the free system to release it, or the process ends. A classic error is memory leakage, which means that we do not release heap space that is no longer being used, causing the heap to grow and the memory available space to be reduced. The size of the stack and heap increases or decreases as the process runs. There is no memory available when the stack and heap grow to meet each other, that is, when the blue area in the memory space diagram disappears completely, unused. The process will have stack overflow (stack overflow) errors, causing the process to terminate.

11.Linux Multi-Threading and synchronization

(1) When creating a new thread, we build a new stack for this thread. Each stack corresponds to a thread. When a stack executes to the full popup, the corresponding thread completes the task and is finished. Therefore, the multithreaded process has multiple stacks in memory. Each thread can invoke parameters and variables in the frame at the bottom of its own stack and share the TEXT,HEAP and global data regions in memory with other threads.

(2) Multi-threaded synchronization

    • Mutual exclusion Lock
    • Condition variable
    • Read/write Lock

12.Linux interprocess Communication

Inter-process communication can be divided into two ways

(1) piping (pipe) mechanism

A pipeline is a buffer that is managed by the kernel. At the beginning, the two arrows above are connected to the same process 1 (two arrows connected to process 1). The two connections are also copied to the new process (process 2) when the fork is copied. Subsequently, each process closes itself without the need for a connection (two black arrows are turned off; Process 1 Closes the input connection from the pipe, process 2 turns off the output to the pipe connection, so that the remaining red connections form the pipe.

Because of the fork mechanism, pipelines can only be used between parent and child processes, or between two child processes that have the same ancestor (between processes that are related to each other). To solve this problem, Linux provides a FIFO way to connect the process. FIFO is also called named pipe (named pipe). When a process opens the file in a read (R) manner and another process opens the file in a write (w), the kernel creates a pipeline between the two processes, so the FIFO is actually managed by the kernel and does not deal with the hard disk.

(2) Traditional IPC (interprocess communication). We mainly refer to Message Queuing (messages queue), semaphore (semaphore), shared memory

(3) Sockets can also be used for communication between processes within a computer.

Linux concepts and Systems reading notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.