Reading Notes for advanced programming in UNIX environment (1)

Source: Internet
Author: User


I have been reading books and taking some notes recently.
The operating system used in this article is centos7.0. If you do not want to install a dual-system, you can install a virtual machine, refer to here:
Http://blog.csdn.net/alex_my/article/details/38142229

Of course, it would be better to install it directly.
File I/O
1 file descriptor
// View the number of FD limit ulimit-N in the current session
// Modify the FD quantity limit in the current session ulimit-N your_need
You can use the preceding commands on one terminal, and open another command to view the information.
In the program, you can use the system function to modify # include <sys/resource. h>
Struct rlimit {rlim_t rlim_cur; rlim_t rlim_max ;}
Int getrlimit (INT resource, struct rlimit * rlim); int setrlimit (INT resource, const struct rlimit * rlim );
Ext:
Resource options include:
Rlimit_core: sets the core file size. 0 indicates that the creation of core files is forbidden. When a program crashes, a core file is generated in the specified directory for debugging.
Rlimit_cpu: sets the CPU time per second. If this limit is exceeded, the sigxcpu signal is sent.
Rlimit_data: sets the data segment size in the process, in bytes. If this limit is exceeded, malloc () will fail. For details about data segment, refer to here: http://blog.csdn.net/fatshaw/article/details/6294557
Rlimit_fsize: sets the maximum length of an in-process file. If the length is exceeded, A sigxfsz signal is generated. If the thread is blocked, or the process captures or ignores this signal, the file size is reduced from the bottom and the efbig error is set.
Rlimit_nofile: sets the file descriptor size, which is greater than the maximum file descriptor. If this limit is exceeded, the file descriptor application fails and the emfile is set incorrectly.
Rlimit_stack: sets the maximum process stack in bytes.
Rlimit_as: Set the available memory of the Process, in bytes. If this limit is exceeded, malloc () and MMAP () will fail and an enomem error will be set.
2 lseek Function
Off_t lseek (INT Fildes, off_t offset, int whence) mobile read/write offset
Whence options include:
Seek_set: Set the file offset to offset.
Seek_cur: Set the file offset to the current offset + offset.
Seek_end: Set the file offset to the file size + offset.
Method for obtaining the current offset:
Offset value = lseek (FD, 0, seek_cur );
When the operation file is pipe, FIFO, socket,-1 is returned, and the error is set to espipe.
For other errors, refer to Man 3 lseek.
Program case: test2.1.cc
# Include <stdio. h> # include <unistd. h>
Int main (INT argc, char ** argv) {If (lseek (stdin_fileno, 0, seek_cur) =-1) printf ("Seek failed \ n "); else printf ("Seek OK \ n ");
Return 0 ;}
G ++-O test2.1 test2.1.cc
./Test2.1 output: Seek failed. Cause: parameter 1 Fildes must be an opened file descriptor.
./Test2.1 </etc/passwd output: Seek OK


3 file sharing -- close-on-Exec
After a subthread is created using fork, The subthread obtains the data space of the parent process, copies of the heap and stack, and also contains the file descriptor, shared open file tag, and current offset.
When exec is called to execute another program in a child process, the body, Data, heap, and stack of the current process are replaced, that is, the file descriptor in the original child process is lost (not closed ), you cannot disable these file descriptors that are no longer used in sub-processes. The file descriptor is a precious resource of the system, with a limited number.
One way is to disable these file descriptors before executing exec, but in a complicated system, this is a very troublesome thing.
Another way is to specify the file when opening it. When the sub-process executes exec, the file is closed. That is, close-on-exec.
// Implement int flags = fcntl (FD, f_getfd); flags | = fd_cloexec; fcntl (FD, f_setfd, flags) through fcntl );
// Specify int FD = open ("text. log", o_creat | o_wronly | o_trunc | o_cloexec) when creating or opening a file );
For more information, see man 2 open or man 2 creat.

4 atomic operations -- o_append
There is an example:
Programs A and B write the same file. Program a writes 1500 bytes first, program B opens, and CALLS lseek. The result is 1500 at the end of the file, and 100 bytes are written. Program a executes. At this time, the file table entry of program a records 1500 at the end of the file, and writes 100 bytes. In this case, the content written by program B is overwritten.
Solution:
// Lseek is called every time you write data to get the offset at the end of the file.
// Set the parameter o_append during creation or opening. After this flag is set, the file offset is updated to the end of the file every time the write operation is called.

