C language project - chat room (1)

1. The role of makefile:

The advantage of makefile is "automatic compilation". Once the makefile is written, only one make command is needed, and the whole project can be compiled automatically, which greatly improves the efficiency of software development. make is a command tool that interprets the instructions in the makefile. Generally speaking, most IDEs have this command, such as: make in Delphhi, nmake in Visual C++, and make in GNU under Linux.

By comparing the update time of the file, if the file is modified, the opening time saved in the computer will change, and the Makefile will update the file according to the time change, which greatly saves compilation time and improves efficiency.

The makefile saves the parameter options of the compiler and the linker, and also expresses the relationship between all source files (specific include files required by source code files, object file modules and libraries required by executable files, etc.). The build program (the make program) first reads the makefile, then activates the compiler, assembler, resource editor and linker to produce the final output, which is usually an executable file. The build program utilizes built-in inference Rules to activate the compiler to generate a specific OBJ file through the compilation of a specific CPP file.

Extended question:

What's in the makefile:

It mainly contains five things: display rules, implicit rules, variable definitions, file instructions and comments

Display rules: Display rules describe how to generate one or more object files, which are specified by the makefile writer to generate files, dependent files, and build commands.

Obscure rules: make has the function of automatic deduction, and the implicit rules allow us to write makefiles simply, which is supported by make.

Variable definition: In the makefile, we need to define a series of variables. The variables are generally strings, similar to the macro definitions in the C language. When the makefile is executed, the variables in it will be expanded to the corresponding reference position.

File instruction: Contains three parts: (1) Reference another makefile in one makefile, similar to include in C language. (2) According to certain circumstances, the effective part of the makefile is formulated, similar to #if in the C language. (3) Define a multi-line command.

Note: There are only line comments in the makefile. Like the UNIX SHELL script, the comments are made with the "#" character, similar to "//" in the C language.

Finally, commands in the makefile must start with the [TAB] key.

 

2. What are the differences between databases and files for data storage?

The file system organizes files into independent data files, which realizes the structure within the record, but there is no structure on the whole; the database file system realizes the structure of the overall data , which is one of the main characteristics of the database, and is also the relationship between the database system and the file. The essential difference of the system. In the file system, data redundancy is large, which wastes storage space and easily causes data inconsistency; in the database system, data is oriented to the entire system and can be shared by multiple users and applications, reducing program redundancy. . The files in the file system serve a specific application. When the logical structure of the data is to be modified, the application program must be modified. There is a lack of independence between the data and the program. In the database system, the data is realized through the two-level image of the DBMS. The physical independence and logical independence of the system separate the definition of data from the program, reducing the maintenance and modification of the application. Both the file system and the database system can store data for a long time, and the data is managed by the data management software. The database system is developed on the basis of the file system.

The database system mainly manages the storage, transactions and operations of the database; the file system is the subsystem of the operating system to manage files and store files, mainly allocating clusters, disks occupied by files, establishing FAT, and managing space. Generally, database systems call the file system to manage their own data files, but some database systems can manage data files themselves, even on the device. The file system is necessary for the operating system, but the database system is only necessary for database management and application.

The difference is:

(1) The file system uses files to store data in external memory for a long time, and the database uses a database to store data uniformly.

(2) The program and data in the file system have a certain relationship, and the program and data in the database system are separated.

(3) The file system uses the access method in the operating system to manage the data, and the database uses the DBMS to manage and control the data uniformly.

(4) The file system shares data in units of files, and the database system realizes data sharing in units of records and fields.

Its connections are:

(1) Both are management techniques for data organization.

(2) The data are managed by the data management software, and the program and the data are converted by the access method.

(3) The database system is developed on the basis of the file system.

3. The difference between system calls and library functions:

A system call refers to the calling process of an application running in the user space requesting some services from the operating system kernel. The system call provides the interface between the user program and the operating system. Generally speaking, system calls are executed in kernel mode. Since system calls do not consider platform differences and are provided directly by the kernel, portability is poor.

A library function is a collection of functions developed by users or organizations with certain functions. Generally, it has better platform portability and provides functional calls to programs through library files (static library or dynamic library). Programmers do not need to care about platform differences, and the library shields platform differences.

