The following question is for learning purpose only.
I work on x86 based RedHat Linux and g++3.4.3
In the following program, I create a temporary file named
'error_log.txt'. Then I do a cin.get() so that the program waits for
some key-press. At this time I open another terminal window and delete
this 'error_log.txt' file created already, by issuing the command:
rm error_log.txt
Now I did 'ls' from the directory where 'error_log,txt' was deleted.
The directory listing did NOT show 'error_log.txt', as expected. Now I
go to the first terminal window where the original program is waiting
for user input. Here I press 'RETURN'. The program tries to write some
contents into the file 'error_log.txt'(which was deleted in the other
terminal window).
Now if I do
if (!ofs)
{
// ...
}
the block of code inside the 'if' is NOT executed. I thought since the
error_log.txt was deleted, the statement
ofs << "test line" << endl;
will result in error for the 'ofs'. But it does not happen.
Why doesn't the operation
ofs << "test line" << endl;
fail (after deleting the error_log.txt' through another terminal
window)? I cannot understand this. Kindly explain. What is the
expected bahaviour ?
Here is the complete program z.cpp
#include <cstdlib>
#include <ostream>
#include <istream>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream ofs("error_log.txt");
if (!ofs)
{
cout << "Could not create the file: error_log.txt"
<< endl;
exit(EXIT_FAILURE);
}
cin.get();
ofs << "test line" << endl;
if (!ofs)
{
cout << "Error encountered while logging error in "
"the file: error_log.txt"
<< endl;
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
This program compiles fine with g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra z.cpp
Thanks
V.Subramanian
|
|
0
|
|
|
|
Reply
|
subramanian100in (346)
|
11/29/2010 3:37:34 PM |
|
subramanian100in@yahoo.com, India <subramanian100in@yahoo.com> wrote:
> I cannot understand this. Kindly explain. What is the
> expected bahaviour ?
Even if you remove a file in Linux (with 'rm' or whatever), Linux won't
physically remove it as long as any program has an open handle to it. It
will be physically removed only after the last program which has an open
handle to it releases it. The file won't show with 'ls' and it cannot be
opened (eg. by a new program), but it will still be there, waiting for
it to be released. Only after that will the system finally mark it as
completely removed.
(I don't remember now, however, if this is related to Linux itself or
to the specific file system you are using.)
This behavior is system-specific so it should not be relied on in
portable programs.
|
|
0
|
|
|
|
Reply
|
Juha
|
11/29/2010 3:51:55 PM
|
|
<subramanian100in@yahoo.com> wrote in message
news:0dd13896-9a15-4c0c-b5ab-287bb60fd8be@v17g2000prc.googlegroups.com=20
> The following question is for learning purpose only.
>=20
> I work on x86 based RedHat Linux and g++3.4.3
>=20
> In the following program, I create a temporary file named
> 'error_log.txt'. Then I do a cin.get() so that the program waits for
> some key-press. At this time I open another terminal window and delete
> this 'error_log.txt' file created already, by issuing the command:
> rm error_log.txt
> Now I did 'ls' from the directory where 'error_log,txt' was deleted.
> The directory listing did NOT show 'error_log.txt', as expected. Now I
> go to the first terminal window where the original program is waiting
> for user input. Here I press 'RETURN'. The program tries to write some
> contents into the file 'error_log.txt'(which was deleted in the other
> terminal window).
> Now if I do
> if (!ofs)
> {
> // ...
> }
> the block of code inside the 'if' is NOT executed. I thought since the
> error_log.txt was deleted, the statement
> ofs << "test line" << endl;
> will result in error for the 'ofs'. But it does not happen.
> Why doesn't the operation
> ofs << "test line" << endl;
> fail (after deleting the error_log.txt' through another terminal
> window)? I cannot understand this. Kindly explain. What is the
> expected bahaviour ?
The behaviour is very opeating system dependent.
I think that for e.g. Windows, a very different behaviour is observed.
So, you will get better answers when this question is placed in a Linux =
newsgroup.
|
|
0
|
|
|
|
Reply
|
Fred
|
11/29/2010 3:55:38 PM
|
|
On Nov 29, 7:51=A0am, Juha Nieminen <nos...@thanks.invalid> wrote:
> =A0 (I don't remember now, however, if this is related to Linux itself or
> to the specific file system you are using.)
It's portable among *nix. There are no links to the file in the file
system, but the inode still hangs around in core.
|
|
0
|
|
|
|
Reply
|
red
|
11/29/2010 5:19:18 PM
|
|
On Nov 29, 5:19=A0pm, red floyd <redfl...@gmail.com> wrote:
> On Nov 29, 7:51=A0am, Juha Nieminen <nos...@thanks.invalid> wrote:
>
> > =A0 (I don't remember now, however, if this is related to Linux itself =
or
> > to the specific file system you are using.)
>
> It's portable among *nix. =A0There are no links to the file in the file
> system, but the inode still hangs around in core.
Which isn't to say you won't get different/peculiar behaviour,
particularly on NFS or Samba shares, where the Unix semantics are not
necessarily supported.
|
|
0
|
|
|
|
Reply
|
gwowen
|
11/30/2010 8:43:06 AM
|
|
"subramanian100in@yahoo.com, India" <subramanian100in@yahoo.com> wrote in
news:0dd13896-9a15-4c0c-b5ab-287bb60fd8be@v17g2000prc.googlegroups.com:
> The following question is for learning purpose only.
>
> I work on x86 based RedHat Linux and g++3.4.3
>
> In the following program, I create a temporary file named
> 'error_log.txt'. Then I do a cin.get() so that the program waits for
> some key-press. At this time I open another terminal window and delete
> this 'error_log.txt' file created already, by issuing the command:
> rm error_log.txt
In Unix/Linux, this does not remove the file, it just unlinks it from the
directory tree. There is a remove(3) library function, but this just
calls unlink(2) for files. Such behavior is needed for supporting hard
links in the filesystem. In Windows world, NTFS is also supporting
hardlinks but I think they are not used very often.
In Windows, the file deletion fails if the file has been opened by some
other application. They have gone to quite extreme measures to overcome
this problem, up to introducing a new system call (MoveFileEx()) to
register the files to be deleted at the restart of the system. So, while
the Unix 'rm' behavior seems strange and counter-intuitive and can cause
loss of data, the Windows "intuitive solution" (resulting in system
reboots) appears to be not better.
I am not sure if there are any OS-es/filesystems where the deletion of
file causes errors in subsequent writes to that file in a running process
(as you expected). One can imagine this approach would bring a bunch of
new problems. For starters, the ownership and access rights of the
directory and the file therein can be different, but deletion of a file
mostly concerns the directory.
hth
Paavo
|
|
0
|
|
|
|
Reply
|
Paavo
|
11/30/2010 10:37:51 PM
|
|
|
5 Replies
160 Views
(page loaded in 0.081 seconds)
Similiar Articles: snapshot error: File system could not be write locked - comp.unix ...error writing to new file - comp.emacs snapshot error: File system could not be write locked - comp.unix ... On the new disk I just put everything into one large file ... What are the ways that files can be shared among users? - comp.sys ...... Write Only - Files dropped in another User's Drop Box are no ... deleted by anyone but the user who first put the file into the folder. *** I am beginning to see why ... error ... Pervasive 8 File Growth Factor - comp.databases.btrieve... same amount of data went into the table as was deleted. When we recover this file ... engine uses TWA only when writing to files ... btrieve Pervasive 8 Fatal File Manager error need help with corrupt MAIL.MAI file - comp.os.vmsThe disk in question shows no errors. A few ... privately OK, I'll try to bring it back into ... MAIL.MAI file using an alternate key, write to a sequential output file ... Odd BACKUP error: unsupported file structure ! - comp.os.vms ...ANA/RMS on the file uncovered no errors. > > I do not trust the disk (which is why I ... (I've deleted 10s of thousands of files that were a ... software write-locked, file ... Insert statements and foreign keys etc.. - comp.databases.mysql ...... until all their orders are deleted. Do I insert the customer data into the ... know if I've handled Index files ... Can a customer really have no Orders? If so, why is ... Old LOC constraint stuck somewhere - comp.arch.fpgaI deleted all files I could > imagine could have this ... listed > > =A0 =A0below to be packed into a ... > > > No warning/error message if they don't exist, no log message ... How to keep only 5 most recent files in certain folder - comp.unix ...Causing rm(1) to emit an error message since no files were ... calls my team receives is "I just deleted a big file but ... How to save files into a specific folder - comp ... HP-UX 11.11 overwriting core file(s) - How to prevent? - comp.sys ...Then the core files are deleted so any new ones found after ... Another solution (writing core into core.pid) can be found ... if (-1 == mkdir(buf, 0700)) { ... error handling ... Weird Error Message - comp.cad.solidworksI would get the error (even though my files did not contain those ... clicked or tried to drag and drop the file into ... Sometimes it can only be > fixed or even deleted ... Error Messages - Microsoft Corporation: Software, Smartphones ...Recommended alternative: Write error message text ... if, for example, they couldn't move a deleted file. For these causes, the error ... the Clipboard data can't be pasted into ... Writing files to CD RW error - Tech Support Guy ForumsI'm trying to write files to a CD RW but the comp is not allowing it for some reason. ... Writing files to CD RW error 7/27/2012 2:52:45 PM
|