5 atomic operations -- pthread, pwrite
There are two steps to write or read a specified file:
-1: Call lseek and set the offset-2: Call the read or write operation.
The two-step operation may be interrupted during the process and the expected result is not obtained.
Pthread/pwrite can solve this problem:
Ssize_t pread (int fd, void * Buf, size_t count, off_t offset );
Ssize_t pwrite (int fd, const void * Buf, size_t count, off_t offset );
Using pthread/pwrite does not change the current file offset.
When the o_append parameter is specified when the file is opened, pwrite is always added to the end of the file, and the position is not specified according to the parameter offset.
6 dup, dup2, dup3
Int DUP (INT oldfd );
Int dup2 (INT oldfd, int newfd );
Int dup3 (INT oldfd, int newfd, int flags );

These system calls will create a copy of the file descriptor.
DUP returns an unused minimum file descriptor.
Dup2 returns a specified file descriptor. If oldfd specifies the file description mark (close-on-exec), newfd does not have it. Use fcntl again.
If newfd already exists, it is disabled first. If the oldfd and newfd values are the same and the oldfd is a valid file descriptor, dup2 returns newfd without doing anything. If the value of oldfd is the same as that of newfd, and oldfd is an invalid file descriptor, the call fails and the steps to disable newfd are not called.
The effect of dup3 is the same as that of dup2, but you can specify flags as o_cloexec to enable newfd to function close-on-exec.
If the oldfd and newfd values are the same, the call fails and the error is set to inval.
7 fcntl Function
# Include <unistd. h>
# Include <fcntl. h>

Int fcntl (int fd, int cmd,.../* Arg */);

You can change the attributes of an opened file.
Among them, the CMD options include:
F_dupfd: returns a file descriptor greater than or equal to parameter 3 and is least unused. It shares a file table item with FD. If FD has a file descriptor flag (currently only one, fd_cloexec ), the flag is cleared. You can view dup2 related information.
F_dupfd_cloexec: (since Linux 2.6.24) is equivalent to f_dupfd, but the close-on-exce flag is added to the returned file descriptor.
F_getfd: returns the file descriptor flag of FD. There is currently only one flag: fd_cloexec
F_setfd: Set parameter 3 to the file descriptor flag of FD.
F_getfl: indicates the File status flag of FD, that is, the flags mark when open is returned. O_rdonly, o_wdonly, o_rdwr, o_exec, o_search, o_append, o_nonblock, o_sync, o_dsync, o_rsync, o_fsync, o_async
F_setfl: sets the File status flag of FD. The values that can be set are as follows: (in Linux, the local machine is centos7.0, and Mac and FreeBSD have different options ):
O_append: each write operation is added to the end of the file. The effect is equivalent to adding the o_append mark when the file is open.
O_nonblock: non-blocking Io. If the read operation is not readable or the write operation is blocked,-1 is returned and the error is set to eagain. O_async: When I/O is available, the sigio signal can be sent to the process group.
O_direct directly performs file IO, and the system does not cache it as much as possible. For details, refer to the following link: http://blog.csdn.net/zhangxinrun/article/details/7635570http://laokaddk.blog.51cto.com/368606/699563/http://www.ukuug.org/events/linux2001/papers/html/AArcangeli-o_direct.html o_noatime, read the file does not change the last access time.
In addition, there are f_getown, f_setown, f_getown_ex, and so on. For details, see fcntl (2)
8 ioctl function
IOCTL is a function used by the device driver to manage the device's Io channel. Different device drivers define their own IOCTL commands. The socket layer is detailed later.
Again detailed points of the story can see here: http://www.linuxidc.com/Linux/2007-12/9623.htm



Refer:
Http://blog.csdn.net/fatshaw/article/details/6294557
Http://laokaddk.blog.51cto.com/368606/699563/
Http://blog.csdn.net/zhangxinrun/article/details/7635570
Http://www.ukuug.org/events/linux2001/papers/html/AArcangeli-o_direct.html
Http://www.linuxidc.com/Linux/2007-12/9623.htm

Reading Notes for advanced programming in UNIX environment (1)

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.