본문 바로가기

C언어

[C언어] Linux Pthread & Signal, Itimer 간단 예제와 의문!! 비교적 정확한 주기로 특정 액션을 수행할 필요가 있어 SIGALRM을 주기적으로 발생시키는 setitimer() 를 사용해 보았습니다. 그런데 이해 안가는 부분이 있어 기록 차원에 간단한 예제를 만들어 설명해 봅니다. 아래 예제는 100ms 주기로 시간을 찍어서 이전의 시간과의 차이를 출력하는 간단한 예제입니다. 그런데 요부분... signal(SIGALRM, SIG_IGN); /* note : why??? */ SIGALRM이 발생하면 무시하라는 코드인데, 이 코드가 없으면 SIGALRM이 발생하는 시점에서 프로세스는 종료합니다. 시그널 핸들러를 등록하지 않을 때의 기본 동작입니다. 그런데 setitimer()를 호출하면 sigset 파라메터로 등록된 시그널을 사용하도록 해야 할 것 같고 thread내..
[C언어] simple hash table - test code #include #include #define MAX_HASH_BUCKET 10#define MAX_HASH_KEY MAX_HASH_BUCKET-1 #define HASH_KEY_GET(data) data % MAX_HASH_BUCKET;#define IS_SAME_NODE(a, b) ( a->data == b )/* * Return code. */typedef enum{ RET_OK = 0, RET_NOK} ret_code_t;/* * hash node */typedef struct _hash_node{ int data; struct _hash_node *next;} hash_node_t;/* * hash h..
[C언어] Fast Integer ID Pool 관리 - 할당/반환 함수 #define INT_SLICE (sizeof(unsigned int)*8) #define MAX_ID 4095 #define ID_POOL_INT_SLICE_SIZE ((MAX_OAM_ID+1)/INT_SLICE) #define RET_OK 1 #define RET_NOK 0 unsigned int id_pool_table[ID_POOL_INT_SLICE_SIZE]; int alloc_id(unsigned int *new_id) { register int ii, jj; for(ii = 0; ii < ID_POOL_INT_SLICE_SIZE; ii++) { if(~id_pool_table[ii]) { for(jj = 0; jj < INT_SLICE; jj++) { if(!(id_pool_table[ii]..
[C언어] 리눅스 프로세스 이름으로 프로세스 아이디(PID) 구하기 #include #include static int which_number (char *s){ int len, i; len = strlen (s); for (i = 0; i '9')) return -1; return atoi (s);}int get_pid_from_proc_by_name (char *str){ DIR *dp; struct dirent *dir; char buf[100], line[1024], tag[100], name[100]; int pid; FILE *fp; dp = opendir ("/proc"); if (!dp) return -1;..
[C언어] uint2bin char *uint2bin(unsigned int i) { static char s[32 + 1] = { '0', }; int count = 32; do { s[--count] = '0' + (char) (i & 1); i = i >> 1; } while (count); return s; }
[C언어] IP 관련 매크로(Macro) Header => #include #define IPV4_INT_TO_STR(int_ipv4_ptr) inet_ntoa(*((struct in_addr *)int_ipv4_ptr)) #define IPV4_STR_TO_INT(str_ipv4_ptr) (unsigned int) inet_addr(str_ipv4_ptr) [참고] 헤더 include가 빠지면 error: dereferencing pointer to incomplete type 에러가 납니다. 계속 추가...
[C언어] parse Mac Address int parse_mac_address(char *strNum, uchar_t rslt_hex[]) { int enteredNum = 0; int orgIndex = 0; int oprIndex = 0; char numChar[100]; while(orgIndex 2 ) return -1; rslt_hex[enteredNum++] = strtoul(numChar, NULL, 16); oprIndex = 0; } else { if(strNum[orgIndex] >'F' || strNum[orgIndex] < '0') return(-1); numChar[oprIndex++] = strNum[orgIndex]; } orgIndex++; } return (enteredNum); } 입력 포맷 : AA:BB:CC..
[C언어] GCC Compile Option Option 설명 -c file 해당 file을 compile -o file 실행 파일명을 file로 설정함 -Wn Warning Level을 n으로 설정함. 대부분의 경우 모든 warning message를 보기위해 -Wall로 설정함 -Ipath Include directory를 path로 설정함 -Lpath Link를 위한 Library를 directory를 path로 설정함 -llib Link시 해당 lib를 같이 link하게 함 -v gcc의 version을 출력 -On Optimize Level을 n으로 설정함 이 option을 주면 실행파일의 크기가 작아지고, 실행속도가 빨라지나 그만큼 위험성이 따름. 대부분의 경우 -O1 또는 -O2를 사용함 Wn 즉, 컴파일 경고 메시지를 설정하는 옵션을..
[C언어] Endian 체크 #define LITTLE_ENDIAN 1 #define BIG_ENDIAN 2 #define PDP_ENDIAN 3 #1 int get_endian() { static long int str[2] = { 0x41424344,0x0 }; /* ASCII "ABCD" */ if (strcmp("DCBA", (char *) str) == 0) return LITTLE_ENDIAN; else if (strcmp("ABCD", (char *) str) == 0) return BIG_ENDIAN; else if (strcmp("BADC", (char *) str) == 0) return PDP_ENDIAN; } #2 int endian(void) { int i = 0x00000001; if ( ((char *)&..
[C언어] BIT READ/WRITE MACRO #define READ_U8(_val, _ptr) { \ _val = *((unsigned char *) (_ptr)); \ } #define READ_S8(_val, _ptr) { \ _val = *((char *) (_ptr)); \ } #define READ_U16(_val, _ptr) { \ _val = *((unsigned short *) (_ptr)); \ } #define READ_S16(_val, _ptr) { \ _val = *((short *) (_ptr)); \ } #define READ_U24(_val, _ptr) { \ _val = *((unsigned int *) (_ptr)) >> 8; \ } #define READ_S24(_val, _ptr) { \ _val = *((int ..
[C언어] structure file dump and reading T_MMDB_EXT_VLAN_TAG_OPER_PROFILE = *MMDB_EXT_VLAN_TAG_OPER_PROFILE[MAX_MMDB_EXT_TAG_RULE]; // DUMPING #ifdef SUPPORT_GPON_EXT_VLAN_OPER sprintf(buf, "rm -rf %s", MMDB_EXT_VLAN_PROFILE_CONFIG_FILE); system(buf); fp = fopen(MMDB_EXT_VLAN_PROFILE_CONFIG_FILE, "wb"); if ( NULL != fp ) { for (ii=0; ii
[C언어] HexaDump START DUMP(%s:%d)\n", __FILE__, __LINE__);\ tmp = msg;\ for ( i=0; i