易截截图软件、单文件、免安装、纯绿色、仅160KB

Linux 下 C语言大文件读写(大于4G)

以下的做法整理自论坛上的帖子。 
如何create大文件
要大就非常大,1T吧。
有两种方法:
 
一.dd
dd if=/dev/zero of=1T.img bs=1G seek=1024 count=0
bs=1G表示每一次读写1G数据,count=0表示读写0次,seek=1024表示略过1024个Block不写,前面block size是1G,所以共略过1T!
这是创建大型sparse文件最简单的方法。
 
二.ftruncate64/ftruncate
如果用系统函数就稍微有些麻烦,因为涉及到宏的问题。我会结合一个实际例子详细说明,其中OPTION标志的就是测试项。
文件sparse.c:
 
//OPTION 1:是否定义与大文件相关的宏
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
 
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define FILENAME "bigfile"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
int main(int argc, char **argv)
{
  int fd, ret;
  off_t offset;
  int total = 0;
  if ( argc >= 2 )
  {
  total = atol(argv[1]);
  printf("total=%d\n", total);
  }
 
  //OPTION 2:是否有O_LARGEFILE选项
  //fd = open(FILENAME, O_RDWR|O_CREAT|O_LARGEFILE, 0644);
  fd = open(FILENAME, O_RDWR|O_CREAT, 0644);
  if (fd < 0) {
  perror(FILENAME);
  return -1;
  }
  offset = (off_t)total *1024ll*1024ll*1024ll;
  printf("offset=%ld\n", offset);
 
  //OPTION 3:是否调用64位系统函数
  //if (ftruncate64(fd, offset) < 0)
  if (ftruncate(fd, offset) < 0)
  {
  printf("[%d]-ftruncate64 error: %s\n", errno, strerror(errno));
  close(fd);
  return 0;
  }
  close(fd);
  printf("OK\n");
  return 0;
}
 
测试环境:
linux:/disk/test/big # gcc --version
gcc (GCC) 3.3.5 20050117 (prerelease) (SUSE Linux)
linux:/disk/test/big # uname -a
Linux linux 2.6.11.4-20a-default #1 Wed Mar 23 21:52:37 UTC 2005 i686 i686 i386 GNU/Linux
 
测试结果


相关文档:

Linux iptable文档

总览
用iptables -ADC 来指定链的规

,-A添加 -D删除 -C 修改
iptables - [RI] chain rule num rule-specification[option]
用iptables - RI 通过规则的顺序指定
iptables -D chain rule num[option]
删除指定规则
iptables -[LFZ] [chain][option]
用iptables -LFZ 链名 [选项]
iptables -[NX] chain
用 -NX ......

fork compare to exec in linux c program

pid_t  pid=fork()
it has 3 situation for the return result pid
0  child
>0 parent process
<0 fork fail
fork create a new process  and it parent live alse when the child process had been created ......

fork compare to exec in linux c program

pid_t  pid=fork()
it has 3 situation for the return result pid
0  child
>0 parent process
<0 fork fail
fork create a new process  and it parent live alse when the child process had been created ......

Linux C 实现线程池

最近做的一些工作需要用到线程池技术,因此参考了一些资料和书籍,如《0bug c/c++商用工程之道》。
为此在linux平台上用纯c写了一个线程池的实现。
在此列出了原代码。
主要用到的数据结构有
1.struct  thread_pool_t    // thread pool 的实现代码
2.struct thread_pool_token_t  &nb ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号