LinuxSir.cn,穿越时空的Linuxsir!

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

我好像是遇到glibc的bug了。

[复制链接]
发表于 2005-10-30 00:04:01 | 显示全部楼层 |阅读模式
今天编写了一个程序:

  1. /* simple.c third editon */
  2. /* this program draws a color triangle on a black background and translate it and print fps */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <GL/gl.h>
  8. #include <GL/glut.h>

  9. #define PI 3.1415926535897932384626433

  10. float v[3] = {PI + PI/6, PI/2, -PI/6};

  11. void display (void);
  12. void init (void);
  13. void myreshape (int, int);
  14. void myidle (void);
  15. long long mySwapBuffers (void);

  16. int main (int argc, char *argv[]){
  17.         glutInit (&argc, argv);
  18.         glutInitWindowSize (600,600);
  19.         glutInitDisplayMode (GLUT_DOUBLE|GLUT_RGB);
  20.         glutCreateWindow (argv[0]);
  21. //        glutFullScreen ();
  22.         glutDisplayFunc (display);
  23. //        glutDisplayString ("Hello");
  24.         glutReshapeFunc (myreshape);
  25.         glutIdleFunc (myidle);
  26.         init ();
  27.         glutMainLoop ();

  28.         return 0;
  29. }

  30. void display (void){
  31.         GLclampf red[]   = {1.0, 0.0, 0.0};
  32.         GLclampf green[] = {0.0, 1.0, 0.0};
  33.         GLclampf blue[]  = {0.0, 0.0, 1.0};

  34.         /* 使用v[3]中存储的三个角度创建3个顶点坐标 */
  35.         float p[][2] = {
  36.                         {cos(v[0]), sin(v[0])},
  37.                         {cos(v[1]), sin(v[1])},
  38.                         {cos(v[2]), sin(v[2])}
  39.                         };

  40.         glClear (GL_COLOR_BUFFER_BIT);

  41.         glBegin (GL_POLYGON);
  42.                 glColor3fv (red);
  43.                 glVertex2fv (p[0]);
  44.                 glColor3fv (green);
  45.                 glVertex2fv (p[1]);
  46.                 glColor3fv (blue);
  47.                 glVertex2fv (p[2]);
  48.         glEnd ();

  49. //        glFlush ();
  50.         //glutSwapBuffers ();                /* for Dobule Buffer */
  51.         printf ("fps=%lld.\n",mySwapBuffers ());
  52. }

  53. void init (void){
  54.         GLclampf red[]   = {1.0, 0.0, 0.0};
  55.         GLclampf black[] = {0.0, 0.0, 0.0, 0.0};

  56.         glClearColor (black[0], black[1], black[2], black[3]);

  57. //        glColor3fv (red);

  58.         //glShadeModel (GL_FLAT);
  59.         glShadeModel (GL_SMOOTH);
  60.        
  61.         glMatrixMode (GL_PROJECTION);
  62.         glLoadIdentity ();
  63.         gluOrtho2D (-1.0, 1.0, -1.0, 1.0);
  64. }

  65. void myreshape (int w, int h){
  66.         glMatrixMode (GL_PROJECTION);
  67.         glLoadIdentity ();
  68.         if (w<=h)
  69.                 gluOrtho2D (-2.0, 2.0, -2.0*(GLfloat)h/(GLfloat)w,
  70.                 2.0*(GLfloat)h / (GLfloat)w);
  71.         else
  72.                 gluOrtho2D (-2.0*(GLfloat)w / (GLfloat)h,
  73.                 2.0 * (GLfloat)w / (GLfloat)h, -2.0, 2.0);

  74.         glMatrixMode (GL_MODELVIEW);
  75.         glViewport (0, 0 ,w, h);
  76. }

  77. void myidle (void){
  78.         static GLdouble a=0;
  79.         a +=1.0;
  80.        
  81.         glMatrixMode (GL_MODELVIEW);
  82.         glLoadIdentity ();
  83.         glRotated (a, 1.0, 1.0, 0.0);

  84.         glutPostRedisplay();
  85. }

  86. long long mySwapBuffers (void){
  87.         static int first = 1;
  88.         static long long fps_count = 0;        /* 计算fps的计数器 */
  89.         static clock_t s_time;                /* 当前时间 */
  90.         static clock_t p_time;                /* 前一个时间 */
  91.         static long long fps = 0;        /* 存储fps */
  92.        
  93.         printf ("first = %d\n", first);
  94.         if (first){
  95.         printf ("CLOCKS_PER_SEC = %ld\n", CLOCKS_PER_SEC);
  96.                 first = !first;
  97.                 p_time = clock ();
  98.         }
  99.        
  100.         s_time = clock ();        /* 获取当前时间 */
  101.         ++fps_count;

  102.         printf ("p_time = %ld\ts_time = %ld\n", p_time, s_time);
  103.         printf ("fps_count = %lld\n", fps_count);
  104.         [color=red]
  105. //        if ( (s_time - p_time) >= CLOCKS_PER_SEC ){        /* 测试是否经过一秒 */
  106.         if ( (s_time - p_time) >= 10000 ){        /* 测试是否经过一秒 */
  107.                 p_time = s_time;        /* 每过一秒,更新”前一个时间“ */
  108.                 fps = fps_count;
  109.                 fps_count = 0;
  110.         }[/color]
  111.        
  112.         glutSwapBuffers ();
  113.         return fps;
  114. }

复制代码


在函数long long mySwapBuffers (void)中有了问题:
1、我使用time.h中的clock()获取当前时间。但是glibc的CLOCKS_PER_SEC看起来有问题。clock()每过一秒增加10000,但是CLOCKS_PER_SEC的值确实1000 000。在C标准库的定义中,这两个值是相等的。
2、使用clock()的时候,发现它每过一秒递增10000,而不是1点1点递增的。
在TCPL中对clock()的定义如下:
clock_t clock (void)
clock函数用于返回程序自运行到目前为止所占用的处理机时间;如果处理机时间不可使用,那么返回值是-1。clock()/CLOCKS_PER_SEC是以秒为单位表示的时间。
是不是glibc有bug呢?
期待高手解惑。

我的Linux是Fedora Core4。
  1. $gcc -v
  2. 使用内建 specs。
  3. 目标:i386-redhat-linux
  4. 配置为:../configure --prefix=/usr --mandir=/usr/share/man
  5. --infodir=/usr/share/info --enable-shared --enable-threads=posix
  6. --enable-checking=release --with-system-zlib --enable-__cxa_atexit
  7. --disable-libunwind-exceptions --enable-libgcj-multifile
  8. --enable-languages=c,c++,objc,java,f95,ada --enable-java-awt=gtk
  9. --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --host=i386-redhat-linux
  10. 线程模型:posix
  11. gcc 版本 4.0.0 20050519 (Red Hat 4.0.0-8)
复制代码
  1. $ /lib/libc.so.6
  2. GNU C Library development release version 2.3.5, by Roland McGrath et al.
  3. Copyright (C) 2005 Free Software Foundation, Inc.
  4. This is free software; see the source for copying conditions.
  5. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
  6. PARTICULAR PURPOSE.
  7. Compiled by GNU CC version 4.0.0 20050525 (Red Hat 4.0.0-9).
  8. Compiled on a Linux 2.4.20 system on 2005-05-30.
  9. Available extensions:
  10.         GNU libio by Per Bothner
  11.         crypt add-on version 2.1 by Michael Glad and others
  12.         Native POSIX Threads Library by Ulrich Drepper et al
  13.         The C stubs add-on version 2.1.2.
  14.         BIND-8.2.3-T5B
  15.         NIS(YP)/NIS+ NSS modules 0.19 by Thorsten Kukuk
  16.         Glibc-2.0 compatibility add-on by Cristian Gafton
  17.         GNU Libidn by Simon Josefsson
  18. Thread-local storage support included.
  19. For bug reporting instructions, please see:
  20. <http://www.gnu.org/software/libc/bugs.html>.
复制代码


编译使用

  1. $gcc -Wall -g -lglut -lGL -lGLU -lX11 -lm -L/usr/X11R6/lib simple3.c simple3
复制代码
发表于 2005-10-30 00:19:29 | 显示全部楼层
安装低版本的glibc,4.0版本的问题不少
回复 支持 反对

使用道具 举报

发表于 2005-10-30 10:52:56 | 显示全部楼层
RETURN VALUE
       The  value  returned is the CPU time used so far as a clock_t; to get the number of seconds
       used, divide by CLOCKS_PER_SEC.  If the processor time used is not available or  its  value
       cannot be represented, the function returns the value (clock_t)-1.
回复 支持 反对

使用道具 举报

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

本版积分规则

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