예제 #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) ;
}
예제 #2
0
파일: feux.c 프로젝트: borgaaydin/3TC
int main(){
  signal(SIGQUIT, quit);
  signal(SIGINT, quit);

  signal(SIGUSR2, priority);
  signal(SIGUSR1, feux);

  key_t cle_shmem = KEY_SHMEM;
  key_t key_mutex = KEY_MUTEX;

  if((int)(id_mutex = open_semaphore(key_mutex)) == -1) {
			printf("Impossible d'ouvrir le mutex.\n");
			quit();
	}

  if((int)(id_shmem = open_shmem(cle_shmem, shmem_size)) == -1) {
      printf("Feux : Impossible d'ouvrir la mémoire partagée.\n");
      quit();
  }
  if((int)(pshmem = attach_shmem(id_shmem)) == -1) {
      printf("Feux : Impossible de s'attacher à la mémoire partagée.\n");
      quit();
  }
  printf("PID Feux : %d\n", getpid());

  down(id_mutex);
  pshmem[PID_FEUX]=getpid();
  up(id_mutex);

	feux();
}
예제 #3
0
void attach_to_feeder_shmem() {
    char path[MAXPATHLEN];
    strncpy(path, config.project_dir, sizeof(path));
    get_key(path, 'a', sema_key);
    int i, retval;
    void* p;

    retval = attach_shmem(config.shmem_key, &p);
    if (retval || p==0) {
        log_messages.printf(MSG_CRITICAL,
            "Can't attach shmem: %d (feeder not running?)\n",
            retval
        );
        log_messages.printf(MSG_CRITICAL,
            "uid %d euid %d gid %d eguid%d\n",
            getuid(), geteuid(), getgid(), getegid()
        );
        send_message(
            "Server error: feeder not running", 3600
        );
        exit(0);
    } else {
        ssp = (SCHED_SHMEM*)p;
        retval = ssp->verify();
        if (retval) {
            log_messages.printf(MSG_CRITICAL,
                "shmem has wrong struct sizes - recompile\n"
            );
            send_message("Server error: recompile needed", 3600);
            exit(0);
        }

        for (i=0; i<10; i++) {
            if (ssp->ready) break;
            log_messages.printf(MSG_DEBUG,
                "waiting for ready flag\n"
            );
            sleep(1);
        }
        if (!ssp->ready) {
            log_messages.printf(MSG_CRITICAL,
                "feeder doesn't seem to be running\n"
            );
            send_message(
                "Server error: feeder not running", 3600
            );
            exit(0);
        }
    }

    all_apps_use_hr = true;
    for (i=0; i<ssp->napps; i++) {
        if (!ssp->apps[i].homogeneous_redundancy) {
            all_apps_use_hr = false;
            break;
        }
    }
}
void generateurTrafficPrioritaire(){

	key_t key_shmem = KEY_SHMEM;
	key_t key_mutex = KEY_MUTEX;

	if((id_mutex = open_semaphore(key_mutex)) == -1) {
			printf("Impossible d'ouvrir le mutex.\n");
			quit();
	}

	if((int)(id_shmem = open_shmem(key_shmem, shmem_size)) == -1) {
			printf("generateurTrafficPrioritaire : Impossible d'ouvrir la mémoire partagée.\n");
			quit();
	}

	if((int)(pshmem = attach_shmem(id_shmem)) == -1) {
			printf("generateurTrafficPrioritaire : Impossible de s'attacher à la mémoire partagée.\n");
			quit();
	}

	down(id_mutex);
	pshmem[SRC_PRIO] = 0;
	pshmem[DEST_PRIO] = 0;
	pshmem[ID_PRIO] = 0;
	pshmem[PID_PRIO]=getpid();
	up(id_mutex);

	int pid_coord = pshmem[PID_COORD];
	printf("PID generPrio : %d\n", getpid());
	printf("PID Coord : %d\n", pid_coord);


	int timer, source, dest, id=0;
	srand(time(NULL));
	for(;;){
		timer=rand()%26;
		while(timer<15){
			timer=rand()%26;
		}
		printf("Timer is UP: %d s\n", timer);
		sleep(timer);
		printf("Timer is DOWN: %d s\n", timer);
		source=(rand()%4)+1;
		dest=(rand()%4)+1;
		while(dest==source){
			dest=(rand()%4)+1;
		}
		down(id_mutex);
		pshmem[SRC_PRIO] = source;
		pshmem[DEST_PRIO] = dest;
		pshmem[ID_PRIO] = id;
		up(id_mutex);
    	kill(pid_coord, SIGUSR1);
		printf("! --- PRIORITAIRE --- ! Source: %d, Dest: %d, ID :%d\n", source, dest, id);
		printf("Coordinateur is notified by signal !\n");
		id++;
	}
}
int main() {
    SCHED_SHMEM* ssp;
    int retval;
    void* p;

    retval = config.parse_file();
    if (retval) {
        printf("Can't parse config.xml: %s\n", boincerror(retval));
        exit(1);
    }
    retval = attach_shmem(config.shmem_key, &p);
    if (retval) {
        printf("can't attach shmem: key %x\n", config.shmem_key);
        exit(1);
    }
    ssp = (SCHED_SHMEM*)p;
    retval = ssp->verify();
    ssp->show(stdout);
}
예제 #6
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) ;	
}
예제 #7
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;

}