C语言 写()工作,即使我阻止我的文件由其他进程

dced5bon  于 2022-12-26  发布在  其他
关注(0)|答案(1)|浏览(97)

当我运行程序时:

int i = fork();

if (!i){
    int id = open("shared.txt", 0600 | O_WRONLY);
    if(flock(id, LOCK_EX | LOCK_NB) == -1)
        perror("flock_ch");
if(write(id, "child", 5)) printf("child did it\n");
else perror("write_ch");
sleep(3);
    close(id);
}
else {
    int id = open("shared.txt", 0600 | O_WRONLY);
    if(flock(id, LOCK_EX | LOCK_NB) == -1)
       perror("flock_PR");
if(write(id, "parent", 6)) printf("parent did it\n");
else perror("write_pr");
sleep(3);
    close(id);
}

wait(NULL);

我希望只有一个进程会在文件中写入一些内容,但即使文件被锁定,也会有两个进程写入文件。

u2nhd7ah

u2nhd7ah1#

由于忽略了flock()的错误返回值,两个进程都在写入文件。

parent did it
flock_ch: Resource temporarily unavailable
child did it

flock_ch: Resource temporarily unavailable是因为flock()返回错误并告诉您文件被锁定。您打印了错误消息,但没有对错误消息做出响应。您可能需要(a)退出或(b)循环,直到flock()成功。
使用循环可能类似于:

#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/wait.h>
#include <unistd.h>

#define LOCK_FLAGS (LOCK_EX | LOCK_NB)

void parent() {
  int fd = open("shared.txt", 0600 | O_WRONLY);

  while (flock(fd, LOCK_FLAGS) == -1) {
    printf("parent waiting for lock\n");
    sleep(1);
  }
  printf("parent acquired lock\n");

  if (write(fd, "parent", 6) >= 0) {
    printf("parent wrote to file\n");
  } else {
    perror("parent: write failed:");
    exit(1);
  }

  sleep(3);

  close(fd);
}

void child() {
  int fd = open("shared.txt", 0600 | O_WRONLY);

  while (flock(fd, LOCK_FLAGS) == -1) {
    printf("child waiting for lock\n");
    sleep(1);
  }
  printf("child acquired lock\n");

  if (write(fd, "child", 6) >= 0) {
    printf("child wrote to file\n");
  } else {
    perror("child: write failed:");
    exit(1);
  }

  sleep(3);

  close(fd);
}

int main() {
  // ensure "shared.txt" exists
  int fd = open("shared.txt", O_CREAT | O_WRONLY, 0600);
  close(fd);

  pid_t pid = fork();

  if (pid == 0) {
    child();
    exit(0);
  }
  parent();
  wait(NULL);

  return 0;
}

运行上述命令将生成:

parent acquired lock
parent wrote to file
child waiting for lock
child waiting for lock
child waiting for lock
child acquired lock
child wrote to file

或者,删除LOCK_NB标志,这样flock()就会阻塞,直到锁可用为止。

#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/wait.h>
#include <unistd.h>

#define LOCK_FLAGS (LOCK_EX) // | LOCK_NB)

void parent() {
  int fd = open("shared.txt", 0600 | O_WRONLY);

  if (flock(fd, LOCK_FLAGS) == -1) {
      perror("parent: flock:");
      exit(1);
  }

  printf("parent acquired lock\n");

  if (write(fd, "parent", 6) >= 0) {
    printf("parent wrote to file\n");
  } else {
    perror("parent: write failed:");
    exit(1);
  }

  sleep(3);

  close(fd);
}

void child() {
  int fd = open("shared.txt", 0600 | O_WRONLY);

  if (flock(fd, LOCK_FLAGS) == -1) {
      perror("child: flock:");
      exit(1);
  }

  printf("child acquired lock\n");

  if (write(fd, "child", 6) >= 0) {
    printf("child wrote to file\n");
  } else {
    perror("child: write failed:");
    exit(1);
  }

  sleep(3);

  close(fd);
}

int main() {
  // ensure "shared.txt" exists
  int fd = open("shared.txt", O_CREAT | O_WRONLY, 0600);
  close(fd);

  pid_t pid = fork();

  if (pid == 0) {
    child();
    exit(0);
  }
  parent();
  wait(NULL);

  return 0;
}

运行上述命令将生成:

parent acquired lock
parent wrote to file
child acquired lock
child wrote to file

相关问题