所以我有一组n个流,我用管道打开,但是使用gdb,我发现程序在我尝试关闭管道的流或写入端时失败了。我可以写管道就好了,但关闭它们不起作用。我在程序上运行了valgrind,它所做的只是打印出来
==4241== Warning: invalid file descriptor -1 in syscall read()
一遍又一遍。这是有问题的代码和错误输出。如果需要更多信息,请告诉我,我会尽力提供。感谢大家。
191 /*closes unused pipes and open streams to sorting processes*/
192 for (i = 0; i < n; i++) {
193 close(parse2sortPipes[i][READ]);
194 close(sort2outputPipes[i][READ]);
195 close(sort2outputPipes[i][WRITE]);
196 outStreams[i] = fdopen(parse2sortPipes[i][WRITE], "w");
197 }
198
199 while (getWord(fstream, wBuf) != -1) {
200 if (i == n) {
201 i = 0;
202 }
203 fputs(wBuf, outStreams[i]);
204 fputc('\n', outStreams[i]);
205 i++;
206 }
207
208
209
210 for (i = 0; i < n; i++) {
211 fflush(outStreams[i]);
212 fclose(outStreams[i]); //this is where it fails
213 close(parse2sortPipes[i][WRITE]);
214 }
215
*** Error in `..../xxx': double free or corruption (top): 0x0000000000603250 ***
======= Backtrace: =========
208 for (i = 0; i < n; i++) {
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7ffff7a92a46]
/lib/x86_64-linux-gnu/libc.so.6(fclose+0x14d)[0x7ffff7a80a6d]
/home/user/school/cs/assignments/hw3/xxx[0x401477]
/home/user/school/cs/assignments/hw3/xxx[0x400ce7]
(gdb) /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7ffff7a33ea5]
/home/user/school/cs/assignments/hw3/xxx[0x400b39]
======= Memory map: ========
00400000-00402000 r-xp 00000000 08:06 12588471 /home/daniel/school/cs311/assignments/hw3/xxx
00601000-00602000 r--p 00001000 08:06 12588471 /home/daniel/school/cs311/assignments/hw3/xxx
00602000-00603000 rw-p 00002000 08:06 12588471 /home/daniel/school/cs311/assignments/hw3/xxx
00603000-00624000 rw-p 00000000 00:00 0 [heap]
7ffff77fc000-7ffff7810000 r-xp 00000000 08:06 3149442 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7810000-7ffff7a10000 ---p 00014000 08:06 3149442 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7a10000-7ffff7a11000 r--p 00014000 08:06 3149442 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7a11000-7ffff7a12000 rw-p 00015000 08:06 3149442 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ffff7a12000-7ffff7bd0000 r-xp 00000000 08:06 3149417 /lib/x86_64-linux-gnu/libc-2.17.so
7ffff7bd0000-7ffff7dcf000 ---p 001be000 08:06 3149417 /lib/x86_64-linux-gnu/libc-2.17.so
7ffff7dcf000-7ffff7dd3000 r--p 001bd000 08:06 3149417 /lib/x86_64-linux-gnu/libc-2.17.so
7ffff7dd3000-7ffff7dd5000 rw-p 001c1000 08:06 3149417 /lib/x86_64-linux-gnu/libc-2.17.so
7ffff7dd5000-7ffff7dda000 rw-p 00000000 00:00 0
7ffff7dda000-7ffff7dfd000 r-xp 00000000 08:06 3149393 /lib/x86_64-linux-gnu/ld-2.17.so
7ffff7fd7000-7ffff7fda000 rw-p 00000000 00:00 0
7ffff7ff6000-7ffff7ff8000 rw-p 00000000 00:00 0
7ffff7ff8000-7ffff7ffa000 rw-p 00000000 00:00 0
7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0 [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00022000 08:06 3149393 /lib/x86_64-linux-gnu/ld-2.17.so
7ffff7ffd000-7ffff7fff000 rw-p 00023000 08:06 3149393 /lib/x86_64-linux-gnu/ld-2.17.so
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
答案 0 :(得分:0)
fclose
将关闭基础文件描述符,因此之后不要调用close
。