并发线程导致程序不执行而退出

发布于 2025-01-26 14:46:12 字数 4115 浏览 2 评论 0原文

我正在研究一个项目,以创建一个可以远程和本地执行LS和CP命令的终端。 为此,我只能创建一个可以根据需要成为客户端或服务器的程序。 总是会有2个服务器线程。 1个用于终端显示的线程(始终执行)。

为了实现此并发执行,我创建了两个线程。 ConnectSocket()中的线程正常工作,但是terminalDisplay()中的线程没有响应。 (程序正确地编译但在我运行之后退出)

PS:我很难调试线程,因为我是新手

    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <fcntl.h> 
    #include <unistd.h> 
    #include<pthread.h>
    
    #define MAX_SIZE 200
    #define BACKLOG 10
 
      
    /* Thread routine to serve connection to client. */
    void *pthread_routine(void *arg);
    
    //terminal routine
    void *terminal_thread();
    
    void *pthread_routine(void *arg) {
      //routine for socket threads
    } 
    
    void *terminal_thread() {
    
    while(1) {
    printf(">> ");
    //memset(input_command,0,strlen(str));
    char *input_command = malloc(MAX_SIZE);
    
    fgets(input_command,MAX_SIZE,stdin);
    
    if((strlen(input_command) > 0) && (input_command[strlen(input_command) - 1] == '\n')) 
      input_command[strlen(input_command) - 1] = '\0';
    
    char list[] = "ls";
    char cp[]= "cp";
    
    /*char s[100];
      
      printf("%s\n", getcwd(s,100));
      
      chdir("Desktop");
    
      printf("%s\n", getcwd(s,100));
    */
     
    
    if(strcmp(input_command,list) == 0) {
      //ls code will run here
       
    
    }
    if (strchr(input_command,'@') != NULL && strchr(input_command,'l') !=NULL) {
         printf("remote ls\n");
         
    }
    
    if(strcmp(input_command,cp) == 0) {
      printf("cp code here\n");
    
    }
    if(strchr(input_command,'@') != NULL && strchr(input_command,'c') !=NULL) {
      printf("remote cp code here\n");
    }
    }
         
    }
 
    void connectSocket() {
   //server code here up till listen function
  
    
        /* Initialise pthread attribute to create detached threads. */
        if (pthread_attr_init(&pthread_attr) != 0) {
            perror("pthread_attr_init");
            exit(1);
        }
        if (pthread_attr_setdetachstate(&pthread_attr, PTHREAD_CREATE_DETACHED) != 0) {
            perror("pthread_attr_setdetachstate");
            exit(1);
        }
    
        while (1) {
            pthread_arg = (pthread_arg_t *)malloc(sizeof *pthread_arg);
            if (!pthread_arg) {
                perror("malloc");
                continue;
            }
    
            /* Accept connection to client. */
            client_address_len = sizeof pthread_arg->client_address;
            new_socket_fd = accept(socket_fd, (struct sockaddr *)&pthread_arg->client_address, &client_address_len);
            if (new_socket_fd == -1) {
                perror("accept");
                free(pthread_arg);
                continue;
            }
    
            /* Initialise pthread argument. */
            pthread_arg->new_socket_fd = new_socket_fd;
    
            /* Create thread to serve connection to client. */
            if (pthread_create(&pthread, &pthread_attr, pthread_routine, (void *)pthread_arg) != 0) {
                perror("pthread_create");
                free(pthread_arg);
                continue;
            }
        }
    
    }
    
    void terminalDisplay() {
    
    pthread_attr_t pthread_attr;
    pthread_t pthread;
    
    /* Initialise pthread attribute to create detached threads. */
        if (pthread_attr_init(&pthread_attr) != 0) {
            perror("pthread_attr_init");
            exit(1);
        }
        if (pthread_attr_setdetachstate(&pthread_attr, PTHREAD_CREATE_DETACHED) != 0) {
            perror("pthread_attr_setdetachstate");
            exit(1);
        }
    
    if (pthread_create(&pthread, &pthread_attr, terminal_thread, NULL) != 0) {
                perror("pthread_create");  
            }
       pthread_join(pthread,NULL);
    }
    
    int main(void) {
    
      terminalDisplay();
      
    }

