QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 550|回复: 6

有请各位帮忙调试一下

[复制链接]
发表于 2004-7-14 15:21:38 | 显示全部楼层 |阅读模式
最近在看Alex Vrenios的《Linux集群体系结构》,里面的客户程序我如何也调试不出,希望高手给够指点。
源程序如下
[code:1]
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "msi.h"

#define FALSE 0
#define TRUE 1
#define NUM 50

#define CATS 10                        /* # of response time categories */
#define cats [CATS] = {.1, .2, .3, .4, .5, .6, .7, .8, .9, .10};

int bars[CATS] = { 0 };                /* counters for bar chart */

int main(int argc, char *argv[])
{
    /*
     ** client.c: reports response time stats
     */
    struct timeval bef, aft;        /* timer value before/after */
    struct sockaddr_in sock;
    struct hostent *hp;
    int flag = (IPC_CREAT | IPC_EXCL | 0660);
    int size = NUM * sizeof(double);
    key_t key = 0x01234567;        /* example shared memory key */
    int shmid;                        /* shared memory area id */
    int Expon = FALSE;                /* boolean: exponential dist */
    int Pulse = FALSE;                /* boolean: pulse distribution */
    int Sweep = FALSE;                /* boolean: sweep distribution */
    double mean = 0.50;                /* mean value for exponential */
    double slow = 0.25;                /* slow value for pulse & sweep */
    double fast = 0.10;                /* fast value for pulse burst */
    double random;                /* random reals: 0.0 -> 1.0 */
    double delay;                /* calculated delay time */
    double *rt;                        /* response times from subtasks */
    long timeleft;                /* time remaining in IAT */
    long usec = 0L;                /* inter-arrival microseconds */
    long secs = 0L;                /* time remaining in seconds */
    int pids[NUM];                /* subtask process ids */
    int opt = 1;                /* setsockopt parameter */
    int fd;                        /* socket file descriptor */
    int ii, jj, kk = 0;
    char distribution[4];
    char buf[QUERYSIZE], *p;
    int status;

    srand((unsigned int) getpid());        /* seed rand() */

    /*
     ** Operands are remote HOST name and distribution code
     */
    if (argc != 3) {
        printf("\n\tUsage: %s <HOST> <DISTRIBUTION>\n\n", argv[0]);
        exit(-1);
    }
    hp = gethostbyname(argv[1]);

    if ((argv[2][0] == 'e') || (argv[2][0] == 'E')) {
        Expon = TRUE;
        strcpy(distribution, "EXP");
        srand((unsigned int) getpid());        /* rand() seed */
    } else if ((argv[2][0] == 'p') || (argv[2][0] == 'P')) {
        Pulse = TRUE;
        strcpy(distribution, "PLS");
    } else if ((argv[2][0] == 's') || (argv[2][0] == 'S')) {
        Sweep = TRUE;
        strcpy(distribution, "SMP");
    } else {
        printf("\n\tUsage: %s <HOST> <DISTRIBUTION>\n", argv[0]);
        printf("\t        DISTRIBUTION = { 'e' | 'p' | 's' }\n");
        printf("\t        for exponential, pulse and sweep\n\n");
        exit(-1);
    }

    /* attach shared memory array */
    shmid = shmget(key, size, flag);
    /* attach the shared memory area */
    rt = (double *) shmat(shmid, 0, 0);

    /*
     ** LOOP : send and receive NUM packets
     */
    for (ii = 0; ii < NUM; ii++) {
        rt[ii] = 0.0;                /* zero shared array values */

        if (Expon) {
            random = rand() / (double) RAND_MAX;
            delay = -mean * log(random);
            if (delay > 0.999999) {
                delay = 0.999999;        /* limit maximum */
            }
        } else if (Pulse) {
            if ((ii > (NUM * 0.3)) && (ii < (NUM * 0.4))) {
                delay = fast;
            } else {
                delay = slow;
            }
        } else if (Sweep) {
            delay = slow - (ii * (slow / NUM));
        } else {
            printf("\n\tError in logic?\n\n");
            exit(-1);
        }
        secs = (long) delay;
        usec = (long) (1000000.0 * (delay - ((double) secs)));

        /* random line numbers: 1 thru 99 */
        random = rand() / (double) RAND_MAX;
        jj = (int) ((double) (99.0) * random) + 1;
        if (jj == 20) {
            jj = 99;
        }
        sprintf(buf, "/home/andy/src/cluster/main/sample.txt %d", jj);

        /* record sending time */
        gettimeofday(&bef, NULL);

        /*
         ** A subtask for each query/response
         */
        if ((pids[kk++] = fork()) == 0) {
            /* attach parent's shared memory */
            rt = (double *) shmat(shmid, 0, 0);
            /* shmid is still set correctly */

            /* Set up TCP socket to the server */
            fd = socket(AF_INET, SOCK_STREAM, 0);
            memset((char *) &sock, 0, sizeof(sock));
            memcpy(&sock.sin_addr, hp->h_addr, hp->h_length);
            sock.sin_family = hp->h_addrtype;
            sock.sin_port = htons(QUERYPORT);
            setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt,
                       sizeof(opt));
            connect(fd, (struct sockaddr *) &sock, sizeof(sock));
            send(fd, buf, strlen(buf) + 1, 0);

            /* await result from server */
            buf[0] = 0;                /* clear buffer */
            if (recv(fd, buf, QUERYSIZE, 0) > 0) {
                printf("\t%d. Line %2d: %s ", kk, jj, buf);
                p = strrchr(buf, ' ') + 2;
                if (jj != atoi(p)) {
                    printf("***");        /* incorrect result! */
                }
                printf("\n");
            }

            /* record response time */
            gettimeofday(&aft, NULL);
            aft.tv_sec -= bef.tv_sec;
            aft.tv_usec -= bef.tv_usec;
            if (aft.tv_usec < 0L) {
                aft.tv_usec += 1000000L;
                aft.tv_sec -= 1;
            }
            rt[ii] =
                (double) aft.tv_sec + ((double) aft.tv_usec / 1000000.0);

            close(fd);
            exit(0);
        }

        /*
         ** Sleep for remainder of IAT
         */
        gettimeofday(&aft, NULL);
        aft.tv_sec -= bef.tv_sec;
        aft.tv_usec -= bef.tv_usec;
        if (aft.tv_usec < 0L) {
            aft.tv_usec += 1000000L;
            aft.tv_sec -= 1;
        }
        bef.tv_sec = secs;
        bef.tv_usec = usec;
        bef.tv_sec -= aft.tv_sec;
        bef.tv_usec -= aft.tv_usec;
        if (bef.tv_usec < 0L) {
            bef.tv_usec += 1000000L;
            bef.tv_sec -= 1;
        }
        timeleft = (bef.tv_sec * 1000000L) + bef.tv_usec;
        if (timeleft < 0) {
            printf("\tERROR: A higher IAT value is required - exiting.\n");
            break;
        }
        usleep(timeleft);
    }
    for (ii = 0; ii < kk; ii++) {
        waitpid(pids[ii], &status, 0);
    }

    /*
     ** Report the response time statistics
     */
    for (ii = 0; ii < NUM; ii++) {
        for (jj = 0; jj < CATS; jj++) {
            if (rt[ii] < cats[jj]) {
                bars[jj]++;
                break;
            }
        }
    }
    sleep(2);
    printf("RESPONSE    |           %3d QUERY PACKETS            %s\n",
           NUM, distribution);
    printf("TIME (msec) |      10       20       30       40      50\n");
    printf("------------+---+---+---+----+---+----+---+----+---+--+\n");
    ii = 0;                        /* total */
    for (jj = 0; jj < CATS; jj++) {
        ii += bars[jj];
        kk = (1000.0 * cats[jj]) + .5;
        printf(" %5d%5d |", kk - 9, kk);
        for (kk = 0; kk < bars[jj]; kk++) {
            printf("*");
        }
        printf("\n");
    }
    printf("  OVER%5d |", (int) (1000 * cats[CATS - 1]));
    jj = NUM - ii;
    for (kk = 0; kk < jj; kk++) {
        printf("*");
    }
    printf("\n");

    /* remove unused shared memory area and exit */
    shmctl(shmid, IPC_RMID, (struct shmid_ds *) 0);

    return 0;
}
[/code:1][/code]
发表于 2004-7-14 15:32:33 | 显示全部楼层
你的 msi.h在哪?
回复

