세마포어로 데이터를 전달할 줄 알았으나, 동기화만 한다. 프로세스간 데이터를 공유할 방법이 없다. 다음 예제 shared memory로 공유한다. named semaphore는 잘 동작하는 듯 한데, 검증을 못했다.
pi@raspberrypi:~/Project/cCode/IPC $ cat named_semaphore_prod.c
/* include main */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <errno.h>
#include <signal.h>
#define NBUFF 10
#define SEM_MUTEX "mutex" /* these are args to px_ipc_name() */
#define SEM_NEMPTY "empty"
#define SEM_NSTORED "nstored"
#define SEM_PROGEND "end"
int nitems; /* read-only by producer and consumer */
struct { /* data shared by producer and consumer */
int buff[NBUFF];
sem_t *mutex, *nempty, *nstored, *endprog;
} shared;
static void signal_hanlder (int signo)
{
/*
* Technically, you shouldn't use printf() in a
* signal handler, but it isn't the end of the
* world. I'll discuss why in the section
* "Reentrancy."
*/
if(signo == SIGINT){
printf ("Caught SIGINT!\ndelete named semaphore\n");
sem_unlink(SEM_MUTEX);
sem_unlink(SEM_NEMPTY);
sem_unlink(SEM_NSTORED);
sem_unlink(SEM_PROGEND);
exit (EXIT_SUCCESS);
}
}
int
main(int argc, char **argv)
{
int i, items;
errno=0;
//if (argc != 2)
// perror("usage: prodcons1 <#items>");
// nitems = atoi(argv[1]);
/* 4create three semaphores */
//무한 루프에서 세마포어 동작..
//signal handler로 삭제필요.
if(signal(SIGINT, signal_hanlder) == SIG_ERR){
fprintf(stderr, "cannot handle SIGINT\n");
exit(EXIT_FAILURE);
}
shared.mutex = sem_open(SEM_MUTEX, O_CREAT | O_EXCL, 0660, 1);
if(shared.mutex == SEM_FAILED && errno == EEXIST ){
printf("delete existing semaphore mutex\n");
sem_unlink(SEM_MUTEX);
shared.mutex = sem_open(SEM_MUTEX, O_CREAT | O_EXCL, 0660, 1);
} //if
shared.nempty = sem_open(SEM_NEMPTY, O_CREAT | O_EXCL, 0660, NBUFF);
if(shared.nempty== SEM_FAILED && errno == EEXIST ){
printf("delete existing semaphore nempty\n");
sem_unlink(SEM_NEMPTY);
shared.nempty = sem_open(SEM_NEMPTY, O_CREAT | O_EXCL, 0660, NBUFF);
} //if
shared.nstored = sem_open(SEM_NSTORED, O_CREAT | O_EXCL, 0660, 0);
if(shared.nstored== SEM_FAILED && errno == EEXIST ){
printf("delete existing semaphore nstored\n");
sem_unlink(SEM_NSTORED);
shared.nstored = sem_open(SEM_NSTORED, O_CREAT | O_EXCL, 0660, 0);
} //if
shared.endprog = sem_open(SEM_PROGEND, O_RDONLY);
if(shared.endprog== SEM_FAILED && errno == EEXIST ){
printf("delete existing semaphore endprog\n");
sem_unlink(SEM_PROGEND);
shared.endprog = sem_open(SEM_PROGEND, O_RDONLY);
} //if
//sem_getvalue(shared.endprog, &items);
//printf("producer. items are %d.\n",items);
while(1){
sem_wait(shared.nempty); /* wait for at least 1 empty slot */
sem_wait(shared.mutex);
shared.buff[i % NBUFF] = i; /* store i into circular buffer */
items--;
sem_post(shared.mutex);
sem_post(shared.nstored); /* 1 more stored item */
//프로그램 end 확인.
//consum에서 nitems를 받지 말고,
//동기화 잘 된다는 생각으로
//prod, consum 양쪽 카운터..
//목표 카운터 도달하면 모두 종료.
//if(items<=0) break;
}
sem_unlink(SEM_MUTEX);
sem_unlink(SEM_NEMPTY);
sem_unlink(SEM_NSTORED);
sem_unlink(SEM_PROGEND);
return 0;
}
pi@raspberrypi:~/Project/cCode/IPC $ cat named_semaphore_consu.c
/* include main */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <errno.h>
#define NBUFF 10
#define SEM_MUTEX "mutex" /* these are args to px_ipc_name() */
#define SEM_NEMPTY "empty"
#define SEM_NSTORED "nstored"
#define SEM_PROGEND "end"
int nitems; /* read-only by producer and consumer */
struct { /* data shared by producer and consumer */
int buff[NBUFF];
sem_t *mutex, *nempty, *nstored, *endprog;
} shared;
int
main(int argc, char **argv)
{
int i;
if (argc != 2)
perror("usage: prodcons1 <#items>");
nitems = atoi(argv[1]);
/* 4create three semaphores */
shared.mutex = sem_open(SEM_MUTEX, O_RDWR);
shared.nempty = sem_open(SEM_NEMPTY, O_RDWR);
shared.nstored = sem_open(SEM_NSTORED, O_RDWR);
shared.endprog = sem_open(SEM_PROGEND, O_CREAT | O_EXCL, 0660, nitems);
if(shared.endprog == SEM_FAILED && errno == EEXIST ){
printf("delete existing semaphore endprog\n");
sem_unlink(SEM_PROGEND);
shared.endprog = sem_open(SEM_PROGEND, O_CREAT | O_EXCL, 0660, nitems);
} //if
for (i = 0; i < nitems; i++) {
sem_wait(shared.nstored); /* wait for at least 1 stored item */
sem_wait(shared.mutex);
if (shared.buff[i % NBUFF] = i)
printf("buff[%d] = %d\n", i, shared.buff[i % NBUFF]);
sem_post(shared.mutex);
sem_post(shared.nempty); /* 1 more empty slot */
}
return 0;
}