Linux Basics Chapter I. Overview

Source: Internet
Author: User
Tags mutex

Chapter I overview 1.1 preface

This chapter discusses the concept of a system, a more profound understanding of computer systems from a hardware and operating system perspective, and a quick tour of the services provided by Linux systems.

1.2 System composed of 1.3 operating systems and applications

Operating system This word has two meanings, sometimes refers to the kernel, sometimes refers to the combination of kernel and system tool software.

The operating system is the software that manages the system hardware. The operating system runs directly on top of bare metal. Other applications run on top of the operating system.

The operating system itself provides the interface to support the user through the interface to the operating system, but the system itself provides the functionality, not enough to complete the user requirements, you need to develop applications to expand the system functions.

Release version:
Different companies use the Linux kernel, together with their own developed system Tools software, to release the Linux operating system version.

1.4 Starting and landing

Configuration file:
/etc/profile: Executed at system startup
~/.BASHRC: Called when the user logs on

1.5 Files

A file is an important concept that is generally defined as a collection of information. A computer is a machine for information processing, and a file is a computer-processed object.

In Unix and Linux systems, the concept of files is generalized, and devices are abstracted into file objects for manipulation.

A collection of data is called a file.
IT industry processes information: conversion, transfer, storage

1.6 Program, process 1.7 error handling

The system call returns an integer in General , and 0 indicates success, and less than 0 indicates failure. When a system call returns a failure, you can get an error through errno, get an error explanation through strerror, or output an error message directly through PERROR in a standard error file.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main ()
{
An integer returned by open, with a special name in Linux
Called File descriptor filename Description abbreviated FD

int fd = open ("A.txt", o_wronly| O_creat| O_EXCL, 0777);
if (FD < 0)//indicates file open failed
{
Perror ("open");
return 0;
}

Put the pointer at the beginning of the file
Lseek (FD, 0, Seek_set);

Working with files
Write (FD, "Hello", 5);

Close the file, and if it does not close, the memory leaks
Files that are not closed are automatically closed when the process exits
Close (FD);

}

1.8 User, group, file permissions

Linux is a multi-user system that allows multiple users to log in simultaneously.
For security reasons, you need to add permissions to the system to the specification.

1.9 Signal

A signal is a means of process communication in which a process receives a signal that may come from the kernel, from another process, or from a user operation. For example, when the user presses CTRL + C, it actually sends a signal to the foreground process.

1.10 system calls and library functions

When learning the Linux system development interface, programmers also need to learn the common third-party library, to expand the programmer's programming ability.


User space and kernel space are common concepts in operating system programming, indicating that the current code is running in user or kernel space, and that the CPU handles the memory slightly differently for different running spaces, and then involves the concept when talking about the process virtual address space.

System invocation refers to the functionality provided by the operating system kernel, which provides an interface to user space code calls. such as Open/read/write/close, all belong to the Linux system operating interface, and Fopen/fread/fwrite/fclose is a C standard provided interface, under Linux, fopen in fact, the bottom call open.

Configuration file:
/etc/profile: Executed at system startup
~/.BASHRC: Called when the user logs on

file Operations

header file:sys/types.h sys/stat.h fcntl.h example : int fd=open ("file path", mode); Mode determines how the file is manipulated the third parameter is optional, the file permissions are processed, because umask exists, create file permissions to 010 with the anti- Cause user rights to start cannot have write permission

Mode Option

Explain

O_rdonly

Read mode open (mutually exclusive with the latter two)

O_wronly

Write mode Open

O_rdwr

Read/write mode open

O_creat

Create file, if file exists, open directly

O_trunc

Truncated

O_append

Additional

O_excl

With o_creat , if the file exists it fails

function:

Perror: Print some kind of error message

Open/creat: Open file / Create file
read: Reading files
Write: Writing a file
Close: Closing files
Lseek: Locating file read-write location
Fcntl: Modifying file properties
sysconf: Read System configuration
Dup/dup2: Copying file descriptors
Sync/fsync/fsyncdata: Synchronizing file Data
Mmap/munmap: File mapping
Mkstemp: Get temporary file path

