/*C언어 기초 - 쓰레드 이해하고 사용하는 방법(windows 10)*/
/*C언어 기초 - 쓰레드 이해하고 사용하는 방법(windows 10)*/
/*C언어 기초 - 쓰레드 이해하고 사용하는 방법(windows 10)*/

#include <stdio.h>
#include <Windows.h>
#include <process.h>

unsigned _stdcall time_tick(void* arg)
{
while(1)
{
Sleep(1000);

draw();
rc=common_check(D_DOWN);
if(rc==FAIL)
{
if(ypos==0)
{
return(FAIL);
}
else
{
while(1)
{
if(check_horizon()==FAIL) break;
}
xpos=MAPX/2;
ypos=0;

//,,
//,,
}
}
}
return(SUCC);
}

void init()
{
_beginthreadex(NULL, 0, time_tick, 0, 0, NULL);
}

void stop()
{}

void main(void)
{
init();
while (1)
{
//,,
//,,
}
stop();
}

>c_tris90_4_4.c 
>gcc -c c_tris90_4_4.c
>gcc -o c_tris90_4_4 c_tris90_4_4.o -lm -lws2_32 -lgdi32

c_tris90_4_4.c
0.01MB

/*C# Timer.ElapsedEventHandler(tme_tick) 과 같은 효과(Linux)*/
/*C# Timer.ElapsedEventHandler(tme_tick) 과 같은 효과(Linux)*/

#include <unistd.h>
#include <time.h>
#include <signal.h>
#include <stdio.h>
 
void time_tick()
{
    printf("timer\n");
}
 
int createTimer( timer_t *timerID, int sec, int msec )  
{  
    struct sigevent         te;  
    struct itimerspec       its;  
    struct sigaction        sa;  
    int                     sigNo = SIGRTMIN;  
   
    /* Set up signal handler. */  
    sa.sa_flags = SA_SIGINFO;  
    sa.sa_sigaction = time_tick;     // 타이머 호출시 호출할 함수 
    sigemptyset(&sa.sa_mask);  
  
    if (sigaction(sigNo, &sa, NULL) == -1)  
    {  
        printf("sigaction error\n");
        return -1;  
    }  
   
    /* Set and enable alarm */  
    te.sigev_notify = SIGEV_SIGNAL;  
    te.sigev_signo = sigNo;  
    te.sigev_value.sival_ptr = timerID;  
    timer_create(CLOCK_REALTIME, &te, timerID);  
   
    its.it_interval.tv_sec = sec;
    its.it_interval.tv_nsec = msec * 1000000;  
    its.it_value.tv_sec = sec;
    
    its.it_value.tv_nsec = msec * 1000000;
    timer_settime(*timerID, 0, &its, NULL);  
   
    return 0;  
}
 
int main()
{
    timer_t timerID;
    
    createTimer(&timerID,5, 0);
    
    while(1)
    {
        //,,
    }
}

#비고)Makefile

CC=gcc
SRC=./
EXE=./
INC=./
CFLAGS=-DSERCMD - DDEBUG -DLOGGING -I.
LIBS = -lcrypt -lnsl -lm

BIN=$(EXE)
all: $(BIN)/sample \

.c.o:
    $(CC) $(CFLAGS) -c -g $*.c

#sample.c R_Option.c R_Future.c 가 모두 존재할경우에 Object는 아래와 같이 명시를 해줘야 한다.
$(BIN)/sample: sample.o R_Option.o R_Future.o
    $(CC) -o $@ sample.o  R_Option.o R_Future.o (LIBS) -s

clean:
    rm *.o

#구조체안에 선언되어진 함수 실행하기(함수를 구조체 변수로 갖는경우)
#구조체안에 선언되어진 함수 실행하기(함수를 구조체 변수로 갖는경우)

#명령인자에 따른 각각의 함수로 분기하는 경우에 사용되는 형태의 코드기법입니다.
#명령인자에 따른 각각의 함수로 분기하는 경우에 사용되는 형태의 코드기법입니다.

#주로 통신시에 서버와 클라이언트에서 주고받는 데이타에 따른 서버의 함수를 실행하고자 할때에 자주 사용됩니다.

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

#define SERCMD

typedef struct ser_map_t
{
    char *cpMethod;
    int (*fpProc)(char *, char *, char *);
}ser_map_t;

#ifdef SERCLI
int icsLoginClient (char *,char *,char *);
int icsDynBigSel (char *,char *,char *);
int icsCmdOsp (char *,char *,char *);
#endif

#ifdef SERCMD
int icsCmdOspMain(char *,char *,char *);
#endif

static ser_map_t ser_map_ims[] = 
{
#ifdef SERCLI
    { "login_client", icsLoginClient },
    { "dynbigsel", icsDynBigSel },
    { "cmd_osp_check", icsCmdOsp },
#endif
#ifdef SERCMD
    { "cmd_osp_check_s", icsCmdOspMain },
#endif
    { NULL,NULL }
};

int
icsCmdOspMain(char *Rcomm, char *Param, char *cppMsg)
{
    return(0);
}

