Esempio n. 1
0
int main()
{
    int key = 123 ;
    int memsize = sizeof(int) ;

    int shmemId = open_shmem(key, memsize) ;
    if (shmemId == -1) {
        perror("shared segment opening error") ;
        exit(EXIT_FAILURE) ;
    }

    int* buffer = (int*) attach_shmem(shmemId) ;
    *buffer = 0 ;

    int emptySemKey = 100 ;
    int emptySemId = open_semaphore(emptySemKey) ;

    int fullSemKey = 101 ;
    int fullSemId = open_semaphore(fullSemKey) ;

    if ((emptySemId == -1) || (fullSemId == -1)) {
        perror("semaphore opening error") ;
        exit(EXIT_FAILURE) ;
    }

    while (*buffer >= 0) {
        down(fullSemId) ;
        if (*buffer >= 0)
            printf("%d\n", *buffer) ;
        up(emptySemId) ;
    }

    detach_shmem(buffer) ;
}
Esempio n. 2
0
int main()
{
	int key = 123 ;
	int memsize = sizeof(int) ;	
	
	int shmemId = create_shmem(key, memsize) ;
	if (shmemId == -1) {
		perror("shared segment creation error") ;
		exit(EXIT_FAILURE) ;
	}
	
	int* buffer = (int*) attach_shmem(shmemId) ;
	*buffer = 0 ;
	
	int emptySemKey = 100 ;
	int emptySemId = create_semaphore(emptySemKey) ;
	
	int fullSemKey = 101 ;
	int fullSemId = create_semaphore(fullSemKey) ;
	
	if ((emptySemId == -1) || (fullSemId == -1)) {
		perror("semaphore creation error") ;
		exit(EXIT_FAILURE) ;
	}
	
	init_semaphore(fullSemId, 0) ;
	init_semaphore(emptySemId, 1) ;
	
	while (*buffer >= 0) {
		down(emptySemId) ;
		if (!scanf("%d", buffer))
			*buffer = -1 ;
		up(fullSemId) ;
	}

	remove_semaphore(fullSemId) ;
	remove_semaphore(emptySemId) ;
	detach_shmem(buffer) ;
	remove_shmem(shmemId) ;	
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
  int size = 0;
  int shared_memory_key = -1;
  int sem_r = -1;
  int sem_w = -1;
  int cnt = -1;
  int buf = -1;
  int *shared_memory = NULL;

  errno = 0;


  /* check command line arguments */
  if ((size = check_arguments(argc, argv))==ERROR)
    {
      print_usage();
      return ERROR;
    }
  /* get both semaphores */
  if((shared_memory_key = get_shmem(SHMEMKEY, size)) == -1)
    {
      clean_up(sem_r, &shared_memory, shared_memory_key, ERROR);
      return ERROR;
    }
  attach_shmem(shared_memory_key, &shared_memory, SEMKEY_R);
  if(shared_memory == NULL)
    {
      clean_up(sem_r, &shared_memory, shared_memory_key, ERROR);
      return ERROR;
    }
  /* initialize shared memory */
  if((sem_r= get_semid(SEMKEY_R, 0)) == ERROR)
    {
      clean_up(sem_r, &shared_memory, shared_memory_key, ERROR);
      return ERROR;
    }
  /* attach memory to process */
  if((sem_w= get_semid(SEMKEY_W, size))==ERROR)
    {
      clean_up(sem_r, &shared_memory, shared_memory_key, ERROR);
      return ERROR;
    }

  /* start the reader */
  do
    {
      if(P(sem_r)==-1)
	{
	  if(errno == EINTR)
            {
	      /*EINTR sagt uns, dass ein Interrupt vorliegt. */
	      errno = 0;
	      continue;
            }
	  else
            {
	      clean_up(sem_r, &shared_memory, shared_memory_key, ERROR);
	      fprintf(stderr, "Error bei P, reader. %s", strerror(errno));
	      return ERROR;
            }
        }
      cnt++;
      cnt = cnt%size;
      buf = (*(shared_memory+cnt));

      if(V(sem_w)==-1)
	{
	  if(errno == EINTR)
            {
	      /*EINTR sagt uns, dass ein Interrupt vorliegt. */
	      errno = 0;
	      continue;
            }
	  else
            {
	      clean_up(sem_r, &shared_memory, shared_memory_key, ERROR);
	      fprintf(stderr, "Error bei V, reader. %s", strerror(errno));
	      return ERROR;
            }
        }
      if(buf!=EOF)
        {
	  /* Do ouput and check for Output error */
	  if (fputc(buf, stdout) == EOF) {
	    fprintf(stderr, "Fehler fputc");
	    clean_up(sem_r, &shared_memory, shared_memory_key, ERROR);
	    return ERROR;

	  }
        }
    }while(buf!=EOF);

  errno = 0;
  if(fflush(stdout)!=0)
    {
      fprintf(stderr, "fflush failed");
      if(errno != 0)
        {
	  fprintf(stderr, "%s", strerror(errno));
	  return ERROR;
        }
    }

  detach_shmem(shared_memory_key,SEMKEY_R);
  clean_up(sem_r, &shared_memory, shared_memory_key, SEMKEY_R);
  return 0;

}