Command

Touch: Modify the access time of a file, create a file
Cat: Accessing file contents
Vim: Edit
Ulimit: Displays some restriction information (maximum file descriptor, space size of the stack)
Umask: Permission mask for file creation
getconf: Corresponding sysconf

DD: can copy block device, but to sudo permission example dd if= location of= file name bs= How many k cout= copy times

Wc: calculates the number of lines of a file number of words bytes

Unlink: Remove soft links

Signal

is a way to control the process of communication, high efficiency, low cost

Signal processing methods: masking, ignoring, default processing

Mask: Processing of delay signals using a signal set

Hide unreliable signals, send multiple times, and handle only once  Mask: Reliable signal processed multiple times

Process

Fork () Create

Threads

The mouse Keyboard is a read-only character folder device, so you can use the function to monitor generally under the /dev/input/mic file attention to permissions problems mouse keyboard reading data, is two processes, note the process of blocking problems can be used in Word process and parent process processing

creation of Threads

pthread_created (1,2,3,4)//1: Thread ID 2 : Properties of Thread 3: function name of new thread, 4: New Thread properties to link -lpthread Library

Note that the child thread is attached to the main thread, the main thread ends, and the child thread cannot run this pthread_exit (0) The main thread ends, the child thread does not exit the exception

Use Pthread_equal to determine if the thread is equal, wait for the return 0 to return a non-0 value

Pthread_jion (1,&ret) Blocking call 1: Thread ID RET: Thread return value

pthread_t tid = pthread_self () Gets the ID of the current running process

the difference between a process and a thread:

Process: The unit thread that allocates resources: dispatched units multithreading can share global variables

Lock

Prevent two threads from manipulating global variables at the same time, the first thread uses the lock, the thread behind waits, waiting to be unlocked, and the subsequent thread comes in.

Dead lock

After two consecutive locks, lock, not unlocked, and continue to lock, will lead to deadlock.  Using a cyclic lock, you can repeat the lock by defining the properties of the lock into a cyclic lock example: pthread_mutexattr_t attr;  Pthread_mutexattr_init (&ATTR); Pthread_mutexattr_settype (&attr, pthread_mutex_recursive);p thread_mutex_init (&mutex,&attr)

After locking, forget to unlock, also will appear deadlock in C + + using destructors, you can avoid forgetting to unlock, define a class

Read/write Lock

pthread_rwlock_t Mutex;

Pthread_rwlock_init (&mutex, NULL);

Read/write lock Pthread_rwlock_rd/wrlock (&mutex);

Unlock: Pthread_rwlock_unlock (&mutex);

Daemon Process

Daemons are not associated with the terminal, note that this process can only have one, create a file record, determine whether this program is turned on

Programming Rules:

set umask=0;

call Fork tolet the parent process exit. Make the parent process init, and if the parent process does not exit, use two fork ()

call setuid to create a new session Setsid

Reset Current directory / root directory chdir

close the unwanted file descriptor use loops to close all file descriptors

Advanced IO

A process is a command.

IO multiplexing Technology The use of select

apply a collection of file descriptors using fd_set to create file descriptor set file interface is relatively small, cross-platform application

Fd_set(1,2) put the file descriptor in the file descriptor collection 1: File descriptor 2 : Collection name

The application of Epoll

EPOLLFD Creating a collection of file descriptors

Epol_ctl Adding a file description to the collection

non-blocking IO

Piping

While reading, writing on the side

Anonymous Pipeline pipe () Create pipeline

Mmap can achieve a parent-child relationship process of file sharing efficiency is low, data write memory, in the memory read data use Shm_open realize file sharing can also

File memory sharing, unable to communicate

Lock to allow process to share memory for communication pthread_mutex_init need to place locks in shared memory

Fork + exec lets the process have different functions

Linux Basics Chapter I. Overview

Related Article

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.