int main(void)
{
    int iRet, kk, Tot;
    char *Rcomm, *Param, *cppMsg, method_cmd[100];

    memset(method_cmd,0x00,sizeof(method_cmd));    
    strcpy(method_cmd, "cmd_osp_check_s");

    Tot=(sizeof(ser_map_ims)/sizeof(ser_map_t))-1;
    for(kk=0; kk<Tot; kk++)
    {
        if(strcmp(method_cmd, ser_map_ims[kk].cpMethod)==0)
        {
            iRet=ser_map_ims[kk].fpProc(Rcomm,Param,cppMsg);
            printf("[%s]:[%d]\n",ser_map_ims[kk].cpMethod, iRet);
        }
    }
    return(0);
}

 

 

 

static ser_map_t ser_map_ims[] = 
{
#ifdef SERCLI
    {"cmd_ops_check", icsCmdOsp},
#endif

#ifdef SERCMD
    {"cmd_osp_check_s", icsCmdOspMain},
#endif
    {NULL,NULL}
};

위에서 SERCLI와 SERCMD는 위 코드전에 선언이 되어져 있어야 한다. 아래와 같이.
#define SERCLI
#define SERCMD

만약에 아무리 찾아봐도 없다면(???????)

Makefile을 열어보면 답을 알수 있다. 
------------------------------------------------------------------------------------------------------------
#define, -D옵션, CFLAGS, avr-gcc에서 Makefile의 CFLAGS, 인수(매개변수)가 있는 사용자 정의 함수
------------------------------------------------------------------------------------------------------------

CFLAGS=-DSERCMD 로 선언이 되어 있으면, 굳이 소스코드에 넣지 않아도 컴파일시에 반영이 되도록 할수 있다.

비고)Makefile

CC=gcc
SRC=./
EXE=./
INC=./
CFLAGS=-DSERCMD - DDEBUG -DLOGGING -I.
LIBS = -lcrypt -lnsl -lm

BIN=$(EXE)
all: $(BIN)/sample \

.c.o:
    $(CC) $(CFLAGS) -c -g $*.c

$(BIN)/sample: sample.o
    $(CC) -o $@ sample.o (LIBS) -s

clean:
    rm *.o

 


#컴파일과정에서 코드추가 가능(#define PIE 3.1416을 대신할수 있다.)
#컴파일과정에서 코드추가 가능(#define PIE 3.1416을 대신할수 있다.)

비고) Makefile 사용시에는 CFLAGS를 추가해서 구성할수 있다.

#include<stdio.h>
int main()
{
    int radius;
    float circumference;

    radius=5;
    circumference = (radius + radius) * PIE;

    printf("The circumference of circle is %7.2f \n", circumference);
    return(0);
}

>gcc -o sample sample.c
>PIE undeclared(first use in this function)
컴파일과정에서 코드추가 가능(#define PIE 3.1416을 대신할수 있다.)
>gcc -DPIE=3.1416 -o sample sample.c

가장 많이 사용하는 경우는 전처리기일것이다.

int main()
{
#ifdef INFO_DEBUG
printf("error ,mmmm1\n");
#endif

#ifdef ERROR_DEBUG
printf("error ,mmmm2\n");
#endif

printf("program terminated,mmmm\n");
}

gcc -o sample sample.c
or
gcc -DINFO_DEBUG -DERROR_DEBUG -o sample sample.c

#include<limits.h> 선언후에 사용되는 정의값

stdio.h 와 마찬가지로 limits.h 도 헤더파일입니다.
다만 틀린 것이 있다면 limits.h 는 매크로 상수처럼 그 값과 변수명이 이미 정해져 있다는 것이죠. 
따라서 유효범위를 알아야 하기위해서는 변수명또한 알아두셔야합니다.

limits.h 헤더파일 내용.

#include<limits.h> 선언후에 사용되는 정의값

CHAR_BIT char의 비트 수
SCHAR_MIN signed char의 최소값
SCHAR_MAX signed char의 최대값
UCHAR_MAX unsigned char의 최대값
CHAR_MIN char의 최소값
CHAR_MAX char의 최대값
MB_LEN_MAX 멀티바이트 문자의 최대 바이트 수
SHRT_MIN short int의 최소값
SHRT_MAX short int의 최대값
USHRT_MAX unsigned short int의 최대값
INT_MIN int의 최소값
INT_MAX int의 최대값
UINT_MAX unsigned int의 최대값
LONG_MIN long int의 최소값
LONG_MAX long int의 최대값
ULONG_MAX unsigned long int의 최대값

ex)

#include<stdio.h>
#include<limits.h>

int main(void)
{
    short min = SHRT_MIN;
    short max = SHRT_MAX;

    printf("MAX:[%d],MIN[%d]\n", max, min);
    return(0);
}



#include <stdio.h>
#include <unistd.h>

int main() 
{
  int i;

  for (i=10;i>=0;i--)
  {
    printf ("Count Down : %02d\r", i);
    fflush(stdout);
    sleep(1);
  }

  return(0);
}

+ Recent posts