LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 672|回复: 7

帮忙调试一下这个程序

[复制链接]
发表于 2004-7-14 15:59:44 | 显示全部楼层 |阅读模式
我最近在看Alex Vrenios的《Linux集群体系结构》, 其中的客户程序如何也调 试不出来。希望高手帮忙. 谢谢!
环境:rh9 compiler:gcc
源代码如下:


  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/ipc.h>
  7. #include <sys/shm.h>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <netdb.h>
  14. #include "msi.h"

  15. #define FALSE 0
  16. #define TRUE 1
  17. #define NUM 50

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

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

  21. int main(int argc, char *argv[])
  22. {
  23.     /*
  24.      ** client.c: reports response time stats
  25.      */
  26.     struct timeval bef, aft;        /* timer value before/after */
  27.     struct sockaddr_in sock;
  28.     struct hostent *hp;
  29.     int flag = (IPC_CREAT | IPC_EXCL | 0660);
  30.     int size = NUM * sizeof(double);
  31.     key_t key = 0x01234567;        /* example shared memory key */
  32.     int shmid;                        /* shared memory area id */
  33.     int Expon = FALSE;                /* boolean: exponential dist */
  34.     int Pulse = FALSE;                /* boolean: pulse distribution */
  35.     int Sweep = FALSE;                /* boolean: sweep distribution */
  36.     double mean = 0.50;                /* mean value for exponential */
  37.     double slow = 0.25;                /* slow value for pulse & sweep */
  38.     double fast = 0.10;                /* fast value for pulse burst */
  39.     double random;                /* random reals: 0.0 -> 1.0 */
  40.     double delay;                /* calculated delay time */
  41.     double *rt;                        /* response times from subtasks */
  42.     long timeleft;                /* time remaining in IAT */
  43.     long usec = 0L;                /* inter-arrival microseconds */
  44.     long secs = 0L;                /* time remaining in seconds */
  45.     int pids[NUM];                /* subtask process ids */
  46.     int opt = 1;                /* setsockopt parameter */
  47.     int fd;                        /* socket file descriptor */
  48.     int ii, jj, kk = 0;
  49.     char distribution[4];
  50.     char buf[QUERYSIZE], *p;
  51.     int status;

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

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

  61.     if ((argv[2][0] == 'e') || (argv[2][0] == 'E')) {
  62.         Expon = TRUE;
  63.         strcpy(distribution, "EXP");
  64.         srand((unsigned int) getpid());        /* rand() seed */
  65.     } else if ((argv[2][0] == 'p') || (argv[2][0] == 'P')) {
  66.         Pulse = TRUE;
  67.         strcpy(distribution, "PLS");
  68.     } else if ((argv[2][0] == 's') || (argv[2][0] == 'S')) {
  69.         Sweep = TRUE;
  70.         strcpy(distribution, "SMP");
  71.     } else {
  72.         printf("\n\tUsage: %s <HOST> <DISTRIBUTION>\n", argv[0]);
  73.         printf("\t        DISTRIBUTION = { 'e' | 'p' | 's' }\n");
  74.         printf("\t        for exponential, pulse and sweep\n\n");
  75.         exit(-1);
  76.     }

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

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

  86.         if (Expon) {
  87.             random = rand() / (double) RAND_MAX;
  88.             delay = -mean * log(random);
  89.             if (delay > 0.999999) {
  90.                 delay = 0.999999;        /* limit maximum */
  91.             }
  92.         } else if (Pulse) {
  93.             if ((ii > (NUM * 0.3)) && (ii < (NUM * 0.4))) {
  94.                 delay = fast;
  95.             } else {
  96.                 delay = slow;
  97.             }
  98.         } else if (Sweep) {
  99.             delay = slow - (ii * (slow / NUM));
  100.         } else {
  101.             printf("\n\tError in logic?\n\n");
  102.             exit(-1);
  103.         }
  104.         secs = (long) delay;
  105.         usec = (long) (1000000.0 * (delay - ((double) secs)));

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

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

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

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

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

  142.             /* record response time */
  143.             gettimeofday(&aft, NULL);
  144.             aft.tv_sec -= bef.tv_sec;
  145.             aft.tv_usec -= bef.tv_usec;
  146.             if (aft.tv_usec < 0L) {
  147.                 aft.tv_usec += 1000000L;
  148.                 aft.tv_sec -= 1;
  149.             }
  150.             rt[ii] =
  151.                 (double) aft.tv_sec + ((double) aft.tv_usec / 1000000.0);

  152.             close(fd);
  153.             exit(0);
  154.         }

  155.         /*
  156.          ** Sleep for remainder of IAT
  157.          */
  158.         gettimeofday(&aft, NULL);
  159.         aft.tv_sec -= bef.tv_sec;
  160.         aft.tv_usec -= bef.tv_usec;
  161.         if (aft.tv_usec < 0L) {
  162.             aft.tv_usec += 1000000L;
  163.             aft.tv_sec -= 1;
  164.         }
  165.         bef.tv_sec = secs;
  166.         bef.tv_usec = usec;
  167.         bef.tv_sec -= aft.tv_sec;
  168.         bef.tv_usec -= aft.tv_usec;
  169.         if (bef.tv_usec < 0L) {
  170.             bef.tv_usec += 1000000L;
  171.             bef.tv_sec -= 1;
  172.         }
  173.         timeleft = (bef.tv_sec * 1000000L) + bef.tv_usec;
  174.         if (timeleft < 0) {
  175.             printf("\tERROR: A higher IAT value is required - exiting.\n");
  176.             break;
  177.         }
  178.         usleep(timeleft);
  179.     }
  180.     for (ii = 0; ii < kk; ii++) {
  181.         waitpid(pids[ii], &status, 0);
  182.     }

  183.     /*
  184.      ** Report the response time statistics
  185.      */
  186.     for (ii = 0; ii < NUM; ii++) {
  187.         for (jj = 0; jj < CATS; jj++) {
  188.             if (rt[ii] < cats[jj]) {
  189.                 bars[jj]++;
  190.                 break;
  191.             }
  192.         }
  193.     }
  194.     sleep(2);
  195.     printf("RESPONSE    |           %3d QUERY PACKETS            %s\n",
  196.            NUM, distribution);
  197.     printf("TIME (msec) |      10       20       30       40      50\n");
  198.     printf("------------+---+---+---+----+---+----+---+----+---+--+\n");
  199.     ii = 0;                        /* total */
  200.     for (jj = 0; jj < CATS; jj++) {
  201.         ii += bars[jj];
  202.         kk = (1000.0 * cats[jj]) + .5;
  203.         printf(" %5d%5d |", kk - 9, kk);
  204.         for (kk = 0; kk < bars[jj]; kk++) {
  205.             printf("*");
  206.         }
  207.         printf("\n");
  208.     }
  209.     printf("  OVER%5d |", (int) (1000 * cats[CATS - 1]));
  210.     jj = NUM - ii;
  211.     for (kk = 0; kk < jj; kk++) {
  212.         printf("*");
  213.     }
  214.     printf("\n");

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

  217.     return 0;
  218. }
复制代码




这里是msi.h头文件:

  1. /*
  2. ** msi.h - master-slave interface header
  3. */
  4. #define QUERYPORT 9000
  5. #define BROADCAST 5000
  6. #define REGISTERS 5001
  7. #define SLAVEPORT 5002
  8. #define QUERYSIZE 256

  9. struct packet
  10. {
  11.         /*
  12.         ** Information request packet
  13.         */
  14. #ifdef TIMING
  15.         /* slave phase times */
  16.         struct timeval remote;
  17.         struct timeval phase4;
  18.         struct timeval phase5;
  19.         struct timeval phase6;
  20. #endif
  21.         char buffer[QUERYSIZE]; /* request */
  22. } packet;
复制代码
发表于 2004-7-14 16:32:12 | 显示全部楼层
这一句是什么意思?!!

  1. #define cats [CATS] = {.1, .2, .3, .4, .5, .6, .7, .8, .9, .10};
复制代码
 楼主| 发表于 2004-7-16 14:42:58 | 显示全部楼层
客户程序最后的一段代码用来输出一张图,这个数组是用来显示的。
这张图表示了服务器响应客户机的时间。
发表于 2004-7-16 19:15:45 | 显示全部楼层
如果是数组,直接后再初始化就行啦,干吗用#define?
这样在预处理时根本发现不了你的错误!
建议你自己找本教科书看看C语言中数组是如何定义的。
发表于 2004-7-17 22:56:49 | 显示全部楼层
楼主是书上的源码吧?
 楼主| 发表于 2004-7-18 15:02:46 | 显示全部楼层
yes, it's from <<Linux Cluster Architecture>>
发表于 2004-7-18 15:48:59 | 显示全部楼层
如果书上就是这样子写的,那一定是搞错了。可以把它的源代码下载来看看。
 楼主| 发表于 2004-7-18 16:44:17 | 显示全部楼层
谢谢斑竹了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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