pthread_create失败并返回-1(或4294967295)

时间:2010-07-29 13:51:57

标签: c multithreading openssl

我正在尝试使用OpenSSL (由Chandra )重现 Network Security的示例。该计划由client.c server.c common.hcommon.c组成。 client.c只是在端口6012上创建与服务器的连接,并从stdin读取数据,然后将这些数据发送到服务器。 server.c从套接字读取数据并将其写回stdout

问题是,server.c总是卡在第{62}行的if(BIO_do_accept(acc) <= 0)中,永远不会从client.c发送数据,因为它运行得非常好。我后来发现问题是第66行pthread_create(...)(在THREAD_CREATE(...)中定义为common.h)失败并返回4294967295。由于THREAD_CREATE(...)失败,程序永远无法在do_server_loop中运行server_thread(...),这解释了服务器从未从客户端获取数据的原因。

我应如何解释pthread_create的返回值以及如何解决? PS。我后来使用strerror转换4294967295,并返回“未知错误”。

非常感谢任何帮助!

/////////////////////////////////////////////// ///

以下是server.c

#include "common.h" 
void do_server_loop(BIO *conn)
{
    int err, nread;
    char buf[80];

    do
    {
        fprintf(stderr, "server_loop executed.\n");
        for(nread = 0; nread < sizeof(buf); nread += err)
        {
            err = BIO_read(conn, buf + nread, sizeof(buf) - nread);
            if(err <= 0){
                break;
            }
        }
        fwrite(buf, 1, nread, stdout);
    }
    while (err > 0);
}

void THREAD_CC server_thread(void *arg)
{
    fprintf(stderr, "server_thread(void *arg) executed.\n");
    BIO *client = (BIO *)arg;
#ifndef WIN32
    pthread_detach(pthread_self());
#endif
    fprintf(stderr, "Connection opened.\n");
    do_server_loop(client);
    fprintf(stderr, "Connection closed.\n");

    BIO_free(client);
    ERR_remove_state(0);
#ifdef WIN32
    _endthread();
#else
    return 0;
#endif
}


int main(int argc, char *argv[])
{
    BIO     *acc, *client;
    int thread_create_result;
    THREAD_TYPE tid;

    init_OpenSSL();

    acc = BIO_new_accept(PORT);
    if(!acc){
        int_error("Error creating server socket");
    }

    if(BIO_do_accept(acc) <= 0){
        int_error("Error binding server socket");   
    }
    for(;;)
    {
        if(BIO_do_accept(acc) <= 0){
            int_error("Error accepting connection");
        }
        client = BIO_pop(acc);
        thread_create_result = THREAD_CREATE(tid, server_thread, client);
        if(thread_create_result != 0){
            fprintf(stderr, "THREAD_CREATE failed! returns: %s.\n", \
                                            strerror(thread_create_result));
            fprintf(stderr, "thread_create_result has the value: %u.\n", \
                                            thread_create_result);
            exit(-1);
        }
    }

    BIO_free(acc);
    return 0;
}

以下是common.h

#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/hmac.h>
#include <openssl/objects.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>


#ifndef WIN32
#include <pthread.h>
#define THREAD_CC *
#define THREAD_TYPE         pthread_t
#define THREAD_CREATE(tid, entry, arg)  pthread_create(&(tid), NULL, \
                                   (entry), (arg))
#else
#include <windows.h>
#include <process.h>
#define THREAD_CC              __cdecl
#define THREAD_TYPE            DWORD
#define THREAD_CREATE(tid, entry, arg) do { _beginthread((entry), 0, (arg));\
                        (tid) = GetCurrentThreadId();\
                       } while (0)
#endif

#define PORT    "6012"      //port
#define SERVER  "10.1.251.24"   //server address
#define CLIENT  "10.1.21.46"    //client address

#define int_error(msg) handle_error(__FILE__, __LINE__, msg)
void handle_error(const char *file, int lineno, const char *msg);

void init_OpenSSL(void);

生成文件:

CC = gcc
OPENSSLDIR = /usr/local/ssl
#CFLAGS = -g -Wall -W -I${OPENSSLDIR}/include -O2 -D_REENTRANT -D__EXTENSIONS__  
CFLAGS = -g -Wall -W -I${OPENSSLDIR}/include -O2

RPATH = -R${OPENSSLDIR}/lib
#LD = ${RPATH} -L${OPENSSLDIR}/lib -lssl -lcrypto -lsocket -lnsl -lpthread
LD = -L${OPENSSLDIR}/lib -lssl -lcrypto -lsocket -lnsl -pthread

OBJS = common.o

PROGS = server

all: ${PROGS}

server: server.o ${OBJS}
        ${CC} server.o ${OBJS} -o server ${LD}


clean:;
        ${RM} ${PROGS} *.ln *.BAK *.bak *.o

2 个答案:

答案 0 :(得分:2)

更改

if(thread_create_result = !0){

if(thread_create_result != 0){

此外,您可以使用 strerror 函数将错误代码转换为人类可读的形式。

答案 1 :(得分:0)

在编译命令行上添加-D_REENTRANT,在链接命令行上添加-lpthread-D_REENTRANT将告诉C / C ++库您的程序处于多线程模式,而-lpthread只是在运行时加载共享库libpthread.so。我在William Garrison's POSIX threads tutorialthis link找到了这个。

这是Makefile:

CC = gcc
OPENSSLDIR = /usr/local/ssl
CFLAGS = -g -Wall -W -I${OPENSSLDIR}/include -O2 -D_REENTRANT -D__EXTENSIONS__  

RPATH = -R${OPENSSLDIR}/lib
LD = ${RPATH} -L${OPENSSLDIR}/lib -lssl -lcrypto -lsocket -lnsl -lpthread

OBJS = common.o

PROGS = server

all: ${PROGS}

server: server.o ${OBJS}
        ${CC} server.o ${OBJS} -o server ${LD}


clean:;
        ${RM} ${PROGS} *.ln *.BAK *.bak *.o