免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1499 | 回复: 0
打印 上一主题 下一主题

开个线程连mysql为啥占用虚存特别多呢。。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-04-26 13:54 |只看该作者 |倒序浏览
第一个占用180m,第二个占用108m  ,怎么这么大阿,代码没问题吧

编译 gcc t.c -lmysqlclient -lpthread
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <mysql/mysql.h>


  4. void *test1( void *arg )
  5. {
  6.         char sql[1024];
  7.         MYSQL_RES *results;
  8.         MYSQL_ROW record;
  9.         unsigned long id = 0;
  10.         MYSQL *mysql;
  11.         int mysql_timeout = 10;
  12.        
  13.         mysql = mysql_init( NULL );

  14.         mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout );

  15.         if ( !mysql_real_connect( mysql, "localhost", "root", "123456","mydb", 0, NULL, 0 ) )
  16.         {
  17.                 printf("error");
  18.                 exit(1);
  19.         }
  20.        
  21.         while( 1 )
  22.         {
  23.                 snprintf( sql, 1024, "select * from test limit %ld,10", id ++ );
  24.                        
  25.                 if ( mysql_query( mysql, sql ) != 0 )
  26.                 {
  27.                         printf( "1 mysql:%s\n", mysql_error( mysql ) );
  28.                 }
  29.        
  30.                 results = mysql_store_result( mysql );

  31.                 if ( results == NULL )
  32.                 {
  33.                         printf( "2 mysql:%s\n", mysql_error( mysql ) );
  34.                 }

  35.                 while ( (record = mysql_fetch_row( results ) ) )
  36.                 {
  37.                         printf("%s - %s\n", record[0], record[1] );
  38.                 }

  39.                 if ( mysql_num_rows( results ) == 0 )
  40.                 {
  41.                         id = 0;
  42.                 }
  43.                
  44.                 mysql_free_result( results );
  45.         }

  46.         return arg;
  47. }

  48. void *test2( void *arg )
  49. {
  50.         char sql[1024], str[1024];
  51.         int id = 0;
  52.         MYSQL *mysql;
  53.         int mysql_timeout = 10;
  54.        
  55.         mysql = mysql_init( NULL );

  56.         mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout );

  57.         sleep( 2 );
  58.        
  59.         if ( !mysql_real_connect( mysql, "localhost", "root",  "123456","mydb", 0, NULL, 0 ) )
  60.         {
  61.                 printf("error");
  62.                 exit(1);
  63.         }
  64.        
  65.         while ( 1 )
  66.         {
  67.                 snprintf( str, 1024, "wangwei %d", id );
  68.                
  69.                 snprintf( sql, 1024, "insert into test values (%ld,'%s')", id ++, str );

  70.                 if ( mysql_query( mysql, sql ) != 0 )
  71.                 {
  72.                         printf( "1 mysql:%s\n", mysql_error( mysql ) );
  73.                 }
  74.                
  75.                 printf("+");
  76.         }
  77.        
  78.         return arg;
  79. }


  80. main()
  81. {
  82.         int status;
  83.         pthread_t id1, id2;       

  84.         printf("mysql_thread_safe = %d\n",mysql_thread_safe());

  85.        
  86.         status = pthread_create( &id1, NULL, test1, NULL );
  87.        
  88.         if (status != 0)
  89.         {
  90.                 perror("thread1");
  91.                 exit(0);
  92.         }

  93.         status = pthread_create( &id2, NULL, test2, NULL );

  94.         if (status != 0)
  95.         {
  96.                 perror("thread2");
  97.                 exit(0);
  98.         }

  99.         while(1);
  100. }
复制代码
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <mysql/mysql.h>

  4. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


  5. void *test1( void *arg )
  6. {
  7.         char sql[1024];
  8.         MYSQL_RES *results;
  9.         MYSQL_ROW record;
  10.         unsigned long id = 0;
  11.         MYSQL *mysql;
  12.         int mysql_timeout = 10;
  13.        
  14.         mysql = ( MYSQL *) arg;
  15.         //mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout );
  16.        
  17.         while( 1 )
  18.         {
  19.                 pthread_mutex_lock( &mutex );
  20.                
  21.                 snprintf( sql, 1024, "select * from test limit %ld,1", id ++ );
  22.                        
  23.                 if ( mysql_query( mysql, sql ) != 0 )
  24.                 {
  25.                         printf( "1 mysql:%s\n", mysql_error( mysql ) );
  26.                 }
  27.        
  28.                 results = mysql_store_result( mysql );

  29.                 if ( results == NULL )
  30.                 {
  31.                         printf( "2 mysql:%s\n", mysql_error( mysql ) );
  32.                 }

  33.                 while ( (record = mysql_fetch_row( results ) ) )
  34.                 {
  35.                         printf("%s - %s\n", record[0], record[1] );
  36.                 }

  37.                 if ( mysql_num_rows( results ) == 0 )
  38.                 {
  39.                         id = 0;
  40.                 }
  41.                
  42.                 mysql_free_result( results );
  43.                
  44.                 pthread_mutex_unlock( &mutex );
  45.         }

  46.         return arg;
  47. }

  48. void *test2( void *arg )
  49. {
  50.         char sql[1024], str[1024];
  51.         int id = 0;
  52.         MYSQL *mysql;
  53.         int mysql_timeout = 10;

  54.         mysql = ( MYSQL *) arg;
  55.         //mysql_options( mysql, MYSQL_OPT_CONNECT_TIMEOUT, &mysql_timeout );

  56.         sleep( 2 );

  57.         while ( 1 )
  58.         {
  59.                 pthread_mutex_lock( &mutex );
  60.                 snprintf( str, 1024, "wangwei %d", id );
  61.                
  62.                 snprintf( sql, 1024, "insert into test values (%ld,'%s')", id ++, str );

  63.                 if ( mysql_query( mysql, sql ) != 0 )
  64.                 {
  65.                         printf( "1 mysql:%s\n", mysql_error( mysql ) );
  66.                 }
  67.                
  68.                 printf("+");
  69.                
  70.                 pthread_mutex_unlock( &mutex );
  71.         }
  72.        
  73.         return arg;
  74. }


  75. main()
  76. {
  77.         int status;
  78.         pthread_t id1, id2;       
  79.         MYSQL *mysql;
  80.        
  81.         printf("mysql_thread_safe = %d\n",mysql_thread_safe());
  82.        
  83.         mysql = mysql_init( NULL );

  84.         if ( !mysql_real_connect( mysql, "localhost", "root", "123456","mydb", 0, NULL, 0 ) )
  85.         {
  86.                 printf("error");
  87.                 exit(1);
  88.         }
  89.        
  90.         status = pthread_create( &id1, NULL, test1, (void*)mysql );
  91.        
  92.         if (status != 0)
  93.         {
  94.                 perror("thread1");
  95.                 exit(0);
  96.         }

  97.         status = pthread_create( &id2, NULL, test2, (void*)mysql );

  98.         if (status != 0)
  99.         {
  100.                 perror("thread2");
  101.                 exit(0);
  102.         }

  103.         while(1);
  104. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP