Exemple #1
0
	void Jerk::update(ApplicationData appData)
	{
		GameEntity::update(appData);
		if(isMovementEnabled())
		{
			Vector2i moveDir = getMovementDir();
			if(moveDir.x!=0 || moveDir.y!=0)
			{
				String dirString = getDirString(moveDir);
				changeAnimation("walk_"+dirString, Animation::NO_CHANGE);
				dir = moveDir;
				Vector2d pos = getPosition();
				double moveSpeed = 24;
				double fsMult = appData.getFrameSpeedMultiplier();
				pos += ((Vector2d)dir)*Vector2d(moveSpeed*fsMult,moveSpeed*fsMult);
				setPosition(pos);
			}
			else
			{
				String dirString = getDirString(dir);
				changeAnimation("stand_"+dirString, Animation::NO_CHANGE);
			}
		}
	}
void main(int argc,char* argv[])
{
	semID = semget((key_t)SEMAPHORE_KEY,6,IPC_CREAT|0666);
	float probability;

	if(argc!=2)
	{
		printf("Please provide probability as argument ! \n");
		exit(1);
	}
	else
	{
		sscanf(argv[1],"%f",&probability);
		if(probability < 0 || probability > 1)
		{
			printf("Please run the program with valid probability value : [0-1] \n");
			exit(1);
		}
	}

	char train_sequence[MAX_SEQUENCE_LENGTH];

	int N,M=4;

	if((N=parseInputFile(train_sequence))==-1)
	{
		printf("Error : Cannot open sequence.txt !\n");
		exit(0);
	}

	printf("The sequence scanned from file : %s, N = %d\n",train_sequence,N);

	initializeAllSubSemaphoreValues();

	initializeMatrixFile(N,M);

	srand(time(NULL));

	int sequence_counter = 0,flag=0;

	while(1)
	{
		if(flag)
		{
			sleep(1);
			if(checkForDeadlock(N,M))
				break;
			continue;
		}

		if( ( (float)(rand()%1000) / 1000.0) < probability )
		{
			if(checkForDeadlock(N,M))
				break;
		}
		else
		{
			if(sequence_counter==N)
				flag = 1;
			else
			{
				char c = train_sequence[sequence_counter];
				sequence_counter++;
				if(c!='N' && c!='S' && c!='W' && c!='E')
				{
					printf("Invalid Sequence in sequence.txt !\n");
					exit(1);
				}
				else
				{
					/*
					char command[50];
					sprintf(command,"./train %c %d &",c,sequence_counter-1);
					pid_arr[sequence_counter-1] = pid;
					direction_arr[sequence_counter-1] = getDirString(c);
					system(command);
					*/
					
					int pid = fork();
					if(pid == 0)
					{
						//char command[50];
						//sprintf(command,"./train %d %c %d",N,c,sequence_counter-1);
						
						char** argv = malloc(4*sizeof(char*));
						int y;
						for(y=0;y<4;y++) argv[y] = malloc(10*sizeof(char));
						strcpy(argv[0],"./train");
						sprintf(argv[1],"%d",N);
						sprintf(argv[2],"%c",c);
						sprintf(argv[3],"%d",sequence_counter-1);

						int retv = execvp(*argv,argv);
						if(retv == -1) {perror("Error in execvp ! \n"); exit(1);}
						exit(0);
					}
					printf("Train %d: %s Train started\n",pid,getDirString(c));
					pid_arr[sequence_counter-1] = pid;
					//printf("pid_arr[%d] = %d\n",sequence_counter-1,pid );
					direction_arr[sequence_counter-1] = getDirString(c);
					//printf("direction_arr[%d] = %s\n",sequence_counter-1,getDirString(c) );
					
				}
			}
			
		}
	}

}