#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <string.h>

#define MAXLINE 99999
#define MAX_SOCK 64

int getmax(int);
void disconnect_client(int); /* 채팅 탈퇴 처리 함수 */
void my_signal(int signo); /* 새로운 시그널 처리 함수 선언 */

int maxfdp1;            /* 최대 소켓번호+1 */
int num_chat = 0;       /* 채팅 참가자 수 */
int ____main_socket;                  /* 초기 소켓 */
int ____client_socket[MAX_SOCK]; /* 채팅에 참가자 소켓번호 목록 */

int main(int argc, char *argv[]) 
{
char rline[MAXLINE+1];
int i, j, kk;
int recv_len;
int client_fd, clilen;

int recv_len_sum=0;
int recv_seq_sum=0;
int recv_divide;


fd_set read_fds; /* 일기 감지할 소켓번호 구조체 */
struct sockaddr_in client_addr, server_addr;

if (argc != 2) {
printf("Usage: %s port\n", argv[0]);
exit(0);
}

/* 초기소켓 생성 */
if ((____main_socket = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
printf("Server: Can't open stream socket.");
exit(0);
}

/* server_addr 구조체의 내용 세팅 */
bzero((char *)&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(atoi(argv[1]));

if (bind(____main_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
printf("Server: Can't bind local address.\n");
exit(0);
}

if (signal(SIGINT, my_signal) == SIG_ERR) { /* Ctrl+C */
printf("Server: signal(SIGINT) error\n");
exit(0);
}
if (signal(SIGTERM, my_signal) == SIG_ERR) { /* software termination */
printf("Server: signal(SIGTERM) error\n");
exit(0);
}
if (signal(SIGQUIT, my_signal) == SIG_ERR) { /* Ctrl+\ */
printf("Server: signal(SIGQUIT) error\n");
exit(0);
}


/* 클라이언트로부터 연결요청을 기다림 */
listen(____main_socket, 5); /* backlog = 5 */

maxfdp1 = ____main_socket + 1; /* 최대 소켓번호+1 */

while (1) 
{
FD_ZERO(&read_fds);
FD_SET(____main_socket, &read_fds);
for (kk=0; kk < num_chat; kk++)
{
    FD_SET(____client_socket[kk], &read_fds);
}

maxfdp1 = getmax(____main_socket) + 1; /* maxfdp1 재 계산 */

if (select(maxfdp1, &read_fds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) < 0) {
printf("Server: select error <= 0\n");
break;
}

if (FD_ISSET(____main_socket, &read_fds)) 
{
clilen = sizeof(client_addr);
client_fd = accept(____main_socket, (struct sockaddr *)&client_addr, &clilen);
if (client_fd == -1) 
{
    printf("Server: accept error\n");
    break;
    }

    ____client_socket[num_chat] = client_fd;
    num_chat++;
}

for (kk=0; kk < num_chat; kk++) 
{
if (FD_ISSET(____client_socket[kk], &read_fds)) 
{
if ((recv_len = recv(____client_socket[kk], rline, MAXLINE, 0)) <= 0) 
{
disconnect_client(kk); /* abrupt exit */
continue;
}
rline[recv_len] = '\0';

            recv_divide = recv_len / getfromframeid(rline, recv_len);
recv_seq_sum++;
recv_len_sum = recv_len_sum + recv_divide;

printf("SESSION SEQ[%07d]/RECV LENGTH[%07d]/DIVIDE[%07d]/REAL SUM[%07d]/SEQ SUM[%07d]\n", 
                                        kk+1, recv_len,
recv_divide, recv_len_sum, recv_seq_sum);
}
}
} /* while */
my_signal(SIGIO);
}

/* 채팅 탈퇴 처리 */
void disconnect_client(int i) {
struct sockaddr_in client_addr;

close(____client_socket[i]);
if (i != num_chat-1)
____client_socket[i] = ____client_socket[num_chat-1];
num_chat--;

printf("[function]disconnect_client>>[%d]\n", num_chat);
}

/* ____client_socket[] 내의 최대 소켓번호 얻기(초기치는 k) */
int getmax(int k) {
int max = k;
int r;
for (r=0; r < num_chat; r++)
if (____client_socket[r] > max)
max = ____client_socket[r];
return max;
}

/* 시그널 처리 함수 정의 */
void my_signal(int signo) {
int i;

signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
signal(SIGQUIT, SIG_IGN);

/* 모든 열려있는 소켓을 닫는다 */
close(____main_socket);
for (i=0; i < num_chat; i++)
close(____client_socket[i]);

exit(0);
}

int getfromframeid(char *tmp, int len)
{
int kk=0;

if(memcmp(tmp, "A0011", 5) == 0) kk=800;

return kk;
}


extern char design[6][2+1];
extern int ____hexa[MAPY][MAPX];
extern int hexa[MAPY][MAPX];

 

 

 

void gotoxy(int x, int y)
{
    COORD Pos = {x - 1, y - 1};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
}

void f_display_all(void)
{
    int i, k;

    for(i=0; i<MAPY; i++)
    {
        for(k=0; k<MAPX; k++)
        {
            if(hexa[i][k] == 10)
            {
                continue;
            }
            else if(hexa[i][k] == 0)
            {

                gotoxy(k * 2 + 1, i + 2);
                printf("□");

            }
            else
            {
               gotoxy(k * 2 + 1, i + 2);
               printf("%s", design[ hexa[i][k] ]);
            }
        }
    }


#if(1)
for(i=0; i<MAPY; i++)
    {
        for(k=0; k<MAPX; k++)
        {
____hexa[i][k] = hexa[i][k];
}
}
#endif
}

void f_display(int x, int y)
{
    int i, k;

#if(1)
    for(i=0; i<MAPY; i++)
    {
        for(k=0; k<MAPX; k++)
        {
            if(hexa[i][k] == 10)
            {
                continue;
            }
else if(hexa[i][k] == ____hexa[i][k]) continue;
            else if(hexa[i][k] == 0)
            {

                gotoxy(k * 2 + 1, i + 2);
                printf("□");

            }
            else
            {
               gotoxy(k * 2 + 1, i + 2);
               printf("%s", design[ hexa[i][k] ]);
            }
        }
    }
#endif

#if(1)
for(i=0; i<MAPY; i++)
    {
        for(k=0; k<MAPX; k++)
        {
____hexa[i][k] = hexa[i][k];
}
}
#endif
}

#if(1)
    if(____type==0)  //처음 한번은 전체출력으로 하고, 다음부터는 변한부분만 출력하도록 한다.
{
        for(i=0; i<MAPY; i++)
{
            for(k=0; k<MAPX; k++)
                {
if(hexa[i][k] == 10)
{
continue;
}
else if(hexa[i][k] == 0)
{
TextOut(hdc, screenxpos + 8 * 2 * k, screenypos + 8 * 2 * i, "□", 2);
}
else
{
TextOut(hdc, screenxpos + 8 * 2 * k, screenypos + 8 * 2 * i, design[ hexa[i][k] ], 2);
}
}
}
____type=1;
}
else
{
for(i=0; i<MAPY; i++)
{
for(k=0; k<MAPX; k++)
{
if(hexa[i][k] == ____hexa[i][k]) continue;
else if(hexa[i][k] == 0)
{
TextOut(hdc, screenxpos + 8 * 2 * k, screenypos + 8 * 2 * i, "□", 2);
}
else
{
TextOut(hdc, screenxpos + 8 * 2 * k, screenypos + 8 * 2 * i, design[ hexa[i][k] ], 2);
}
}
}
}
#endif

#include <time.h>
struct tm *localtime_r(const time_t *timep, struct tm *result);



----DESCRIPTION
localtime_r 함수는 time_t type의 초단위의 시간을 지역시간(locale정보)의 struct tm type으로 변환하는 함수입니다.
localtime()함수가 변환된 결과를 local static변수에 저장하는것과 달리 localtime_r()함수는 data를 변환한 결과값을
result에 저장하기때문에 multi-thread에서도 안전한 thread-safe함수입니다.





#include <time.h>
#include <stdio.h>

struct tm *localtime_r(const time_t *timep, struct tm *result);

int main(int argc, char **argv[])
{
    time_t t;
    struct tm lt;
 
    if((t=time(NULL))==-1) {
        printf("time() call error\n");
        return -1;
    }
    if(localtime_r(&t, &lt)==NULL) {
        printf("localtime_r() call error\n");
        return -1;
    }
    printf("NOW TIME:%04d-%02d-%02d %02d:%02d:%02d\n",
    lt.tm_year + 1900,
    lt.tm_mon + 1,
    lt.tm_mday,
    lt.tm_hour,
    lt.tm_min,
    lt.tm_sec);
 
    return 0;

/* 

 * printf에서 자주사용되고, 유용한 *에 대해서 알아본다.

 * printf에서 자주사용되고, 유용한 *에 대해서 알아본다.

 * printf에서 자주사용되고, 유용한 *에 대해서 알아본다.

 */

 

#include <stdio.h>

 

struct TESTSCORE_ST

{

    char Number[5];

    char Name[20];

char KorScore[10];

char EngScore[10];

char MathScore[10];

};

 

//구조체변수 초기화및 데이타 세팅하기

struct TESTSCORE_ST score[7] = {

{"1001", "Park", "90.2", "65.43", "56.99"},

{"1002", "Lee", "90.2", "65.43", "56.99"},

{"1003", "Choi", "90.2", "65.43", "56.99"},

{"1004", "Jung", "90.2", "65.43", "56.99"},

{"1005", "Son", "90.2", "65.43", "56.99"},

{"1006", "Nam", "90.2", "65.43", "56.99"},

{"1007", "Kim", "90.2", "65.43", "56.99"} };

 

int main(void)

{

    int kk;

 

for(kk=0; kk<7; kk++)

{

        //LEFT정렬,ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

        printf("[%-*s]"

           "[%-*s]"

          "[%-*s]"

   "[%-*s]"

   "[%-*s]",

      sizeof(score[kk].Number), score[kk].Number,

      sizeof(score[kk].Name), score[kk].Name,

      sizeof(score[kk].KorScore), score[kk].KorScore,

      sizeof(score[kk].EngScore), score[kk].EngScore,

      sizeof(score[kk].MathScore), score[kk].MathScore);

printf("\n");

        //RIGHT정렬,,ㅡㅡㅡㅡㅡㅡㅡㅡ

printf("[%*s]"

        "[%*s]"

       "[%*s]"

       "[%*s]"

       "[%*s]",

      sizeof(score[kk].Number), score[kk].Number,

      sizeof(score[kk].Name), score[kk].Name,

      sizeof(score[kk].KorScore), score[kk].KorScore,

      sizeof(score[kk].EngScore), score[kk].EngScore,

      sizeof(score[kk].MathScore), score[kk].MathScore);

printf("\n");

printf("[%s]"

           "[%s]"

   "[%s]"

   "[%s]"

          "[%s]",

      score[kk].Number,

      score[kk].Name,

      score[kk].KorScore,

      score[kk].EngScore,

      score[kk].MathScore);

        printf("\n");

}

return 0;

}

 

/*RESULT

Microsoft Windows [Version 6.1.7601]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

 

[1001 ][Park                ][90.2      ][65.43     ][56.99     ]

[ 1001][                Park][      90.2][     65.43][     56.99]

[1001][Park][90.2][65.43][56.99]

[1002 ][Lee                 ][90.2      ][65.43     ][56.99     ]

[ 1002][                 Lee][      90.2][     65.43][     56.99]

[1002][Lee][90.2][65.43][56.99]

[1003 ][Choi                ][90.2      ][65.43     ][56.99     ]

[ 1003][                Choi][      90.2][     65.43][     56.99]

[1003][Choi][90.2][65.43][56.99]

[1004 ][Jung                ][90.2      ][65.43     ][56.99     ]

[ 1004][                Jung][      90.2][     65.43][     56.99]

[1004][Jung][90.2][65.43][56.99]

[1005 ][Son                 ][90.2      ][65.43     ][56.99     ]

[ 1005][                 Son][      90.2][     65.43][     56.99]

[1005][Son][90.2][65.43][56.99]

[1006 ][Nam                 ][90.2      ][65.43     ][56.99     ]

[ 1006][                 Nam][      90.2][     65.43][     56.99]

[1006][Nam][90.2][65.43][56.99]

[1007 ][Kim                 ][90.2      ][65.43     ][56.99     ]

[ 1007][                 Kim][      90.2][     65.43][     56.99]

[1007][Kim][90.2][65.43][56.99]

----------------------------------------------------------------------------*/

 

 

 

   

mysql> select @r:=@r+1 As rowNum, T2.t_regtime,T2.t_cnt from DUMMY T1, (select substr(regtime,1,17) as t_regtime, count(1) as t_cnt from RB_OCUS_POS_TEST2 group by substr(regtime,1,17)) T2, (Select @r:=0)r;
+--------+-------------------+-------+
| rowNum | t_regtime         | t_cnt |
+--------+-------------------+-------+
|      1 | 20190829 12:42:03 |   669 |
|      2 | 20190829 12:42:04 |  3021 |
|      3 | 20190829 12:42:05 |  5429 |
|      4 | 20190829 12:42:06 |  5005 |
|      5 | 20190829 12:42:07 |  3661 |
|      6 | 20190829 12:42:08 |  3310 |
|      7 | 20190829 12:42:09 |  2807 |
|      8 | 20190829 12:42:10 |  3950 |
|      9 | 20190829 12:42:11 |  4715 |
|     10 | 20190829 12:42:12 |  5130 |
|     11 | 20190829 12:42:13 |  4318 |
|     12 | 20190829 12:42:14 |  2918 |
|     13 | 20190829 12:42:15 |  2980 |
|     14 | 20190829 12:42:16 |  4156 |
|     15 | 20190829 12:42:17 |  5870 |
|     16 | 20190829 12:42:18 |  5236 |
|     17 | 20190829 12:42:19 |  5546 |
|     18 | 20190829 12:42:20 |  5367 |
|     19 | 20190829 12:42:21 |  3181 |
|     20 | 20190829 12:42:22 |  2807 |
|     21 | 20190829 12:42:23 |  2554 |
|     22 | 20190829 12:42:24 |  1379 |
|     23 | 20190829 12:42:25 |  4924 |
|     24 | 20190829 12:42:26 |  4724 |
|     25 | 20190829 12:42:27 |  5641 |
|     26 | 20190829 12:42:28 |  5661 |
|     27 | 20190829 12:42:29 |  3936 |
|     28 | 20190829 12:42:30 |  3557 |
|     29 | 20190829 12:42:31 |  4186 |
|     30 | 20190829 12:42:32 |  4821 |
|     31 | 20190829 12:42:33 |  4621 |
|     32 | 20190829 12:42:34 |  4697 |
|     33 | 20190829 12:42:35 |  3264 |
|     34 | 20190829 12:42:36 |  3706 |
|     35 | 20190829 12:42:37 |  2423 |
|     36 | 20190829 12:42:38 |  2043 |
|     37 | 20190829 12:42:39 |  4207 |
|     38 | 20190829 12:42:40 |  3524 |
|     39 | 20190829 12:42:41 |  4656 |
|     40 | 20190829 12:42:42 |  3582 |
|     41 | 20190829 12:42:43 |  2995 |
|     42 | 20190829 12:42:44 |   120 |
+--------+-------------------+-------+
42 rows in set (0.25 sec)

mysql>


 

다음과 같은 테이블이 존재한다. (테이블명 : ISS_HH)

SEQ_NO(PK) USER_ID INFO1 INFO2
1 10001 HH KK
2 10002 HH KK
3 10003 HH KK
4 10004 HH KK
5 10005 HH KK
6 10006 HH KK
7 10007 HH KK
8 10008 HH KK
9 10009 HH KK
10 10010 HH KK
11 10011 HH KK
12 10012 HH KK

 

위의 SEQ_NO를 1씩 증가시키기 위해서는 보통 아래와 같은 SQL을 생각하기 쉽다.

 

SQL>UPDATE ISS_HH SET SEQ_NO = SEQ_NO + 1 WHERE USER_ID > 10000

 

그러나 위를 실행하면, DUPLICATE ERROR가 발생한다. 이유는 멀까? 

 

PK인 SEQ_NO에 UPDATE시에 데이타가 중복으로 발생하게 된다. 즉 이런 경우는 가장큰 SEQ_NO를 증가시키는것이 일반적이다.

 

>>>>>>>>>OK QUERY

SQL>UPDATE ISS_HH SET SEQ_NO = SEQ_NO + 1 WHERE USER_ID > 10000 ORDER BY SEQ_NO DESC

 

-1 을 할경우는 반대로 생각하면 된다.

SQL>UPDATE ISS_HH SET SEQ_NO = SEQ_NO -1 WHERE USER_ID > 10000 ORDER BY SEQ_NO ASC

 

 

비고) 한번에 처리하지 않고, 하나씩 처리하는 프로그램으로 작성해보라,ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

비고) 한번에 처리하지 않고, 하나씩 처리하는 프로그램으로 작성해보라,ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

비고) 한번에 처리하지 않고, 하나씩 처리하는 프로그램으로 작성해보라,ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

비고) 한번에 처리하지 않고, 하나씩 처리하는 프로그램으로 작성해보라,ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

 

 

