pthread 缺乏同步

pthread lack of synchronization

我有以下代码。

此代码适用于为接收到的每个请求创建分支或线程的 TFTP 服务器。我的问题出在线程方法中。

例如我向服务器请求30个文件,应该创建30个线程并将请求的文件交给客户端,每个线程将发送每个文件。 如果我使用 pthread_join(已注释),代码将完美运行,但如果我没有 pthread_join,它可以正确提供一些文件,但其中一些文件已损坏或为空。

我认为这是一个同步问题,所以我尝试 malloc 为每个客户端文件描述符分配一块内存,这样线程只能修改它自己的文件描述符,但没有成功。以下代码按原样工作并提供一些

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pthread.h>

#define BUFSIZE 8096
#define OperationMode 1

#if OperationMode
    typedef struct {
        int * fd;
        int hit;
    } THREAD_ARGS;

    void *attendFTP(void *);
#endif

int ftp(int fd, int hit);

void getFunction(int fd, char * fileName);

void putFunction(int fd, char * fileName);

char * listFilesDir(char * dirName);

void lsFunction(int fd, char * dirName);

void mgetFunction(int fd, char *dirName);

/* just checks command line arguments, setup a listening socket and block on accept waiting for clients */

int main(int argc, char **argv) {
    int i, port, pid, listenfd, socketfd, hit;
    socklen_t length;
    static struct sockaddr_in cli_addr; /* static = initialised to zeros */
    static struct sockaddr_in serv_addr; /* static = initialised to zeros */

    if (argc < 3 || argc > 3 || !strcmp(argv[1], "-?")) {
        printf("\n\nhint: ./tftps Port-Number Top-Directory\n\n""\ttftps is a small and very safe mini ftp server\n""\tExample: ./tftps 8181 ./fileDir \n\n");
        exit(0);
    }

    if (chdir(argv[2]) == -1) {
        printf("ERROR: Can't Change to directory %s\n", argv[2]);
        exit(4);
    }

    printf("LOG tftps starting %s - pid %d\n", argv[1], getpid());

    /* setup the network socket */
    if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
        printf("ERROR system call - setup the socket\n");
    port = atoi(argv[1]);
    if (port < 0 || port > 60000)
        printf("ERROR Invalid port number (try 1->60000)\n");


    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_addr.sin_port = htons(port);

    if (bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
        printf("ERROR system call - bind error\n");
    if (listen(listenfd, 64) < 0)
        printf("ERROR system call - listen error\n");


    // Main LOOP
    for (hit = 1 ;; hit++) {
        length = sizeof(cli_addr);

        /* block waiting for clients */
        socketfd = accept(listenfd, (struct sockaddr *) &cli_addr, &length);
        if (socketfd < 0)
            printf("ERROR system call - accept error\n");
        else
        {
            #if OperationMode            
                pthread_t thread_id;

                THREAD_ARGS *args = malloc(sizeof(THREAD_ARGS));

                int * sockAUX = (int *) malloc(sizeof(int *));

                *sockAUX = socketfd;

                args->fd = sockAUX;
                args->hit = hit;

                if (args != NULL) {
                    if (pthread_create(&thread_id, NULL, &attendFTP, args)) {
                        perror("could not create thread");
                        return 1;
                    }
                }

                //pthread_join(thread_id,NULL);
            #else
                pid = fork();
                if(pid==0) {
                    ftp(socketfd, hit);
                } else {
                    //Temos de fechar o socketfd para que seja apenas a child a tratar dos pedidos, caso contrário iria ficar aqui pendurado
                    close(socketfd);
                    kill(pid, SIGCHLD);
                }
            #endif
        }
    }
}

#if OperationMode
    void *attendFTP(void *argp) {
        THREAD_ARGS *args = argp;

        int sock = *args->fd;

        printf("FD SOCK: %d\n\n", sock);

        ftp(sock, args->hit);

        free(args);
        //printf("Thread executou\n\n");
        pthread_exit(NULL);
        return NULL;
    }
#endif

/* this is the ftp server function */
int ftp(int fd, int hit) {
    int j, file_fd, filedesc;
    long i, ret, len;
    char * fstr;
    static char buffer[BUFSIZE + 1]; /* static so zero filled */

    printf("FD: %d\n\n", fd);

    ret = read(fd, buffer, BUFSIZE); // read FTP request

    if (ret == 0 || ret == -1) { /* read failure stop now */
        close(fd);
        return 1;
    }
    if (ret > 0 && ret < BUFSIZE) /* return code is valid chars */
        buffer[ret] = 0; /* terminate the buffer */
    else
        buffer[0] = 0;

    for (i = 0; i < ret; i++) /* remove CF and LF characters */
        if (buffer[i] == '\r' || buffer[i] == '\n')
            buffer[i] = '*';

    printf("LOG request %s - hit %d\n", buffer, hit);

    /* null terminate after the second space to ignore extra stuff */
    for (i = 4; i < BUFSIZE; i++) {
        if (buffer[i] == ' ') { /* string is "GET URL " +lots of other stuff */
            buffer[i] = 0;
            break;
        }
    }

    if (!strncmp(buffer, "get ", 4)) {
        // GET
        getFunction(fd, &buffer[5]);
    } else if (!strncmp(buffer, "ls ", 3)) {
        // LS
        lsFunction(fd,&buffer[3]);
    } else if (!strncmp(buffer, "mget ", 4)) {
        // MGET
        mgetFunction(fd, &buffer[5]);
    }

    sleep(1); /* allow socket to drain before signalling the socket is closed */
    close(fd);
    return 0;
}

void getFunction(int fd, char * fileName){
    int file_fd;
    long ret;

    printf("FD GET: %d\n\n", fd);

    static char buffer[BUFSIZE + 1]; /* static so zero filled */

    if ((file_fd = open(fileName, O_RDONLY)) == -1) { /* open the file for reading */
        printf("ERROR failed to open file %s\n", fileName);
        printf("Err: %d\n\n",errno);
        sprintf(buffer, "%s", "erro");
        write(fd,buffer,BUFSIZE);
        close(fd);
        return;
    }

    printf("GET -> LOG SEND %s \n", fileName);

    /* send file in 8KB block - last block may be smaller */
    while ((ret = read(file_fd, buffer, BUFSIZE)) > 0) {
        write(fd, buffer, ret);
    }
}

void lsFunction(int fd, char * dirName){
    printf("LS -> LOG Header %s \n", dirName);

    static char buffer[BUFSIZE + 1];

    sprintf(buffer, "%s", listFilesDir(dirName));
    write(fd,buffer,BUFSIZE);
}

void mgetFunction(int fd, char *dirName)
{
    FILE *fp;
    char path[255];

    static char buffer[BUFSIZE + 1];

    printf("MGET COUNT -> LOG Header %s \n", dirName);

    sprintf(buffer, "%s", listFilesDir(dirName));
    write(fd,buffer,BUFSIZE);
}

char * listFilesDir(char * dirName)
{
    DIR *midir;
    struct dirent* info_archivo;
    struct stat fileStat;
    char fullpath[256];
    char *files = malloc (sizeof (char) * BUFSIZE);

    if ((midir=opendir(dirName)) == NULL)
    {
        return "\nO directorio pedido não existe.\n";
    }

    while ((info_archivo = readdir(midir)) != 0)
    {
        strcpy (fullpath, dirName);
        strcat (fullpath, "/");
        strcat (fullpath, info_archivo->d_name);
        if (!stat(fullpath, &fileStat))
        {
            if(!S_ISDIR(fileStat.st_mode))
            {
                strcat (files, info_archivo->d_name);
                strcat (files, "$$");
            }
        } else {
            return "\nErro ao ler o directório.\n";
        }
    }
    closedir(midir);

    return files;
}

这是来自服务器的日志示例

LOG tftps starting 8181 - pid 15416 FD SOCK: 4

FD: 4

LOG request ls . - hit 1 LS -> LOG Header . FD SOCK: 5

FD: 5

LOG request mget . - hit 2 MGET COUNT -> LOG Header . FD SOCK: 4

FD: 4

FD SOCK: 5

FD: 5

LOG request get /.gitconfig - hit 4 FD GET: 5

GET -> LOG SEND .gitconfig LOG request get /

ERROR failed to open file

FD SOCK: 4

FD: 4

LOG request get /ghostdriver.log - hit 6 FD GET: 4

GET -> LOG SEND ghostdriver.log FD SOCK: 8

FD: 8

LOG request get /.bash_history - hit 7 FD GET: 8

GET -> LOG SEND .bash_history FD SOCK: 6

FD: 6

LOG request get /.profile - hit 5 FD GET: 6

GET -> LOG SEND .profile FD SOCK: 10

FD: 10

LOG request get /.ICEauthority - hit 8 FD GET: 10

GET -> LOG SEND .ICEauthority FD SOCK: 13

FD: 13

FD SOCK: 14

FD SOCK: 22

FD: 22

FD SOCK: 16

FD SOCK: 27

FD: 27

FD SOCK: 18

FD: 18

FD SOCK: 19

LOG request get /.gtk-bookmarks - hit 14 FD SOCK: 25

FD: 25

FD SOCK: 15

LOG request get /.bashrc - hit 20 LOG request get /.bashrc - hit 9 FD GET: 18

FD GET: 25

FD GET: 13

GET -> LOG SEND .bashrc GET -> LOG SEND .bashrc FD SOCK: 23

FD: 23

LOG request get /.zshrc - hit 18 FD SOCK: 26

FD: 26

FD GET: 23

FD SOCK: 29

FD: 29

GET -> LOG SEND .bash_logoutks GET -> LOG SEND .bash_logout FD SOCK: 17

FD: 17

LOG request get /.zsh-update - hit 13 LOG request get /.zsh-update - hit 22 FD SOCK: 28

FD GET: 27

FD: 14

LOG request get /.nano_history - hit 10 LOG request get /.nano_history - hit 24 LOG request get /.nano_history - hit 21 FD: 16

LOG request get /.zsh_history - hit 12 FD SOCK: 21

FD: 21

FD GET: 16

FD: 19

LOG request get /.selected_editor - hit 15 FD SOCK: 24

FD: 24

FD: 15

FD GET: 14

FD GET: 17

FD GET: 29

GET -> LOG SEND .zcompdump-MacPearl-5.0.2 LOG request get /.zcompdump-MacPearl-5.0.2 - hit 17 FD: 28

LOG request get /.Xauthority - hit 23 GET -> LOG SEND .Xauthority GET -> LOG SEND .Xauthority FD GET: 28

LOG request get /.Xauthority - hit 11 GET -> LOG SEND .Xauthority FD GET: 15

GET -> LOG SEND .Xauthority GET -> LOG SEND .Xauthority FD GET: 19

FD GET: 26

GET -> LOG SEND .Xauthority LOG request get /.Xauthority - hit 16 FD GET: 21

GET -> LOG SEND .Xauthority FD GET: 22

LOG request get /.Xauthority - hit 19 GET -> LOG SEND .Xauthority GET -> LOG SEND .Xauthority GET -> LOG SEND .Xauthority FD GET: 24

GET -> LOG SEND .Xauthority FD SOCK: 30

FD: 30

FD SOCK: 31

FD: 31

LOG request get /.zcompdumpy - hit 25 LOG request get /.zcompdump - hit 26 FD GET: 30

FD GET: 31

GET -> LOG SEND .zcompdump GET -> LOG SEND .zcompdump

我发现有时他在多个线程中使用相同的文件描述符。我只是不知道如何解决这个同步问题。

删除声明中的 'static' 存储限定符,例如:

静态字符缓冲区[BUFSIZE + 1];

您不得对要从多个线程访问的数据随意使用静态限定符 - 此类数据在全局范围内只有一个实例,除非应用额外的显式同步,否则线程将对数据进行切分和切分粉碎。

如果使用自动存储,所有常见的 C 编译器都会将数据放在当前堆栈上。有多个线程,即。多个堆栈,这意味着每个线程一个数据项,因此没有切片和切块。

请注意,代码中的静态缓冲区有注释:'/* static so zero filled */'。删除静态变量可能会对 code/data 的其余部分产生影响 - 自动变量不会初始化为零,任何依赖于此的代码都会受到影响。

另请注意,代码还有其他问题,与多线程无关。 'ftp' 函数无法正确处理 TCP 的八位字节流性质,并错误地假设大于一个字节的消息将在默认字节流模式下通过一次调用 read() 来完整接收。

这段代码很糟糕,即使是非库代码,作者应该被解雇:)