使用道具 举报

 楼主| 发表于 2004-7-14 15:47:33 | 显示全部楼层
不好意思

[code:1]
/*
** msi.h - master-slave interface header
*/
#define QUERYPORT 9000
#define BROADCAST 5000
#define REGISTERS 5001
#define SLAVEPORT 5002
#define QUERYSIZE 256

struct packet
{
        /*
        ** Information request packet
        */
#ifdef TIMING
        /* slave phase times */
        struct timeval remote;
        struct timeval phase4;
        struct timeval phase5;
        struct timeval phase6;
#endif
        char buffer[QUERYSIZE]; /* request */
} packet;
[/code:1]
回复

使用道具 举报

发表于 2004-7-14 15:54:16 | 显示全部楼层
temp12.c:208: error: syntax error before '[' token
temp12.c: At top level:
temp12.c:213: error: syntax error before '}' token
temp12.c:214: error: syntax error before numeric constant
temp12.c:214: error: conflicting types for `sleep'
/usr/include/unistd.h:390: error: previous declaration of `sleep'
temp12.c:214: warning: data definition has no type or storage class
temp12.c:215: error: syntax error before string constant
temp12.c:216: warning: conflicting types for built-in function `printf'
temp12.c:216: warning: data definition has no type or storage class
temp12.c:217: error: syntax error before string constant
temp12.c:217: warning: data definition has no type or storage class
temp12.c:218: error: syntax error before string constant
temp12.c:218: warning: data definition has no type or storage class
temp12.c:219: warning: data definition has no type or storage class
temp12.c:220: error: syntax error before "for"
temp12.c:222: error: syntax error before '[' token
temp12.c:222: warning: data definition has no type or storage class
temp12.c:223: error: syntax error before string constant
temp12.c:223: warning: data definition has no type or storage class
temp12.c:227: error: syntax error before string constant
temp12.c:227: warning: data definition has no type or storage class
temp12.c:229: error: syntax error before string constant
temp12.c:230: error: initializer element is not constant
temp12.c:230: warning: data definition has no type or storage class
temp12.c:231: error: syntax error before "for"
temp12.c:234: error: syntax error before string constant
temp12.c:234: warning: data definition has no type or storage class
temp12.c:237: error: syntax error before numeric constant
楼主不厚道,这么多问题不解决就贴出来   
回复

使用道具 举报

 楼主| 发表于 2004-7-14 15:59:19 | 显示全部楼层
[quote:43556e8ac0="sagaeon"]楼主不厚道,这么多问题不解决就贴出来   [/quote]

恕我愚昧,我不清楚那些地方为什么会错。我看是一切正常的
回复

使用道具 举报

 楼主| 发表于 2004-7-14 16:34:49 | 显示全部楼层
版主有没有想法?我已经困扰几天了,快崩溃了~`
回复

使用道具 举报

发表于 2004-7-14 18:58:24 | 显示全部楼层
你的[code:1]#define cats [CATS]={............}[/code:1]是什么意思,这儿有问题。如果 你想赋值,用笨点的办法改过来,编译时加上 -lm选项就可以通过了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-7 20:47 , Processed in 0.043435 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表