非常奇怪的问题 - C 和线程



我对这段代码有一个非常奇怪的问题,对不起,它很混乱。基本上它是一个页面排名算法。每个结构网页都包含在动态数组"pages"中。页面向量通过算法,直到其绝对值 (|P|)小于"厄普西隆"。现在问题出在 195-201 行。如果我删除这些行中数组的迭代(即空的 while 循环),它适用于只需要一次迭代的情况。但是,当我确实有 for 循环时(即使是一次迭代情况),它甚至没有运行插入的循环就抛出 error6(第 179 行,调试显示 e == NULL)。我设置了断点等,甚至没有阅读额外的代码,仍然给出错误6。这是怎么回事?我对 C 和并行编程很陌生,所以它可能是基本的东西。将不胜感激任何帮助!

输入格式:

number_of_cores
number_of_pages
...
page_names
...
page_links

输出格式:

...
page_rank
...

法典

#include <assert.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const double D = 0.85;
static const double EPSILON = 0.005;
int ncores;
int npages;
struct webpage** pages;
int maxdepth;
struct webpage* has(char s[20], int e);
void* threadf(void* ptr);
int quit(void);
double rec(int s, int f, int depth);


struct webpage {
    char name[20];
    double oldrank;
    double rank;
    struct node* in;
    int incount;
    int outcount;
};
struct node {
    struct webpage* data;
    struct node* next;
};
struct arg {
    int s;
    int f;
    int depth;
    double ret;
};
struct webpage*
has(char s[20], int e) {
    int p;
    for (p=0; p<e; ++p) {
        if (strcmp(s, pages[p]->name) == 0) {
            return pages[p];
        }
    }
    return NULL;
}
void *
threadf(void* ptr) {
    struct arg* curr = (struct arg*)ptr;
    curr->ret = rec(curr->s, curr->f, curr->depth);
}
int
quit(void) {
    int i;
    for(i=0; i<npages; ++i) {
        struct node* curr = pages[i]->in;
        struct node* next;
        while(curr != NULL) {
            next = curr->next;
            free(curr);
            curr = next;
        }
        free(pages[i]);
    }
    free(pages);
    return 0;
}
double 
seq(int s, int f) {
    double sum;
    sum = 0;
    int w;
    for (w=s; w<=f; w++) {
        struct webpage* curr = pages[w];
        double ser;
        ser = 0;
        struct node* currn = curr->in;
        while (currn != NULL) {
            struct webpage* n = currn->data;
            ser = ser + ((n->oldrank)/(n->outcount));
            currn = currn->next;
        }
        double temp = (((1-D)/npages) + (D*ser)); 
        sum = sum + pow((temp - curr->oldrank), 2);
        curr->oldrank = curr->rank;
        curr->rank = temp;
    }
    return sum;
}

double 
rec(int s, int f, int depth) {
    if (depth == maxdepth ) {
        return seq(s, f);
    } else {
        if (s < f){
            int m;
            m = (s+f)/2;
            struct arg l;
            struct arg r;
            l.s = s;
            l.f = m;
            l.depth = depth+1;
            r.s = m+1;
            r.f = f;
            r.depth = depth+1;
            pthread_t left, right;
            pthread_create(&left, NULL, threadf, (void*) &l);
            pthread_create(&right, NULL, threadf, (void*) &r);
            pthread_join(left, NULL);
            pthread_join(right, NULL);
            double res;
            res = l.ret + r.ret;
            return res;
        } 
        return seq(s, f);
    }
}
int
main(void) {
    if (scanf("%d", &ncores) != 1) {
        printf("error1n");
        return quit();
    }
    if (scanf(" %d", &npages) != 1) {
        printf("error2n");
        return quit();
    }
    int i;
    char n[20];
    pages = (struct webpage**)malloc(npages*sizeof(struct webpage*));
    for (i=0; i<npages; ++i) {
        if (scanf(" %c", n) != 1 || has(n, i) != NULL) {
            printf("error3n");
            return quit();
        }
        pages[i] = (struct webpage*)malloc(sizeof(struct webpage));
        struct webpage* curr = pages[i];
        strcpy(curr->name, n);
        curr->oldrank = 1/npages;
        curr->in = NULL;
        curr->incount = 0;
        curr->outcount = 0;
    }
    int nedges;
    if (scanf(" %d", &nedges) != 1) {
        printf("error4n");
        return quit();
    }
    for (i=0; i<nedges; ++i) {
        char f[20], t[20];
        if (scanf(" %s %s", f, t) != 2) {
            printf("error5n"); 
            return quit();
        }
        char from[20], to[20];
        strcpy(from, f);
        strcpy(to, t);
        struct webpage* s = has(from, npages);
        struct webpage* e = has(to, npages);
        if (s == NULL || e == NULL) {
            printf("error6n");
            return quit();
        }
        s->outcount++;
        e->incount++;
        struct node* new;
        new = (struct node*)malloc(sizeof(struct node));
        new->data = s;
        if (e->in == NULL) {
            e->in = new;
        } else {
            new->next = e->in;
            e->in = new;
        }
    }
    maxdepth = (log(ncores))/(log(2)) + 0.5;
    while (sqrt(rec(0, npages-1, 0)) > EPSILON){
        int c;
        for (c=0; c<npages; ++c) {
            struct webpage* curr = pages[c];
            curr->oldrank = curr->rank;
        }
    }
    int z;
    for (z=0; z<npages; ++z) {
        struct webpage* curr = pages[z];
        printf("%s %.4lfn", curr->name, curr->rank);
    }
    return quit();
}

示例输入:

8
4
a
b
c
d
4
a a

输出:

error6
char n[20];
[ ... ]
    if (scanf(" %c", n) != 1 || has(n, i) != NULL) {

scanf%c格式说明符仅读取一个字符。因此,n由您键入的字符加上您调用scanf()之前恰好在堆栈上的任何垃圾组成。如果您使用 %s ,它将由您键入的字符加上用于终止字符串的 NUL 字节以及您不关心的垃圾组成。

另请注意,您可以使用宽度说明符限制scanf()读取的字符数,如下所示:

scanf("%19s", n)

(含义:读取 19 个字符并添加一个 NUL 字节)。否则,缓冲区可能会溢出,可能导致任意代码执行(或者当非恶意用户使用时至少崩溃)。

相关内容

  • 没有找到相关文章

最新更新