1. RB_OCUS_POS_TEST2테이블에 200,00개의 데이타를 넣는다.

2. 1초에 3,000개 이상의 데이타가 들어가는 횟수를 알고 싶다면?

3. 아래는 위의 문제를 구하기 위해서 구성한 쿼리이다.

 

 

mysql> show create table RB_OCUS_POS_TEST2;
+-------------------+-----------------------------------
| Table             | Create

+-------------------+-----------------------------------
| RB_OCUS_POS_TEST2 | CREATE TABLE `RB_OCUS_POS_TEST2` (
  `seqno` int(11) NOT NULL,
  `account_no` varchar(10) NOT NULL,
  `stock_symbol` varchar(20) NOT NULL,
  `start_volume` varchar(10) DEFAULT NULL,
  `start_amount` varchar(13) DEFAULT NULL,
  `start_price` varchar(13) DEFAULT NULL,
  `stock_type` varchar(2) DEFAULT NULL,
  `trustee_id` varchar(2) DEFAULT NULL,
  `regtime` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`seqno`,`account_no`,`stock_symbol`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------------------+-----------------------------------
1 row in set (0.00 sec)

mysql>

mysql> desc RB_OCUS_POS_TEST2;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| seqno        | int(11)     | NO   | PRI | NULL    |       |
| account_no   | varchar(10) | NO   | PRI | NULL    |       |
| stock_symbol | varchar(20) | NO   | PRI | NULL    |       |
| start_volume | varchar(10) | YES  |     | NULL    |       |
| start_amount | varchar(13) | YES  |     | NULL    |       |
| start_price  | varchar(13) | YES  |     | NULL    |       |
| stock_type   | varchar(2)  | YES  |     | NULL    |       |
| trustee_id   | varchar(2)  | YES  |     | NULL    |       |
| regtime      | varchar(30) | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+
9 rows in set (0.00 sec)

mysql>

mysql> select * from RB_OCUS_POS_TEST2 limit 10;
+-------+------------+----------------------+--------------+---------------+---------------+------------+------------+------------------------+
| seqno | account_no | stock_symbol         | start_volume | start_amount  | start_price   | stock_type | trustee_id | regtime                |
+-------+------------+----------------------+--------------+---------------+---------------+------------+------------+------------------------+
|     1 | 0016100    | BBL                  | 0000001500   | 0000266962.22 | 000177.974813 | 02         |            | 20190828 11:19:25(140) |
|     2 | 0016100    | BPP                  | 0000002000   | 0000042000.00 | 000021.000000 | 02         |            | 20190828 11:19:25(141) |
|     3 | 0016100    | CK                   | 0000015375   | 0000296816.51 | 000019.305139 | 02         |            | 20190828 11:19:25(142) |
|     4 | 0016100    | IEC                  | 0012286603   | 0000308909.54 | 000000.025142 | 02         |            | 20190828 11:19:25(142) |
|     5 | 0016100    | IVL                  | 0000011769   | 0000309729.03 | 000026.317362 | 02         |            | 20190828 11:19:25(142) |
|     6 | 0016100    | KTB                  | 0000005000   | 0000103785.51 | 000020.757102 | 02         |            | 20190828 11:19:25(142) |
|     7 | 0055920    | ASAP                 | 0000001000   | 0000003106.87 | 000003.106870 | 02         |            | 20190828 11:19:25(142) |
|     8 | 0055920    | BCP                  | 0000001000   | 0000033574.22 | 000033.574220 | 02         |            | 20190828 11:19:25(142) |
|     9 | 0055920    | CBG                  | 0000000800   | 0000042844.70 | 000053.555875 | 02         |            | 20190828 11:19:25(143) |
|    10 | 0055920    | CHAYO                | 0000002000   | 0000008739.30 | 000004.369650 | 02         |            | 20190828 11:19:25(143) |
+-------+------------+----------------------+--------------+---------------+---------------+------------+------------+------------------------+
10 rows in set (0.00 sec)

mysql>

mysql> select T2.t_cnt from DUMMY T1, (select substr(regtime,1,17) as t_regtime, count(1) as t_cnt from RB_OCUS_POS_TEST2 group by substr(regtime,1,17)) T2 limit 10;
+-------+
| t_cnt |
+-------+
|  2149 |
|  2556 |
|  3462 |
|  2635 |
|  2780 |
|  3051 |
|  2450 |
|  2310 |
|  2695 |
|  2716 |
+-------+
10 rows in set (0.22 sec)

mysql> 
mysql> select T2.t_cnt from DUMMY T1, (select substr(regtime,1,17) as t_regtime, count(1) as t_cnt from RB_OCUS_POS_TEST2 group by substr(regtime,1,17)) T2 where T2.t_cnt > 3000;
+-------+
| t_cnt |
+-------+
|  3462 |
|  3051 |
|  3192 |
|  3761 |
|  3085 |
|  3418 |
|  3341 |
|  3066 |
|  3353 |
|  3287 |
|  3037 |
+-------+
11 rows in set (0.23 sec)

mysql>

mysql> select * from DUMMY;
+-------+------------+----------------------+--------------+---------------+---------------+------------+------------+
| seqno | account_no | stock_symbol         | start_volume | start_amount  | start_price   | stock_type | trustee_id |
+-------+------------+----------------------+--------------+---------------+---------------+------------+------------+
|     1 | 0016100    | BANPU                | 0000045000   | 0000645569.57 | 000014.345990 | 02         |            |
+-------+------------+----------------------+--------------+---------------+---------------+------------+------------+
1 row in set (0.00 sec)

mysql>

 

mysql> select T2.t_regtime,T2.t_cnt from DUMMY T1, (select substr(regtime,1,17) as t_regtime, count(1) as t_cnt from RB_OCUS_POS_TEST2 group by substr(regtime,1,17)) T2 where T2.t_cnt > 3000;
+-------------------+-------+
| t_regtime         | t_cnt |
+-------------------+-------+
| 20190828 11:19:27 |  3462 |
| 20190828 11:19:30 |  3051 |
| 20190828 11:19:38 |  3192 |
| 20190828 11:19:46 |  3761 |
| 20190828 11:19:47 |  3085 |
| 20190828 11:19:55 |  3418 |
| 20190828 11:20:00 |  3341 |
| 20190828 11:20:03 |  3066 |
| 20190828 11:20:12 |  3353 |
| 20190828 11:20:15 |  3287 |
| 20190828 11:20:20 |  3037 |
+-------------------+-------+
11 rows in set (0.19 sec)

mysql>  

 

mysql> select T2.t_regtime,T2.t_cnt from DUMMY T1, (select substr(regtime,1,17) as t_regtime, count(1) as t_cnt from RB_OCUS_POS_TEST2 group by substr(regtime,1,17)) T2 where T2.t_cnt > 3000 order by T2.t_cnt desc;
+-------------------+-------+
| t_regtime         | t_cnt |
+-------------------+-------+
| 20190828 11:19:46 |  3761 |
| 20190828 11:19:27 |  3462 |
| 20190828 11:19:55 |  3418 |
| 20190828 11:20:12 |  3353 |
| 20190828 11:20:00 |  3341 |
| 20190828 11:20:15 |  3287 |
| 20190828 11:19:38 |  3192 |
| 20190828 11:19:47 |  3085 |
| 20190828 11:20:03 |  3066 |
| 20190828 11:19:30 |  3051 |
| 20190828 11:20:20 |  3037 |
+-------------------+-------+
11 rows in set (0.00 sec)

mysql>

 

 

'데이타베이스 > MySQL' 카테고리의 다른 글

MySQL ROWNUM(select @r:=@r+1 As rowNum)  (0) 2019.10.31
TIP - SEQ증감  (0) 2019.10.31
FUNCTION 생성및 활용  (0) 2019.10.31
LINUX,프로그래밍을 통한 PROCEDURE 호출후 결과  (0) 2019.10.30
#LINUX,common.c(MySQL)  (0) 2019.10.30

 

#####FUNCTION 생성

 

DROP FUNCTION IF EXISTS GETMASTER_SYMB;

DELIMITER $$
CREATE FUNCTION GETMASTER_SYMB(intput1 TEXT, intput2 TEXT) RETURNS TEXT
BEGIN

    DECLARE p_result    TEXT;
    DECLARE p_input1    TEXT;
    DECLARE p_input2    TEXT;

 

    SET p_input1 = intput1;
    SET p_input2 = intput2;

 

    SET p_result = (SELECT  SYMB FROM fssmaster WHERE ( COD2 = p_input1 OR COD1 = p_input2 ))
    ;

    RETURN p_result;
END $$
DELIMITER ;

 

 

#####FUNCTION 활용

C:\Users\kiwoom\Downloads>
C:\Users\kiwoom\Downloads>mysql -u root -p
Enter password: ***********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.1.35-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use fss;
Database changed
mysql> SELECT fss.GETMASTER_SYMB('4262', '4263');
+------------------------------------+
| fss.GETMASTER_SYMB('4262', '4263') |
+------------------------------------+
| AAV                                |
+------------------------------------+
1 row in set (0.02 sec)

mysql>
mysql>
mysql>

 

/*

 * gcc -c sample.c

 * gcc -o sample sample.o libmysql.lib

 */

 

#include "C:\Program Files\MySQL\MySQL Connector C 6.1\include\mysql.h"
#include <string.h>
#include <stdio.h>

#define DB_HOST "127.0.0.1"
#define DB_USER "root"
#define DB_PASS "eogksalsrnr"
#define DB_NAME "fss"
    
int main(void)
{
    MYSQL *connection=NULL, conn;
    MYSQL_RES *sql_result;
    MYSQL_ROW sql_row;
    int query_stat; 
    
    mysql_init(&conn);

    connection = mysql_real_connect(&conn, DB_HOST, DB_USER, DB_PASS, DB_NAME, 3306, (char *)NULL, 0);

    if (connection == NULL)
    {
        fprintf(stderr, "Mysql connection error : %s", mysql_error(&conn));
        return 1;
    }

    query_stat = mysql_query(connection, "SELECT fss.GETMASTER_SYMB('4262', '4263')");
    if (query_stat != 0)
    {
        fprintf(stderr, "Mysql query error : %s", mysql_error(&conn));
        return 1;
    }
    
    sql_result = mysql_store_result(connection);
    
    while ( (sql_row = mysql_fetch_row(sql_result)) != NULL )
    {
        printf(">>>>>>%s\n", sql_row[0]);
    }

    mysql_free_result(sql_result);

    mysql_close(connection);

    return 0;​
}
 

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

 

#define LEAP(y) ((y % 4 == 0 && y % 100 != 0 || y % 400 == 0) ? 1 : 0)

 

static void ha_ltona0(int idx, int length, char *answer);

static int ha_antol(char *request, int length);

static void cb1_compute_date (char *odate, char *idate, int addval);

static int getRemainDay(char *odate, char *idate);

 

int main(int argc, char *argv[])

{

    int rc,mm,dd;    char begin_date[10] = "20190101";

   char end_date [10];

   for(mm=1; mm<=12; mm++)

   {

      for(dd=1; dd<=31; dd++)

      {

         memset(end_date,0x00,sizeof(end_date));

         sprintf(end_date, "2019%.2d%.2d", mm, dd);

         rc = getRemainDay(end_date,begin_date);

         if(rc<0) printf("(%s)(%s):(%s)\n", begin_date, end_date, " is not valid day!! error");

         else printf("(%s)(%s):(%d)\n", begin_date, end_date, getRemainDay(end_date,begin_date));

      }

   }

for(mm=1; mm<=12; mm++) { for(dd=1; dd<=31; dd++)

{

memset(end_date,0x00,sizeof(end_date));

sprintf(end_date, "2019%.2d%.2d", mm, dd);

rc = getRemainDay(end_date,begin_date);

if(rc<0) printf("(%s)(%s):(%s)\n", begin_date, end_date, " is not valid day!! error");

}

}

   return 0;

}

 

/*- ------------------------------------------------ -*

 *- ha_ltona0

 *- ------------------------------------------------ -*/

void ha_ltona0(int idx, int length, char *answer)

{

    if(length==1)

    {

        sprintf(answer,"%.1d",idx);

    }

    else if(length==2)

    {

        sprintf(answer,"%.2d",idx);

    }

    else if(length==3)

    {

        sprintf(answer,"%.3d",idx);

    }

    else if(length==4)

    {

        sprintf(answer,"%.4d",idx);

    }

}

 

/*- ------------------------------------------------ -*

 *- ha_antol

 *- ------------------------------------------------ -*/

int ha_antol(char *request, int length)

{

char answer[100];

 

memset(answer,0x00,sizeof(answer));

 

memcpy(answer,request,length);

 

return(atoi(answer));

}

 

/*- ------------------------------------------------ -*

 *- cb1_compute_date

 *- ------------------------------------------------ -*/

void cb1_compute_date (odate, idate, addval)

char *odate;

char *idate;

int addval;

{

int yy;

int mm;

int dd;

int leap;

static int mmt[2] [13] =

{ {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},

   {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };

 

yy = ha_antol(idate, 4);

mm = ha_antol(idate+4, 2);

dd = ha_antol(idate+6, 2) + addval;

leap = LEAP(yy);

if (addval == 0) {

sprintf(odate, "%.8s", idate);

return;

    }

else

if (addval > 0) {

     for ( ; dd > mmt[leap][mm]; mm++)

     {

     dd -= mmt[leap] [mm];

     if (mm >= 12)

     {

         mm = 0;

         yy++;

         leap = LEAP(yy);

     }

     }

}

else {

     mm--;

     if (mm == 0)

     {

     mm = 12;

     yy--;

     leap = LEAP(yy);

     }

 

     for ( ; dd < 1; mm--)

     {

     dd += mmt [leap] [mm];

     if (mm <= 1)

     {

         mm = 13;

         yy--;

         leap = LEAP(yy);

     }

     }

     mm++;

     if (mm > 12)

     {

     mm = 1;

     yy++;

     }

}

 

ha_ltona0(yy, 4, odate);

ha_ltona0(mm, 2, odate+4);

ha_ltona0(dd, 2, odate+6);

}

 

/*- ------------------------------------------------ -*

 *- getRemainDay

 *- ------------------------------------------------ -*/

int getRemainDay(char *odate, char *idate)

{

int k;

char begin_date[10];

 

if(strcmp(odate,idate)<0) return(-1);

 

if(memcmp(odate,idate,strlen("YYYYMMDD"))==0) return 1;

 

    for(k=1; k<9999; k++)

{

     memset(begin_date,0x00,sizeof(begin_date));

     cb1_compute_date(begin_date,idate,k);

 

if(memcmp(begin_date,odate,strlen("YYYYMMDD"))==0) break;

}

 

if(k==9999) return(-1);

return k+1;

}

 

/*RESULT

Microsoft Windows [Version 6.1.7601]

Copyright (c) 2009 Microsoft Corporation. All rights reserved.

 

C:\Users\Downloads>10

,,

,,

,,

,,

(20190101)(20190818):(230)

(20190101)(20190819):(231)

(20190101)(20190820):(232)

(20190101)(20190821):(233)

(20190101)(20190822):(234)

(20190101)(20190823):(235)

(20190101)(20190824):(236)

(20190101)(20190825):(237)

(20190101)(20190826):(238)

(20190101)(20190827):(239)

(20190101)(20190828):(240)

(20190101)(20190829):(241)

(20190101)(20190830):(242)

(20190101)(20190831):(243)

(20190101)(20190901):(244)

(20190101)(20190902):(245)

(20190101)(20190903):(246)

(20190101)(20190904):(247)

(20190101)(20190905):(248)

(20190101)(20190906):(249)

(20190101)(20190907):(250)

(20190101)(20190908):(251)

(20190101)(20190909):(252)

(20190101)(20190910):(253)

(20190101)(20190911):(254)

(20190101)(20190912):(255)

(20190101)(20190913):(256)

(20190101)(20190914):(257)

(20190101)(20190915):(258)

(20190101)(20190916):(259)

(20190101)(20190917):(260)

(20190101)(20190918):(261)

(20190101)(20190919):(262)

(20190101)(20190920):(263)

(20190101)(20190921):(264)

(20190101)(20190922):(265)

(20190101)(20190923):(266)

(20190101)(20190924):(267)

(20190101)(20190925):(268)

(20190101)(20190926):(269)

(20190101)(20190927):(270)

(20190101)(20190928):(271)

(20190101)(20190929):(272)

(20190101)(20190930):(273)

(20190101)(20190931):( is not valid day!! error)

(20190101)(20191001):(274)

(20190101)(20191002):(275)

(20190101)(20191003):(276)

(20190101)(20191004):(277)

(20190101)(20191005):(278)

(20190101)(20191006):(279)

(20190101)(20191007):(280)

(20190101)(20191008):(281)

(20190101)(20191009):(282)

(20190101)(20191010):(283)

(20190101)(20191011):(284)

(20190101)(20191012):(285)

(20190101)(20191013):(286)

(20190101)(20191014):(287)

(20190101)(20191015):(288)

(20190101)(20191016):(289)

(20190101)(20191017):(290)

(20190101)(20191018):(291)

(20190101)(20191019):(292)

(20190101)(20191020):(293)

(20190101)(20191021):(294)

(20190101)(20191022):(295)

(20190101)(20191023):(296)

(20190101)(20191024):(297)

(20190101)(20191025):(298)

(20190101)(20191026):(299)

(20190101)(20191027):(300)

(20190101)(20191028):(301)

(20190101)(20191029):(302)

(20190101)(20191030):(303)

(20190101)(20191031):(304)

(20190101)(20191101):(305)

(20190101)(20191102):(306)

(20190101)(20191103):(307)

(20190101)(20191104):(308)

(20190101)(20191105):(309)

(20190101)(20191106):(310)

(20190101)(20191107):(311)

(20190101)(20191108):(312)

(20190101)(20191109):(313)

(20190101)(20191110):(314)

(20190101)(20191111):(315)

(20190101)(20191112):(316)

(20190101)(20191113):(317)

(20190101)(20191114):(318)

(20190101)(20191115):(319)

(20190101)(20191116):(320)

(20190101)(20191117):(321)

(20190101)(20191118):(322)

(20190101)(20191119):(323)

(20190101)(20191120):(324)

(20190101)(20191121):(325)

(20190101)(20191122):(326)

(20190101)(20191123):(327)

(20190101)(20191124):(328)

(20190101)(20191125):(329)

(20190101)(20191126):(330)

(20190101)(20191127):(331)

(20190101)(20191128):(332)

(20190101)(20191129):(333)

(20190101)(20191130):(334)

(20190101)(20191131):( is not valid day!! error)

(20190101)(20191201):(335)

(20190101)(20191202):(336)

(20190101)(20191203):(337)

(20190101)(20191204):(338)

(20190101)(20191205):(339)

(20190101)(20191206):(340)

(20190101)(20191207):(341)

(20190101)(20191208):(342)

(20190101)(20191209):(343)

(20190101)(20191210):(344)

(20190101)(20191211):(345)

(20190101)(20191212):(346)

(20190101)(20191213):(347)

(20190101)(20191214):(348)

(20190101)(20191215):(349)

(20190101)(20191216):(350)

(20190101)(20191217):(351)

(20190101)(20191218):(352)

(20190101)(20191219):(353)

(20190101)(20191220):(354)

(20190101)(20191221):(355)

(20190101)(20191222):(356)

(20190101)(20191223):(357)

(20190101)(20191224):(358)

(20190101)(20191225):(359)

(20190101)(20191226):(360)

(20190101)(20191227):(361)

(20190101)(20191228):(362)

(20190101)(20191229):(363)

(20190101)(20191230):(364)

(20190101)(20191231):(365)

(20190101)(20190229):( is not valid day!! error)

(20190101)(20190230):( is not valid day!! error)

(20190101)(20190231):( is not valid day!! error)

(20190101)(20190431):( is not valid day!! error)

(20190101)(20190631):( is not valid day!! error)

(20190101)(20190931):( is not valid day!! error)

(20190101)(20191131):( is not valid day!! error)

*/

 

#include <stdio.h>

 

#if(1)

#pragma pack(1)

struct PacketHeader {

    char flags;    // 1 BYTE

    int seq;       // 4 BYTE

};

#pragma pack()

#endif

 

#if(0)

struct PacketHeader {

    char flags;    // 1 BYTE

    int seq;       // 4 BYTE

} __attribute__((aligned(1), packed));

#endif

 

#if(0)

struct PacketHeader {

    char flags;    // 1 BYTE

    int seq;       // 4 BYTE

};

#endif

 

int main()

{

    struct PacketHeader header;

 

    printf("%d\n", sizeof(header.flags));

    printf("%d\n", sizeof(header.seq));  

    printf("%d\n", sizeof(header));      

 

    return 0;

}

 

//정상적인 경우는 1+4=8바이트의 크기로 구조체크기가 결정되지만

//#pragma pack(1)를 사용함으로써 구조체 정렬 크기조정할수 있다.

 

 

#include <stdio.h>

 

int main(void)

{

    printf("%.2f\n\n", 0.25676);

    return 0;

}

 

/*

결과:0.26

*/

'c 언어 > 초급과정' 카테고리의 다른 글

중복되지않는 문자출력  (0) 2020.08.26
printf () 함수 활용  (0) 2019.10.31
[C언어] Header File 한번만 포함하기.  (0) 2019.10.31
WINDOWS, 키보드 주므르기(102)  (0) 2019.10.31
WINDOWS, 키보드 주므르기(101)  (0) 2019.10.31

C언어로 프로그래밍을 하다보면 하나의 헤더파일이 여러 곳에서 참조될 때가 있다.

컴파일 시 해당 헤더파일이 여러번 중복되서 참조되는 상황을 막기 위해

 

#ifndef 와 #define이 사용된다.

 

예를 들어 test.h라는 헤더파일이 있다고 하자.

 

헤더파일의 가장 위에

 

#ifndef __TEST_H__

#define __TEST_H__

 

// 내용 작성

#define MAX_LEVEL 999

 

#endif

 

위와 같이 작성하게 되면 __TEST_H__이 상수화 되어 헤더파일 참조 시 해당 파일 참조되었으므로 

한번만 참조하게 된다.

 

#변수가 아닌이상, 여러번 참조해도 괜찮지만, 코드의 가독성및 사이즈에 영향을 미치게 된다.

#변수가 아닌이상, 여러번 참조해도 괜찮지만, 코드의 가독성및 사이즈에 영향을 미치게 된다.

#변수가 아닌이상, 여러번 참조해도 괜찮지만, 코드의 가독성및 사이즈에 영향을 미치게 된다.

#변수가 아닌이상, 여러번 참조해도 괜찮지만, 코드의 가독성및 사이즈에 영향을 미치게 된다.

#변수가 아닌이상, 여러번 참조해도 괜찮지만, 코드의 가독성및 사이즈에 영향을 미치게 된다.

#변수가 아닌이상, 여러번 참조해도 괜찮지만, 코드의 가독성및 사이즈에 영향을 미치게 된다.

#변수가 아닌이상, 여러번 참조해도 괜찮지만, 코드의 가독성및 사이즈에 영향을 미치게 된다.

#변수가 아닌이상, 여러번 참조해도 괜찮지만, 코드의 가독성및 사이즈에 영향을 미치게 된다.

#변수가 아닌이상, 여러번 참조해도 괜찮지만, 코드의 가독성및 사이즈에 영향을 미치게 된다.

 

 

 

 

#APACHE KAFKA 데이타마이그래이션 작업

 

> cd /home1/irteamsu/kafka2/kafka_2.11-1.1.0/

 

> base

> bin/kafka-server-stop.sh

> bin/zookeeper-server-stop.sh

 

########################cp -R /tmp/kafka-logs/ /home1/irteamsu/kafkadata/

 

cp /tmp/kafka-logs/20180416-0/* /home1/irteamsu/kafkadata/kafka-logs/20180416-0/

cp /tmp/kafka-logs/recovery-point-offset-checkpoint /home1/irteamsu/kafkadata/kafka-logs/

cp /tmp/kafka-logs/log-start-offset-checkpoint /home1/irteamsu/kafkadata/kafka-logs/

cp /tmp/kafka-logs/replication-offset-checkpoint /home1/irteamsu/kafkadata/kafka-logs/

 

 

> /home1/irteamsu/kafka2/kafka_2.11-1.1.0/config/server.properties 변경

 

> bin/zookeeper-server-start.sh config/zookeeper.properties &

> bin/kafka-server-start.sh config/server.properties &

 

 

#APACHE KAFKA 데이타마이그래이션 작업 확인

 

> bin/kafka-topics.sh --list --zookeeper localhost:2181

 

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$ netstat -an | grep 2181

tcp        0      0 0.0.0.0:2181            0.0.0.0:*               LISTEN

tcp        0      0 127.0.0.1:49964         127.0.0.1:2181          TIME_WAIT

tcp        0      0 127.0.0.1:35802         127.0.0.1:2181          ESTABLISHED

tcp        0      0 127.0.0.1:2181          127.0.0.1:35802         ESTABLISHED

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$

 

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$

[irteamsu@cdb021.imgr 20180509-0]$ netstat -an | grep 9092

tcp        0      0 0.0.0.0:9092            0.0.0.0:*               LISTEN

tcp        0     52 10.113.68.29:9092       10.113.68.28:59222      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59274      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59254      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59268      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59278      ESTABLISHED

tcp        0      0 127.0.0.1:9092          127.0.0.1:42516         ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59276      ESTABLISHED

tcp        0     44 10.113.68.29:9092       10.113.68.28:59246      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59272      ESTABLISHED

tcp        0     72 10.113.68.29:52474      10.113.68.29:9092       ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59234      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59270      ESTABLISHED

tcp        0     44 10.113.68.29:9092       10.113.68.28:59240      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59266      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59262      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59284      ESTABLISHED

tcp        0     52 10.113.68.29:9092       10.113.68.28:59227      ESTABLISHED

tcp        0     52 10.113.68.29:9092       10.113.68.28:59226      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.29:52474      ESTABLISHED

tcp        0     44 10.113.68.29:9092       10.113.68.28:59242      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59250      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59248      ESTABLISHED

tcp        0     52 10.113.68.29:9092       10.113.68.28:59252      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59446      ESTABLISHED

tcp        0     52 10.113.68.29:9092       10.113.68.28:59230      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59224      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59286      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59280      ESTABLISHED

tcp        1      0 10.113.68.29:52470      10.113.68.29:9092       CLOSE_WAIT

tcp        0      0 10.113.68.29:9092       10.113.68.28:59258      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59256      ESTABLISHED

tcp        0     44 10.113.68.29:9092       10.113.68.28:59244      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59232      ESTABLISHED

tcp        0     52 10.113.68.29:9092       10.113.68.28:59236      ESTABLISHED

tcp        0      0 127.0.0.1:42516         127.0.0.1:9092          ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59220      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59448      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59264      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:59238      ESTABLISHED

[irteamsu@cdb021.imgr 20180509-0]$

 

 

 

 

 

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$ bin/kafka-topics.sh --list --zookeeper localhost:2181

[2018-04-16 18:14:17,021] INFO Accepted socket connection from /127.0.0.1:54720 (org.apache.zookeeper.server.NIOServerCnxnFactory)

[2018-04-16 18:14:17,024] INFO Client attempting to establish new session at /127.0.0.1:54720 (org.apache.zookeeper.server.ZooKeeperServer)

[2018-04-16 18:14:17,025] INFO Established session 0x162cdbb20e00001 with negotiated timeout 30000 for client /127.0.0.1:54720 (org.apache.zookeeper.server.ZooKeeperServer)

20180411

20180412

20180413

20180414

20180415

20180416

20180417

20180418

20180419

20180420

20180421

20180422

20180423

20180424

20180425

20180426

20180427

20180428

20180429

20180430

20180431

 

[2018-04-16 18:14:17,066] INFO Processed session termination for sessionid: 0x162cdbb20e00001 (org.apache.zookeeper.server.PrepRequestProcessor)

[2018-04-16 18:14:17,067] INFO Closed socket connection for client /127.0.0.1:54720 which had sessionid 0x162cdbb20e00001 (org.apache.zookeeper.server.NIOServerCnxn)

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$ netstat -an | grep 2181

tcp        0      0 0.0.0.0:2181            0.0.0.0:*               LISTEN

tcp        0      0 127.0.0.1:54646         127.0.0.1:2181          ESTABLISHED

tcp        0      0 127.0.0.1:54720         127.0.0.1:2181          TIME_WAIT

tcp        0      0 127.0.0.1:2181          127.0.0.1:54646         ESTABLISHED

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$ netstat -an | grep 9092

tcp        0      0 0.0.0.0:9092            0.0.0.0:*               LISTEN

tcp        0      0 10.113.68.29:9092       10.113.68.28:45470      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45450      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45414      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45476      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45424      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45440      ESTABLISHED

tcp        0      0 127.0.0.1:9092          127.0.0.1:48314         ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45426      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45442      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.29:58272      ESTABLISHED

tcp        0      0 127.0.0.1:48314         127.0.0.1:9092          ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45462      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45436      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45432      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45458      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45474      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45446      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45434      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45472      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.29:58268      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45460      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45420      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45464      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45444      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45438      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45422      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45452      ESTABLISHED

tcp        0      0 10.113.68.29:58268      10.113.68.29:9092       ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45428      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45448      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45418      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45478      ESTABLISHED

tcp        0      0 10.113.68.29:58272      10.113.68.29:9092       ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45480      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45454      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45466      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45430      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.10:53490      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45456      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45468      ESTABLISHED

tcp        0      0 10.113.68.29:9092       10.113.68.28:45416      ESTABLISHED

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$

Linux - du (Disk Usage)

Syntax
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

SAMPLE>>>>
[irteamsu@cdb021.imgr kafka-logs]$ du -sch 2018*
4.0K    20180411-0
4.0K    20180412-0
8.0K    20180413-0
4.0K    20180414-0
4.0K    20180415-0
4.0K    20180416-0
4.0K    20180417-0
838M    20180418-0
8.0K    20180419-0
6.7G    20180420-0
1.1M    20180421-0
1.2M    20180422-0
17G     20180423-0
18G     20180424-0
18G     20180425-0
18G     20180426-0
18G     20180427-0
1.2M    20180428-0
1.1M    20180429-0
17G     20180430-0
0       20180431-0
1.1M    20180501-0
18G     20180502-0
17G     20180503-0
13G     20180504-0


3.1 - Options
3.1.1 - Data Filtering
-a, –all: get info for all files, not just directories
--max-depth=N: print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize
-X FILE, --exclude-from=FILE: Exclude files that match any pattern in FILE.
--exclude=PATTERN Exclude files that match PATTERN.
3.1.2 - Summarize
-s, --summarize display only a total for each argument
-c, --total: produce a grand total
3.1.3 - Calculation
-S, --separate-dirs do not include size of subdirectories
--apparent-size: print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in (“sparse”) files, internal fragmentation, indirect blocks, and the like

3.1.4 - Symlink
-l, --count-links: show sizes many times if hard linked
-L, --dereference: dereference all symbolic links
-P, --no-dereference: don’t follow any symbolic links (this is the default)
3.1.5 - Formatting
3.1.5.1 - Time
--time show time of the last modification of any file in the directory, or any of its subdirectories
--time=WORD show time as WORD instead of modification time: atime, access, use, ctime or status
--time-style=STYLE show times using style STYLE:
full-iso,
long-iso,
iso,
+FORMAT FORMAT is interpreted like ‘date’
3.1.5.2 - Newline
-0, --null: end each output line with 0 byte rather than newline
3.1.5.3 - Size
-h, --human-readable: print sizes in human readable format (e.g., 1K 234M 2G)
--si like -h, but use powers of 1000 not 1024
--block-size=SIZE: display the size as a multiple of SIZE. Shortcut
-b: bit --block-size=SIZE
-k: --block-size=1k
-m: --block-size=1M

Apache Kafka ( 아파치 카프카 ) 2.11-0.9.0.0 소개 및 설치하기

Apache Kafka ( 아파치 카프카 ) 
2.11-0.9.0.0 소개 및 설치하기

아파치 카프카(Apache Kafka)는 아파치 소프트웨어 재단이 스칼라로 개발한 오픈 소스 메시지 브로커 프로젝트이다. 
이 프로젝트는 실시간 데이터 피드를 관리하기 위해 통일된, 높은 스루풋의 낮은 레티어신을 지닌 플랫폼을 제공하는 것이 목표이다. 
요컨대 분산 트랜잭션 로그로 구성된, 상당히 확장 가능한 pub/sub 메시지 큐로 정의할 수 있으며, 스트리밍 데이터를 처리하기 위한 기업 인프라를 위한 고부가 가치 기능이다.
디자인은 트랜잭션 로그에 많은 영향을 받았다
아파치 카프카는 원래 링크드인이 개발한 것으로, 2011년 초에 최종적으로 오픈 소스화되었다. 2012년 10월 23일에는 아파치 인큐베이터로부터 완전히 빠져나왔다. 
2014년 11월 링크드인에서 카프카를 만들던 일부 엔지니어들이 카프카에 집중하기 위해 Confluent라는 새로운 회사를 창립하였다

카프카 설치는 간단합니다.

아파치 카프카 다운로드 링크
https://kafka.apache.org/downloads


이후 설치할 공간에서 압출을 푼 뒤

$tar zvxf kafka_2.11-0.9.0.0.tgz
$cd kafka_2.11-0.9.0.0

환경 설정을 진행합니다.
환경설정 파일은 config 폴더에 위치해 있습니다.

vi config/server.properties

그 후 사용할 Datadir 폴더와 
DataLogdir 폴더를 사용할 위치에 만들어 줍니다.
데이터가 큰 경우에는 OS에 구성하면 안되고 Data 영역에 사용하여야 합니다.

log.dirs 설정
만들어 준 위치를 넣어줍니다.

broker.id 를 설정해 줍니다.
유니크한 숫자를 브로커 마다 각각 입력하시면 됩니다.
주키퍼를 설치한 서버정보를 입력합니다.


설치를 안했을 경우 주키퍼를 실행하기 위해서
vi config/zookeeper.properties
zookeeper.connect=localhost:2181 (주키퍼를 설치한 서버)

주키퍼 설정을 해줍시다.

bin/zookeeper-server-start.sh config/zookeeper.properties
(주키퍼 실행 미리 설치 안했을 경우)

bin/kafka-server-start.sh config/server.properties 
(카프카 실행)

READY FILE

wget http://apache.mirror.cdnetworks.com/kafka/1.1.0/kafka_2.11-1.1.0.tgz 
wget http://apache.mirror.cdnetworks.com/zookeeper/zookeeper-3.4.11/zookeeper-3.4.11.tar.gz


KAFKA INSTALL(JAVA VERSION) - https://kafka.apache.org/quicksta
1. Download the 1.1.0 release and un-tar it. 
> tar -xzf kafka_2.11-1.1.0.tgz
> cd kafka_2.11-1.1.0


2. Start the server
2.1 Kafka uses ZooKeeper so you need to first start a ZooKeeper server if you don't already have one. You can use the convenience script packaged with kafka to get a quick-and-dirty single-node ZooKeeper instance.

> bin/zookeeper-server-start.sh config/zookeeper.properties

3. Step 3: Create a topic
3.1 Let's create a topic named "test" with a single partition and only one replica:

> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
> bin/kafka-server-start.sh config/server.properties
> for ii in `seq 20180411 20180431`; do bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic $ii; done
> cd /home1/irteamsu/kafka2/kafka_2.11-1.1.0
> bin/kafka-server-start.sh config/server.properties
> bin/kafka-topics.sh --list --zookeeper localhost:2181

4. Send some messages
4.1 Kafka comes with a command line client that will take input from a file or from standard input and send it out as messages to the Kafka cluster. By default, each line will be sent as a separate message.

Run the producer and then type a few messages into the console to send to the server.

> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
 
5. Start a consumer
5.1 Kafka also has a command line consumer that will dump out messages to standard output.

> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic 20180412 --from-beginning
 

[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$ netstat -an | grep 2181
tcp        0      0 0.0.0.0:2181            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:49964         127.0.0.1:2181          TIME_WAIT
tcp        0      0 127.0.0.1:35802         127.0.0.1:2181          ESTABLISHED
tcp        0      0 127.0.0.1:2181          127.0.0.1:35802         ESTABLISHED
[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$ netstat -an | grep 9092
tcp        0      0 0.0.0.0:9092            0.0.0.0:*               LISTEN
tcp        0      0 10.113.68.29:9092       10.113.68.28:50494      ESTABLISHED
tcp        0      0 10.113.68.29:9092       10.113.68.28:50516      ESTABLISHED
tcp        0      0 10.113.68.29:9092       10.113.68.28:50502      ESTABLISHED
tcp        0      0 10.113.68.29:9092       10.113.68.28:50504      ESTABLISHED
tcp        0      0 10.113.68.29:9092       10.113.68.28:50480      ESTABLISHED
tcp        0      0 127.0.0.1:43490         127.0.0.1:9092          ESTABLISHED
tcp        0      0 127.0.0.1:9092          127.0.0.1:43490         ESTABLISHED
tcp        0      0 10.113.68.29:9092       10.113.68.28:50520      ESTABLISHED
tcp        0      0 10.113.68.29:9092       10.113.68.28:50490      ESTABLISHED
tcp        0      0 10.113.68.29:9092       10.113.68.28:50481      ESTABLISHED
tcp        0     72 10.113.68.29:53448      10.113.68.29:9092       ESTABLISHED
tcp        0      0 10.113.68.29:9092       10.113.68.28:50508      ESTABLISHED
[irteamsu@cdb021.imgr kafka_2.11-1.1.0]$

/*------------------------------------------------------------
 * The Apache Kafka C/C++ library 
 *-----------------------------------------------------------/

[irteamsu@cdb021.imgr librdkafka]$ history
    1  2018-04-04_17:44:32\ ls
    2  2018-04-04_17:44:40\ cd
    3  2018-04-04_17:44:40\ ls
    4  2018-04-04_17:44:45\ cd install
    5  2018-04-04_17:44:49\ mkdir install
    6  2018-04-04_17:44:51\ cd install/
    7  2018-04-04_17:44:51\ ls
    8  2018-04-04_17:45:04\ mkdir kafka
    9  2018-04-04_17:45:05\ cd kafka/
   10  2018-04-04_17:45:06\ ls
   11  2018-04-04_17:45:10\ git clone https://github.com/edenhill/librdkafka
   12  2018-04-04_17:45:14\ ls
   13  2018-04-04_17:45:16\ cd librdkafka/
   14  2018-04-04_17:45:16\ ls
   15  2018-04-04_17:45:23\ ./configure
   16  2018-04-04_17:45:26\ make
   17  2018-04-04_17:46:08\ vi README.
   18  2018-04-04_17:46:13\ vi README.md
   19  2018-04-04_17:46:26\ sudo make install
   20  2018-04-04_17:49:19\ pwd
   21  2018-04-04_17:49:21\ history
[irteamsu@cdb021.imgr librdkafka]$ pwd
/home1/irteamsu/install/kafka/librdkafka

#include:/usr/local/include/librdkafka/
#lib:/usr/local/lib


ReadQueueApacheData: error while loading shared libraries: librdkafka.so.1: cannot open shared object file: No such file or directory
ReadQueueApacheData: error while loading shared libraries: librdkafka.so.1: cannot open shared object file: No such file or directory
ReadQueueApacheData: error while loading shared libraries: librdkafka.so.1: cannot open shared object file: No such file or directory

#프로파일에 저장
export LD_LIBRARY_PATH=/usr/local/lib

 

https://github.com/edenhill/librdkafka/blob/master/examples/rdkafka_simple_producer.c
https://github.com/edenhill/librdkafka/blob/master/examples/rdkafka_consumer_example.c

 

 

//구조체에 맞는 파일을 읽어들여서 해당변수로 정렬하는 프로그램(정렬시에 스택변수사용시)

//구조체에 맞는 파일을 읽어들여서 해당변수로 정렬하는 프로그램(정렬시에 스택변수사용시)

//구조체에 맞는 파일을 읽어들여서 해당변수로 정렬하는 프로그램(정렬시에 스택변수사용시)

//구조체에 맞는 파일을 읽어들여서 해당변수로 정렬하는 프로그램(정렬시에 스택변수사용시)

 

//CALL BY VALUE & CALL BY REFERENCE

//CALL BY VALUE & CALL BY REFERENCE

//CALL BY VALUE & CALL BY REFERENCE

//CALL BY VALUE & CALL BY REFERENCE

 

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

 

struct KSEMASTBATCH {

    char dataclass[2];

    char infoclass[2];

    char marketclass;

    char stockcode[12];

    char serialno[8];

    char abbrstockcode[9];

    char abbrstocknamekor[40];

    char abbrstocknameeng[40];

    char senddate[8];

    char infodivisiongroupno[5];

    char stockgroupid[2];

    char isunittrade;

    char exclasscode[2];

    char ff;

};

#define SZ_KSEMASTBATCH sizeof(struct KSEMASTBATCH)

 

int main(int argc, char *argv[])

{

    FILE *fp=NULL;

char rbuf[4096];

    struct KSEMASTBATCH *kse1[1024];

struct KSEMASTBATCH *kse2[1024];

struct KSEMASTBATCH *kse3[1024];

struct KSEMASTBATCH *kse_src[1024];

struct KSEMASTBATCH *kse_tmp;

struct KSEMASTBATCH kse_1_address[1024];

struct KSEMASTBATCH kse_2_address[1024];

int ii,kk,mm,ff,jj,cnt;

 

if(argc != 2)

{

     return(-1);

}

 

if((fp=fopen(argv[1], "rt"))==NULL) 

{

     return(-1);

}

 

kk=0;

while(1)

{

     memset(rbuf,0x00,sizeof(rbuf));

if(fgets(rbuf, sizeof(rbuf), fp)==NULL) break;

 

//STACK REGISTER & POINTER REGISTER

//STACK REGISTER & POINTER REGISTER

//STACK REGISTER & POINTER REGISTER

//STACK REGISTER & POINTER REGISTER

memcpy(&kse_1_address[kk], rbuf, SZ_KSEMASTBATCH);

memcpy(&kse_2_address[kk], rbuf, SZ_KSEMASTBATCH);

 

        //[kse1 == kse2 == kse3] SAME ADDRESS POINTER

        //[kse1 == kse2 == kse3] SAME ADDRESS POINTER

        //[kse1 == kse2 == kse3] SAME ADDRESS POINTER

        //[kse1 == kse2 == kse3] SAME ADDRESS POINTER

kse1[kk]     = &kse_1_address[kk];

kse2[kk]     = &kse_1_address[kk];

kse3[kk]     = &kse_1_address[kk];

kse_src[kk]  = &kse_2_address[kk];

 

kk++;

 

if(kk>1000) break;

}

cnt=kk;

if(fp != NULL) fclose(fp);

 

    //ASCENDING SORT BY SYMBOL

//ASCENDING SORT BY SYMBOL

//ASCENDING SORT BY SYMBOL

for(kk=0; kk<cnt; kk++)

{

for(ii=kk+1; ii<cnt; ii++)

{

if(memcmp(kse1[kk]->stockcode,kse1[ii]->stockcode,sizeof(kse1[ii]->stockcode))>0)

{

kse_tmp=NULL;

 

kse_tmp  = kse1[kk];

kse1[kk] = kse1[ii];

kse1[ii] = kse_tmp;

}

}

}

 

for(kk=0; kk<cnt; kk++)

{

       printf("(1)[%.4d],SYM[%.12s]>>>>[%x][%.12s],[%x][%.12s]\n", 

      kk+1, kse_1_address[kk].stockcode, kse1[kk]->stockcode, kse1[kk]->stockcode, kse_src[kk]->stockcode, 

        kse_src[kk]->stockcode);

}

for(kk=0; kk<cnt; kk++)

{

       printf("(2)[%.4d],SYM[%.12s]>>>>[%x][%.12s],[%x][%.12s]\n", 

      kk+1, kse_1_address[kk].stockcode, kse2[kk]->stockcode, kse2[kk]->stockcode, kse_src[kk]->stockcode, 

        kse_src[kk]->stockcode);

}

for(kk=0; kk<cnt; kk++)

{

       printf("(3)[%.4d],SYM[%.12s]>>>>[%x][%.12s],[%x][%.12s]\n", 

      kk+1, kse_1_address[kk].stockcode, kse3[kk]->stockcode, kse3[kk]->stockcode, kse_src[kk]->stockcode, 

        kse_src[kk]->stockcode);

}

return(0);

}

 

/*결과

C:\Users\Downloads>src_0419 A0011.dat

(1)[0001],SYM[HK0000050325]>>>>[162f21][HK0000050325],[9af21][HK0000050325]

(1)[0002],SYM[KR5707017R97]>>>>[163241][KR5707017R97],[9b241][KR5707017R97]

(1)[0003],SYM[KR5713017R38]>>>>[163561][KR5713017R38],[9b561][KR5713017R38]

(1)[0004],SYM[KR571301A1B4]>>>>[163881][KR571301A1B4],[9b881][KR571301A1B4]

(1)[0005],SYM[KR5713027R36]>>>>[163ba1][KR5713027R36],[9bba1][KR5713027R36]

(1)[0006],SYM[KR5713037R34]>>>>[163ec1][KR5713037R34],[9bec1][KR5713037R34]

(1)[0007],SYM[KR571901A586]>>>>[1641e1][KR571901A586],[9c1e1][KR571901A586]

(1)[0008],SYM[KR572501A3B6]>>>>[164501][KR572501A3B6],[9c501][KR572501A3B6]

(1)[0009],SYM[KR572502A3B5]>>>>[164821][KR572502A3B5],[9c821][KR572502A3B5]

(1)[0010],SYM[KR572503A3B4]>>>>[164b41][KR572503A3B4],[9cb41][KR572503A3B4]

(2)[0001],SYM[HK0000050325]>>>>[162f21][HK0000050325],[9af21][HK0000050325]

(2)[0002],SYM[KR5707017R97]>>>>[163241][KR5707017R97],[9b241][KR5707017R97]

(2)[0003],SYM[KR5713017R38]>>>>[163561][KR5713017R38],[9b561][KR5713017R38]

(2)[0004],SYM[KR571301A1B4]>>>>[163881][KR571301A1B4],[9b881][KR571301A1B4]

(2)[0005],SYM[KR5713027R36]>>>>[163ba1][KR5713027R36],[9bba1][KR5713027R36]

(2)[0006],SYM[KR5713037R34]>>>>[163ec1][KR5713037R34],[9bec1][KR5713037R34]

(2)[0007],SYM[KR571901A586]>>>>[1641e1][KR571901A586],[9c1e1][KR571901A586]

(2)[0008],SYM[KR572501A3B6]>>>>[164501][KR572501A3B6],[9c501][KR572501A3B6]

(2)[0009],SYM[KR572502A3B5]>>>>[164821][KR572502A3B5],[9c821][KR572502A3B5]

(2)[0010],SYM[KR572503A3B4]>>>>[164b41][KR572503A3B4],[9cb41][KR572503A3B4]

(3)[0001],SYM[HK0000050325]>>>>[162f21][HK0000050325],[9af21][HK0000050325]

(3)[0002],SYM[KR5707017R97]>>>>[163241][KR5707017R97],[9b241][KR5707017R97]

(3)[0003],SYM[KR5713017R38]>>>>[163561][KR5713017R38],[9b561][KR5713017R38]

(3)[0004],SYM[KR571301A1B4]>>>>[163881][KR571301A1B4],[9b881][KR571301A1B4]

(3)[0005],SYM[KR5713027R36]>>>>[163ba1][KR5713027R36],[9bba1][KR5713027R36]

(3)[0006],SYM[KR5713037R34]>>>>[163ec1][KR5713037R34],[9bec1][KR5713037R34]

(3)[0007],SYM[KR571901A586]>>>>[1641e1][KR571901A586],[9c1e1][KR571901A586]

(3)[0008],SYM[KR572501A3B6]>>>>[164501][KR572501A3B6],[9c501][KR572501A3B6]

(3)[0009],SYM[KR572502A3B5]>>>>[164821][KR572502A3B5],[9c821][KR572502A3B5]

(3)[0010],SYM[KR572503A3B4]>>>>[164b41][KR572503A3B4],[9cb41][KR572503A3B4]

*/

//kse1, kse2, kse3는 같은 주소를 공유하므로, kse1이 변경되면 kse2, kse3도 참조하는 주소가 함께 변경(공유)되어진다.

//kse1, kse2, kse3는 같은 주소를 공유하므로, kse1이 변경되면 kse2, kse3도 참조하는 주소가 함께 변경(공유)되어진다.

//kse1, kse2, kse3는 같은 주소를 공유하므로, kse1이 변경되면 kse2, kse3도 참조하는 주소가 함께 변경(공유)되어진다.

//kse1, kse2, kse3는 같은 주소를 공유하므로, kse1이 변경되면 kse2, kse3도 참조하는 주소가 함께 변경(공유)되어진다.

//kse1, kse2, kse3는 같은 주소를 공유하므로, kse1이 변경되면 kse2, kse3도 참조하는 주소가 함께 변경(공유)되어진다.

//kse1, kse2, kse3는 같은 주소를 공유하므로, kse1이 변경되면 kse2, kse3도 참조하는 주소가 함께 변경(공유)되어진다.

//kse1, kse2, kse3는 같은 주소를 공유하므로, kse1이 변경되면 kse2, kse3도 참조하는 주소가 함께 변경(공유)되어진다.

//kse1, kse2, kse3는 같은 주소를 공유하므로, kse1이 변경되면 kse2, kse3도 참조하는 주소가 함께 변경(공유)되어진다.

 

/*SAMPLE DATA:A0011.dat

A0011HK000005032500000001A900050  중국원양자원                            CHINA OCEAN                             2016020400002FSN0000N00N1N00NNNNN000000000137105    00NN    NNNNNNN NN NN NN000003305100000330500000228404100000000751338750500000429500000231500000231000000000000000000310020090522000000096359369N+000000000+000000N+000004765+000069NY00000000N0000000                                000000000000000000001125666856000Y0000700007000010000700001000N0000000000000000000000000000000100001                      HKD344NYYYYYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR5707017R9700000002F707017R9동양 강남대 1호                         TongYang Kangnam 1                      2016020400002BCN0000N00N1N00NNNNN                      N  07NNNNNNN NN NN NN000005000300000500000000000000000000000000000000000000650000000350000000352000000500000000000500020051209000000001010764N+000000000+000000 +000000000+000000 N00000000 00000002005091420170913                000000000000000000005053820000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR5713017R3800000003F713017R3산은건대사랑1-1                         KDBKonkukAsset1-1                       2016020400003BCN0000N00N1N00NNNNN                      N  03NNNNNNN NN NN NN000005070300000507000000000000000000000000000000000000659000000355000000351000000500000000000500020050520000000002039243N+000000000+000000 +000000000+000000 N00000000 00000002005032820230327                000000000000000000010196215000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR571301A1B400000004F71301A1BKDB월지급식안심튼튼제2호                KDB Anshim TeunTeun Fund No.2           2016020400004BCN0000N00N1N00NNNNN                      N  01NNNNNNN NN NN NN000000815300000081500000000000000000000000000000000000105500000057100000066000000100000000000100020120224000000010605957N+000000000+000000 +000000000+000000 N00000000 00000002011112820211128                000000000000000000010605957000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR5713027R3600000005F713027R3산은건대사랑1-2                         KDBKonkukAsset1-2                       2016020400005BCN0000N00N1N00NNNNN                      N  03NNNNNNN NN NN NN000005050300000505000000000000000000000000000000000000656000000354000000351000000500000000000500020050520000000000620572N+000000000+000000 +000000000+000000 N00000000 00000002005032820230327                000000000000000000003102860000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR5713037R3400000006F713037R3산은건대사랑2                           KDBKonkukAsset2                         2016020400005BCN0000N00N1N00NNNNN                      N  03NNNNNNN NN NN NN000005060300000506000000000000000000000000000000000000657000000355000000351000000500000000000500020050520000000002001000N+000000000+000000 +000000000+000000 N00000000 00000002005032820230327                000000000000000000010005000000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR571901A58600000007F71901A58KB서울햇빛발전소특별자산                KB Seoul Solar Special Asset            2016020400004BCN0000N00N1N00NNNNN                      N  07NNNNNNN NN NN NN000001015300000101500000000000000000000000000000000000131500000071500000070000000100000000000100020151022000000008180797N+000000000+000000 +000000000+000000 N00000000 00000002015081720180703                000000000000000000008180797000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR572501A3B600000008F72501A3B신한BNPP서울시지하철9호선1호            SHBNPP Seoul Metro 9 No.1               2016020400003BCN0000N00N1N00NNNNN                      N  07NNNNNNN NN NN NN000001020300000102000000000000000000000000000000000000132500000071500000070000000100000000000100020140221000000025000000N+000000000+000000 +000000000+000000 N00000000 00000002013112720180214                000000000000000000025000000000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR572502A3B500000009F72502A3B신한BNPP서울시지하철9호선2호            SHBNPP Seoul Metro 9 No.2               2016020400002BCN0000N00N1N00NNNNN                      N  07NNNNNNN NN NN NN000001050300000105000000000000000000000000000000000000136500000073500000070000000100000000000100020140221000000025000000N+000000000+000000 +000000000+000000 N00000000 00000002013112720190214                000000000000000000025000000000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR572503A3B400000010F72503A3B신한BNPP서울시지하철9호선3호            SHBNPP Seoul Metro 9 No.3               2016020400001BCN0000N00N1N00NNNNN                      N  07NNNNNNN NN NN NN000001000300000100000000000000000000000000000000000000130000000070000000070000000100000000000100020140221000000025000000N+000000000+000000 +000000000+000000 N00000000 00000002013112720200214                000000000000000000025000000000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR572504A3B300000011F72504A3B신한BNPP서울시지하철9호선4호            SHBNPP Seoul Metro 9 No.4               2016020400001BCN0000N00N1N00NNNNN                      N  07NNNNNNN NN NN NN000001020300000102000000000000000000000000000000000000132500000071500000070000000100000000000100020140221000000025000000N+000000000+000000 +000000000+000000 N00000000 00000002013112720210216                000000000000000000025000000000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR5737027S7500000012F737027S7PAM부동산3호                            PAM Real Estate 3                       2016020400001BCN0000N00N1N00NNNNN                      N  03NNNNNNN NN NN NN000005220300000522000000000000000000000000000000000000678000000366000000244000000500000000000500020090424000000027885679N+000000000+000000 +000000000+000000 N00000000 00000002006072020170220                000000000000000000139428395000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR574401722300000013F74401722맵스프런티어브라질1                     MiraeassetMAPSBrazilTrust 1             2016020400005BCN0000N00N1N00NNNNN                      N  03NNNNNNN NN NN NN000000480300000048000000000000000000000000000000000000062400000033600000020000000100000000000100020120511000000080000000N+000000000+000000 +000000000+000000 N00000000 00000002012021720181231                000000000000000000080000000000000N0000700000000000000000000000N0000000000000000000000000000000100000                      KRW410NNNNNYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR700002000800000014A000020  동화약품                                DongwhaPharm                            2016020400002STN0000N00N1N00NNNNN000009000032102    03YN    YNNNNNN NN NN NN000009510100000951000000161193900000001545183455000001235000000666000000665000000100000000000000019760324000000027931470N+000000177+005373N+000008225+000116NN00000080N0000008                                000000000000000000027931470000000Y0000700007000010000700001000N0000000000000000000000000000000100001                      KRW410NYYYYYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR700003000700000015A000030  우리은행                                Woori Bank                              2016020400005STN0000N00N1N00NNNNN000021022116401    61NY    YYYNNNY NN NN NN000008580100000858000000096828500000000832510939000001115000000601000000686000000500000000001707620141119000000676000000N+000001621+000529N+000026592+000032NN00000500N0000058                                000000000000000003381391855000000Y0000700007000010000700001000N0000000000000000000000000000000100001                      KRW410NYYYYYY   +00000000000 Y    NNNN000080000000  N N        Y0  YY                                                           NN                                                                                                                                                       

A0011KR700004000600000016A000040  KR모터스                                KR MOTORS                               2016020400005STN0000N00N1N00NNNNN000015000033109    03YN    YNNNNNN NN NN NN000001175100000117500000182254600000000215271575000000152500000082500000082000000050000000000000019760525000000175307764N+000000000+000000N+000000385+000305NY00000000N0000000                                000000000000000000087653882000000Y0000700007000010000700001000N0000000000000000000000000000000100001                      KRW410NYYYYYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR700005000500000017A000050  경방                                    Kyungbang                               2016020400002STN0000N00N1N00NNNNN000006000031301    82YN    YNNNNNN NN NN NN000203000100020300000000000158100000000031873900000026350000014250000014210000000500000000000000019560303000000002741527N+000004190+004845N+000236250+000086NN00000500N0000002                                000000000000000000013707635000000Y0000700007000010000700001000N0000000000000000000000000000000100001                      KRW410NYYYYYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR700006000400000018A000060  메리츠화재                              Meritz Insurance                        2016020400005STN0000N00N1N00NNNNN000021025116501    02NN    YNNNNNN NN NN NN000014800100001480000000010811800000000160339960000001920000001040000001036000000050000000000000019560702000000105963000N+000001131+001309N+000013190+000112NN00000380N0000026                                000000000000000000052981500000000Y0000700007000010000700001000N0000000000000000000000000000000100001                      KRW410NYYYYYY   +00000000000 N    YNNN000070000000  N N        Y0  NN                                                           NN                                                                                                                                                       

A0011KR700007000300000019A000070  삼양홀딩스                              SAMYANGHOLDINGS                         2016020400003STN0000N00N1N00NNNNN000026000137105    72NN    YNNNNNN NN NN NN000177000100017700000000002857900000000504358400000023000000012400000014160000000500000000000000019681227000000008564271N+000000327+054128N+000148290+000119NN00001500N0000008                                000000000000000000042821355000000Y0000700007000010000700001000N0000000000000000000000000000000100001                      KRW410NYYYYYY   +00000000000 N    NNNN000080000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR700007100100000020A000075  삼양홀딩스우                            SAMYANGHOLDINGS(1P)                     2016020400001STN0000N00N1N00NNNNN000026000137105    00NN    YNNNNNN NN NN NN000077800100007780000000000017700000000001351540000010100000005450000005446000000500000000000000019920221000000000304058N+000000000+000000Y+000000000+000000YN00001550N0000020                                000000000000000000001520290000000Y0000700007000010000700001001N0000000000000000000000000000000100001                      KRW410NYYYYYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

A0011KR700008000200000021A000080  하이트진로                              HITEJINRO                               2016020400004STN0000N00N1N00NNNNN000005000031101    72YN    YNNNNNN NN NN NN000030900100003090000000033727900000001036100460000004015000002165000002472000000500000000004100020091019000000070133611N+000000295+010475N+000018628+000166NN00001000N0000032                                000000000000000000363168055000000Y0000700007000010000700001000N0000000000000000000000000000000100001                      KRW410NYYYYYY   +00000000000 N    NNNN000080000000  N N        N0  YY                                                           NN                                                                                                                                                       

A0011KR700008200800000022A000087  하이트진로2우B                          HITEJINRO(2PB)                          2016020400004STN0000N00N1N00NNNNN000005000031101    00YN    YNNNNNN NN NN NN000019500100001950000000000148600000000002878700000002535000001365000001365000000500000000000500020110926000000001130138N+000000000+000000Y+000000000+000000YN00001050N0000054                                000000000000000000005650690000000Y0000700007000010000700001002N0000000000000000000000000000000100001                      KRW410NYYYYYY   +00000000000 N    NNNN000070000000  N N        N0  NN                                                           NN                                                                                                                                                       

*/

//구조체에 맞는 파일을 읽어들여서 해당변수로 정렬하는 프로그램(정렬시에 스택변수사용시)

//구조체에 맞는 파일을 읽어들여서 해당변수로 정렬하는 프로그램(정렬시에 스택변수사용시)

//구조체에 맞는 파일을 읽어들여서 해당변수로 정렬하는 프로그램(정렬시에 스택변수사용시)

//구조체에 맞는 파일을 읽어들여서 해당변수로 정렬하는 프로그램(정렬시에 스택변수사용시)

 

//CALL BY VALUE & CALL BY REFERENCE

//CALL BY VALUE & CALL BY REFERENCE

//CALL BY VALUE & CALL BY REFERENCE

//CALL BY VALUE & CALL BY REFERENCE

 

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

 

struct KSEMASTBATCH {

    char dataclass[2];

    char infoclass[2];

    char marketclass;

    char stockcode[12];

    char serialno[8];

    char abbrstockcode[9];

    char abbrstocknamekor[40];

    char abbrstocknameeng[40];

    char senddate[8];

    char infodivisiongroupno[5];

    char stockgroupid[2];

    char isunittrade;

    char exclasscode[2];

    char facevaluechangeclasscode[2];

    char isopenpricebasedstandardprice;

    char isrevaluationstockreason[2];

    char isstandardpricechangestock;

    char israndomendpossibillty;

    char ismarketalarmdangernotice;

    char marketalarmclasscode[2];

    char iscorporategovernancefine;

    char ismanagementstock;

    char isinsinceritypublicnewsappoint;

    char isbackdoorlisting;

    char istradestop;

    char indexbusinesstypelarge[3];

    char indexbusinesstypemedium[3];

    char indexbusinesstypesmall[3];

    char standardindustrycode[10];

    char businesstypekospi200;

    char listpricesizecode;

    char ismanufactureindustry;

    char iskrx100stock;

    char isdividendindexstock;

    char iscorporategovernanceindexstock;

    char investorganclasscode[2];

    char iskospi;

    char iskospi100;

    char iskospi50;

    char iskrxsectorindexcar;

    char iskrxsectorindexsemiconductor;

    char iskrxsectorindexbio;

    char iskrxsectorindexfinance;

    char iskrxsectorindexit;

    char iskrxsectorindexenergychemical;

    char iskrxsectorindexsteel;

    char iskrxsectorindexnecessary;

    char iskrxsectorindexmediacomm;

    char iskrxsectorindexconstruction;

    char iskrxsectorindexfinanceservice;

    char iskrxsectorindexstock;

    char iskrxsectorindexship;

    char standardprice[9];

    char ff;

};

#define SZ_KSEMASTBATCH sizeof(struct KSEMASTBATCH)
#define MAX_KSEMASTBATCH_CNT 1024

 

int main(int argc, char *argv[])

{

    FILE *fp=NULL;

char rbuf[4096];

    struct KSEMASTBATCH *kse1[MAX_KSEMASTBATCH_CNT];

struct KSEMASTBATCH *kse2[MAX_KSEMASTBATCH_CNT];

struct KSEMASTBATCH *kse3[MAX_KSEMASTBATCH_CNT];

struct KSEMASTBATCH *kse_src[MAX_KSEMASTBATCH_CNT];

struct KSEMASTBATCH *kse_tmp;

struct KSEMASTBATCH kse_1_address[MAX_KSEMASTBATCH_CNT];

struct KSEMASTBATCH kse_2_address[MAX_KSEMASTBATCH_CNT];

struct KSEMASTBATCH kse_address_tmp;

int ii,kk,mm,ff,jj,cnt;

 

if(argc != 2)

{

     return(-1);

}

 

if((fp=fopen(argv[1], "rt"))==NULL) 

{

     return(-1);

}

 

kk=0;

while(1)

{

     memset(rbuf,0x00,sizeof(rbuf));

if(fgets(rbuf, sizeof(rbuf), fp)==NULL) break;

 

//STACK REGISTER & POINTER REGISTER

//STACK REGISTER & POINTER REGISTER

//STACK REGISTER & POINTER REGISTER

//STACK REGISTER & POINTER REGISTER

memcpy(&kse_1_address[kk], rbuf, SZ_KSEMASTBATCH);

//memcpy(&kse_2_address[kk], rbuf, SZ_KSEMASTBATCH);

kse_2_address[kk] = kse_1_address[kk];

 

//[kse1 == kse2 == kse3] SAME ADDRESS POINTER

//[kse1 == kse2 == kse3] SAME ADDRESS POINTER

//[kse1 == kse2 == kse3] SAME ADDRESS POINTER

//[kse1 == kse2 == kse3] SAME ADDRESS POINTER

kse1[kk]     = &kse_1_address[kk];

kse2[kk]     = &kse_1_address[kk];

kse3[kk]     = &kse_1_address[kk];

kse_src[kk]  = &kse_2_address[kk];

 

kk++;

 

if(kk>1000) break;

}

cnt=kk;

if(fp != NULL) fclose(fp);

 

    //ASCENDING SORT BY SYMBOL(POINTER VARIABLE)

//ASCENDING SORT BY SYMBOL(POINTER VARIABLE)

//ASCENDING SORT BY SYMBOL(POINTER VARIABLE)

for(kk=0; kk<cnt; kk++)

{

for(ii=kk+1; ii<cnt; ii++)

{

if(memcmp(kse1[kk]->stockcode,kse1[ii]->stockcode,sizeof(kse1[ii]->stockcode))>0)

{

kse_tmp=NULL;

 

kse_tmp  = kse1[kk];

kse1[kk] = kse1[ii];

kse1[ii] = kse_tmp;

}

}

}

 

//ASCENDING SORT BY SYMBOL(STACK VARIABLE)

//ASCENDING SORT BY SYMBOL(STACK VARIABLE)

//ASCENDING SORT BY SYMBOL(STACK VARIABLE)

for(kk=0; kk<cnt; kk++)

{

for(ii=kk+1; ii<cnt; ii++)

{

if(memcmp(kse_1_address[kk].stockcode,kse_1_address[ii].stockcode,sizeof(kse_1_address[ii].stockcode))>0)

{

kse_address_tmp   = kse_1_address[kk];

kse_1_address[kk] = kse_1_address[ii];

kse_1_address[ii] = kse_address_tmp;

}

}

}

 

//DESCENDING SORT BY SYMBOL(STACK VARIABLE)

//DESCENDING SORT BY SYMBOL(STACK VARIABLE)

//DESCENDING SORT BY SYMBOL(STACK VARIABLE)

for(kk=0; kk<cnt; kk++)

{

for(ii=kk+1; ii<cnt; ii++)

{

if(memcmp(kse_2_address[kk].stockcode,kse_2_address[ii].stockcode,sizeof(kse_2_address[ii].stockcode))<0)

{

kse_address_tmp   = kse_2_address[kk];

kse_2_address[kk] = kse_2_address[ii];

kse_2_address[ii] = kse_address_tmp;

}

}

}

 

cnt = 5;

for(kk=0; kk<cnt; kk++)

{

     printf("(1)[%06x],SYM[%.12s]>>>>[%06x][%.12s],[%06x][%.12s]\n", 

     kk+1, kse_1_address[kk].stockcode, kse_1_address[kk].stockcode, kse1[kk]->stockcode, 

kse1[kk]->stockcode, kse_src[kk]->stockcode, kse_src[kk]->stockcode);

}

for(kk=0; kk<cnt; kk++)

{

     printf("(2)[%06x],SYM[%.12s]>>>>[%06x][%.12s],[%06x][%.12s]\n", 

     kk+1, kse_1_address[kk].stockcode, kse_1_address[kk].stockcode, kse2[kk]->stockcode, 

kse2[kk]->stockcode, kse_src[kk]->stockcode, kse_src[kk]->stockcode);

}

for(kk=0; kk<cnt; kk++)

{

     printf("(3)[%06x],SYM[%.12s]>>>>[%06x][%.12s],[%06x][%.12s]\n", 

     kk+1, kse_1_address[kk].stockcode, kse_1_address[kk].stockcode, kse3[kk]->stockcode, 

kse3[kk]->stockcode, kse_src[kk]->stockcode, kse_src[kk]->stockcode);

}

for(kk=0; kk<cnt; kk++)

{

     printf("(4)[%06x],SYM[%.12s]>>>>[%06x][%.12s],[%06x][%.12s]\n", 

     kk+1, kse_2_address[kk].stockcode, kse_2_address[kk].stockcode, kse3[kk]->stockcode, 

kse3[kk]->stockcode, kse_src[kk]->stockcode, kse_src[kk]->stockcode);

}

return(0);

}

 

 

/*결과

C:\Users\Downloads>20 A0011.dat

(1)[000001],SYM[HK0000050325]>>>>[162f21][HK0000050325],[162f21][KR7182490003]

(1)[000002],SYM[KR5707017R97]>>>>[163241][KR5707017R97],[163241][KR7182480004]

(1)[000003],SYM[KR5713017R38]>>>>[163561][KR5713017R38],[163561][KR7181710005]

(1)[000004],SYM[KR571301A1B4]>>>>[163881][KR571301A1B4],[163881][KR7181480005]

(1)[000005],SYM[KR5713027R36]>>>>[163ba1][KR5713027R36],[163ba1][KR7181450008]

(2)[000001],SYM[HK0000050325]>>>>[162f21][HK0000050325],[162f21][KR7182490003]

(2)[000002],SYM[KR5707017R97]>>>>[163241][KR5707017R97],[163241][KR7182480004]

(2)[000003],SYM[KR5713017R38]>>>>[163561][KR5713017R38],[163561][KR7181710005]

(2)[000004],SYM[KR571301A1B4]>>>>[163881][KR571301A1B4],[163881][KR7181480005]

(2)[000005],SYM[KR5713027R36]>>>>[163ba1][KR5713027R36],[163ba1][KR7181450008]

(3)[000001],SYM[HK0000050325]>>>>[162f21][HK0000050325],[162f21][KR7182490003]

(3)[000002],SYM[KR5707017R97]>>>>[163241][KR5707017R97],[163241][KR7182480004]

(3)[000003],SYM[KR5713017R38]>>>>[163561][KR5713017R38],[163561][KR7181710005]

(3)[000004],SYM[KR571301A1B4]>>>>[163881][KR571301A1B4],[163881][KR7181480005]

(3)[000005],SYM[KR5713027R36]>>>>[163ba1][KR5713027R36],[163ba1][KR7181450008]

(4)[000001],SYM[KR7182490003]>>>>[09af21][HK0000050325],[162f21][KR7182490003]

(4)[000002],SYM[KR7182480004]>>>>[09b241][KR5707017R97],[163241][KR7182480004]

(4)[000003],SYM[KR7181710005]>>>>[09b561][KR5713017R38],[163561][KR7181710005]

(4)[000004],SYM[KR7181480005]>>>>[09b881][KR571301A1B4],[163881][KR7181480005]

(4)[000005],SYM[KR7181450008]>>>>[09bba1][KR5713027R36],[163ba1][KR7181450008]

*/

#include <stdlib.h>

#include <stdio.h>

#include <limits.h>

#include <string.h>

 

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

/*65536 : KEY POINT, NEVER CHANGE VALUE, VERY IMFORTANT INFORMATION*/

#define MAX_HASHTABLE_SIZE 65536

 

#if(1)

/*

 * HASH TABLE FUNCTION DEFINITION BEGIN

 */

struct entry_s {

char *key;

char *value;

struct entry_s *next;

};

 

typedef struct entry_s entry_t;

 

struct hashtable_s {

int size;

struct entry_s **table;

};

 

typedef struct hashtable_s hashtable_t;

 

static hashtable_t *ht_create( int size );

static int ht_hash( hashtable_t *hashtable, char *key );

static entry_t *ht_newpair( char *key, char *value );

static void ht_set( hashtable_t *hashtable, char *key, char *value );

static char *ht_get( hashtable_t *hashtable, char *key );

/*

 * HASH TABLE FUNCTION DEFINITION END

 */

#endif

 

#if(1)

/*TEST, CODE RELATED WITH THIS HASH PROGRAM*/

/*TEST, CODE RELATED WITH THIS HASH PROGRAM*/

size_t sizeof_malloc( void* p )

{

    return  _msize( p );

}

#endif

 

int main( int argc, char **argv ) 

{

hashtable_t *hashtable = ht_create( MAX_HASHTABLE_SIZE );

 

char tmp_key [10+1];

char tmp_data[10+1];

 

int ii,kk,jj,ff;

 

for(kk=0; kk<4000; kk++)

{

memset(tmp_key,0x00,sizeof(tmp_key));

memset(tmp_data,0x00,sizeof(tmp_data));

 

sprintf(tmp_key,  "%d", 1000000 + kk + 1);

sprintf(tmp_data, "%d", 3000000 + kk + 1);

 

ht_set( hashtable, tmp_key, tmp_data);

}

 

for(kk=0; kk<4000; kk++)

{

memset(tmp_key,0x00,sizeof(tmp_key));

 

sprintf(tmp_key,  "%d", 1000000 + kk + 1);

 

if(ht_get( hashtable, tmp_key ) != NULL)

{

printf( "NO[%.5d],KEY(%s),DATA(%s) SUCC\n", kk + 1, tmp_key, ht_get( hashtable, tmp_key ) );

}

else

{

printf( "NO[%.5d],KEY(%s),DATA(%s) FAIL\n", kk + 1, tmp_key, ht_get( hashtable, tmp_key ) );

}

}

return 0;

}

 

int main_tmp( int argc, char **argv ) 

{

hashtable_t *hashtable = ht_create( MAX_HASHTABLE_SIZE );

    char *hash_size = malloc(50000); /*TEST, CODE RELATED WITH THIS HASH PROGRAM*/

 

char tmp_key [10+1];

char tmp_data[10+1];

 

int ii,kk,jj,ff;

 

for(kk=0; kk<4000; kk++)

{

memset(tmp_key,0x00,sizeof(tmp_key));

memset(tmp_data,0x00,sizeof(tmp_data));

 

sprintf(tmp_key,  "%d", 1000000 + kk + 1);

sprintf(tmp_data, "%d", 3000000 + kk + 1);

 

ht_set( hashtable, tmp_key, tmp_data);

 

        printf( "sizeof_malloc(%d)\n", sizeof_malloc( hashtable ) ); /*TEST, CODE RELATED WITH THIS HASH PROGRAM*/

printf( "sizeof_malloc(%d)\n", sizeof_malloc( hash_size ) ); /*TEST, CODE RELATED WITH THIS HASH PROGRAM*/

}

 

for(kk=0; kk<4000; kk++)

{

memset(tmp_key,0x00,sizeof(tmp_key));

 

sprintf(tmp_key,  "%d", 1000000 + kk + 1);

 

if(ht_get( hashtable, tmp_key ) != NULL)

{

printf( "NO[%.5d],KEY(%s),DATA(%s) SUCC\n", kk + 1, tmp_key, ht_get( hashtable, tmp_key ) );

}

else

{

printf( "NO[%.5d],KEY(%s),DATA(%s) FAIL\n", kk + 1, tmp_key, ht_get( hashtable, tmp_key ) );

}

}

 

free(hashtable);

    free(hash_size);  /*TEST, CODE RELATED WITH THIS HASH PROGRAM*/

return 0;

}

 

#if(1)

/*

 * HASH TABLE FUNCTION BEGIN

 */

 

/* Create a new hashtable. */

hashtable_t *ht_create( int size ) 

{

hashtable_t *hashtable = NULL;

int i;

 

if( size < 1 ) return NULL;

 

/* Allocate the table itself. */

if( ( hashtable = malloc( sizeof( hashtable_t ) ) ) == NULL ) {

return NULL;

}

 

/* Allocate pointers to the head nodes. */

if( ( hashtable->table = malloc( sizeof( entry_t * ) * size ) ) == NULL ) {

return NULL;

}

for( i = 0; i < size; i++ ) {

hashtable->table[i] = NULL;

}

 

hashtable->size = size;

 

return hashtable;

}

 

/* Hash a string for a particular hash table. */

int ht_hash( hashtable_t *hashtable, char *key ) 

{

unsigned long int hashval;

int i = 0;

 

/* Convert our string to an integer */

while( hashval < ULONG_MAX && i < strlen( key ) ) {

hashval = hashval << 8;

hashval += key[ i ];

i++;

}

 

return hashval % hashtable->size;

}

 

/* Create a key-value pair. */

entry_t *ht_newpair( char *key, char *value ) 

{

entry_t *newpair;

 

if( ( newpair = malloc( sizeof( entry_t ) ) ) == NULL ) {

return NULL;

}

 

if( ( newpair->key = strdup( key ) ) == NULL ) {

return NULL;

}

 

if( ( newpair->value = strdup( value ) ) == NULL ) {

return NULL;

}

 

newpair->next = NULL;

 

return newpair;

}

 

/* Insert a key-value pair into a hash table. */

void ht_set( hashtable_t *hashtable, char *key, char *value ) 

{

int bin = 0;

entry_t *newpair = NULL;

entry_t *next = NULL;

entry_t *last = NULL;

 

bin = ht_hash( hashtable, key );

 

next = hashtable->table[ bin ];

 

while( next != NULL && next->key != NULL && strcmp( key, next->key ) > 0 ) {

last = next;

next = next->next;

}

 

/* There's already a pair.  Let's replace that string. */

if( next != NULL && next->key != NULL && strcmp( key, next->key ) == 0 ) {

 

free( next->value );

next->value = strdup( value );

 

/* Nope, could't find it.  Time to grow a pair. */

    else {

newpair = ht_newpair( key, value );

 

/* We're at the start of the linked list in this bin. */

if( next == hashtable->table[ bin ] ) {

newpair->next = next;

hashtable->table[ bin ] = newpair;

 

/* We're at the end of the linked list in this bin. */

} else if ( next == NULL ) {

last->next = newpair;

 

/* We're in the middle of the list. */

} else  {

newpair->next = next;

last->next = newpair;

}

}

}

 

/* Retrieve a key-value pair from a hash table. */

char *ht_get( hashtable_t *hashtable, char *key ) 

{

int bin = 0;

entry_t *pair;

 

bin = ht_hash( hashtable, key );

 

/* Step through the bin, looking for our value. */

pair = hashtable->table[ bin ];

while( pair != NULL && pair->key != NULL && strcmp( key, pair->key ) > 0 ) {

pair = pair->next;

}

 

/* Did we actually find anything? */

if( pair == NULL || pair->key == NULL || strcmp( key, pair->key ) != 0 ) {

return NULL;

} else {

return pair->value;

}

}

 

/*

 * HASH TABLE FUNCTION END

 */

#endif

 

 

/*결과

NO[03938],KEY(1003938),DATA(3003938) SUCC

NO[03939],KEY(1003939),DATA(3003939) SUCC

NO[03940],KEY(1003940),DATA(3003940) SUCC

NO[03941],KEY(1003941),DATA(3003941) SUCC

NO[03942],KEY(1003942),DATA(3003942) SUCC

NO[03943],KEY(1003943),DATA(3003943) SUCC

NO[03944],KEY(1003944),DATA(3003944) SUCC

NO[03945],KEY(1003945),DATA(3003945) SUCC

NO[03946],KEY(1003946),DATA(3003946) SUCC

NO[03947],KEY(1003947),DATA(3003947) SUCC

NO[03948],KEY(1003948),DATA(3003948) SUCC

NO[03949],KEY(1003949),DATA(3003949) SUCC

NO[03950],KEY(1003950),DATA(3003950) SUCC

NO[03951],KEY(1003951),DATA(3003951) SUCC

NO[03952],KEY(1003952),DATA(3003952) SUCC

NO[03953],KEY(1003953),DATA(3003953) SUCC

NO[03954],KEY(1003954),DATA(3003954) SUCC

NO[03955],KEY(1003955),DATA(3003955) SUCC

NO[03956],KEY(1003956),DATA(3003956) SUCC

NO[03957],KEY(1003957),DATA(3003957) SUCC

NO[03958],KEY(1003958),DATA(3003958) SUCC

NO[03959],KEY(1003959),DATA(3003959) SUCC

NO[03960],KEY(1003960),DATA(3003960) SUCC

NO[03961],KEY(1003961),DATA(3003961) SUCC

NO[03962],KEY(1003962),DATA(3003962) SUCC

NO[03963],KEY(1003963),DATA(3003963) SUCC

NO[03964],KEY(1003964),DATA(3003964) SUCC

NO[03965],KEY(1003965),DATA(3003965) SUCC

NO[03966],KEY(1003966),DATA(3003966) SUCC

NO[03967],KEY(1003967),DATA(3003967) SUCC

NO[03968],KEY(1003968),DATA(3003968) SUCC

NO[03969],KEY(1003969),DATA(3003969) SUCC

NO[03970],KEY(1003970),DATA(3003970) SUCC

NO[03971],KEY(1003971),DATA(3003971) SUCC

NO[03972],KEY(1003972),DATA(3003972) SUCC

NO[03973],KEY(1003973),DATA(3003973) SUCC

NO[03974],KEY(1003974),DATA(3003974) SUCC

NO[03975],KEY(1003975),DATA(3003975) SUCC

NO[03976],KEY(1003976),DATA(3003976) SUCC

NO[03977],KEY(1003977),DATA(3003977) SUCC

NO[03978],KEY(1003978),DATA(3003978) SUCC

NO[03979],KEY(1003979),DATA(3003979) SUCC

NO[03980],KEY(1003980),DATA(3003980) SUCC

NO[03981],KEY(1003981),DATA(3003981) SUCC

NO[03982],KEY(1003982),DATA(3003982) SUCC

NO[03983],KEY(1003983),DATA(3003983) SUCC

NO[03984],KEY(1003984),DATA(3003984) SUCC

NO[03985],KEY(1003985),DATA(3003985) SUCC

NO[03986],KEY(1003986),DATA(3003986) SUCC

NO[03987],KEY(1003987),DATA(3003987) SUCC

NO[03988],KEY(1003988),DATA(3003988) SUCC

NO[03989],KEY(1003989),DATA(3003989) SUCC

NO[03990],KEY(1003990),DATA(3003990) SUCC

NO[03991],KEY(1003991),DATA(3003991) SUCC

NO[03992],KEY(1003992),DATA(3003992) SUCC

NO[03993],KEY(1003993),DATA(3003993) SUCC

NO[03994],KEY(1003994),DATA(3003994) SUCC

NO[03995],KEY(1003995),DATA(3003995) SUCC

NO[03996],KEY(1003996),DATA(3003996) SUCC

NO[03997],KEY(1003997),DATA(3003997) SUCC

NO[03998],KEY(1003998),DATA(3003998) SUCC

NO[03999],KEY(1003999),DATA(3003999) SUCC

NO[04000],KEY(1004000),DATA(3004000) SUCC

*/

+ Recent posts