Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c code to copy and reset an open file

Status
Not open for further replies.

dexlabs

Programmer
Nov 20, 2003
5
US
Anyone out there kind enough to write me this c code, I'd greatly appreciate it because I'm not much of a c programmer. Or any advise that might help is ok too. Basically I want to write the contents of a moving/open file at the time of the read to another file, and then removing that part of file from the source. The file descriptor should be preserve so the application will keep on writing to the source file.

In a nutshell, I need:
$ mv source_file dest_file; cat /dev/null>source_file

the problem with this is the application will still write to dest_file because I think its going by file descriptor, not filename.

This command on the otherhand looses some data since its a constantly moving file:
$ cp source_file dest_file; cat /dev/null>source_file

Thanks in advance.
 
Do you have access to the source code of the program which is creating the file?

Do you have any choice over how the program is invoked?
Like can it write its output to stdout, as in
[tt]prog > file[/tt]
or is it always invoked something like this
[tt]prog -output file[/tt]

--
 
Unfortunately we don't have access to the source code that appends to the file. My main goal is chop the file at that point in time and process the file and zero it out without stopping the application that's writing to the file. It should be pretty simple enough but the application just writes soo fast that the simple "cp and >" is not fast enough to get all the records. I appreciate the reply tho.
 
Do you have a command called 'renice' on your machine?
You should already have a command called 'nice'

These alter the priority of processes. Most of the time, processes occupy a sort of middle ground, between high and low priority.

To minimise the loss of data, I'm thinking of the following
- renice the running program to a really low priority. This will all but stop it running without actually killing it off.
- use nice to run your 'cp' at a really high priority.
- when that's done, renice the running program to the level it normally has.

You'll need superuser privilege to pull this off though.

--
 
Good input, I'll try that out.

One more question, is there a way we to change the filename connected to a file descriptor? I was thinking of tricking the application since its going by fildes and not filename to change the filename so it will start writing to that new file. Then I can process the old file and delete it.
 
> One more question, is there a way we to change the filename connected to a file descriptor?
I very much doubt you can do this from outside the program itself.

--
 
The nice/renice work-around seems to be working, but is there a way to see which pid is writing to that file so I'll renice only that pid. There's no way to distinguish it by just doing a ps command. If I have a c-program maybe to pass the file and see who's using it and return me the pid of that process, that would be awesome.
 
How do you run this program?

If you run it from some command file, like
[tt]prog &[/tt]
Then you can get the process ID using
[tt]echo $![/tt]
to print the PID of the previous background command. Save this somewhere, and use it with the renice

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top