Linux 32-bit platform. The file size is limited by 2 GB.

Source: Internet
Author: User
Tags signal handler

 

The company's Asterisk System has already had two crash. The check logs are all caused by the log writing continuing when the log file is fully written to 2 GB and the dumping is automatically executed. After Google, I found the following article, like!
Solve the problem that the file size is limited to 2 GB, and repost it to your own space.

Break through the 2 GB file size limit of the ftell function in Linux
Http://www.demix.cn/h? * Z = 28507
If the file size exceeds 2 GB in 32-bit Linux, an error may occur, or even cause the program to stop running.
This is because the index used for file processing in the Linux system is defined as long, while the size of long on the 32-bit system is 32-bit, therefore, a maximum of 2 ^ 31-1 = 2,147,483,647 bytes is supported. Therefore, 2 GB is used to deduct 1 byte.
64-bit systems (such as amd64 or IA64) are defined as 64-bit long, so there is no problem ..
# If _ wordsize = 64
Typedef long int int64_t;
# Endif
However, on FreeBSD, even 32-bit systems do not have a limit on the size of 2 GB files, because when FreeBSD processes files internally, originally, 64-bit numbers are used as indicators, so there is no problem.
Therefore, in 32-bit Linux, the program requires some additional processing to correctly write files larger than 2 GB
Let's first write a small program to test (large. C)
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <fcntl. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <signal. h>
# Include <unistd. h>
# Include <errno. h>
Void sig_xfsz (INT sig)
{
Printf ("error: sigxfsz (% d) signal received! \ N ", sig );
}
Int main ()
{
Int I, FD;
Char dum'my [4096];

Signal (sigxfsz, sig_xfsz );

Unlink ("large. log ");
FD = open ("large. log", o_creat | o_wronly, 0644 );

Bzero (dummy, 4096 );
/* 2 GB = 4 kb x 524288 */
For (I = 0; I <524287; I ++)
Write (FD, dummy, 4096 );
Write (FD, dummy, 4095 );
Printf ("large. Log: 2147483647 bytes \ n ");

If (write (FD, dummy, 1) <0)
Printf ("error: % s [errno: % d] \ n", strerror (errno), errno );
Else
Printf ("large. Log: 2147483648 bytes \ n ");

Close (FD );
Exit (0 );
}
In 32-bit Linux, if no special processing is performed after the above program is compiled, the execution result is as follows:
# Gcc-O large32 large. c
#./Large32
Large. Log: 2147483647 bytes
Error: sigxfsz (25) signal received!
Error: file too large [errno: 27]
When writing 2,147,483,648th bytes, the program will receive signal sigxfsz, write () will return the-1 error, and errno will be 27 (file too large ). Even worse, if the program does not process sigxfsz as above, the built-in signal handler may even cause the program to stop running and generate core dump
Next, we will add-d_file_offset_bits = 64 when compiling the same program and try again:
# Gcc-d_file_offset_bits = 64-O large64 large. c
#./Large64
Large. Log: 2147483647 bytes
Large. Log: 2147483648 bytes
The limit of 2 GB was exceeded!
The same program runs correctly under the 32-bit FreeBSD, whether or not this definition is added.
However, when processing these large files, in addition to the different parameters during program compilation, some functions also need to be adjusted, such as fseek () and ftell () the two functions originally used long integer as offset:
Int fseek (File * stream, long offset, int whence );
Long ftell (File * stream );
As long as the system is 32-bit, the off_t version must be used even under FreeBSD:
Int fseeko (File * stream, off_t offset, int whence );
Off_t ftello (File * stream );
In Linux, if _ file_offset_bits is defined as 64, the off_t type is automatically converted to 64-bit RMB (on FreeBSD, off_t is originally the size of 64-bit RMB)
There are some differences in the compilation options required for each system to support File Reading and Writing larger than 2 GB. Even for Linux, 32-bit yuan or 64-bit yuan may vary. There is a simple way to judge, that is, to use the getconf provided by glibc to obtain the parameters required for compilation (compile) and linking (linking:
# Getconf lfs_cflags
-D_largefile_source-d_file_offset_bits = 64
# Getconf lfs_ldflags

#
The above is the result of running on 32-bit RedHat Linux, which indicates that on this system, if you want your program to support 2 GB of file read/write, compile (compile) -d_largefile_source-d_file_offset_bits = 64 must be added. No parameter is required for linking.

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.