Example #1
0
int server(void)
{
   int s = mseminit(SEM0, 0600, 2, 0, 1);
   if (s == -1) {
      perror("mseminit");
      return 1;
   }

   int cnt = 1;

   for (int i = 0; i < 5; ++i) {
      if (mP(s, 1, 0) != 0) {
         if (errno == EINTR) {
            /* interrupted by syscall, try again */
            continue;
         }
         perror("mP(0)");
         break;
      }

      printf("s%d", cnt);
      cnt += 2;
      fflush(stdout);

      if (mV(s, 1, 1) != 0) {
         perror("mV(1)");
         break;
      }
   }

   /* clean up semaphores */
   if (msemrm(s) != 0) {
      perror("msemrm");
   }

   return 0;
}
Example #2
0
/** Initialize all variables needed for ipc */
static void ipc_init(void)
{

	/* All communicating processes need to be started from within the ssame working dir in order to make key have the same value */
	key_t key = ftok(".", 'x');

	if(key == -1) {
		bail_out(EXIT_FAILURE, "Couldn't create ipc key via ftok");
	}

	/* cnd is a two field semaphore (0 = write, 1 = read)*/
	if((cnd = mseminit(key, (0600), 2, 1, 0)) == -1) {
		if((cnd = msemgrab(key, 2)) == -1) {
			bail_out(EXIT_FAILURE, "Error aquiring conditional semephores");
		}
	}

	/* mtx is a mutex semaphore to protect access to shm */
	if((mtx = seminit(key + 1, (0600), 0)) == -1) {
		if((mtx = semgrab(key + 1)) == -1) {
			bail_out(EXIT_FAILURE, "Error aquiring mutual exclusive semephore");
		}
	}
	

	/* Create or fetch shm segment */
	if((shm = shmget(key, sizeof(ipc_data_t), IPC_CREAT | IPC_EXCL | (0600))) == -1) {

		if((shm = shmget(key, sizeof(ipc_data_t), 0)) == -1) {
			bail_out(EXIT_FAILURE, "Error aquiring shared mem");
		}

		/* The process that creates the shm segment will up the mtx, after it's initialized */
		if(semdown(mtx) == -1) {
			bail_out(EXIT_FAILURE, "Error downing mutex");

		}

		/* Mount shm */
		if((shared = shmat(shm, NULL, 0)) == (void *) -1) {
			bail_out(EXIT_FAILURE, "Error attaching shm segment");
		}

#ifndef _BUILD_READIN
		/* Signal listening reader */
		shared->flag |= READER_F;	
#endif

		/* Let other processes proceed */
		if(semup(mtx) == -1) {
			bail_out(EXIT_FAILURE, "Error upping mutex");
		}

	} else {

		/* Mount shm */
		if((shared = shmat(shm, NULL, 0)) == (void *) -1) {
			bail_out(EXIT_FAILURE, "Error attaching shm segment (creator)");
		}

		/* If we created the segment, we'll initialize it */
		(void) memset(shared, 0, sizeof(ipc_data_t));

#ifndef _BUILD_READIN
		/* Signal listening reader */
		shared->flag |= READER_F;	
#endif

		/* And signal other processes to proceed */
		if(semup(mtx) == -1) {
			bail_out(EXIT_FAILURE, "Error opening mutex");
		}
		
	} 

}