熟悉pro*c/c++和多线程的大哥大姐,父老乡亲们,帮我看看程序
我在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) ¶ms))
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
小弟先谢谢各位了。
去oracle版本看,多线程的PROC程序需要某些东西。
大哥?需要什么呀?你能不能发给资料给我呀?油箱:jiangyc@wellhope.sh