简体   繁体   English

与gcc链接时,Solaris和GNU / Linux之间的lpthread库差异

[英]lpthread libary difference between Solaris and GNU/Linux when linking with gcc

In Linux(Debian 7), when I run the server, the pointer to array *arg for *doSomething it crashes with segmentation fault. 在Linux(Debian 7)中,当我运行服务器时,指向* do的数组* arg的指针由于分段错误而崩溃。 (Line int *arg = malloc(sizeof(*arg)); ) When I run the same code in Solaris, it runs like a charm. (Line int *arg = malloc(sizeof(*arg)); )当我在Solaris中运行相同的代码时,它的运行就像一个魅力。 Everything is compiled at its own respective OS (running at Solaris, compiled at Solaris, etc) with no errors or warnings. 一切都在自己的操作系统(在Solaris上运行,在Solaris上编译,等等)下编译,没有错误或警告。 A code snippet of the main body of the server without the doSomething method 没有doSomething方法的服务器主体的代码段

int main(int argc, char *argv[])
{
     int sockfd, portno,pErr;
     socklen_t clilen;
     //char buffer[256];
     //char servPlay[10];

     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
      while(1) {
        int newsockfd;
        printf("Listening\n");
        listen(sockfd,5);
        clilen = sizeof(cli_addr);
        newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
        if (newsockfd < 0) 
            error("ERROR on accept");
        printf("Client %s connected\n", inet_ntoa(cli_addr.sin_addr));
        int *arg = malloc(sizeof(*arg));
        if(arg ==NULL) {
            fprintf(stderr, "No memory for thread\n");
        }
        *arg=newsockfd;
        pErr = pthread_create(NULL,NULL,doSomething,arg);
        if(pErr!=0)
        {
            error("Cannot cread thread\n");
            //break;
            }
        }
     close(sockfd);
     return 0; 
} 

More probably than not mnunberg hit the mark and the crash happens in pthread_create() rather than the line you think. mnunberg很可能没有达到目标,崩溃发生在pthread_create()而不是您认为的那一行。

man pthread_create : man pthread_create

Before returning, a successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by thread ; 回国之前,在成功调用在pthread_create()存储在缓冲区中的新线程的ID指向线程 ; this identifier is used to refer to the thread in subsequent calls to other pthreads functions. 此标识符用于在后续对其他pthreads函数的调用中引用线程。

It would be pointless allowing to pass a NULL pointer as the first argument ( thread ). 允许将NULL指针作为第一个参数( thread )传递是毫无意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM