fork 和 vfork
linux下的fork和vfork都是为了产生子进程的,两者主要区别是
1)fork产生了子进程之后,先执行子进程还是先执行父进程是随机的。
2)vfork产生了子进程之后,肯定先执行子进程,然后等子进程退出或者等子进程调用了execl函数之后再执行父进程。
但是,但是,我遇到了问题。
同样的程序,如果用vfork,那么就会出现问题,问题报告如下:
我改用fork之后,除了执行顺序没问题之外,其他一切都好了,也不会报错了
这是为什么呢?
这是为什么呢?
难道非要我去看vfork的源代码么?
1)fork产生了子进程之后,先执行子进程还是先执行父进程是随机的。
2)vfork产生了子进程之后,肯定先执行子进程,然后等子进程退出或者等子进程调用了execl函数之后再执行父进程。
但是,但是,我遇到了问题。
同样的程序,如果用vfork,那么就会出现问题,问题报告如下:
***
glibc detected
***
.
/
client_daemon: free(): invalid next size (fast):
0x0000000000878eb0
***
======= Backtrace: =========
/ lib / x86_64 - linux - gnu / libc.so. 6 ( + 0x7e626 )[ 0x7f2a76bbf626 ]
/ usr / lib / libpq.so. 5 ( + 0xc1b0 )[ 0x7f2a76f0a1b0 ]
/ usr / lib / libpq.so. 5 ( + 0xc615 )[ 0x7f2a76f0a615 ]
/ usr / lib / libpq.so. 5 (PQsetdbLogin + 0x20f )[ 0x7f2a76f0c68f ]
. / client_daemon[ 0x401241 ]
. / client_daemon[ 0x40110e ]
/ lib / x86_64 - linux - gnu / libc.so. 6 ( + 0x364c0 )[ 0x7f2a76b774c0 ]
/ lib / x86_64 - linux - gnu / libc.so. 6 (pause + 0x10 )[ 0x7f2a76bfefc0 ]
. / client_daemon[ 0x400eb4 ]
/ lib / x86_64 - linux - gnu / libc.so. 6 (__libc_start_main + 0xed )[ 0x7f2a76b6276d ]
. / client_daemon[ 0x400d89 ]
======= Memory map: ========
00400000 - 00402000 r - xp 00000000 08 : 09 721455 / home / iamdsy / git - jzl / client_daemon
00601000 - 00602000 r -- p 00001000 08 : 09 721455 / home / iamdsy / git - jzl / client_daemon
00602000 - 00603000 rw - p 00002000 08 : 09 721455 / home / iamdsy / git - jzl / client_daemon
0086c000 - 0088d000 rw - p 00000000 00 : 00 0 [heap]
7f2a72284000 - 7f2a72299000 r - xp 00000000 08 : 09 1150604 / lib / x86_64 - linux - gnu / libgcc_s.so. 1
7f2a72299000 - 7f2a72498000 --- p 00015000 08 : 09 1150604 / lib / x86_64 - linux - gnu / libgcc_s.so. 1
7f2a72498000 - 7f2a72499000 r -- p 00014000 08 : 09 1150604 / lib / x86_64 - linux - gnu / libgcc_s.so. 1
7f2a72499000 - 7f2a7249a000 rw - p 00015000 08 : 09 1150604 / lib / x86_64 - linux - gnu / libgcc_s.so. 1
7f2a7249a000 - 7f2a724a3000 r - xp 00000000 08 : 09 1150593 / lib / x86_64 - linux - gnu / libcrypt - 2.15 .so
7f2a724a3000 - 7f2a726a3000 --- p 00009000 08 : 09 1150593 / lib / x86_64 - linux - gnu / libcrypt - 2.15 .so
7f2a726a3000 - 7f2a726a4000 r -- p 00009000 08 : 09 1150593 / lib / x86_64 - linux - gnu / libcrypt - 2.15 .so
7f2a726a4000 - 7f2a726a5000 rw - p 0000a000 08 : 09 1150593 / lib / x86_64 - linux - gnu / libcrypt - 2.15 .so
7f2a726a5000 - 7f2a726d3000 rw - p 00000000 00 : 00 0
7f2a726d3000 - 7f2a72771000 r - xp 00000000 08 : 09 52248 / usr / lib / x86_64 - linux - gnu / libsqlite3.so. 0.8 . 6
7f2a72771000 - 7f2a72971000 --- p 0009e000 08 : 09 52248 / usr / lib / x86_64 - linux - gnu / libsqlite3.so. 0.8 . 6
7f2a72971000 - 7f2a72973000 r -- p 0009e000 08 : 09 52248 / usr / lib / x86_64 - linux - gnu / libsqlite3.so. 0.8 . 6
7f2a72973000 - 7f2a72975000 rw - p 000a0000 08 : 09 52248 / usr / lib / x86_64 - linux - gnu / libsqlite3.so. 0.8 . 6
7f2a72975000 - 7f2a72976000 rw - p 00000000 00 : 00 0
7f2a72976000 - 7f2a729bb000 r - xp 00000000 08 : 09 53124 / usr / lib / x86_64 - linux - gnu / libhx509.so. 5.0 . 0
7f2a729bb000 - 7f2a72bbb000 --- p 00045000 08 : 09 53124 / usr / lib / x86_64 - linux - gnu / libhx509.so. 5.0 . 0
7f2a72bbb000 - 7f2a72bbd000 r -- p 00045000 08 : 09 53124 / usr / lib / x86_64 - linux - gnu / libhx509.so. 5.0 . 0
7f2a72bbd000 - 7f2a72bbf000 rw - p 00047000 08 : 09 53124 / usr / lib / x86_64 - linux - gnu / libhx509.so. 5.0 . 0
7f2a72bbf000 - 7f2a72bc0000 rw - p 00000000 00 : 00 0
7f2a72bc0000 - 7f2a72bce000 r - xp 00000000 08 : 09 52516 / usr / lib / x86_64 - linux - gnu / libheimbase.so. 1.0 . 0
7f2a72bce000 - 7f2a72dcd000 --- p 0000e000 08 : 09 52516 / usr / lib / x86_64 - linux - gnu / libheimbase.so. 1.0 . 0
7f2a72dcd000 - 7f2a72dce000 r -- p 0000d000 08 : 09 52516 / usr / lib / x86_64 - linux - gnu / libheimbase.so. 1.0 . 0
7f2a72dce000 - 7f2a72dcf000 rw - p 0000e000 08 : 09 52516 / usr / lib / x86_64 - linux - gnu / libheimbase.so. 1.0 . 0
7f2a72dcf000 - 7f2a72df7000 r - xp 00000000 08 : 09 52551 / usr / lib / x86_64 - linux - gnu / libwind.so. 0.0 . 0
7f2a72df7000 - 7f2a72ff6000 --- p 00028000 08 : 09 52551 / usr / lib / x86_64 - linux - gnu / libwind.so. 0.0 . 0
7f2a72ff6000 - 7f2a72ff7000 r -- p 00027000 08 : 09 52551 / usr / lib / x86_64 - linux - gnu / libwind.so. 0.0 . 0
7f2a72ff7000 - 7f2a72ff8000 rw - p 00028000 08 : 09 52551 / usr / lib / x86_64 - linux - gnu / libwind.so. 0.0 . 0
7f2a72ff8000 - 7f2a72ffb000 r - xp 00000000 08 : 09 1150858 / lib / x86_64 - linux - gnu / libgpg - error.so. 0.8 . 0
7f2a72ffb000 - 7f2a731fa000 --- p 00003000 08 : 09 1150858 / lib / x86_64 - linux - gnu / libgpg - error.so. 0.8 . 0
7f2a731fa000 - 7f2a731fb000 r -- p 00002000 08 : 09 1150858 / lib / x86_64 - linux - gnu / libgpg - error.so. 0.8 . 0
7f2a731fb000 - 7f2a731fc000 rw - p 00003000 08 : 09 1150858 / lib / x86_64 - linux - gnu / libgpg - error.so. 0.8 . 0
7f2a731fc000 - 7f2a7320d000 r - xp 00000000 08 : 09 52256 / usr / lib / x86_64 - linux - gnu / libp11 - kit.so. 0.0 . 0
7f2a7320d000 - 7f2a7340c000 --- p 00011000 08 : 09 52256 / usr / lib / x86_64 - linux - gnu / libp11 - kit.so. 0.0 . 0
7f2a7340c000 - 7f2a7340d000 r -- p 00010000 08 : 09 52256 / usr / lib / x86_64 - linux - gnu / libp11 - kit.so. 0.0 . 0
7f2a7340d000 - 7f2a7340e000 rw - p 00011000 08 : 09 52256 / usr / lib / x86_64 - linux - gnu / libp11 - kit.so. 0.0 . 0
7f2a7340e000 - 7f2a7341e000 r - xp 00000000 08 : 09 50596 / usr / lib / x86_64 - linux - gnu / libtasn1.so. 3.1 . 12
7f2a7341e000 - 7f2a7361d000 --- p 00010000 08 : 09 50596 / usr / lib / x86_64 - linux - gnu / libtasn1.so. 3.1 . 12
7f2a7361d000 - 7f2a7361e000 r -- p 0000f000 08 : 09 50596 / usr / lib / x86_64 - linux - gnu / libtasn1.so. 3.1 . 12
7f2a7361e000 - 7f2a7361f000 rw - p 00010000 08 : 09 50596 / usr / lib / x86_64 - linux - gnu / libtasn1.so. 3.1 . 12
7f2a7361f000 - 7f2a73633000 r - xp 00000000 08 : 09 52250 / usr / lib / x86_64 - linux - gnu / libroken.so. 18.1 . 0
7f2a73633000 - 7f2a73832000 --- p 00014000 08 : 09 52250 / usr / lib / x86_64 - linux - gnu / libroken.so. 18.1 . 0
7f2a73832000 - 7f2a73833000 r -- p 00013000 08 : 09 52250 / usr / lib / x86_64 - linux - gnu / libroken.so. 18.1 . 0
7f2a73833000 - 7f2a73834000 rw - p 00014000 08 : 09 52250 / usr / lib / x86_64 - linux - gnu / libroken.so. 18.1 . 0
7f2a73834000 - 7f2a73865000 r - xp 00000000 08 : 09 52513 / usr / lib / x86_64 - linux - gnu / libhcrypto.so. 4.1 . 0
7f2a73865000 - 7f2a73a65000 --- p 00031000 08 : 09 52513 / usr / lib / x86_64 - linux - gnu / libhcrypto.so. 4.1 . 0
7f2a73a65000 - 7f2a73a66000 r -- p 00031000 08 : 09 52513 / usr / lib / x86_64 - linux - gnu / libhcrypto.so. 4.1 . 0
7f2a73a66000 - 7f2a73a67000 rw - p 00032000 08 : 09 52513 / usr / lib / x86_64 - linux - gnu / libhcrypto.so. 4.1 . 0
7f2a73a67000 - 7f2a73a68000 rw - p 00000000 00 : 00 0
7f2a73a68000 - 7f2a73b03000 r - xp 00000000 08 : 09 52253 / usr / lib / x86_64 - linux - gnu / libasn1.so. 8.0 . 0
7f2a73b03000 - 7f2a73d02000 --- p 0009b000 08 : 09 52253 / usr / lib / x86_64 - linux - gnu / libasn1.so. 8.0 . 0
7f2a73d02000 - 7f2a73d04000 r -- p 0009a000 08 : 09 52253 / usr / lib / x86_64 - linux - gnu / libasn1.so. 8.0 . 0
7f2a73d04000 - 7f2a73d08000 rw - p 0009c000 08 : 09 52253
Aborted (core dumped)
但是,但是
======= Backtrace: =========
/ lib / x86_64 - linux - gnu / libc.so. 6 ( + 0x7e626 )[ 0x7f2a76bbf626 ]
/ usr / lib / libpq.so. 5 ( + 0xc1b0 )[ 0x7f2a76f0a1b0 ]
/ usr / lib / libpq.so. 5 ( + 0xc615 )[ 0x7f2a76f0a615 ]
/ usr / lib / libpq.so. 5 (PQsetdbLogin + 0x20f )[ 0x7f2a76f0c68f ]
. / client_daemon[ 0x401241 ]
. / client_daemon[ 0x40110e ]
/ lib / x86_64 - linux - gnu / libc.so. 6 ( + 0x364c0 )[ 0x7f2a76b774c0 ]
/ lib / x86_64 - linux - gnu / libc.so. 6 (pause + 0x10 )[ 0x7f2a76bfefc0 ]
. / client_daemon[ 0x400eb4 ]
/ lib / x86_64 - linux - gnu / libc.so. 6 (__libc_start_main + 0xed )[ 0x7f2a76b6276d ]
. / client_daemon[ 0x400d89 ]
======= Memory map: ========
00400000 - 00402000 r - xp 00000000 08 : 09 721455 / home / iamdsy / git - jzl / client_daemon
00601000 - 00602000 r -- p 00001000 08 : 09 721455 / home / iamdsy / git - jzl / client_daemon
00602000 - 00603000 rw - p 00002000 08 : 09 721455 / home / iamdsy / git - jzl / client_daemon
0086c000 - 0088d000 rw - p 00000000 00 : 00 0 [heap]
7f2a72284000 - 7f2a72299000 r - xp 00000000 08 : 09 1150604 / lib / x86_64 - linux - gnu / libgcc_s.so. 1
7f2a72299000 - 7f2a72498000 --- p 00015000 08 : 09 1150604 / lib / x86_64 - linux - gnu / libgcc_s.so. 1
7f2a72498000 - 7f2a72499000 r -- p 00014000 08 : 09 1150604 / lib / x86_64 - linux - gnu / libgcc_s.so. 1
7f2a72499000 - 7f2a7249a000 rw - p 00015000 08 : 09 1150604 / lib / x86_64 - linux - gnu / libgcc_s.so. 1
7f2a7249a000 - 7f2a724a3000 r - xp 00000000 08 : 09 1150593 / lib / x86_64 - linux - gnu / libcrypt - 2.15 .so
7f2a724a3000 - 7f2a726a3000 --- p 00009000 08 : 09 1150593 / lib / x86_64 - linux - gnu / libcrypt - 2.15 .so
7f2a726a3000 - 7f2a726a4000 r -- p 00009000 08 : 09 1150593 / lib / x86_64 - linux - gnu / libcrypt - 2.15 .so
7f2a726a4000 - 7f2a726a5000 rw - p 0000a000 08 : 09 1150593 / lib / x86_64 - linux - gnu / libcrypt - 2.15 .so
7f2a726a5000 - 7f2a726d3000 rw - p 00000000 00 : 00 0
7f2a726d3000 - 7f2a72771000 r - xp 00000000 08 : 09 52248 / usr / lib / x86_64 - linux - gnu / libsqlite3.so. 0.8 . 6
7f2a72771000 - 7f2a72971000 --- p 0009e000 08 : 09 52248 / usr / lib / x86_64 - linux - gnu / libsqlite3.so. 0.8 . 6
7f2a72971000 - 7f2a72973000 r -- p 0009e000 08 : 09 52248 / usr / lib / x86_64 - linux - gnu / libsqlite3.so. 0.8 . 6
7f2a72973000 - 7f2a72975000 rw - p 000a0000 08 : 09 52248 / usr / lib / x86_64 - linux - gnu / libsqlite3.so. 0.8 . 6
7f2a72975000 - 7f2a72976000 rw - p 00000000 00 : 00 0
7f2a72976000 - 7f2a729bb000 r - xp 00000000 08 : 09 53124 / usr / lib / x86_64 - linux - gnu / libhx509.so. 5.0 . 0
7f2a729bb000 - 7f2a72bbb000 --- p 00045000 08 : 09 53124 / usr / lib / x86_64 - linux - gnu / libhx509.so. 5.0 . 0
7f2a72bbb000 - 7f2a72bbd000 r -- p 00045000 08 : 09 53124 / usr / lib / x86_64 - linux - gnu / libhx509.so. 5.0 . 0
7f2a72bbd000 - 7f2a72bbf000 rw - p 00047000 08 : 09 53124 / usr / lib / x86_64 - linux - gnu / libhx509.so. 5.0 . 0
7f2a72bbf000 - 7f2a72bc0000 rw - p 00000000 00 : 00 0
7f2a72bc0000 - 7f2a72bce000 r - xp 00000000 08 : 09 52516 / usr / lib / x86_64 - linux - gnu / libheimbase.so. 1.0 . 0
7f2a72bce000 - 7f2a72dcd000 --- p 0000e000 08 : 09 52516 / usr / lib / x86_64 - linux - gnu / libheimbase.so. 1.0 . 0
7f2a72dcd000 - 7f2a72dce000 r -- p 0000d000 08 : 09 52516 / usr / lib / x86_64 - linux - gnu / libheimbase.so. 1.0 . 0
7f2a72dce000 - 7f2a72dcf000 rw - p 0000e000 08 : 09 52516 / usr / lib / x86_64 - linux - gnu / libheimbase.so. 1.0 . 0
7f2a72dcf000 - 7f2a72df7000 r - xp 00000000 08 : 09 52551 / usr / lib / x86_64 - linux - gnu / libwind.so. 0.0 . 0
7f2a72df7000 - 7f2a72ff6000 --- p 00028000 08 : 09 52551 / usr / lib / x86_64 - linux - gnu / libwind.so. 0.0 . 0
7f2a72ff6000 - 7f2a72ff7000 r -- p 00027000 08 : 09 52551 / usr / lib / x86_64 - linux - gnu / libwind.so. 0.0 . 0
7f2a72ff7000 - 7f2a72ff8000 rw - p 00028000 08 : 09 52551 / usr / lib / x86_64 - linux - gnu / libwind.so. 0.0 . 0
7f2a72ff8000 - 7f2a72ffb000 r - xp 00000000 08 : 09 1150858 / lib / x86_64 - linux - gnu / libgpg - error.so. 0.8 . 0
7f2a72ffb000 - 7f2a731fa000 --- p 00003000 08 : 09 1150858 / lib / x86_64 - linux - gnu / libgpg - error.so. 0.8 . 0
7f2a731fa000 - 7f2a731fb000 r -- p 00002000 08 : 09 1150858 / lib / x86_64 - linux - gnu / libgpg - error.so. 0.8 . 0
7f2a731fb000 - 7f2a731fc000 rw - p 00003000 08 : 09 1150858 / lib / x86_64 - linux - gnu / libgpg - error.so. 0.8 . 0
7f2a731fc000 - 7f2a7320d000 r - xp 00000000 08 : 09 52256 / usr / lib / x86_64 - linux - gnu / libp11 - kit.so. 0.0 . 0
7f2a7320d000 - 7f2a7340c000 --- p 00011000 08 : 09 52256 / usr / lib / x86_64 - linux - gnu / libp11 - kit.so. 0.0 . 0
7f2a7340c000 - 7f2a7340d000 r -- p 00010000 08 : 09 52256 / usr / lib / x86_64 - linux - gnu / libp11 - kit.so. 0.0 . 0
7f2a7340d000 - 7f2a7340e000 rw - p 00011000 08 : 09 52256 / usr / lib / x86_64 - linux - gnu / libp11 - kit.so. 0.0 . 0
7f2a7340e000 - 7f2a7341e000 r - xp 00000000 08 : 09 50596 / usr / lib / x86_64 - linux - gnu / libtasn1.so. 3.1 . 12
7f2a7341e000 - 7f2a7361d000 --- p 00010000 08 : 09 50596 / usr / lib / x86_64 - linux - gnu / libtasn1.so. 3.1 . 12
7f2a7361d000 - 7f2a7361e000 r -- p 0000f000 08 : 09 50596 / usr / lib / x86_64 - linux - gnu / libtasn1.so. 3.1 . 12
7f2a7361e000 - 7f2a7361f000 rw - p 00010000 08 : 09 50596 / usr / lib / x86_64 - linux - gnu / libtasn1.so. 3.1 . 12
7f2a7361f000 - 7f2a73633000 r - xp 00000000 08 : 09 52250 / usr / lib / x86_64 - linux - gnu / libroken.so. 18.1 . 0
7f2a73633000 - 7f2a73832000 --- p 00014000 08 : 09 52250 / usr / lib / x86_64 - linux - gnu / libroken.so. 18.1 . 0
7f2a73832000 - 7f2a73833000 r -- p 00013000 08 : 09 52250 / usr / lib / x86_64 - linux - gnu / libroken.so. 18.1 . 0
7f2a73833000 - 7f2a73834000 rw - p 00014000 08 : 09 52250 / usr / lib / x86_64 - linux - gnu / libroken.so. 18.1 . 0
7f2a73834000 - 7f2a73865000 r - xp 00000000 08 : 09 52513 / usr / lib / x86_64 - linux - gnu / libhcrypto.so. 4.1 . 0
7f2a73865000 - 7f2a73a65000 --- p 00031000 08 : 09 52513 / usr / lib / x86_64 - linux - gnu / libhcrypto.so. 4.1 . 0
7f2a73a65000 - 7f2a73a66000 r -- p 00031000 08 : 09 52513 / usr / lib / x86_64 - linux - gnu / libhcrypto.so. 4.1 . 0
7f2a73a66000 - 7f2a73a67000 rw - p 00032000 08 : 09 52513 / usr / lib / x86_64 - linux - gnu / libhcrypto.so. 4.1 . 0
7f2a73a67000 - 7f2a73a68000 rw - p 00000000 00 : 00 0
7f2a73a68000 - 7f2a73b03000 r - xp 00000000 08 : 09 52253 / usr / lib / x86_64 - linux - gnu / libasn1.so. 8.0 . 0
7f2a73b03000 - 7f2a73d02000 --- p 0009b000 08 : 09 52253 / usr / lib / x86_64 - linux - gnu / libasn1.so. 8.0 . 0
7f2a73d02000 - 7f2a73d04000 r -- p 0009a000 08 : 09 52253 / usr / lib / x86_64 - linux - gnu / libasn1.so. 8.0 . 0
7f2a73d04000 - 7f2a73d08000 rw - p 0009c000 08 : 09 52253
Aborted (core dumped)
我改用fork之后,除了执行顺序没问题之外,其他一切都好了,也不会报错了
这是为什么呢?
这是为什么呢?
难道非要我去看vfork的源代码么?