Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 0.60 KB  |  hits: 4  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to unlock using lockf()?
  2. int main() {
  3.   int x;
  4.   pid_t child = fork();
  5.   FILE *file;
  6.   //flock(fileno(file),LOCK_EX);
  7.   file = fopen ("list.txt", "r");
  8.   //printf("file is locked");
  9.   int fdSource = (int)file;
  10.   if (fdSource > 0){
  11.     if (lockf(fdSource, F_LOCK, 0) == -1)
  12.        x = readValue(file);
  13.        return 0; /* FAILURE */
  14.     }
  15.     else {
  16.       return 1;
  17.     }
  18.     if (lockf(fdSource, F_ULOCK, 0) == -1){
  19.        printf("file is not lock");
  20.        appendValue(x);
  21.     }
  22.     else {
  23.        return 1;
  24.     }
  25.     appendValue(x);
  26. }
  27.        
  28. int fd = fileno(file);
  29.        
  30. flock(fd, LOCK_EX);
  31.        
  32. flock(fd, LOCK_UN);