C Language and Linux under Windows and C standard library and system API

Source: Internet
Author: User

1. What are our problems?

The standard C library is the same! Everyone must have done in Windows file programming, under Linux is the same function name, parameters are the Same. There was a doubt, because we knew very well

Its nature is not the same, because it is two operating systems ah! however, because the C standard library is packaged, it implements the same interface. But if we go deeper, we know that there is a layer of encapsulation on this layer that is actually the encapsulation of the operating system on file operations, which is file io. While our c-language encapsulation is called standard io, what difference do they have? Obviously standard IO is based on file io, and standard IO calls file io, and also optimizes file io, such as the introduction of caching Mechanisms.

2. I'm Talking about the next few Points.

But when it comes to system calls, it's different!

    • System calls from different systems are different, and some calls may be the same (interface), but the internal implementation is different!
    • There is no difference if there are no library functions or programming methods that are unique to the system (i.e., code and methods that conform to ANSI C standards only) (standard library, standard Io)
    • When it comes to system programming, the difference is that the library functions are different, and the file system differs from the way some functions are executed. (file io, Operating System Api)
3. Some specific differences

\ n for ASCII 0x0a line break

\ r for ASCII 0x0d carriage return

In a Windows system, when you enter a carriage return, it automatically becomes \ r \ n

The ENTER key under Linux only represents \ n

And the ENTER key under Windows is \ r \ n

\ nto go to the next line, \ R for the print head back to the beginning

Linux/unix only use \ n, It means carriage return + newline

and under windows, \ r only returns no newline, \ n is a newline, but in some edits, the individual \ n is not wrapped (E.G. notepad)

Generally in the program, write \ n, It can be in Linux or Windows can achieve carriage return + line-wrapping function (only in the text file, Linux will only have 0x0a,windows will automatically change to 0x0d 0x0a)

Here's an example:

#include <stdio.h> int main () {char a[10]= "abc\r"; printf (a); return 0;}

The program runs without any Output.

The reason is that the \ r carriage return means that the printhead is back to the start of the line and thus covers the abc, so there is no output on the console!

The difference between function libraries

The C function library under Linux is not the same mechanism as the library system call under Windows

The glibc contains the main c libraries. This library provides basic routines for allocating memory, searching directories, opening closed files, reading and writing files, string processing, pattern matching, mathematical calculations, and so On. The mechanism is different not only refers to the problem of the interrupt number, the interrupt number is also through the input parameter and output to locate the function address and address in the register, those functions under Windows and Linux implementation should be different, take the file system, How can EXT3 and FAT32 be the Same. there are also mm memory management, are not the Same. interrupts or hardware layer, the X86 should be similar, but the implementation of the operating system layer is very different.

4. Other Differences:

1. System platform is not the same

The underlying development involves the kernel of the system, and for linux, you can know what the structure is, while Windows ...

2. compiler environment not the same

Linux uses the GCC compiler, gdb debugging tools, and a variety of visual editors such as Emacs,kedit and so on, there is also the text of the VI/VIM,GDB function is very powerful, personally think more than win under the good

Although the win down Mingw,devcpp integrated gcc, but it is always uncomfortable ~,GCC support for the standard is quite good

3. Not the same for the CROWD.

Win under the main or commercial development, and the vast majority of programming enthusiasts like to belong to their own open and free system programming, do not want to imprison under the Windows (MS) surrounded

4. The direction of development is not the Same.

Opensource's ideas have blossomed in the Linux land, see GPL ..... Easy to find information, source code open, You can experience the fun of development

win, Ms gradually put a number of developers bound in its own system inside, development environment more and more fools, this can be called humanization? Get to the end ... May go further and farther ~

5. Copyright Issues

Win a lot of things are related to copyright issues, Linux free software, Although it is open software, but a lot of many are freely used for commercialization ... Of course some need to develop source code, a lot of do not need ~

The difference between C + + is similar

From the point of view of the function of the program, the functions provided by the library are usually services that do not require an operating system, and functions are performed within the user space, unless the function involves I/O operations, etc., it is generally not cut to the kernel Mentality. System call is to require the operating system to provide users with processes, to provide a service, usually involving the hardware resources of the system and some sensitive software Resources.

Functions of function libraries, especially those related to input and output, must be done by Linux system Calls. So we can think of function library functions as an intermediate layer between the Application designer and the system invoker, through which we can safely invoke the system calls with a consistent interface. This allows programmers to write code once to be able to use the backlog between different versions of Linux systems to implement completely different system Calls. As for how to implement compatibility issues with different system calls, This is a concern for library Developers.

From the point of view of program execution efficiency, the execution efficiency of system call is higher than that of function, especially the function of processing input and Output. When the amount of data processed is relatively small, the function Library's function execution may be more efficient, because the function Library's approach is to buffer the data that will be processed, wait until the buffer is full, and then write or read the data once. This approach is relatively efficient when dealing with small amounts of data, but when making system calls, because the user process enters the system core mode from user mode, involves a lot of additional task switching, called context switching, which can affect the efficiency of the system Execution. however, when the amount of data to be processed is large, for example, when the amount of data input and output exceeds the size of the file system definition, the system call can be used to achieve higher efficiency.

From the point of view of the portability of the program, the standard library of C (ANSI C) has a high portability relative to the system call, and in different system environments, as long as there is little modification, it is usually not necessary to modify it.

Differences between library functions and system calls in Linux C

Library functions are high-level, fully running in user space, providing programmers with a more convenient interface to invoke Real-world system calls that do Real-world transactions behind the Scenes. System calls run in the kernel state and are provided by the kernel itself. The standard C library function printf () can be seen as a generic output statement, but what it actually does is convert the data into a conforming string and call the system to call write () to output the strings.

Do you want to see what system calls are being used by printf ()? It's easy to compile the following Code.

Copy the code code as Follows:

# include <stdio.h>

int main (void)

{printf ("hello"); return 0;}

Use the command Gcc-wall-o Hello hello.c compilation. Use the command strace hello to trace the executable file. Are you surprised? Each row corresponds to a system Call. Strace is a very useful program that can tell you which system calls the program uses and the parameters of these system calls, and the return Value. This is a very valuable tool to see what the program is Doing. At the end of the output, you should see such a line like write (1, "hello", 5hello). That's What we're looking for. Hidden in the mask of printf () the true face. Since most people use library functions to manipulate file I/O (like fopen, fputs, fclose). You can view the second part of the man description using the command Man 2 Write. The second part of the man description is devoted to system calls (like kill () and Read ()). The third part of the man description is devoted to library functions that you might be more familiar with (like cosh () and Random ()).

You can even write code to overwrite system calls, as we will soon do. Hackers often do this to install a backdoor or Trojan for the System. But you can use it to do something more rewarding, like letting the kernel output "Tee hee, that tickles!" information every time someone deletes a file.

--zhang Fei online October 14, 2016 23:06:45

C Language and Linux under Windows and C standard library and system API

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.