0

我有一个小程序,它在一台服务器上与显示准确数据的线程正常工作,但在其他服务器上显示重复。

我有以下代码:

// Structure
struct words_list {
    char myword[20];
    struct words_list * next;
};
struct myrepl_list {
    char myrepl[20];
    struct myrepl_list * next;
};

struct arg_struct {
    char *word;
    char *repl;
    int t;
};


int max_thread = 1;
// Mutex variables
pthread_mutex_t repl_list;
pthread_mutex_t thrd_list;

struct words_list * first_word = NULL;
struct myrepl_list * first_repl = NULL;

/*
do_process()
*/
void* do_process(void *arguments)
{
    int *res = malloc(sizeof(int));
    struct arg_struct *args = arguments;
    char *word, *repl;
    int t;
    pthread_mutex_lock(&thrd_list);
    word = args->word;
    repl = args->repl;
    t = args->t;
    pthread_mutex_unlock(&thrd_list);
    fprintf(stderr,"(%d) WORD: %s REPL: %s\n",t,word,repl);

//test example of return
    if (strstr(word, repl))
        *res = 1;
    else
        *res = 0;
//
    return res;
}


int main ()
{
    int ex = 0, i = 0;
    char myword[20];
    char myrepl[20];
    struct words_list * curr_word = first_word;
    struct myrepl_list * curr_repl = first_repl;
    struct arg_struct args;
    pthread_t thread_id[MAX_THREADS];

    while(ex == 0)
    {
        int ret = -1;
        for(i = 0 ; i < max_thread; i++)
        {
            // Get current word and myrepl
            pthread_mutex_lock(&repl_list);
            strncpy(myword,curr_word->myword,sizeof(myword) - 1);
            strncpy(myrepl,curr_repl->myrepl,sizeof(myrepl) - 1);
            pthread_mutex_unlock(&repl_list);
            args.myword = myword;
            args.myrepl = myrepl;
            args.t = i;

            //start threads
            if(pthread_create(&thread_id[i],NULL,&do_process,&args) != 0)
            {
                i--;
                fprintf(stderr,RED "\nError in creating thread\n" NONE);
            }
            else
            {
                pthread_mutex_lock(&repl_list);
                if(curr_repl->next == NULL)
                {
                    if(curr_word->next != NULL)
                    {
                        curr_word = curr_word->next;
                        curr_repl = first_repl;
                    }
                    else
                    {
                        ex = 1;
                        break;
                    }
                }
                else
                    curr_repl = curr_repl->next;
                pthread_mutex_unlock(&repl_list);
            }
        }

        for(i = 0 ; i < max_thread; i++)
        {
            void *join_result;
            if(pthread_join(thread_id[i],&join_result) != 0)
                fprintf(stderr,RED "\nError in joining thread\n" NONE);
            else
            {
                ret = *(int *)join_result;
                free(join_result);
                if(ret == 1)
                {
                    ex = 1;
                    break;
                }
                else
                {
                    //code missing
                }
            }
        }
    }//end while
}

在一台服务器上显示此输出:

(0) WORD: test1 REPL: bla0
(1) WORD: test1 REPL: bla1
(2) WORD: test1 REPL: bla2
(0) WORD: test1 REPL: bla3
(1) WORD: test1 REPL: bla4
(2) WORD: test1 REPL: bla5
(0) WORD: test1 REPL: bla6
(1) WORD: test1 REPL: bla7
(2) WORD: test1 REPL: bla8
(0) WORD: test1 REPL: bla9
(1) WORD: test1 REPL: bla10
(2) WORD: test2 REPL: bla0
(0) WORD: test2 REPL: bla1
(1) WORD: test2 REPL: bla2
(2) WORD: test2 REPL: bla3
(0) WORD: test2 REPL: bla4
(1) WORD: test2 REPL: bla5
(2) WORD: test2 REPL: bla6

在另一台服务器上显示:

(2) WORD: test1 REPL: bla2
(2) WORD: test1 REPL: bla2
(2) WORD: test1 REPL: bla2
(1) WORD: test1 REPL: bla1
(2) WORD: test1 REPL: bla4
(2) WORD: test1 REPL: bla4
(1) WORD: test1 REPL: bla6
(2) WORD: test1 REPL: bla7
(2) WORD: test1 REPL: bla7
(1) WORD: test1 REPL: bla9
(2) WORD: test1 REPL: bla10
(2) WORD: test2 REPL: bla10
(1) WORD: test2 REPL: bla1
(2) WORD: test2 REPL: bla2
(2) WORD: test2 REPL: bla2
(1) WORD: test2 REPL: bla4
(2) WORD: test2 REPL: bla3
(2) WORD: test2 REPL: bla3

最后我也得到了这个,可能是因为线程循环内部的中断:

Error in joining thread

Error in joining thread

Error in joining thread

Error in joining thread

我在这里做错了什么?

为什么在一台服务器上正确显示线程号和其余信息,但在另一台服务器上显示混乱的数据?

我尝试了一整天来解决它,但没有成功。

4

1 回答 1

1

在这一行

strncpy(myword,curr_word->myword,sizeof(myword) - 1);

程序NULL按原样取消引用。这会引发不法行为,因此在此之后任何事情都可能发生。curr_wordNULL


另请注意,strncpy()不一定将0-termination 附加到目标字符数组。详情man strncpy参阅。

于 2013-08-24T17:58:59.873 回答