본문 바로가기

C20

C POSIX ] pthread, mutex 사용법 Pthread는 모든 유닉스 계열 POSIX 시스템에서, 일반적으로 이용되는 라이브러리로 병렬적으로 작동하는 소프트웨어 작성을 위해 사용할 수 있다. 소스코드에선 #include 로 헤더를 포함하고 컴파일 시 -pthread 옵션을 붙여 컴파일 한다.   주요 thread 함수  1. pthread_create 새로운 스레드를 생성하고 지정된 함수 start_routine을 실행한다.int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); 파라미터 :pthread_t *thread: 생성된 스레드의 식별자를 저장할 포인터, 해당 식별자를 통해 이후 스레드를 제어할 .. 2024. 6. 26.
C] epoll 사용법 epoll이란?epoll은 리눅스 커널 2.5.44부터 도입된 Multiplexing IO 함수로 다수의 파일 디스크립터를 모니터링 할 때 효율적이다. epoll은 epoll_event 구조체와 다음의 세 가지 주요 함수로 구성된다.epoll_create : epoll 파일 디스크립터 저장소 생성epoll_ctl : 저장소에 파일 디스크립터 등록 및 삭제epoll_wait : 파일 디스크립터의 변화를 대기   select와의 차이점select의 경우 매번 호출할 때마다 파일 디스크립터 집합을 사용자 공간에서 커널 공간으로 복사해야 하지만 epoll는 한 번 설정한 파일 디스크립터 집합을 커널 공간에서 유지하여 불필요한 복사를 방지한다. 따라서 많은 수의 파일 디스크립터를 다룰 때 epoll이 더욱 효율적.. 2024. 6. 24.
C] leetCode 2540 - Minimum Common Value (+ Two Pointers) 리트코드 2540 문제 Given two integer arrays nums1 and nums2, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1 and nums2, return -1. Note that an integer is said to be common to nums1 and nums2 if both arrays have at least one occurrence of that integer. Example 1: Input: nums1 = [1,2,3], nums2 = [2,4] Output: 2 Explanation: T.. 2024. 4. 18.
C언어 ] qsort (Quick Sort, 퀵정렬) 함수 사용법 C언어 표준 라이브러리에 포함된 qsort 함수는 퀵 정렬(Quick Sort) 알고리즘을 사용하여 배열을 정렬하는 데에 사용된다. 아래는 qsort 함수의 사용법에 대한 예시이다. 1. 헤더 파일 include qsort 함수를 사용하려면 stdlib.h 헤더 파일을 포함해야 한다. #include 2. 비교 함수 작성 qsort 함수를 사용하기 위해선 정렬을 위해 사용할 비교 함수를 만들어야 하며, 이 함수는 두 요소를 받아들여 비교한 결과를 반환해야 한다. 반환값이 음수, 0, 양수인지에 따라 정렬의 순서가 결정된다. 비교 함수의 반환값이 양수면, qsort 함수는 두 요소의 순서를 바꾼다. int compare(const void *a, const void *b) { // 비교 로직 작성 retu.. 2024. 1. 30.
C언어 ] leetCode 3016 - Minimum Number of Pushes to Type Word II leetCode 3016 문제 You are given a string word containing lowercase English letters. Telephone keypads have keys mapped with distinct collections of lowercase English letters, which can be used to form words by pushing them. For example, the key 2 is mapped with ["a","b","c"], we need to push the key one time to type "a", two times to type "b", and three times to type "c" . It is allowed to remap .. 2024. 1. 28.
C/C++ ] start 명령어 사용 새로운 프롬프트 창에서 시스템 명령 사용하기 start 명령어는 일반적으로 명령 프롬프트나 배치 파일에서 다른 프로그램 또는 명령을 시작하는 데 사용한다. 사용 방법 start [/d ] [/i] [{/min | /max}] [{/separate | /shared}] [{/low | /normal | /high | /realtime | /abovenormal | /belownormal}] [/node ] [/affinity ] [/wait] [/b] [/machine ] [ [... ] | [... ]] https://learn.microsoft.com/ko-kr/windows-server/administration/windows-commands/start start 지정된 프로그램 또는 명령을 실행하기 위해 별도의 명령 프롬프트 창을 시작하는 시.. 2023. 12. 8.
C ] Data Types Range C type stdint.h type Bits Sign Range char uint8_t 8 Unsigned 0 .. 255 signed char int8_t 8 Signed -128 .. 127 unsigned short uint16_t 16 Unsigned 0 .. 65,535 short int16_t 16 Signed -32,768 .. 32,767 unsigned int uint32_t 32 Unsigned 0 .. 4,294,967,295 int int32_t 32 Signed -2,147,483,648 .. 2,147,483,647 unsigned long long uint64_t 64 Unsigned 0 .. 18,446,744,073,709,551,615 long long int64_t 6.. 2023. 9. 15.
C, C++ ] system("pause") 대신 사용할 수 있는 방법 system("pause")는 운영체제에 종속적이고 운영체제에게 "pause"라는 명령을 실행하도록 요청하므로 비슷한 기능을 구현할 다른 방법을 생각해보자. C 윈도우 환경에서만 가능한 방법 #include #include int main() { printf("Press any key to continue...\n"); while (!_kbhit()); (void)_getch(); return 0; } conio.h 는 윈도우에서만 사용 가능하다. C #include int main() { printf("Press Enter to continue...\n"); (void) getchar(); return 0; } getchar()는 표준 라이브러리이기 때문에 이식성이 좋으며, 보다 간단하게 사용자의 입력.. 2023. 7. 11.
C ] 포인터와 const const는 변수나 함수 매개변수를 선언할 때 사용되는 한정자로 값을 변경할 수 없음을 나타낸다. 변수를 상수로 취급하고자 할 때 유용하며, 많은 곳에서 참조되고 변경되는 것을 원하지 않는 정적인 정보를 보호하는데 사용된다.  포인터 형식과 const 한정자가 같이 쓰일 때 const의 위치에 따라 다음과 같은 차이가 있다.   1. 상수를 가리키는 포인터 : ptr의 역참조를 통해 값을 변경할 수 없지만, ptr이 다른 주소를 가리키도록 변경할 수 있다.const int* ptr     2. 상수 포인터 : ptr 자체를 다른 주소를 가리키도록 변경할 수 없지만, ptr의 역참조를 통해 값을 변경할 수 있다.int* const ptr     3. 상수를 가리키는 상수 포인터 : ptr이 가진 주소를 변.. 2023. 5. 30.
C언어 ] leetCode 2181 - Merge Nodes in Between Zeros You are given the head of a linked list, which contains a series of integers separated by 0's. The beginning and end of the linked list will have Node.val == 0. For every two consecutive 0's, merge all the nodes lying in between them into a single node whose value is the sum of all the merged nodes. The modified list should not contain any 0's. Return the head of the modified linked list. Exampl.. 2023. 5. 7.