콘텐츠로 바로가기

now0930 일지

이런저런 생각

  • 홈
  • 비공개
  • 강좌
  • 잔여 작업 조회
  • 위치

shared memory introduction, p303 ~ p315

semaphore로 process간 데이터를 주고 받을 줄 알았는데, 아니었다. semaphore로 동기하고 process간 데이터 전달은 메모리 공유로 한다.

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#define	SEM_NAME	"mysem"
#define FILE_MODE 0666
struct shared{
	sem_t mutex;
	int count;
} shared;

int
main(int argc, char **argv)
{
	int		fd, i, nloop, zero = 0;
	//int		*ptr;
	//sem_t	*mutex;
	struct shared *ptr;

	if (argc != 3)
		perror("usage: incr2 <pathname> <#loops>");
	nloop = atoi(argv[2]);

		/* 4open file, initialize to 0, map into memory */
	fd = open(argv[1], O_RDWR | O_CREAT, FILE_MODE);
	write(fd, &shared, sizeof(struct shared));
	ptr = mmap(NULL, sizeof(struct shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	close(fd);

		/* 4create, initialize, and unlink semaphore */
	//mutex = sem_open(SEM_NAME, O_CREAT | O_EXCL, FILE_MODE, 1);
	sem_init(&ptr->mutex,1,1);


	sem_unlink(SEM_NAME);

	setbuf(stdout, NULL);	/* stdout is unbuffered */
	if (fork() == 0) {		/* child */
		for (i = 0; i < nloop; i++) {
			sem_wait(&ptr->mutex);
			printf("child: %d\n", (ptr->count)++);
			sem_post(&ptr->mutex);
		}
		exit(0);
	}

		/* 4parent */
	for (i = 0; i < nloop; i++) {
		sem_wait(&ptr->mutex);
		printf("parent: %d\n", ptr->count++);
		sem_post(&ptr->mutex);
	}
	exit(0);
}
pi@raspberrypi:~/Project/cCode/IPC $ ./a.out tt 100
parent: 0
parent: 1
parent: 2
parent: 3
parent: 4
parent: 5
child: 6
child: 7
child: 8
child: 9
child: 10
child: 11
child: 12
child: 13
child: 14
child: 15
child: 16
child: 17
child: 18
child: 19
child: 20
child: 21
child: 22
child: 23
child: 24
child: 25
child: 26
child: 27
child: 28
child: 29
child: 30
child: 31
child: 32
child: 33
child: 34
child: 35
child: 36
child: 37
child: 38
child: 39
child: 40
child: 41
child: 42
child: 43
child: 44
child: 45
child: 46
child: 47
child: 48
child: 49
child: 50
child: 51
child: 52
child: 53
child: 54
child: 55
child: 56
child: 57
child: 58
child: 59
child: 60
child: 61
child: 62
child: 63
child: 64
child: 65
child: 66
child: 67
child: 68
child: 69
child: 70
child: 71
child: 72
child: 73
child: 74
child: 75
child: 76
child: 77
child: 78
child: 79
child: 80
child: 81
child: 82
child: 83
child: 84
child: 85
child: 86
child: 87
child: 88
child: 89
child: 90
child: 91
child: 92
child: 93
child: 94
child: 95
child: 96
child: 97
child: 98
child: 99
child: 100
child: 101
child: 102
child: 103
child: 104
child: 105
parent: 106
parent: 107
parent: 108
parent: 109
parent: 110
parent: 111
parent: 112
parent: 113
parent: 114
parent: 115
parent: 116
parent: 117
parent: 118
parent: 119
parent: 120
parent: 121
parent: 122
parent: 123
parent: 124
parent: 125
parent: 126
parent: 127
parent: 128
parent: 129
parent: 130
parent: 131
parent: 132
parent: 133
parent: 134
parent: 135
parent: 136
parent: 137
parent: 138
parent: 139
parent: 140
parent: 141
parent: 142
parent: 143
parent: 144
parent: 145
parent: 146
parent: 147
parent: 148
parent: 149
parent: 150
parent: 151
parent: 152
parent: 153
parent: 154
parent: 155
parent: 156
parent: 157
parent: 158
parent: 159
parent: 160
parent: 161
parent: 162
parent: 163
parent: 164
parent: 165
parent: 166
parent: 167
parent: 168
parent: 169
parent: 170
parent: 171
parent: 172
parent: 173
parent: 174
parent: 175
parent: 176
parent: 177
parent: 178
parent: 179
parent: 180
parent: 181
parent: 182
parent: 183
parent: 184
parent: 185
parent: 186
parent: 187
parent: 188
parent: 189
parent: 190
parent: 191
parent: 192
parent: 193
parent: 194
parent: 195
parent: 196
parent: 197
parent: 198
parent: 199


pi@raspberrypi:~/Project/cCode/IPC $ od -A d -t d tt
0000000           2         128           0           0
0000016         200
000002

이 글 공유하기:

  • Tweet
발행일 2020-07-21글쓴이 이대원
카테고리 생활코딩 태그 c, linux, network program, semaphore, shared memory

댓글 남기기응답 취소

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.

글 내비게이션

이전 글

다른 process간 named semaphore 사용(실패)

다음 글

posix shared memory, p325~p335

2025 5월
일 월 화 수 목 금 토
 123
45678910
11121314151617
18192021222324
25262728293031
4월    

최신 글

  • common mode, differential mode 2025-05-11
  • signal conditioner, 신호 처리기 2025-05-10
  • strain gage 2025-05-09
  • 칼만 필터 2025-05-01
  • positioner(I/P) 2025-04-26

카테고리

  • 산업계측제어기술사
  • 삶 자국
    • 책과 영화
    • 투자
  • 생활코딩
    • LEGO
    • ROS
    • tensorflow
  • 전기기사
  • 피아노 악보

메타

  • 로그인
  • 엔트리 피드
  • 댓글 피드
  • WordPress.org

페이지

  • 소개
  • 잔여 작업 조회
    • 작업 추가
    • 작업의 사진 조회
    • 작업 수정 페이지
  • 사진
    • GPS 입력된 사진
    • 사진 조회
  • 위치
    • 하기 휴가 방문지
    • 해외 출장

태그

android bash c docker driver FSM gps java kernel LEGO linux mysql network program opcua open62541 plc programmers python raspberry reinforcementLearning ros state space system program tensorflow transfer function 경제 미국 민수 삼국지 세계사 실기 에너지 역사 유전자 일본 임베디드 리눅스 전기기사 조선 중국 채윤 코딩 테스트 통계 한국사 한국어

팔로우하세요

  • Facebook
now0930 일지
WordPress로 제작.