检查资源是否由不同进程共享
Check that a ressource is shared by different proccess
我写了下面的代码来检查是否有两个进程,让我们称它们为 pid1 和 pid2 与它们各自的文件描述符共享同一个进程。
1) 我在第一个进程中打开了一个文件。
2) 存储文件描述符。
3)叉子
4)在子进程中打开同一个文件
5) 使用kcmp检查
fd1 = open("test", O_RDWR | O_TRUNC | O_CREAT, 0600);
pid1 = getpid();
pid2 = fork();
if (!pid2) {
pid2 = getpid();
fd2 = open("test", O_RDWR | O_TRUNC);
i = kcmp(pid1, pid2, 0, fd1, fd2);
printf("%d\n", i);
}
else
{
int status;
int s;
while ((s = wait(&status)) > 0);
}
为了检查这一点,我使用带有标志 KCMP_FILE(相当于 0)的系统调用 kcmp。但是系统调用总是 returns 1 或 2 而不是 0.
预期结果为 0,因为两个进程共享相同的资源及其文件描述符。
我是不是误解了手册页,或者我做错了什么来检查这个?
Did I misunderstand the man page or I am doing something wrong to check this?
你误解了man page,它是这样说的:
KCMP_FILE
Check whether a file descriptor idx1 in the process pid1
refers to the same open file description (see open(2)) as file
descriptor idx2 in the process pid2.
具体的措辞是经过深思熟虑的,非常重要:对于KCMP_FILE
,kcmp()
决定了FD是否引用同一个打开文件描述,这与引用同一个底层文件是截然不同的事情。在引用 open(2)
之后,我们发现:
A call to open()
creates a new open file description, an entry in the
system-wide table of open files.
(强调已添加。)您有两次调用 open()
。每个都创建自己的新打开文件描述。它们是不一样的,即使它们指的是同一个文件,kcmp()
告诉你的。我知道在同一进程中获得两个引用相同打开文件描述的不同 FD 的唯一方法是通过 dup()
函数族。
我写了下面的代码来检查是否有两个进程,让我们称它们为 pid1 和 pid2 与它们各自的文件描述符共享同一个进程。
1) 我在第一个进程中打开了一个文件。 2) 存储文件描述符。 3)叉子 4)在子进程中打开同一个文件 5) 使用kcmp检查
fd1 = open("test", O_RDWR | O_TRUNC | O_CREAT, 0600);
pid1 = getpid();
pid2 = fork();
if (!pid2) {
pid2 = getpid();
fd2 = open("test", O_RDWR | O_TRUNC);
i = kcmp(pid1, pid2, 0, fd1, fd2);
printf("%d\n", i);
}
else
{
int status;
int s;
while ((s = wait(&status)) > 0);
}
为了检查这一点,我使用带有标志 KCMP_FILE(相当于 0)的系统调用 kcmp。但是系统调用总是 returns 1 或 2 而不是 0.
预期结果为 0,因为两个进程共享相同的资源及其文件描述符。
我是不是误解了手册页,或者我做错了什么来检查这个?
Did I misunderstand the man page or I am doing something wrong to check this?
你误解了man page,它是这样说的:
KCMP_FILE Check whether a file descriptor idx1 in the process pid1 refers to the same open file description (see open(2)) as file descriptor idx2 in the process pid2.
具体的措辞是经过深思熟虑的,非常重要:对于KCMP_FILE
,kcmp()
决定了FD是否引用同一个打开文件描述,这与引用同一个底层文件是截然不同的事情。在引用 open(2)
之后,我们发现:
A call to
open()
creates a new open file description, an entry in the system-wide table of open files.
(强调已添加。)您有两次调用 open()
。每个都创建自己的新打开文件描述。它们是不一样的,即使它们指的是同一个文件,kcmp()
告诉你的。我知道在同一进程中获得两个引用相同打开文件描述的不同 FD 的唯一方法是通过 dup()
函数族。