Search code examples
cubuntusignalsfcntl

Why does this program give different outputs on ubuntu windows desktop app and on ubuntu virtual box?


The program is basically checking for how fcntl function works. The main program(Pm.c) creates a pipe and 3 child processes by forking. It then does necessary fcntl function on the pipe and then writes to it. The child processes terminate after receiving the signal from pipe in ubuntu virtual box but they don't receive any signal in the ubuntu windows app.

Pm.c -

#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 %d\n", c, c1, c2, c3);
    printf("%d %d %d %d\n", 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 and P3.c is similar to this)

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

int pr = 1;

void hdfn(int x)
{
    printf("Recieved signal from pm\n");
    pr--;
}

int main()
{
    signal(SIGUSR1, hdfn);
    printf("P1 is running\n");
    while(pr);
    return 0;
}

Here's the output in Ubuntu virtual Box. The program terminates and is the correct output -

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

output on ubuntu windows app. (Program doesn't terminate) -

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

Can someone please tell why there's a difference in output and how I can get the correct output on ubuntu app.


Solution

  • This is because fcntl doesn't exist on windows subsystem for linux. I'd expect your "ubuntu windows app" is this app. Your code is compiling because headers exist but fcntl is not supported. See this github issue.