int32_t main(int32_t argc, char **argv) {
	
	int32_t OCUPACAO_OK = atoi(argv[1]);
	int32_t MAX_OCUPACAO = atoi(argv[2]);
	int32_t POLITICA = atoi(argv[3]);
	int32_t MAX_FRAMES = atoi(argv[4]);
	int32_t idShM;
	int32_t idFrames;
	int32_t idSem;
	list *frames;
	int32_t *pShM = NULL;
	
	// Recupera area de memoria compartilhada com o Memory Manager.
	if((idShM = shmget(KEY_DAEMON_FRAMES, sizeof(int32_t), 0x1FF)) < 0) {
		printf("[ REPLACEMENT DAEMON ] Erro na recuperacao da memoria compartilhada com o Memory Manager: %d.\n", errno);
		exit(EXIT_FAILURE);
	}
	if((pShM = (int32_t*) shmat(idShM, (char*) 0, 0)) == (int32_t*)-1) {
		printf("[ REPLACEMENT DAEMON ] Erro no mapeamento da memoria compartilhada com o Memory Manager: %d.\n", errno);
		exit(EXIT_FAILURE);
	}
	
	// Recupera area memoria compartilhada com o Memory Manager para a lista de frames
	if((idFrames = shmget(KEY_DAEMON_LIST, MAX_FRAMES*sizeof(list), 0x1FF)) == -1) {
		printf("[ REPLACEMENT DAEMON ] Erro na recuperacao da memoria compartilhada com o Memory Manager: %d.\n", errno);
		exit(EXIT_FAILURE);
	}
	if((frames = (list *) shmat(idFrames, (char*) 0, 0)) == (list *) -1) {
		printf("[ REPLACEMENT DAEMON ] Erro no mapeamento da memoria compartilhada com o Memory Manager: %d.\n", errno);
		exit(EXIT_FAILURE);
	}
	
	// Criacao do semaforo com o Memory Manager.
	if((idSem = semget(KEY_SEMAPHORE, 1, 0x1FF)) == -1) {
		printf("[ REPLACEMENT DAEMON ] Erro na criacao do semaforo com o Memory Manager: %d.\n", errno);
		exit(EXIT_FAILURE);
	}
	
	printf("[ REPLACEMENT DAEMON ] Executando Replacement Daemon...\n");
	do {
		p_sem(idSem);
		if(*pShM >= MAX_OCUPACAO) {
			while(*pShM >= OCUPACAO_OK) {
				removePage(POLITICA, MAX_FRAMES, &frames);
				(*pShM)--;
				printf("[ REPLACEMENT DAEMON ] Remove página. Frames Ocupados: %d.\n", *pShM);
				v_sem(idSem);
				//sleep(1);
				p_sem(idSem);
			}
		}
		v_sem(idSem);
		sleep(2);
	} while(1);
	
	return EXIT_SUCCESS;
}
Exemple #2
0
void scheduler_FIFO(){

	int i,j,k,timeout,pid,status;
	int * processes_running;
	PROG_T prog;
	char cmd[50];


	i = 0;

	p_sem();
		for(i=0;i<NUM_TAB;i++){
			if((pshm[i].nreq < 0)&&(pshm[i].status == FINISHED)){
				pshm[i].nreq = 0;
				p_sem2();
					/*printf("Liberando antes: %d\n",proc_livres);*/
					proc_livres += pshm[i].num_proc;
					/*printf("Liberando depois: %d\n",proc_livres);*/
				v_sem2();
			}else if((pshm[i].nreq > 0)&&(pshm[i].status == RUNNING)){
				timeout = checkTime(i);
				if(timeout){
					
					sprintf(cmd,"pkill -P %d",pshm[i].pid);
					system(cmd);
					kill(pshm[i].pid,SIGKILL);

					pshm[i].nreq = 0;
					pshm[i].status = FINISHED;
					p_sem2();
						proc_livres += pshm[i].num_proc;
					v_sem2();

				}
			}
		}
	v_sem();

	

	p_sem();

		i = chooseReq();
		
		if((pshm[i].nreq > 0)&&(pshm[i].status == PENDING)&&(pshm[i].num_proc <= proc_livres)){
			
			p_sem2();
			/*printf("Alocando antes: %d\n",proc_livres);*/
			proc_livres -= pshm[i].num_proc;
			/*printf("Alocando depois: %d\n",proc_livres);*/
			v_sem2();

			
			prog.n_params = countParams(pshm[i].proc);
			getArgs(pshm[i],&prog);
			pshm[i].status = RUNNING;

			pid = fork();
			if(pid<0){
				printf("Erro no fork dispatcher\n");
				exit(-1);
			}else if(!pid){
				/*Processo dispatcher*/
				processes_running = (int*)calloc(pshm[i].num_proc,sizeof(int));

				for(j=0;j<pshm[i].num_proc;j++){

					processes_running[j] = fork();
					if(processes_running[j] < 0){
						printf("Erro no fork worker\n");
						exit(-1);
					}else if(processes_running[j] == 0){
						/*Processos worker*/

						execv(prog.nome,prog.params);
					}else{
						/*Processo dispatcher*/

						for(k=0;k<pshm[i].num_proc;k++){
							wait(&status);
							if((k == pshm[i].num_proc-1)&&(j==pshm[i].num_proc-1)){
								pshm[i].status = FINISHED;
								pshm[i].nreq = -1;

								exit(0);
							}
						}
					}
				}
			}else{
				pshm[i].pid = pid;
			}
		}
		
	v_sem();


	for(i=0;i<NUM_TAB;i++){
		if(pshm[i].status == FINISHED){
			waitpid(pshm[i].pid,&status,WNOHANG);
		}
	}

}
Exemple #3
0
int main (int argc, char* argv[]){

	FILE* fp;
	char arqName[50], temp[50];
	PROCESSO_T  aux;
	int idshm, id2shm;
	INFO_T *p2shm;
	PROCESSO_T *pshm,*paux;
	int i = 0;

	if(argc == 1){
		printf("Nenhum arquivo de entrada informado\n");
		exit(-1);
	}

	strcpy(arqName, argv[1]);

	fp = fopen(arqName, "r");
	if(fp == NULL){
		printf("Arquivo nao encontrado\n");
		exit(-1);
	}

	/*da um shmget na mem compartilhada*/	
	if ((idshm = shmget(90108094, NUM_TAB*sizeof(struct processo), IPC_CREAT|0x1ff)) < 0){
	    printf("erro na criacao da memoria compartilhada(idshm)\n");
	    exit(1);
	}

	/*da um attach na mem compartilhada*/
	pshm = (struct processo *) shmat(idshm, (char *)0, 0);
	if (pshm == (struct processo *)-1) {
        printf("erro no attach\n");
        exit(1);
    }

	/*da um shmget na mem compartilhada de bloqueio*/	
	if ((id2shm = shmget(90108012, sizeof(INFO_T), IPC_CREAT|0x1ff)) < 0){
	    printf("erro na criacao da memoria compartilhada(id2shm)\n");
	    exit(1);
	}

	/*da um attach na mem compartilhada de bloqueio para ver se pode escrever ou nao*/
	p2shm = (INFO_T *) shmat(id2shm, (char *)0, 0);
	if (p2shm == (INFO_T *)-1){
        printf("erro no attach\n");
        exit(1);
    }

	/*da um semget em semaforo para cria-lo*/
	if ((idsem = semget(90015266, 1, IPC_CREAT|0x1ff)) < 0){
	     printf("erro na criacao do semaforo\n");
	     exit(1);
	}


	getValue(&fp,aux.proc);/*pega o nome do programa*/
	getValue(&fp,aux.max_time);/*Pega o tempo m‡ximo de execucao*/
	getValue(&fp,temp);
	aux.num_proc = atoi(temp);/*Pega o numero de processos e transforma em inteiro*/
	aux.start_time = time(NULL);/*Pega o tempo de inicio da execucao*/
	aux.status = PENDING;


	/*printProcesso(aux);*/	

	p_sem();

		if(p2shm->write_permission != NAO_PODE_ESCREVER){
			i=0;

			paux = pshm;
			while(paux[i].nreq != 0 ){
				i++;
			}

			paux[i].nreq = ++p2shm->last_nreq;
			strcpy(paux[i].max_time,aux.max_time);
			paux[i].num_proc = aux.num_proc;
			paux[i].start_time = aux.start_time;
			paux[i].status = aux.status;
		   	strcpy(paux[i].proc,aux.proc); 
		   	paux[i].pid = 0;
		}else{
			printf("Gerenciador em processo de desligamneto\n");
		}


	v_sem();

	fclose(fp);
	return 0;
}
Exemple #4
0
int data_on_connection(int fd, callback_remove_handler remove)
{
	int nread;
	char *network_packet;
	char network_line[8192];
	char *p;
	unsigned long id;

	char string[1024];
	unsigned long msg_id = UINT32_MAX;

	int version;

	
	ioctl(fd, FIONREAD, &nread);

	univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "new connection data = %d\n",nread);

	if(nread == 0)
	{
		univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_PROCESS, "%d failed, got 0 close connection to listener ", fd);
		close(fd);
		FD_CLR(fd, &readfds);
		remove(fd);
		network_client_dump ();
		return 0;
	}


	if ( nread >= 8192 ) {

		univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ERROR, "%d failed, more than 8192 close connection to listener ", fd);
		close(fd);
		FD_CLR(fd, &readfds);
		remove(fd);

		return 0;
	}

	/* read the whole package */
	network_packet=malloc((nread+1) * sizeof(char));
	read(fd, network_packet, nread);
	network_packet[nread]='\0';

	memset(network_line, 0, 8192);
	p=network_packet;
	p_sem(sem_id);

	while ( get_network_line(p, network_line) ) {

		if ( strlen(network_line) > 0 ) {
			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "line = [%s]",network_line);
		}

		
		if ( !strncmp(network_line, "MSGID: ", strlen("MSGID: ")) ) {
			/* read message id  */

			msg_id=strtoul(&(network_line[strlen("MSGID: ")]), NULL, 10);

			p+=strlen(network_line);


		} else if ( !strncmp(network_line, "Version: ", strlen("Version: ")) ) {

			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "RECV: VERSION");

			id=strtoul(&(network_line[strlen("Version: ")]), NULL, 10);

			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "VERSION=%ld", id);

			if ( id < VERSION ) {
				network_client_set_version(fd, id);
			} else {
				network_client_set_version(fd, VERSION);
			}
			
			/* reset message id */
			msg_id = UINT32_MAX;

			p+=strlen(network_line);


		} else if ( !strncmp(network_line, "Capabilities: ", strlen("Capabilities: ")) ) {

			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "RECV: Capabilities");

			version=network_client_get_version(fd);

			if ( version > -1 ) {

				memset(string, 0, sizeof(string));
				
				snprintf(string, sizeof(string), "Version: %d\nCapabilities: \n\n", version);

				univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "SEND: %s", string);

				write(fd, string, strlen(string));

			} else {
				
				univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "Capabilities recv, but no version line");
				
			}

			p+=strlen(network_line);


		} else if ( !strncmp(network_line, "GET_DN ", strlen("GET_DN ")) && msg_id != UINT32_MAX && network_client_get_version(fd) > 0) {

			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "RECV: GET_DN");

			id=strtoul(&(network_line[strlen("GET_DN ")]), NULL, 10);

			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "id: %ld",id);

			if ( id <= notify_last_id.id) {

				char *dn_string = NULL;

				univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "try to read %ld from cache", id);

				/* try to read from cache */
				if ( (dn_string = notifier_cache_get(id)) == NULL ) {

					univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "%ld not found in cache", id);

					univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "%ld get one dn", id);

					/* read from transaction file, because not in cache */
					if( (dn_string=notify_transcation_get_one_dn ( id )) == NULL ) {

						univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "%ld failed ", id);
						/* TODO: maybe close connection? */

						univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ERROR, "%d failed, close connection to listener ", fd);
						close(fd);
						FD_CLR(fd, &readfds);
						remove(fd);

						return 0;
					}
				}

				if ( dn_string != NULL ) {

					snprintf(string, sizeof(string), "MSGID: %ld\n%s\n\n",msg_id,dn_string);

					univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "--> %d: [%s]",fd, string);

					write(fd, string, strlen(string));

					free(dn_string);

				}


			} else {
				/* set wanted id */

				network_client_set_next_id(fd, id);
				network_client_set_msg_id(fd, msg_id);

			}

			p+=strlen(network_line)+1;
			msg_id = UINT32_MAX;


		} else if ( !strncmp(network_line, "GET_ID", strlen("GET_ID")) && msg_id != UINT32_MAX  && network_client_get_version(fd) > 0) {

			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "RECV: GET_ID");

			memset(string, 0, sizeof(string));

			snprintf(string, sizeof(string), "MSGID: %ld\n%ld\n\n",msg_id,notify_last_id.id);

			write(fd, string, strlen(string));

			p+=strlen(network_line)+1;
			msg_id = UINT32_MAX;


		} else if ( !strncmp(network_line, "GET_SCHEMA_ID", strlen("GET_SCHEMA_ID")) && msg_id != UINT32_MAX  && network_client_get_version(fd) > 0) {

			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "RECV: GET_SCHEMA_ID");

			memset(string, 0, sizeof(string));

			snprintf(string, sizeof(string), "MSGID: %ld\n%ld\n\n",msg_id,SCHEMA_ID);

			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "--> %d: [%s]",fd, string);

			write(fd, string, strlen(string));

			p+=strlen(network_line)+1;
			msg_id = UINT32_MAX;


		} else if ( !strncmp(network_line, "ALIVE", strlen("ALIVE")) && msg_id != UINT32_MAX  && network_client_get_version(fd) > 0) {

			univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "RECV: ALIVE");

			snprintf(string, sizeof(string), "MSGID: %ld\nOKAY\n\n",msg_id);

			write(fd, string, strlen(string));

			p+=strlen(network_line)+1;
			msg_id = UINT32_MAX;

		} else {

			p+=strlen(network_line);

			if (strlen(network_line) == 0 ) {
				p+=1;
 			} else {
				univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ERROR, "Drop package [%s]", network_line);
			}

		}
	}
	v_sem(sem_id);

	univention_debug(UV_DEBUG_TRANSFILE, UV_DEBUG_ALL, "END Package");
	

	network_client_dump ();

	return 0;
}