问一个文件映射的问题

恰好你也喜欢我 2017-04-26 09:50:37
现在有两个进程要同时读写同一个文本文件,第一个进程fopen文件后,每隔一段时间就一直循环写入内容,但不关闭文件。第二个进程检测文件大小,当到一定大小后就压缩并删除文件,然后再创建一个新的空白文件出来供第一个进程继续写入内容,现在是在windows下第二个进程是肯定删除不了的,有没有什么办法让第二个进程关闭掉这个文件然后操作它。
...全文
281 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
第一个进程要求效率高,不能做多余的事。所以第二个进程来做一些其他事
示申○言舌 2017-04-26
  • 打赏
  • 举报
回复
额。。。这个完全是一个进程就可以搞定的事情嘛.... 非要用两个进程,那就通信嘛。。第一个进程写文件,写啊写,其实他自己是可以知道文件大小的,非让第二个进程去判断大小的话,那就第二个判断好了,第二个发现达到一定的尺寸了,那么就给进程1发一个通知,说你写够了没我要删了,第一个进程收到通知后,停止写入并关闭文件,然后告诉第二个进程说,找嘛急啊,好了,你删吧。然后第二个进程压缩删除完成后再告诉第一个进程,我又弄了一个新文件,你继续写吧。第一个进程收到后重新打开,告诉第二个进程,好的。 其实这个完全用一个进程就OK,缓冲+状态机。。
赵4老师 2017-04-26
  • 打赏
  • 举报
回复
_locking Locks or unlocks bytes of a file. int _locking( int handle, int mode, long nbytes ); Routine Required Header Optional Headers Compatibility _locking <io.h> and <sys/locking.h> <errno.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value _locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values: EACCES Locking violation (file already locked or unlocked). EBADF Invalid file handle. EDEADLOCK Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts. EINVAL An invalid argument was given to _locking. Parameters handle File handle mode Locking action to perform nbytes Number of bytes to lock Remarks The _locking function locks or unlocks nbytes bytes of the file specified by handle. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file. mode must be one of the following manifest constants, which are defined in LOCKING.H: _LK_LOCK Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error. _LK_NBLCK Locks the specified bytes. If the bytes cannot be locked, the constant returns an error. _LK_NBRLCK Same as _LK_NBLCK. _LK_RLCK Same as _LK_LOCK. _LK_UNLCK Unlocks the specified bytes, which must have been previously locked. Multiple regions of a file that do not overlap can be locked. A region being unlocked must have been previously locked. _locking does not merge adjacent regions; if two locked regions are adjacent, each region must be unlocked separately. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program. Example /* LOCKING.C: This program opens a file with sharing. It locks * some bytes before reading them, then unlocks them. Note that the * program works correctly only if the file exists. */ #include <io.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/locking.h> #include <share.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> void main( void ) { int fh, numread; char buffer[40]; /* Quit if can't open file or system doesn't * support sharing. */ fh = _sopen( "locking.c", _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE ); if( fh == -1 ) exit( 1 ); /* Lock some bytes and read them. Then unlock. */ if( _locking( fh, LK_NBLCK, 30L ) != -1 ) { printf( "No one can change these bytes while I'm reading them\n" ); numread = _read( fh, buffer, 30 ); printf( "%d bytes read: %.30s\n", numread, buffer ); lseek( fh, 0L, SEEK_SET ); _locking( fh, LK_UNLCK, 30L ); printf( "Now I'm done. Do what you will with them\n" ); } else perror( "Locking failed\n" ); _close( fh ); } Output No one can change these bytes while I'm reading them 30 bytes read: /* LOCKING.C: This program ope Now I'm done. Do what you will with them File Handling Routines See Also _creat, _open
  • 打赏
  • 举报
回复
之前想了个方法是把文件指针的内存值通过共享内存共享到第二个进程,但发现第二个进程获取到值但操作不到这个文件。

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