memory based semaphore, unix network programming p241

이름있는 세마포어 말고 메모리에 올려 사용하는 방법도 있다. 책은 sem_init(…, 두번째 arg,…)에서 두번째 입력하는 숫자를 thread에서 공유할 거면 0, 프로세스간 할거면 1로 하라고 한다. 1로 하여 thread도 잘 된다.

named semaphore는 포인터를 선언했고 파일에 영역을 만들었다. 이와 다르게 직접 데이터를 선언하고 주소로 넘겨줘야 한다.

ipcs로 보일 법 한데, 안 보인다. 무엇이 잘못되었는지 모르겠다.

/* include main */
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <unistd.h>
#define	NBUFF	 10
#define	SEM_MUTEX	"mutex"	 	/* these are args to px_ipc_name() */
#define	SEM_NEMPTY	"empty"
#define	SEM_NSTORED	"nstored"

int		nitems;					/* read-only by producer and consumer */
struct {	/* data shared by producer and consumer */
  int	buff[NBUFF];
  //포인터 대신 세마포어로 변경.
  //sem_t	*mutex, *nempty, *nstored;
  sem_t	mutex, nempty, nstored;		/* semaphores, not pointers */

} shared;

void	*produce(void *), *consume(void *);

int
main(int argc, char **argv)
{
	pthread_t	tid_produce, tid_consume;

	if (argc != 2)
		perror("usage: prodcons1 <#items>");
	nitems = atoi(argv[1]);
		/* 4create three semaphores */
	//memory based로 변경.
	/*
	shared.mutex = sem_open(SEM_MUTEX, O_CREAT | O_EXCL,
							0660, 1);
	shared.nempty = sem_open(SEM_NEMPTY, O_CREAT | O_EXCL,
							 0660, NBUFF);
	shared.nstored = sem_open(SEM_NSTORED, O_CREAT | O_EXCL,
							  0660, 0);
							  */


	//두번째 파라미터를 0으로 설정해야 thread간 전달.
	//sem_init(&shared.mutex, 1, 1) 설정해도 잘 됨..

	sem_init(&shared.mutex, 0, 1);
	sem_init(&shared.nempty, 0, NBUFF);
	sem_init(&shared.nstored, 0, 0);


		/* 4create one producer thread and one consumer thread */
	//pthread_setconcurrency(2);
	//pthread_setconcurrency();
	pthread_create(&tid_produce, NULL, produce, NULL);
	pthread_create(&tid_consume, NULL, consume, NULL);

		/* 4wait for the two threads */
	pthread_join(tid_produce, NULL);
	pthread_join(tid_consume, NULL);

		/* 4remove the semaphores */
	//메모리 베이스로 변경.
	/*
	sem_unlink(SEM_MUTEX);
	sem_unlink(SEM_NEMPTY);
	sem_unlink(SEM_NSTORED);
	*/


	//ipcs로 확인하기위해 대기
	sleep(10);

	sem_destroy(&shared.mutex);
	sem_destroy(&shared.nempty);
	sem_destroy(&shared.nstored);

	exit(0);
}
/* end main */

/* include prodcons */
void *
produce(void *arg)
{
	int		i;

	for (i = 0; i < nitems; i++) {

		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 */
		sem_post(&shared.mutex);
		sem_post(&shared.nstored);	/* 1 more stored item */


	}
	return(NULL);
}

void *
consume(void *arg)
{
	int		i;
	int tmpnempty, tmpmutex, tmpnstored;

	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(NULL);
}
/* end prodcons */
pi@raspberrypi:~/Project/cCode/IPC $ ipcs

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages    

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     

ipcs는 system V 기능이라 posix semaphore는 해당하지 않는다. 보이지 않음이 당연하다.

코멘트

댓글 남기기

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