Docker with Python3 and C extension using popen() causes segmentation fault

My target is to develop a python program which has some C extension to invoke some C libraries. Finally to make the solution as part of Docker container.

I have written a Python script with C extension which loads some C dll and execute some operation.
The C library has code to invoke shell command through popen() and output of the command is read from the pipe using fgets().
If I execute the python script outside of the Docker container, it executes perfectly.
If I execute the C dll through C executable inside the Docker container, it executes perfectly.
If I execute the python script inside Docker container, on first call of fgets() of the FILE* returned by popen() causes segmentation fault.

I am running my Docker Container with –privileged=true option.
The C code which crashes look like

void readDir(){

        char cmd[] = "ls -l /home/user1/*";
        FILE* fp = popen(cmd,"r");

        if (!fp){
                printf("popen didn't work");
        }
        else{
                char buf[256]={'{$content}'};
                fgets(buf,sizeof(buf),fp);
                printf("buf=%sn",buf);
                pclose(fp);
        }
}

Later I replaced popen() by system() and dumping the output of the command to a file and then opening theat file using fopen() and reading the content through python script. Interestingly, that also works.
So looks like invoking popen() from C extension of Python script inside a Docker container is the only issue.

Appreciate any help or direction on this matter. Let me know if you have any question on the Docker file configuration or anything else.

Source: StackOverflow