2010년 3월 22일 월요일

Proc Thread 연동

 

cpdemo1.pc

/*
*  cpdemo1.pc
*
* Description:
*            The program creates as many sessions as there are threads.
*      Each thread connects to the default database and executes 5 times
*      SELECT statement. And each thread have its own runtime contexts.
*
*/

#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 "hr/hr"
#define THREADS  40

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

struct timeval tp1;
struct timeval tp2;

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

main()
{
  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[i];
    logon(ctx[i],CONNINFO);
  }

  /*Spawn threads*/
  for(i=0;i<THREADS;i++)
  {
    params[i].ctx=ctx[i];
    params[i].thread_id=i;

    if (pthread_create(&thread_id[i],pthread_attr_default,
      (pthread_startroutine_t)do_transaction,
      (pthread_addr_t) &params[i]))
      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[i],&status))
      printf("Error when waiting for thread % to terminate\n", i);
    else
      printf("stopped\n");

    printf("Detach thread...");
    if (pthread_detach(thread_id[i]))
      printf("Error detaching thread! \n");
    else
      printf("Detached!\n");
    if(i==THREADS-1)
    {
      logoff(ctx[i]);
      EXEC SQL CONTEXT FREE :ctx[i];
    }

  }

  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;

  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;
  EXEC SQL SELECT count(*) into :src_count from EMPLOYEES;
  EXEC SQL SELECT count(*) into :src_count from EMPLOYEES;
  EXEC SQL SELECT count(*) into :src_count from EMPLOYEES;
  EXEC SQL SELECT count(*) into :src_count from EMPLOYEES;
  EXEC SQL SELECT count(*) into :src_count from EMPLOYEES;
}

/**************************************************************
* 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");
}

댓글 없음:

댓글 쓰기