C语言 为什么这个程序在 ubuntu Windows 桌面应用程序和 ubuntu 虚拟盒子上给出不同的输出?



该程序基本上是在检查fcntl函数的工作方式。主程序(Pm.c(通过分叉创建一个管道和3个子进程。然后,它在管道上执行必要的 fcntl 功能,然后写入它。 子进程在收到来自 ubuntu 虚拟框中管道的信号后终止,但它们在 ubuntu Windows 应用程序中没有收到任何信号。

下午 -

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
int main()
{
int c = getpid(), c1, c2, c3;
int pp[2];
pipe(pp);
char *args[1];
args[0] = NULL;
if((c1 = fork()) == 0)
execvp("./p1", args);
if((c2 = fork()) == 0)
execvp("./p2", args);
if((c3 = fork()) == 0)
execvp("./p3", args);
setpgid(0, 0);
setpgid(c1, c);
setpgid(c2, c);
setpgid(c3, c);
printf("%d %d %d %dn", c, c1, c2, c3);
printf("%d %d %d %dn", getpgid(c), getpgid(c1), getpgid(c2), getpgid(c3));
int gid = getpgid(c);
sleep(1);
fcntl(pp[0], F_SETFL, O_ASYNC);
fcntl(pp[0], __F_SETSIG, SIGUSR1);
fcntl(pp[0], F_SETOWN, -gid);
write(pp[1], "bruh ", 4);
close(pp[1]);
wait(NULL);
return 0;
}

P1.c(P2.c和P3.c与此类似(

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int pr = 1;
void hdfn(int x)
{
printf("Recieved signal from pmn");
pr--;
}
int main()
{
signal(SIGUSR1, hdfn);
printf("P1 is runningn");
while(pr);
return 0;
}

这是 Ubuntu 虚拟盒子中的输出。程序终止,并且是正确的输出 -

1846 1847 1848 1849
1846 1846 1846 1846
P1 is running
P2 is running
P3 is running
Recieved signal from pm
Recieved signal from pm
Recieved signal from pm
User defined signal 1

在 ubuntu Windows 应用程序上的输出(程序不会终止( -

26 27 28 29
26 26 26 26
P1 is running
P2 is running
P3 is running

有人可以告诉为什么输出存在差异以及如何在 ubuntu 应用程序上获得正确的输出。

这是因为Linux的Windows子系统上不存在fcntl。我希望你的"ubuntu Windows应用程序"是这个应用程序。您的代码正在编译,因为标头存在,但不支持fcntl标头。请参阅此 github 问题。

最新更新