简体   繁体   English

我对输出文件执行“ rm”操作后,C程序在哪里写输出?

[英]Where does a C program write output after I did “rm” on the output file?

I ran a rather nasty C program on a computing cluster running Redhat that got into an infinite loop, within each of which it printed a line of output. 我在运行Redhat的计算集群上运行了一个相当讨厌的C程序,该程序陷入了无限循环,在每个循环中它打印了一行输出。 When I realized it was quickly creating a file that would eventually use up all the disk space, I ran "rm" on that output file before I killed the program. 当我意识到它正在迅速创建一个最终将占用所有磁盘空间的文件时,我在终止程序之前对该输出文件运行了“ rm”命令。 Unfortunately, per "df -h" space continued to get used up on the drive before I finally killed the program. 不幸的是,在我最终杀死该程序之前,每个“ df -h”空间一直在驱动器上用尽。

I now can't find the file that was written, so I'm unable to delete it. 我现在找不到写入的文件,因此无法删除它。 Where would such a file be written to? 这样的文件将写入哪里?

Killing the program should release the disk space. 终止程序应释放磁盘空间。 Until then, the file is unlinked from its folder, but will not cease to exist (as an inode) as long as it is actively open. 在此之前,该文件从其文件夹中取消链接,但是只要它是主动打开的,它就不会停止存在(作为inode)。

Check ls -l /proc/<pid>/fd to see what the files are; 检查ls -l /proc/<pid>/fd以查看文件是什么。 essentially, everything the kernel knows about that process is somewhere in /proc/<pid> . 本质上,内核有关该进程的所有信息都在/proc/<pid>

Read a lot more on how to deal with these problems here: https://unix.stackexchange.com/questions/68523/find-and-remove-large-files-that-are-open-but-have-been-deleted 在此处阅读有关如何处理这些问题的更多信息: https : //unix.stackexchange.com/questions/68523/find-and-remove-large-files-that-are-open-but-have-been-deleted

It was writing to the file you deleted. 它正在写入您删除的文件。

Per the man page for unlink(3) : 根据unlink(3)的手册页:

The unlink() function shall remove a link to a file. unlink()函数应删除到文件的链接。 If path names a symbolic link, unlink() shall remove the symbolic link named by path and shall not affect any file or directory named by the contents of the symbolic link. 如果路径命名为符号链接,则unlink()应当删除由path命名的符号链接,并且不影响由该符号链接的内容命名的任何文件或目录。 Otherwise, unlink() shall remove the link named by the pathname pointed to by path and shall decrement the link count of the file referenced by the link. 否则,unlink()将删除由path指向的路径名所命名的链接,并应减少该链接所引用文件的链接计数。

When the file's link count becomes 0 and no process has the file open, the space occupied by the file shall be freed and the file shall no longer be accessible. 当文件的链接计数变为0且没有进程打开文件时,应释放文件所占用的空间,并且不再可访问该文件。 If one or more processes have the file open when the last link is removed, the link shall be removed before unlink() returns, but the removal of the file contents shall be postponed until all references to the file are closed . 如果在删除最后一个链接时一个或多个进程打开了文件,则该链接应在unlink()返回之前被删除, 但是文件内容的删除应推迟到所有对该文件的引用都关闭为止

In Unix/Linux a file is a datastructure (on disc). 在Unix / Linux中,文件是数据结构(在光盘上)。 As long as a pointer (or reference, see below) to this exists in the system, the data is not deleted. 只要系统中存在指向该指针的指针(或引用,请参见下文),就不会删除数据。 Once there is no pointer anymore, it is, however. 一旦不再有指针,它就会存在。

A pointer can be basically having the file opened by a program. 指针基本上可以使程序打开文件。 Once the program (or these programs) terminates, this pointer becomes invalid. 一旦程序终止,该指针将无效。

A pointer can also be an entry in a directoy of the filesystem. 指针也可以是文件系统目录中的条目。 This would be the entry you see with ls or in a graphical filemanager. 这将是您在ls或图形文件管理器中看到的条目。 So, once you remove this entry, that pointer is also deleted, but no other! 因此,一旦删除该条目, 指针也将被删除,但是没有其他! Note that there can be multiple such entries all over the (same) file system. 请注意,整个(相同)文件系统上可以有多个这样的条目。 These are called hardlinks (many (pointers) to one (file) relation). 这些称为硬链接(到一个(文件)关系的许多(指针))。

Actually, the file counts all these references (permanent on disc for directory entries/hardlinks or in system memory for open files ("file handles)). The latter costs precious system memory for each open file and is one reason why a file most always be closed and should be asap. 实际上,该文件会计算所有这些引用(永久保存在磁盘上的目录条目/硬链接或打开的文件(“文件句柄”)在系统内存中)。后者为每个打开的文件占用宝贵的系统内存,这是文件始终处于最常运行状态的原因之一封闭,应尽快。

So, once all pointers are deleted, the file cannot be accessed anymore and so it will be deleted. 因此,一旦删除所有指针,该文件将无法再访问,因此将被删除。 This is called garbage collection (if that was invented nowadays, it might have been called "memory recycling" perhaps) btw. 顺便说一句,这被称为垃圾收集 (如果是当今发明的,那么它可能被称为“内存回收”)。 and is similar to what languages like Java and Python do for unused objects. 类似于Java和Python等语言对未使用对象的处理方式。

Reason why I called it "pointer" is the similarity: It is actually a specific index (address) of a data structure called "inode" where it points to, not the directory entry. 我之所以将其称为“指针”是相似之处:它实际上是数据结构的特定索引(地址),该数据结构指向 “ inode” ,而不是目录条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM