免费注册 查看新帖 |

Chinaunix

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

熟悉pro*c/c++和多线程的大哥大姐,父老乡亲们,帮我看看程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-02-07 14:15 |只看该作者 |倒序浏览
我在redhat linux server 2.1 上编译一个多线程的PROC程序(基本上是由oracle公司提

供的例子改造的),
我将其例子中的main函数改为main_test函数,自己加了一个main函数,每个三秒钟调用

一次main_test;
出现两个令我迷惑的问题:
1.经过几分钟的测试(运行后)出现Error while trying to retrieve text for error ORA-12540;
2.我用top命令观察进程情况,发现其内存在增长:
    PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
开始:
  16567 oracle    15   0 18536  18M  3704 S     1.8  1.8   0:00 thread_demo
一段时间后:
  16567 oracle    15   0 31932  31M  3704 S     2.7  3.1   0:00 thread_demo
又一段时间后:
  16567 oracle    15   0 39636  38M  3704 S     3.7  3.8   0:00 thread_demo
又一段时间后:
  16567 oracle    15   0 44796  43M  3704 S     1.1  4.3   0:01 thread_demo
又一段时间后:
  16567 oracle    15   0 51680  50M  3704 S     0.7  5.0   0:01 thread_demo
又一段时间后:
  16567 oracle    15   0 57700  56M  3704 S     2.5  5.6   0:01 thread_demo
又一段时间后:
  16567 oracle    15   0 62864  61M  3704 S     1.7  6.1   0:01 thread_demo
又一段时间后:
  16567 oracle    15   0 69912  68M  3704 S     1.9  6.7   0:01 thread_demo
又一段时间后:
  16567 oracle    15   0 73172  71M  3704 S     1.7  7.1   0:01 thread_demo
又一段时间后,程序自动退出:
  Error while trying to retrieve text for error ORA-12540;

#include <stdio.h>;
#include <stdlib.h>;
#include <string.h>;
#include

<sqlca.h>;

#define      _EXC_OS_        _EXC__UNIX
#define      _CMA_OS_     

   _CMA__UNIX

#ifdef DCE_THREADS
#include <pthread.h>;
#else
#include

<pthread.h>;
typedef void*       pthread_addr_t;
typedef void*      

(*pthread_startroutine_t) (void*);
#define pthread_attr_default  (const

pthread_attr_t *)NULL
#endif

/* Function prototypes */
void   err_report();
void   do_transaction();
void   get_transaction();
void   logon();
void   

logoff();

#define CONNINFO "ora/ora@ora9"
#define THREADS  40

struct

parameters
  {
   sql_context * ctx;
   int thread_id;
  };
typedef

struct parameters parameters;

struct timeval tp1;
struct timeval tp2;

/***************************************
*  Main
***************************************/

main_test()
{
  sql_context

ctx[THREADS];
  pthread_t thread_id[THREADS];
  pthread_addr_t status;
  

parameters params[THREADS];
  int i;

  EXEC SQL ENABLE THREADS;
  EXEC SQL

WHENEVER SQLERROR DO err_report(sqlca);

  if(gettimeofday(&tp1, (void*)NULL)

== -1)
  {
    perror("First: ";
    exit(0);
  }

  /* Create THREADS

sessions by connecting THREADS times */
  for(i=0;i<THREADS;i++)
  {
   

printf("Start Session %d....",i);
    EXEC SQL CONTEXT ALLOCATE :ctx;
   

logon(ctx,CONNINFO);
  }

  /*Spawn threads*/
  for(i=0;i<THREADS;i++)


{
    params.ctx=ctx;
    params.thread_id=i;

    if

(pthread_create(&thread_id,pthread_attr_default,
      

(pthread_startroutine_t)do_transaction,
      (pthread_addr_t) &params))
   

   printf("Cant create thread %d\n",i);
    else
      printf("Created

Thread %d\n", i);
  }

  /* Logoff sessions....*/
  for(i=0;i<THREADS;i++)
  {
    /*wait for thread to end */
    if

(pthread_join(thread_id,&status))
      printf("Error when waiting for

thread % to terminate\n", i);
    else
      printf("stopped\n";

   

printf("Detach thread...";
    if (pthread_detach(thread_id))
      

printf("Error detaching thread! \n";
    else
      printf("Detached!\n";
    /*if(i==THREADS-1) */  /*注:此处的if语句原来是有的,oracle的例子当中,
    但是因为在运行中出现超出最大连接数,我才删掉的*/
    {
      

logoff(ctx);
      EXEC SQL CONTEXT FREE :ctx;
    }

  }

  

if(gettimeofday(&tp2, (void*)NULL) == -1)
  {
    perror("Second: ";
   

exit(0);
  }

    printf(" \n\nTHE TOTAL TIME TAKEN FOR THE PROGRAM EXECUTION

= %f \n\n", (float)(tp2.tv_sec - tp1.tv_sec) + ((float)(tp2.tv_usec -

tp1.tv_usec)/1000000.0));

}

/***********************************************************************
*

Function: do_transaction
* Description:  This functions executes SELECT 5

times and calls COMMIT.
***********************************************************************/
void

do_transaction(params)
parameters *params;
{
  struct sqlca sqlca;
  int

src_count;
  sql_context ctx=params->;ctx;
  int no;
       
  struct {
                 

        char name[10];
                          float salary;
                 

        float comm;
  }emp_record;

  EXEC SQL WHENEVER SQLERROR DO

err_report(sqlca);
  EXEC SQL CONTEXT USE :ctx;
  printf("Thread %d

executing transaction\n",params->;thread_id);
  EXEC SQL COMMIT;
  

for(no=101;no<105;no++)  
  {
          EXEC SQL SELECT ename,sal,comm INTO

:emp_record FROM emp WHERE empno= :no;
          printf("雇员名: %s, 实发工资

: %6.2f\n",emp_record.name,emp_record.salary+emp_record.comm);
  }
}

/**************************************************************
* Function:

err_report
* Description: This routine prints out the most recent error
**************************************************************/
void      

err_report(sqlca)
struct sqlca sqlca;
{
  if (sqlca.sqlcode < 0)
   

printf("\n%.*s\n\n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);
  

exit(1);
}

/************************************************************
*

Function: logon
* Description: Logs on to the database as USERNAME/PASSWORD
************************************************************/
void      

logon(ctx,connect_info)
sql_context ctx;
char * connect_info;
{
  EXEC SQL

WHENEVER SQLERROR DO err_report(sqlca);
  EXEC SQL CONTEXT USE :ctx;
  EXEC

SQL CONNECT :connect_info;
  printf("Connected!\n";
}

/***************************************************
* Function: logoff
*

Description: This routine logs off the database
***************************************************/
void      logoff(ctx)
sql_context ctx;
{
  EXEC SQL WHENEVER SQLERROR DO err_report(sqlca);
  

EXEC SQL CONTEXT USE :ctx;
  EXEC SQL COMMIT WORK RELEASE;
  printf("Logged

off!\n";
}

main()
{
  time_t start;
  int i=0;
  while(1)
  {
       

main_test();
        sleep(3);
  }
}

数据库端的表数据为:     
         EMPNO ENAME           SAL     COMM
---------- ---------- -------- --------
       101 kilter      4100.00  2000.00
       102 jorhn       4100.00  1000.00
       103 kiven       3100.00   500.00
       104 yangzi      3100.00   500.00
       105 mafeng      2500.00   500.00
       106 guli        3500.00   800.00

论坛徽章:
0
2 [报告]
发表于 2004-02-07 14:16 |只看该作者

熟悉pro*c/c++和多线程的大哥大姐,父老乡亲们,帮我看看程序

可以直接给我发邮件:jiangyc@wellhope.sh谢谢各位了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP