'thread function doesn't terminate until Enter is pressed

The following code (in the end) represents thread function which takes in ls command from remote client and send current working directory to that client.

It successfully sends but there is one issue: When it stops sending completely, I want it to start listening again. At line:

printf("Enter 1 if you want to exit or 0 if you don't: "); 
                 fgets(exit_status,MAX_SIZE,stdin);

It gets stuck and it is terminated (and starts another thread) when I press Enter

Which I don't understand why? and while I was debugging I saw above print statement executes after pressing Enter despite the debugger being at the end of function (means it passed this print statement).

I want it to start listening again automatically when it finish sending data.

If anyone wants to look at my full code here is the link:https://pastebin.com/9UmTkPge

void  *server_socket_ls(void *arg) {
    int* exit_status = (int*)malloc(sizeof(int));
    *exit_status = 0;   
    while (*exit_status == 0) {
    //socket code is here
        
    //code for ls
        
    char buffer[BUFFSIZE];
    int received = -1;
    char data[MAX];
    memset(data,0,MAX);
       // this will make server wait for another command to run until it receives exit
        data[0] = '\0';
        if((received = recv(new_socket, buffer,BUFFSIZE,0))<0){
    
            perror("Failed");
        }
    
        buffer[received] = '\0';
    
        strcat (data,  buffer);
        if (strcmp(data, "exit")==0) // this will force the code to exit
            exit(0);
    
        puts (data);
            char *args[100];
            setup(data,args,0);
            int pipefd[2],lenght;
    
            if(pipe(pipefd))
                perror("Failed to create pipe");
    
            pid_t pid = fork();
            char path[MAX];
    
            if(pid==0)
            {
                close(1); // close the original stdout
                dup2(pipefd[1],1); // duplicate pipfd[1] to stdout
                close(pipefd[0]); // close the readonly side of the pipe
                close(pipefd[1]); // close the original write side of the pipe
                execvp(args[0],args); // finally execute the command
            }
            else
                if(pid>0)
                {
                    close(pipefd[1]);
                    memset(path,0,MAX);
    
                    while(lenght=read(pipefd[0],path,MAX-1)){
                        printf("Data read so far %s\n", path);
                        if(send(new_socket,path,strlen(path),0) != strlen(path) ){
                            perror("Failed");
                        }
                        //fflush(NULL);
                        printf("Data sent so far %s\n", path);
                        memset(path,0,MAX);
                        
                    }
    
                    close(pipefd[0]);
                    //removed so server will not terminate
                              
                }
                else 
                {
                    printf("Error !\n");
                    exit(0);
                }
                 printf("Enter 1 if you want to exit or 0 if you don't: "); 
                 fgets(exit_status,MAX_SIZE,stdin);    
                
        }
    
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source