I am working on a project to create a terminal that can execute ls and cp commands remotely and locally.
For this, i can only create one program which can become client or server as required.
There will be 2 server threads listening always.
1 thread for terminal display (always executing).

To achieve this concurrent execution, I created two threads. Thread in ConnectSocket() is working fine, but thread in terminalDisplay() is not responding. (program compiles correctly but exit just after I run it)

P.S: Its too difficult for me to debug threads as I am a newbie to C.

Code:

    #include <stdio.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <fcntl.h> 
    #include <unistd.h> 
    #include<pthread.h>
    
    #define MAX_SIZE 200
    #define BACKLOG 10
 
      
    /* Thread routine to serve connection to client. */
    void *pthread_routine(void *arg);
    
    //terminal routine
    void *terminal_thread();
    
    void *pthread_routine(void *arg) {
      //routine for socket threads
    } 
    
    void *terminal_thread() {
    
    while(1) {
    printf(">> ");
    //memset(input_command,0,strlen(str));
    char *input_command = malloc(MAX_SIZE);
    
    fgets(input_command,MAX_SIZE,stdin);
    
    if((strlen(input_command) > 0) && (input_command[strlen(input_command) - 1] == '\n')) 
      input_command[strlen(input_command) - 1] = '\0';
    
    char list[] = "ls";
    char cp[]= "cp";
    
    /*char s[100];
      
      printf("%s\n", getcwd(s,100));
      
      chdir("Desktop");
    
      printf("%s\n", getcwd(s,100));
    */
     
    
    if(strcmp(input_command,list) == 0) {
      //ls code will run here
       
    
    }
    if (strchr(input_command,'@') != NULL && strchr(input_command,'l') !=NULL) {
         printf("remote ls\n");
         
    }
    
    if(strcmp(input_command,cp) == 0) {
      printf("cp code here\n");
    
    }
    if(strchr(input_command,'@') != NULL && strchr(input_command,'c') !=NULL) {
      printf("remote cp code here\n");
    }
    }
         
    }
 
    void connectSocket() {
   //server code here up till listen function
  
    
        /* Initialise pthread attribute to create detached threads. */
        if (pthread_attr_init(&pthread_attr) != 0) {
            perror("pthread_attr_init");
            exit(1);
        }
        if (pthread_attr_setdetachstate(&pthread_attr, PTHREAD_CREATE_DETACHED) != 0) {
            perror("pthread_attr_setdetachstate");
            exit(1);
        }
    
        while (1) {
            pthread_arg = (pthread_arg_t *)malloc(sizeof *pthread_arg);
            if (!pthread_arg) {
                perror("malloc");
                continue;
            }
    
            /* Accept connection to client. */
            client_address_len = sizeof pthread_arg->client_address;
            new_socket_fd = accept(socket_fd, (struct sockaddr *)&pthread_arg->client_address, &client_address_len);
            if (new_socket_fd == -1) {
                perror("accept");
                free(pthread_arg);
                continue;
            }
    
            /* Initialise pthread argument. */
            pthread_arg->new_socket_fd = new_socket_fd;
    
            /* Create thread to serve connection to client. */
            if (pthread_create(&pthread, &pthread_attr, pthread_routine, (void *)pthread_arg) != 0) {
                perror("pthread_create");
                free(pthread_arg);
                continue;
            }
        }
    
    }
    
    void terminalDisplay() {
    
    pthread_attr_t pthread_attr;
    pthread_t pthread;
    
    /* Initialise pthread attribute to create detached threads. */
        if (pthread_attr_init(&pthread_attr) != 0) {
            perror("pthread_attr_init");
            exit(1);
        }
        if (pthread_attr_setdetachstate(&pthread_attr, PTHREAD_CREATE_DETACHED) != 0) {
            perror("pthread_attr_setdetachstate");
            exit(1);
        }
    
    if (pthread_create(&pthread, &pthread_attr, terminal_thread, NULL) != 0) {
                perror("pthread_create");  
            }
       pthread_join(pthread,NULL);
    }
    
    int main(void) {
    
      terminalDisplay();
      
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文