簡體   English   中英

從鍵盤線程在列表中覆蓋的項目

[英]Items overriden in list from keyboard thread

[1]我的鍵盤輸入線程有問題。 我目前正在使用fgets從鍵盤讀取輸入,然后使用List_add將其存儲到列表中。 問題是,我的程序似乎只是打印我輸入的最后一個字符而不是不同的消息字符串。

更新:鍵盤輸入線程中的代碼

#define MAX_LEN 258

static pthread_t keyboardThread;
static pthread_mutex_t *psharedMutex;
static List* pList;

void* inputFunction(void* unused){
    printf("Enter message:\n");
    while (1) {         
        char* messageAllocated;
        messageAllocated = malloc(MAX_LEN);

        if(messageAllocated != NULL) {
            //Wait for user input
            fgets(messageAllocated, MAX_LEN, stdin);      // This is how C read something from keyboard

            pthread_mutex_lock(psharedMutex);
            {
                List_add(pList, messageAllocated);                // Add message to list for FCFS
            }
            pthread_mutex_unlock(psharedMutex);

            if (messageAllocated[0] == '!' && messageAllocated[2] == '\0') {
                break;
            }
            
            memset(messageAllocated, 0, MAX_LEN);
        }
        else {
            printf("Malloc Failed.\n");
        }
            
     };

    return NULL;
}

void KeyboardInput_init(List* myList, pthread_mutex_t *sharedMutex) {
    psharedMutex = sharedMutex;
    pList = myList;
    pthread_create(&keyboardThread, NULL, inputFunction, NULL);

}

void KeyboardInput_shutdown() {

    pthread_join(keyboardThread, NULL);
}

main.c 中的代碼

int main(int argc, char** args) {
    // Create 2 Lists for transmit and receive
    //List listPool;
    List* outboundList;                         // Used in keyboard and UDP sendto threads
    List* inboundList;                          // Used in screen and UDP recvfrom threads

    outboundList = List_create();
    inboundList = List_create();


    // Receive information for s-talk transmission (ports, machine names)
    setUpSTalkApplication();

    Printer_init();
    Signaller_init();
    Printer_waitForShutdown();
    Signaller_shutdown();

    // Startup Modules
    Receiver_init("Message received on Port ", &sharedMutex, remoteMachineName, hostPort);            // goes before keyboard
    KeyboardInput_init(outboundList, &sharedMutex);
    Sender_init(outboundList, &sharedMutex, &condVar, remoteMachineName, targetPort);
    //ScreenOutput_init(inboundList, &condVar, &sharedMutex);


    // Shutdown Modules
    //ScreenOutput_shutdown();
    KeyboardInput_shutdown();
    Receiver_shutdown();
    Sender_shutdown();


    printf("Done.\n");
    while(List_count(outboundList) != 0) {
        char* x = List_trim(outboundList);
        printf("%s", x);
    };
    

    return 0;
}

首先,您應該在malloc之后驗證指針messageAllocated並且sizeof(messageAllocated)只有48個字節,您應該使用MAX_LEN而不是sizeof(messageAllocated)

while (1) {
    char * messageAllocated;
    messageAllocated = malloc(MAX_LEN);
    if(messageAllocated != NULL)
    {
        fgets(messageAllocated, MAX_LEN, stdin);
        List_add(pList, messageAllocated)
        if (messageAllocated[0] == '!') {
            break;
        }
      //hope you are doing this before reading another string, or else there will be memory leak
       free(messageAllocated); messageAllocated = NULL;
    }
    else
    {
        printf("malloc failed\n");     
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM