unlink 된 fd를 갖고 write() 하였을 때, 반환값이 success??
글쓴이: aswip / 작성시간: 월, 2005/06/20 - 11:12오전
내용을 정리하면 다음과 같습니다.
1. A-Process 파일 Open "111.txt"
2. B-Process 파일 삭제 "111.txt"
3. A-Process write some data, and write function's return value is right, but there is no file
그리고 코드로 풀어쓰면 다음과 같습니다.
#include <iostream> #include <string.h> #include <string> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/types.h> using namespace std; int main ( int argc, char **argv ) { char szFoo[10] = {0}; string strPath = "./111.txt"; // 1 정상적으로 파일을 오픈 한후.. int fd = open ( strPath.c_str(), O_RDWR | O_CREAT); if ( fd < 0 ) { printf ("File open error [%s]\n", strerror ( errno ) ); } else printf ( "fd [%d] \n", fd ); printf("check [%s] file\n", strPath.c_str()); getchar(); // 2 어떤 프로세스에 의해서 파일이 삭제되었다. int nRet = unlink ( strPath.c_str() ); printf ( "unlink result [%d]\n", nRet ); getchar(); // 3 그 후, 1번 과정에서 오픈한 fd를 가지고 파일을 쓰면, 성공이라고 리턴값이 반환된다. nRet = write ( fd, "test", 4 ); printf (" write result [%d]\n", nRet ); close ( fd ); return 0; }
3번 과정속에서 만약, 오픈한 파일이 unlink 되었다면, write () 함수가 실패되어야 하는건 아닌가요?
움.. 아니면, write() 함수호출전, 현재 오픈된 fd가 유효한 fd 인지
체크하는 함수 같은 것이 있나요?
현재는 stat() 보다 부하가 적은 access() 함수를 생각하고 있긴 하지만, 좀 더 낳은 방법이 있지 않을까 해서, 이렇게 조언을 구하고 있습니다. :D
Forums:
man unlink
manual page 에 unlink(2) 에 대한 설명을 보면, 해당 화일을 열고 있는
프로세스가 아무 것도 없을 때 비로소 화일이 지워진다고 나왔네요.
프로그램 실행 중간에 lsof 로 확인해보시면 알 수 있을듯..
DESCRIPTION
unlink deletes a name from the filesystem.
If that name was the last link to a file and
no processes have the file open the file is deleted and
the space it was using is made available for reuse.
댓글 달기