[C] 设置文件共享锁 →→→→→进入此内容的聊天室

来自 , 2020-07-02, 写在 C, 查看 160 次.
URL http://www.code666.cn/view/4cb81113
  1. #include <io.h>
  2. #include <fcntl.h>
  3. #include <sys\stat.h>
  4. #include <process.h>
  5. #include <share.h>
  6. #include <stdio.h>
  7.  
  8. int main(void)
  9. {
  10.    int handle, status;
  11.    long length;
  12.  
  13.    /* Must have DOS Share.exe loaded for */
  14.    /* file locking to function properly */
  15.  
  16.    handle = sopen("c:\\autoexec.bat",
  17.       O_RDONLY,SH_DENYNO,S_IREAD);
  18.  
  19.    if (handle < 0)
  20.    {
  21.       printf("sopen failed\n");
  22.       exit(1);
  23.    }
  24.  
  25.    length = filelength(handle);
  26.    status = lock(handle,0L,length/2);
  27.  
  28.    if (status == 0)
  29.       printf("lock succeeded\n");
  30.    else
  31.       printf("lock failed\n");
  32.  
  33.    status = unlock(handle,0L,length/2);
  34.  
  35.    if (status == 0)
  36.       printf("unlock succeeded\n");
  37.    else
  38.       printf("unlock failed\n");
  39.  
  40.    close(handle);
  41.    return 0;
  42. }

回复 "设置文件共享锁"

这儿你可以回复上面这条便签

captcha