Beispiel #1
0
void init_platforms(){
	if(signal(SIGUSR1, train_leaving_signal) == SIG_ERR){
		printf("%s\n", "Error in catching SIGUSR1");
  	}
	int shmid;
	shmid = allocateSharedMemory(sizeof(int), SHMTOKEN);
   	platform_pid = (int *) mapSharedMemory(shmid);
	int i;
	for (i = 0; i< PLATFORMS; i++){
		int pipefd[2];
		pipe(pipefd);
		int pid = fork();
		print_error(pid, "Faield to fork");
		if (pid == 0){
			close(pipefd[1]);
			char *port_arg = (char*)malloc(sizeof(char)*20);
			sprintf(port_arg,"%d",platforms[i].port);
			char *pno = (char*)malloc(sizeof(char)*20);
			sprintf(pno, "%d", platforms[i].pno);
			dup2(pipefd[0],0);
			execl("platform.out", "platform.out", pno, port_arg, (char*)0);
		}
		else{
			close(pipefd[0]);
			platforms[i].pid = pid;
			platforms[i].pfd = pipefd[1];
		}
	}
}
Beispiel #2
0
//DESCRIPTION: Initalize the slab object. This is an abstraction for a contiguous block of objects that can be index into using
//the id of the object. The slab works exclusively with mmap'd regions. Thus it takes a file descriptor and does an ftruncate if
//it needs to resize the region. Also, the slab is a homogenous storage device, so it only stores the same type (size) of objects
ObjectSlab * initObjectSlab(char * segmentName, void * initAddress, int sizeOfType, int slabIncrementSize){
	ObjectSlab * slab = malloc(sizeof(ObjectSlab));
	if (slab) {
		int sizeInBytes=0;
		openSharedMemory(slab,segmentName,&sizeInBytes);
		if (slab->shm_fd == -1){
			perror("creating shared memory segment failed");
			return NULL;
		}
		if (sizeInBytes == 0){
			sizeInBytes=slabIncrementSize * sizeOfType + sizeof(ObjectSlabHeader);
			int result = ftruncate(slab->shm_fd, sizeInBytes);	//make the file bigger
			if (result==-1){
				perror("An error occured when trying to truncate a file");	
				return NULL;
			}
		}
		slab->objects = mapSharedMemory(slab, sizeInBytes, initAddress);
		if (!slab->objects){
			perror("failed to map the segment into the address space");
			return NULL;
		}
		
		slab->slabIncrementSize = slabIncrementSize;
		slab->sizeInObjects = (sizeInBytes - sizeof(ObjectSlabHeader))/sizeOfType;
		slab->sizeOfType = sizeOfType;
	}
	return slab;
}
Beispiel #3
0
int main(){
	signal(SIGUSR1, _signal);
	int token = 123413;
	int shmid;
	shmid = allocateSharedMemory(sizeof(int), token);
	printf("shmid is %d\n", shmid);
	PID = (int *) mapSharedMemory(shmid);
	*(PID) = 0;

	_p2();
	_p6();
	while(1){
		sleep(7);
		printf("[p1->hey]\n");
		fflush(stdout);
		// sleep(10);
	}

}
Beispiel #4
0
int main(){

	token = ftok(".",'r');
	shmid = allocateSharedMemory(sizeof(struct sems), token);
	s = (struct sems *) mapSharedMemory(shmid);
	s->x = 0;
	s->y = 1;

	if(signal(SIGINT, clean) == SIG_ERR){
		printf("%s\n", "Error in catching SIGINT");
	}

	printf("P1.this is x %d \n",s->x);
	printf("P1.this is y %d \n",s->y);

	sem_init(&s->s1, 0, token+1);
	sem_init(&s->s2, 0, token+2);

	// printf("P1.this is s1 %d \n",s->s1);
	// printf("P1.this is s2 %d \n",s->s2);
	while(1){
		printf("P1 just wrote X as %d\n", s->x);
		sem_up(&s->s1);
		sem_down(&s->s2);
		int y = s->y;
		s->x = y+1;
	}

	raise(SIGINT);
	// printf("unlocked\n");
	// sem_up(&s->s1);
	// printf("first\n");
	// sem_down(&s->s1);
	// printf("second\n");
	// sem_down(&s->s1);
	// printf("fourth\n");
}