unlink 된 fd를 갖고 write() 하였을 때, 반환값이 success??

aswip의 이미지

내용을 정리하면 다음과 같습니다.

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

cmoh1110의 이미지

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.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.