在Linux中有时候手抖会误删了一些文件,所以就搜集了一点解决办法。
-
当你创建一个进程(线程),在不断的往filea里面写日志吧。然后一不小心给删除了。那么此进程(线程)会不会继续往该filea里面写日志呢?
a. 会的。可以ps aux查看你的执行该进程的ID,然后cd pro/ID/fd,
查看就可以发现,该进程仍旧会往filea里面写日志。
b. 可不可以在误删filea之后,创建一个同名文件filea?
经过测试,是不行的。该进程不会往新创建的同名文件filea中打印日志。
测试代码:1 #include<string.h> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdlib.h> 5 #include<sys/time.h> 6 #include<sys/types.h> 7 #include<sys/stat.h> 8 #include<ctype.h> 9 #include<fcntl.h> 10 #include<unistd.h> 11 using namespace std; 12 int main() 13 { 14 int fd = open("falsedelete",O_CREAT|O_RDWR); 15 char str[] = "\nhaha"; 16 char str2[5] = {0}; 17 char str3[20] = {0}; 18 int i = 1; 19 time_t timer; 20 struct tm *tblock; 21 while(1) 22 { 23 sleep(1); 24 tblock = localtime(&timer); 25 timer = time(NULL); 26 27 write(fd,str,sizeof(str)); 28 cout<<str<<" "; 29 30 memset(&str2,0,sizeof(str2)); 31 sprintf(str2,"%d",i); 32 write(fd,str2,sizeof(str2)); 33 cout<<str2<<" "; 34 35 memset(str3,0,sizeof(str3)); 36 sprintf(str3,"%s",asctime(tblock)); 37 write(fd,str3,sizeof(str3)); 38 cout<<str3<<endl; 39 i++; 40 } 41 return 0; 42 }
如果
int fd = open(“falsedelete”,O_CREAT|O_RDWR);
改成
int fd = open(“falsedelete”,o_CREAT|C_RDWR|O_APPEND);
并且放到while里面呢?
我试了,删不掉,