Linux System Programming Notes

Source: Internet
Author: User

Written at the beginning: Out of the future career planning considerations (in fact, a mess), has developed a basic learning direction, that is, from the system programming learning API slowly deep into the kernel, this is a more mature learning route. So from this beginning, in this period of time will continue to record the Linux system programming learning notes, in addition to learning to review the only use, but also hope to record the first entry into the field of 3 years of career.

Chapter I. File I/O

The basic invocation of file access is generally read () and write (), but before accessing the file, it is important to do the job: open it, yes! Implemented by calling open () or create ()

#include <sys/type.h> #include <sys/stat.h> #include <fcntl.h>int open (const char *name,int flags) int Open (const char *name,int flags,mode_t mode);

The call successfully returns FD (pointing to the file referred to by name), which is determined by the flags.

The flags parameter has three access modes O_rdonly,o_wronly,o_rdwr

The flags parameter also allows you to enrich the way the file is opened by bitwise OR operation with the following parameters

O_append: Append mode opens with pointer pointing to end of file (name pointer)


O_async: For special files (FIFO, pipe, socket, terminal), if readable or writable, generates a signal (default is Sigio)


O_cloexec: When executing, the file shuts down automatically when the new process is executed, avoiding competition.


O_creat:name The specified file does not exist, it is created automatically, and is not valid if the file exists in this bit and o_excl federated.


O_direct: Open file for direct I/O


O_directory: If the parameter name is not a directory, the open () call fails, and the location bit is actually called internally Opendir ()

dir* opendir (const char *path)

Successfully returned FD, failed to return null pointer.


O_EXCL: O_creat () is used in conjunction, if the name parameter specifies that the file exists then the open () call fails to prevent contention when the file is created.


O_largefile: File offset uses 64-bit integers to support files larger than 2G, and 64-bit systems use this parameter by default


O_noatime+: The last access time is not updated when the file is read, and can be used to: backup, index, etc., to avoid a large number of writes to update the index nodes of each file.


O_noctty: If the given name parameter points to the end device, it will not become the control terminal for this process. This flag bit is seldom used.


O_nofollow: If name points to a symbolic connection, open () fails. Normally, the symbolic connection is parsed and the file is opened, and if the subdirectory is still a link, open () resolves and opens the file. This situation can be used by celebrities who blog can use this flag bit, that is, adding links to his blog is invalid.


O_nonblock: Non-blocking mode is turned on, either open () or other operation, which does not cause the process to block (sleep) in I/O. Applies only to FIFO.


O_sync: Open file for synchronous I/O, after each write (), the data will be synchronized to the physical disk


O_trunc: If the file exists, it is a normal file, there is write permission, the location bit will truncate the file length to 0.

Linux System Programming 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.