프로그래밍/ExampleCode.com 7

[semaphore] c++ semaphore example code

개요 - multi process간에 사용할 semaphore 를 c++로 구현함. - named semaphore를 사용 - semaphore name은 process명으로 자동 사용 Source Code /* Copyright (C) * 2021 - doitnowman * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later versio..

[IPC] shared memory 예제 코드

[IPC] shared memory 예제 코드 1. 구조 - 커널에서 제공하는 메모리를 이용한 프로세스가 데이터를 공유하는 구조이다. - 자세한 설명은 다음 포스트 참조 [프로세스간 통신] IPC(inter process communication) 종류 2. 예제 코드 (1) Header #ifndef __SHARE_MEMORY_H__ #define __SHARE_MEMORY_H__ #define SHM_INFO_COUNT 30 typedef struct _shm_info{ char str_ip[40]; unsigned int int_ip; unsigned int int_id; }SHM_INFOS; #endif//__SHARE_MEMORY_H__ (2) shared Memory Writing 코드 #in..

[IPC] pipe 예제 코드

[IPC] pipe 예제 코드 1. 구조 - 부모 프로세스와 자식 프로세스간에 통신을 할때 사용 한다. - 자세한 설명은 다음 포스트 참조 [프로세스간 통신] IPC(inter process communication) 종류 2. 예제 코드 #include #include #include #include int main(void) { int fd[2], nbytes, rc = 0; pid_t childpid; char string[] = "Hello, world!\n"; char readbuffer[80]; if ((rc = pipe(fd)) < 0) { printf("Creating Pipe is Error [%d]\n", rc); } if((childpid = fork()) == -1) { perror..

[IPC] named pipe 예제 코드

[IPC] name pipe 예제 코드 1. 구조 - 단방향 통신 구조이며 한쪽에서 쓰면 다른 한쪽에서 읽을 수 있는 구조로 되어있다. - 자세한 설명은 다음 포스트 참조 [프로세스간 통신] IPC(inter process communication) 종류 2. 예제 코드 1) Client(Writer) #include #include #include #include #include #define MSG_SIZE 80 #define PIPENAME "./named_pipe_file" int main(void) { char msg[MSG_SIZE]; int fd; int nread, i; /* named pipe 열기, Write 전용으로 열기 */ if ((fd = open(PIPENAME, O_WRONL..

[IPC] message queue 예제 코드

[IPC] message queue 예제 코드 1. 구조 - message queue는 생상자와 소비자 구조로 되어있다. - 자세한 설명은 다음 포스트 참조 [프로세스간 통신] IPC(inter process communication) 종류 2. message_queue_constructer.예제 코드 #include #include #include #include #include #include #include struct msgbuf { // 이부분은 고정!!!! long msgtype; // 아래 부분은 모두 변경 가능 char mtext[256]; char myname[16]; int seq; }; struct msgbuf1 { // 이부분은 고정!!!! long msgtype; // 아래 부분은..

[IPC] 메모리 맵 mmap() 예제 코드

[IPC] 메모리 맵 mmap() 예제 코드 1. 구조 - 메모리 맵은 파일과 메모리를 직접 mapping일 시켜주는 구조로 이루어 져있다. - 자세한 설명은 다음 포스트 참조 [프로세스간 통신] IPC(inter process communication) 종류 2. 예제 코드 /* Copyright (C) * 2018 - doitnow-man * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the Licen..

[fseek] 파일 포인터이동 예제

fseek Example Code in C 1. 함수 설명 - 파일 Pointer의 값을 이동 시키는 함수 있다. - 파일은 Open 한 후 처음 부터 읽는 것이 아니라 nbyte 떨어진 곳부터 읽고 싶은 때 사용한다. 헤더 stdio.h 형태 int fseek( FILE *stream, long offset, int whence); 인수 FILE *stream 파일 포인터 Long offset 건너 뛸 byte수 int whence 시작 시점 SEEK_SET : 파일 시작 SEEK_CUR : 현재 포인터 위치 SEEK_END : 파일 끝 반환 int 2. 연관 함수 1) long ftell(FILE *stream); 2) void rewind(FILE *stream); 3) int fgetpos(FIL..