示例#1
0
int send(const char* filename)
{
	int flagid = 0;
	int master_sender = is_sender(&flagid);
	if (!master_sender)
		exit(EXIT_SUCCESS);

	int shmemid = 0;
	void* smbuf = get_memptr( FILE_NAME_SHMEM_ATTACH,
				  BUF_SIZE + sizeof(long),
				  &shmemid);
	CHECK(smbuf != (void*)-1, "Failed to get shared memory pointer");
	long* nbytes_to_save = smbuf + BUF_SIZE;
	*nbytes_to_save = 0;

	CHECK(smbuf != (void*)-1, "Failed to get memory pointer");

	int semid = get_sems(  FILE_NAME_SHMEM_ATTACH, 5);
	CHECK(semid != -1, "Failed to get semaphors id");

	int cond = snd_protect_connection(semid);
	CHECK(cond == 0, "Failed to set up protected connection");

	int fileid = open(filename, O_RDONLY);
	CHECK(fileid != -1, "Failed to open file to send");

	struct sembuf rcv_activate[2] = {
		{RCV_MUTEX,   1, 0},
		{RCV_CONNECT, 1, 0}
		};
	cond = semop(semid, rcv_activate, 2);
	CHECK(cond != -1, "Failed to connect to receiver");

	struct sembuf mutexes[3] = {
		{SND_MUTEX,  1, 0},
		{RCV_MUTEX, -1, 0},
		{SND_MUTEX,  0, 0}
	};
	int nbytes = 0;
	while ((nbytes = read(fileid, smbuf, BUF_SIZE)) > 0)
	{
		*nbytes_to_save = nbytes;
		OUT("# Sending\n");
		cond = semop(semid, &mutexes[0], 1);
		cond = semop(semid, &mutexes[1], 1);
		cond = semop(semid, &mutexes[2], 1);
		CHECK(cond == 0, "# Failed to set semaphors");
		OUT("# Package sent\n");
	}

	OUT("# Finishing...\n");
	*nbytes_to_save = -1;
	cond = semop(semid, &mutexes[0], 1);
	cond = semop(semid, &mutexes[1], 1);
	cond = semop(semid, &mutexes[2], 1);
	OUT("# Finished!\n");

	cond = snd_clean(semid, fileid, shmemid, flagid);
	exit(EXIT_SUCCESS);
}
示例#2
0
int receive()
{
	int flagid = 0;
	int master_receiver = is_receiver(&flagid);
	if (!master_receiver)
		exit(EXIT_SUCCESS);

	int semid = get_sems(  FILE_NAME_SHMEM_ATTACH, 5);
	CHECK(semid != -1, "Failed to get semaphors id");

	int cond = rcv_protect_connection(semid);
	CHECK(cond == 0, "Failed to set up protected connection");

	int shmemid = 0;
	void* smbuf = get_memptr( FILE_NAME_SHMEM_ATTACH,
				  BUF_SIZE + sizeof(long),
				  &shmemid);
	CHECK(smbuf != (void*)-1, "Failed to get shared memory pointer");
	struct sembuf activate = {RCV_CONNECT, -1, 0};
	cond = semop(semid, &activate, 1);
	OUT("# Activated\n");

	struct sembuf mutexes[3] = {
		{SND_MUTEX, -1, 0},
		{RCV_MUTEX,  1, 0},
		{RCV_MUTEX,  0, 0}
	};
	long* nbytes_to_save = smbuf + BUF_SIZE;

	cond = semop(semid, &mutexes[2], 1);
	CHECK(cond == 0, "Failed to set semaphors");

	while (*nbytes_to_save != -1)
	{
		OUT1("# Printing %ld bytes\n", *nbytes_to_save);
		cond = write(STDOUT_FILENO, smbuf, *nbytes_to_save);

		if (cond != *nbytes_to_save)
			fprintf(stderr, "Printed = %d\nExpected = %ld\n", cond, *nbytes_to_save);

		CHECK(cond == *nbytes_to_save, "Printed ammount of bytes isn't valid");
		OUT("# Getting...\n");
		cond = semop(semid, &mutexes[1], 1);
		cond = semop(semid, &mutexes[0], 1);
		OUT1("# Current SND_MUTEX is %d\n", semctl(semid, SND_MUTEX, GETVAL));
		OUT1("# Current RCV_MUTEX is %d\n", semctl(semid, RCV_MUTEX, GETVAL));
		
		cond = semop(semid, &mutexes[2], 1);
		OUT("# Got package\n");
		CHECK(cond == 0, "Failed to set semaphors");
	};

	OUT("# Finishing receiving\n");
	semop(semid, &mutexes[0], 1);
	unlink(RCV_FLAG);
	semctl(flagid, 0, IPC_RMID);

	exit(EXIT_SUCCESS);
}
示例#3
0
int main(int argc, char *argv[]) {
  struct TData_t *data;
  struct TTask_t *task;
  int shm_data, shm_task;
  sem_t *sem_task_ready, *sem_task_read, *sem_task_processed;

  /* Get shared memory segments and semaphores */
  get_shm_segments(&shm_data, &shm_task, &data, &task);
  get_sems(&sem_task_ready, &sem_task_read, &sem_task_processed);

  /* One single iteration */
  get_and_process_task(sem_task_ready, sem_task_read, data, task);
  notify_task_completed(sem_task_processed);

  close_shared_memory_segments(shm_data, shm_task);

  return EXIT_SUCCESS;
}
int main(int argc,char *argv[]){
  int shm_symbol; /* Descriptor del archivo de simbolos */
  
  struct TSymbol_t *symbol; /* Estructura de datos de simbolos */

  sem_t *sem_symbol_ready; /* Indica que el simbolo esta listo */
  sem_t *sem_symbol_decoded; /* Indica que el simbolo esta decodificado */

  /* Crear segmentos de memoria compartida */
  get_shm_segments(&shm_symbol,&symbol);
  
  /* Obtener semaforos */
  get_sems(&sem_symbol_ready,&sem_symbol_decoded);

  while(1){
    get_and_process_task(sem_symbol_ready,sem_symbol_decoded,symbol);
  }
  
  return EXIT_SUCCESS;
}