The differences between the two are:

library function call system call
Good platform portability Depends on the kernel, no guarantee of portability
Call a program (or function) in a library function Invoke the service of the system kernel
A normal function call is an entry point to the operating system
execute in user control execute in kernel space
Its runtime belongs to "user time" Its run time belongs to "system time"
It is a procedure call, and the call overhead is small Switching between user space and kernel context is expensive
A large number of library functions There are about 90 system calls in UNIX, less
Typical C library calls: printf, scanf, malloc Typical system calls: fork, open, write

connect:

Generally speaking, services closely related to kernel functions and operating system characteristics are provided by system calls; functions with common characteristics generally require better platform portability, so they are provided by library functions.

The library function and the system call function complement each other, such as the management of inter-process communication resources, process control and other functions are closely related to the platform characteristics and the kernel, and must be implemented by the system call.

The functions that platforms have, such as file I/O operations, generally use library functions, which are convenient for platform transplantation.

In some cases, library functions also intersect with system calls. For example, the internal implementation of I/O operations in library functions needs to be implemented by calling system I/O.

 

4. Why choose system call? When to choose a system call action file?

Compared with the standard library functions (fopen, fread, fwrite....), etc., the system call method can only be used under UNIX-like, and the system call is used without buffering mechanism.

1. Buffer mechanism:

1. Complete buffering: Only when the data exceeds the buffer size can the real read and write operations be performed. For example, the fread and fwrite functions are completely buffered. Under normal circumstances, specify how many bytes to read and how many bytes to read;

2. Line buffering: When the data is a newline, a real read and write operation is performed. For example, standard I/O is line buffering, and the terminal is generally the form of line buffering;

3. Unbuffered: that is, no buffer operation is performed on characters, and the read and write functions of standard error output and system calls are unbuffered.

It can be seen from the description that no buffering is generally more urgent, but because the IO speed is slower than the actual running speed of the CPU, the efficiency will be relatively low, and the efficiency of a one-time operation after it is fully read is high, but there is still data in the buffer. Stopping the program at different times may result in the loss of data in the buffer.

2. File operation

System calls operate on file descriptors (the corresponding standard function library operates on file pointers (FILE *))

1. Open the file:

When the program runs, three files are opened by default: standard input (0), standard output (1), and standard error output (2)

The next process in Linux opens up to 1024 files by default. In fact, because Linux treats other device files as files, such as the descriptors of pipes and sockets, file descriptors will also be occupied. Unnecessary files should be closed in time.

(1) open function:

int open(const char* pathname, int flags);

int open(const char* pathname, int flages, mode_t mode);

path: the path and file name of the file

The value of flags: O_RDONLY (read-only) or O_WRONLY (write-only) or O_RDWR (readable and writable) These values ​​can also be bit-ored to add these options O_CREAT (create if the file does not exist) O_EXCL (file If it exists, an error occurs) O_TRUNC (if the file exists, the file is cleared) O_APPEND (write data is appended to the end of the file) O_NONBLOCK (non-blocking)

Mode selects file permissions when creating files, the same as chmod options.

The return value of the function is the file descriptor.

(2) Create a file:

int  creat(const char * pathname, mode_t mode);

(3) Close the file:

int close(int fd);

(4) Read the file:

ssize_t read(int fd, void* buf, size_t count);

fd: file descriptor, buf buffer pointer, count number of bytes read;

The return value is the number of bytes read

(5) Write to file:

ssize_t write(itn fd,void* buf, size_t count);

Parameter setting is similar to return value and read function

(6) File location:

off_t lseek(int fd, off_t offset, int whence);

fd: file descriptor

offset: number of bytes to offset, can be negative

whence: SEEK_SET (beginning of file) SEEK_CUR (current position) SEEK_END (end of file)

Function: move the current position from whence by offset bytes, the return value is the new file offset that can be moved to the end of the file, and then write data again, it will be formed at the end of the original file and where the data is written. A "hole" with 0 data. You can also move the position to the end of the file, and get the size of the file according to the return value.
 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324471374&siteId=291194637