dongsui8162 2013-07-11 11:07
浏览 30
已采纳

在使用文件时,使用PHP删除(取消链接)或创建(fwrite)文件

I am running a website with a LAMP system.

Contents come from a database.

For caching purposes, I create files on my webserver (containing cachable contents) (via fwrite()).

Every once in a while I am deleting the cache files (via unlink()).

File creation and deletion is done with a cronjob.

My question is: what happens when a visitor to my website is currently browsing (=requesting from webserver) file A.php and I am trying to write to or delete this very same file A.php.

To be precise: what happens if my cronjob issues the unlink() command and at the same time serves the file to the user.

Will this create a race condition?

I know how to retrieve the status from unlink and fwrite operations, so could I just loop over my unlink/fwrite for as long as these function return false (could not be deleted/written)?

Looking forward to reading your opinions/answers!

  • 写回答

1条回答 默认 最新

  • duanchi1230 2013-07-11 11:09
    关注

    Yes, that obviously will create a race condition. The request for the file from the webserver and the deletion from the cron job are independent processes and there is no way for you to know if one or the other is doing something with the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!