bramante@matrix:~/test$ g++ -o ./hang ./hang.cpp
bramante@matrix:~/test$ gdb ./hang
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/bramante/test/hang...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/bramante/test/hang
test()
block...
block...
block...
block...
block...
block...
block...
^C
Program received signal SIGINT, Interrupt.
0x00007ffff77db090 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:82
82 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) bt
#0 0x00007ffff77db090 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:82
#1 0x00007ffff77daf4c in __sleep (seconds=0) at ../sysdeps/unix/sysv/linux/sleep.c:138
#2 0x0000000000400770 in PARENT::~PARENT() ()
#3 0x00007ffff77575b1 in __run_exit_handlers (status=0, listp=0x7ffff7ad3688, run_list_atexit=true) at exit.c:78
#4 0x00007ffff7757635 in __GI_exit (status=<optimized out>) at exit.c:100
#5 0x0000000000400746 in main ()
(gdb)
bramante@matrix:~/test$ g++ -o ./crash ./crash.cpp
bramante@matrix:~/test$ gdb ./crash
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/bramante/test/crash...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/bramante/test/crash
test()
test()
Program received signal SIGSEGV, Segmentation fault.
__GI___libc_free (mem=0xdeadbeef) at malloc.c:2970
2970 malloc.c: No such file or directory.
(gdb) bt
#0 __GI___libc_free (mem=0xdeadbeef) at malloc.c:2970
#1 0x0000000000400831 in PARENT::~PARENT() ()
#2 0x00007ffff77575b1 in __run_exit_handlers (status=0, listp=0x7ffff7ad3688, run_list_atexit=true) at exit.c:78
#3 0x00007ffff7757635 in __GI_exit (status=<optimized out>) at exit.c:100
#4 0x00000000004007ea in main ()
(gdb)
#include <stdio.h>#include <stddef.h>#include <dirent.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <linux/fcntl.h>#define BLOCK_SIZE 4096intmain(intargc,char*argv[]){intfhInput;intfhOutput;intlen;intsum;staticunsignedcharbuf[BLOCK_SIZE]__attribute__((aligned(4096)));if(argc==3){fhInput=open(argv[1],O_RDONLY|O_DIRECT);fhOutput=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC|O_DIRECT);if((fhInput<0)||(fhOutput<0)){printf("[Error] open file fail.\n");if(fhInput>=0){close(fhInput);}else{printf("[Error] Can not open %s\n",argv[1]);}if(fhOutput>=0){close(fhOutput);}else{printf("[Error] Can not open %s\n",argv[2]);}return-1;}else{sum=0;do{len=read(fhInput,buf,BLOCK_SIZE);//printf("len: %d\n",len);write(fhOutput,buf,BLOCK_SIZE);sum+=BLOCK_SIZE;}while(len==BLOCK_SIZE);if(len>0){ftruncate(fhOutput,sum-BLOCK_SIZE+len);}structstatfileAttr;fstat(fhInput,&fileAttr);fchmod(fhOutput,fileAttr.st_mode);close(fhOutput);close(fhInput);return0;}}else{printf("[help] ./my_copy ./src_file ./dest_file \n");return-1;}}
編譯與執行:
Terminal
12345678910111213
bramante@matrix:~/test$ gcc -o ./my_copy ./my_copy.c
bramante@matrix:~/test$ which ls
/bin/ls
bramante@matrix:~/test$ ll /bin/ls
-rwxr-xr-x 1 root root 105840 Nov 20 2012 /bin/ls*
bramante@matrix:~/test$ ./my_copy /bin/ls ./ls
bramante@matrix:~/test$ ll | grep ls
-rwxr-xr-x 1 bramante bramante 105840 Sep 4 11:15 ls*
bramante@matrix:~/test$ diff ./ls /bin/ls
bramante@matrix:~/test$
bramante@matrix:~/test$ ./ls -al | grep ls
-rwxr-xr-x 1 bramante bramante 105840 Sep 4 11:15 ls
bramante@matrix:~/test$
Direct IO 所read/write的byte數, 以及read/write的起點,
還有read()/write()所使用的memory buffer, 應該都要align在512 byte的倍數上,
精確的alignment限制可以參考網路上的說明:
bramante@matrix:~$ ll ./test.bin
-rwxr--r-- 1 bramante bramante 72087592 Sep 1 15:46 ./test.bin*
bramante@matrix:~$
bramante@matrix:~$ scp ./test.bin minions@192.168.1.3:~/
The authenticity of host '192.168.1.3 (192.168.1.3)' can't be established.
ECDSA key fingerprint is a1:3e:45:35:f5:23:e2:f3:56:c8:50:bf:22:05:4b:9b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.3' (ECDSA) to the list of known hosts.
minions@192.168.1.3's password:
test.bin 100% 69MB 11.5MB/s 00:06
bramante@matrix:~$