gpt4 book ai didi

我们可以使用不带 usleep() 的咨询记录锁定在父子之间交替吗?

转载 作者:太空狗 更新时间:2023-10-29 12:31:43 25 4
gpt4 key购买 nike

我正在学习《Unix 环境中的高级编程》,第 15 章的练习 17 有问题。

练习要求读者“使用建议记录锁定在父项和子项之间交替”。

而且我发现如果不使用 usleep() 就无法完成。 没有什么可以阻止内核在解锁记录后继续调度父进程,反之亦然。

有人知道答案吗?提前致谢。

这是我的代码:

ex17.c

#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/wait.h>
#include "ch14/lock.h"

#define NLOOPS 1000
#define SIZE sizeof(long) /* size of shared memory area */

static int update(long *ptr)
{
return (*ptr)++; /* return value before increment */
}

int main(int argc, char* argv[])
{
int fd, counter, lockFd;
pid_t pid;
void *area;


if((fd = open("/dev/zero", O_RDWR)) < 0)
{
perror("open error");
exit(1);
}

if((area = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
{
perror("mmap error");
exit(1);
}

close(fd); /*can close /dev/zero now that it's mapped */

if((lockFd = open("ex17.lock", O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0)
{
perror("open error");
exit(-1);
}

/* insure parent runs first */
writew_lock(lockFd, 0, SEEK_SET, 1); /* first byte in the lock file represent parent */
writew_lock(lockFd, 1, SEEK_SET, 1); /* second byte in the lock file represent child */

if((pid = fork()) < 0)
{
perror("fork error");
exit(1);
}else if(pid > 0){ /* parent */
for(int i = 0; i<NLOOPS; i+=2)
{
writew_lock(lockFd, 0, SEEK_SET, 1);
readw_lock(lockFd, 1, SEEK_SET, 1);

if((counter = update((long*)area)) != i)
{
fprintf(stderr, "parent: expected %d, got %d\n", i, counter);
exit(1);
}else{
printf("%s got %d\n", "parent", counter);
}
un_lock(lockFd, 1, SEEK_SET, 1);
usleep(1000); /*if without this line, there is nothing to stop kernel continually scheduling parent.*/
readw_lock(lockFd, 0, SEEK_SET, 1);
}
waitpid(pid, NULL, 0);
} else {
for(int i=1; i<NLOOPS+1; i+=2)
{
writew_lock(lockFd, 1, SEEK_SET, 1);
readw_lock(lockFd, 0, SEEK_SET, 1);

if((counter = update((long*)area)) != i)
{
fprintf(stderr, "child: expected %d, got %d\n", i, counter);
exit(1);
}else{
printf("%s got %d\n", "child", counter);
}
un_lock(lockFd, 0, SEEK_SET, 1);
usleep(1000);
readw_lock(lockFd, 1, SEEK_SET, 1);
}
}
exit(0);
}

ch14/lock.h

#ifndef CH14_LOCK_H_INCLUDED
#define CH14_LOCK_H_INCLUDED

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int lock_reg(int, int, int, off_t, int, off_t);
pid_t lock_test(int, int, off_t, int, off_t);

#define read_lock(fd, offset, whence, len) lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define readw_lock(fd, offset, whence, len) lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
#define writew_lock(fd, offset, whence, len) lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len))
#define un_lock(fd, offset, whence, len) lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))

#define is_read_lockable(fd, offset, whence, len) (lock_test((fd), F_RDLCK, (offset), (whence), (len)) == 0)
#define is_write_lockable(fd, offset, whence, len) (lock_test((fd), F_WRLCK, (offset), (whence), (len)) == 0)


#endif // CH14_LOCK_H_INCLUDED

ch14/lock.c

#include "lock.h"

int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
struct flock lock;

lock.l_type = type; /* F_RDLCK, F_WRLCK, F_UNLCK */
lock.l_whence = whence; /* byte offset, relative to l_whence */
lock.l_start = offset; /* byte offset, relative to l_whence */
lock.l_len = len; /* #bytes (0 means to EOF) */

return fcntl(fd, cmd, &lock);
}

pid_t lock_test(int fd, int type, off_t offset, int whence, off_t len)
{
struct flock lock;

lock.l_type = type; /* F_RDLCK OR F_WRLCK */
lock.l_start = offset; /* byte offset, relative to l_whence */
lock.l_whence = whence; /* SEEK_SET, SEEK_CUR, SEEK_END */
lock.l_len = len; /* #bytes (0 means to EOF) */

if(fcntl(fd, F_GETLK, &lock) < 0)
{
perror("fcntl error");
exit(-1);
}

if(lock.l_type == F_UNLCK)
{
return 0; /* false, region isn't locked by another proc */
}else{
return lock.l_pid; /* true, return pid of lock owner */
}
}

最佳答案

建议锁实现因类 unix 操作系统而异,但它们通常 are not mandatory by default ,Linux 就是这种情况。 man fcntl 包含咨询锁和强制锁的部分;从后者开始(这里的“以上”是指前者):

The above record locks may be either advisory or mandatory, and are advisory by default. Advisory locks are not enforced and are useful only between cooperating processes.

这意味着为了使锁有效,每个协作进程都必须检查它是否可以在获取锁之前获取一个锁。 man fcntlF_GETLK 调用说了一些关于此的事情:

F_GETLK (struct flock *)

On input to this call, lock describes a lock we would like to place on the file. If the lock could be placed, fcntl() does not actually place it, but returns F_UNLCK in the l_type field of lock and leaves the other fields of the structure unchanged. If one or more incompatible locks would prevent this lock being placed, then fcntl() returns details about one of these locks in the l_type, l_whence, l_start, and l_len fields of lock and sets l_pid to be the PID of the process holding that lock.

因此您使用 F_GETLK 提交的结构可能更改了一些字段以指示结果——这是您需要检查的内容。如果成功,您可以调用 F_SETLK 来实际应用锁。1如果不成功,您需要等到它成功,并且这些调用都不是阻塞,意思是无论情况如何,他们都会立即返回。这就是需要短暂 sleep 的地方,因为如果您只是一遍又一遍地循环检查锁,您将忙于循环一个处理器(使其无所事事地最大化)。然而,如果你加入 5 或 10 毫秒的被动延迟,循环将大部分被动地不做任何事情(即,没有最大化处理器)。这种延迟应该只发生在检查失败之后;如果检查成功,立即设置锁。

所有这些都可以放在一个函数中。它并不关心让进程稳定交替,但这实际上是一种人为的标准:如果你想在“现实生活”中实现那个目标,你就不会这样做。但是,这并非不可能。我要尝试的第一件事是在函数开始时使用比循环中长几倍的 sleep ,这样释放锁然后再次获取它的进程将延迟比一个同时试图在检查/设置循环中获取它的进程。


1。不幸的是,这意味着一个潜在的竞争条件,一个进程可能会在另一个进程的检查和设置调用之间设置一个锁——这个系统的一个弱点。

关于我们可以使用不带 usleep() 的咨询记录锁定在父子之间交替吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24850183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com