이름있는 세마포어 말고 메모리에 올려 사용하는 방법도 있다. 책은 sem_init(…, 두번째 arg,…)에서 두번째 입력하는 숫자를 thread에서 공유할 거면 0, 프로세스간 할거면 1로 하라고 한다. 1로 하여 thread도 잘 된다. named semaphore는 포인터를 선언했고 파일에 영역을 만들었다. 이와 다르게 직접 데이터를 선언하고 주소로 넘겨줘야 한다. ipcs로 보일 법 한데, 안 보인다. 무엇이 잘못되었는지 모르겠다. /* include…… memory based semaphore, unix network programming p241 계속 읽기
[카테고리:] 생활코딩
semaphore p223 ~ p238
name semaphore는 /dev/shm에 저장된다. 이름을 엄한 /tmp/xxx 이런 식으로 만들면 세마포어를 만들 수 없다. segment error로 죽는다. 책에서는 /tmp/로 넣어도 잘 동작해서 확인하는데 오래 걸렸다. 디버그 하다 중간에 멈추면 /dev/shm에 sem*로 파일을 지울 수 없어 다음 실행에 세마포어를 받을 수 없다. 처음 시작 전 자기 이름으로 된 세마포어 있으면 지워야 한다. thread를 돌리면 미친 race…… semaphore p223 ~ p238 계속 읽기
sync, mutex. unix network programming p161
다시 thread로 돌아왔다. #include <stdlib.h> #include <stdio.h> #include <string.h> #include <pthread.h> #define MAXNITEMS 1000000 #define MAXNTHREADS 100 #define MIN(X,Y) ((X)<(Y) ? (X) : (Y)) //gcc -lpthread 옵션으로 컴파일. int nitems; /* read-only by producer and consumer */ //shared를 초기화 하는데, //맨 처음에 mutext가 있음. //나머지는 0으로 초기화됨.. struct { pthread_mutex_t mutex; int buff[MAXNITEMS]; int nput;…… sync, mutex. unix network programming p161 계속 읽기
fifo 예제. unix network programming 55p
pipe는 이름을 없어 parent, child process만 통신이 가능하다. 이를 보안한게 named pipe인데, fifo라고도 한다. 책은 다른 유닉스 버전으로 작성하여, linux에 맞게 수정했다. 결론적으로 pipe와 비슷하고 더 쉽다. #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h> #include <sys/wait.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> #define FIFO1 “/tmp/fifo.1” #define FIFO2 “/tmp/fifo.2” #define MAXLINE…… fifo 예제. unix network programming 55p 계속 읽기