Exemple #1
0
int main(int argc, char **argv)
{
	if (argc != 3)
		systemError("Wrong number of parameters");
	
	if (signal(SIGINT, exitServer) == SIG_ERR)
		systemError("signal");
	
	int K = atoi(argv[1]);
	int N = atoi(argv[2]);
	mainThread = pthread_self();
	pthread_attr_t attr;
	
	createResources(K, N);
	createIPC();
	initiateThreads(&attr);
	
	fprintf(stderr,"Server prepared to work\n");
	
	for (;;) {
		pid_t newClient = getNewClientPid();
		createClientThread(newClient, &attr);
	}
	
	endSafe();
	return 0;
}
Exemple #2
0
int main(int argc, char *argv[])
{
	ind = atoi(argv[1]);
	quit = 0;
	createIPC(ind);
	signal(SIGINT, sigHand);
	printf("created %d\n",ind );
	while(quit==0)
	{
		waitForClient();
		printf("Came into while now\n");
		showMemory();
	}
	if(shmdt(data) == -1)
	{
        perror("shmdt");
        exit(1);
    }
    if(shmctl(shmid, IPC_RMID, NULL) == -1)
    {
    	perror("shmctl");
    	exit(1);
    }
    if(msgctl(msgqid, IPC_RMID, NULL) == -1)
	{
        perror("msgctl");
        exit(1);
    }
	return 0;
}
Exemple #3
0
int main(int argc, char *argv[]) {

	initializeSigHandler();

	setpgid(0, 0);
	int i = 0;
	int ret = 0;
	pid_t pid;
	cmpQty = argc - 2;
	cmp = malloc(sizeof(company*) * cmpQty);
	map = malloc(sizeof(graph));
	sem_id = createSem(SEM_CREAT_KEY, cmpQty + SEM_QTY);
	setAllSem(sem_id, cmpQty + SEM_QTY, 0);

	if (initPIDS() == MALLOC_ERROR
	)
		return MALLOC_ERROR;

	/***PARSER***/
	if (cmp == NULL || map == NULL
	)
		return parserError(NULL, NULL, MALLOC_ERROR, NULL);
	if ((ret = parseFiles(argc, argv, map, cmp)) != EXIT_SUCCESS
	)
		return ret;

	/***INITIALIZES ARRAYS FOR THE EXECL***/

	initializeArray(&semMsg, sem_id);
	initializeArray(&procQty, ID_QTY + cmpQty);

	/***MAP AND COMPANIES CREATION***/
	if (createIPC(MAIN_ID, cmpQty + ID_QTY) == ERROR
	)
		return ERROR;

	int map_pid = 0;
	int io_pid = 0;
	int mult_pid = 0;
	if ((pid = fork()) == 0) {
		execl("io", semMsg, procQty, NULL);
		exit(1);
	} else {
		io_pid = pid;
		addPid(io_pid);
		if ((pid = fork()) == 0) {
			execl("map", semMsg, procQty, NULL);
			exit(1);
		} else {
			map_pid = pid;
			addPid(map_pid);
			if ((pid = fork()) == 0) {
				execl("multitasker", semMsg, procQty, NULL);
				exit(1);
			} else {
				mult_pid = pid;
				addPid(mult_pid);
				for (i = 0; i < cmpQty; i++) {
					if ((pid = fork()) == 0) {
						char *buf = NULL;
						int id = CMP_ID + i;
						initializeArray(&buf, id);
						execl("company", buf, procQty, semMsg, NULL);
					} else {
						addPid(pid);
					}
				}
			}
		}
	}
	upSem(sem_id, MAIN_ID);
	downSem(sem_id, IO_ID);
	downSem(sem_id, MULTITASKER_ID);
	for (i = 0; i < cmpQty; i++) {
		downSem(sem_id, CMP_ID + i);
		if (initializeCmp(cmp[i], MAIN_ID, CMP_ID + i) == ERROR
		)
			return ERROR;
	}
	downSem(sem_id, MAP_ID);
	if (initializeMap(map, MAIN_ID, MAP_ID) == ERROR) {
		return ERROR;
	}
	downSem(sem_id, MAP_ID);

	for (i = 0; i < cmpQty; i++) {
		wait(0); //Waits for all the companies to finish
	}
	kill(mult_pid, SIGTERM);
	waitpid(mult_pid, 0, 0);

	kill(map_pid, SIGTERM);
	waitpid(map_pid, 0, 0);

	kill(io_pid, SIGTERM);
	waitpid(io_pid, 0, 0);

	if (disconnectFromIPC(MAIN_ID) == ERROR) {
		destroyIPC(MAIN_ID);
	}
	destroySem(sem_id);

	/***FREES***/
	freeAll();

	return EXIT_SUCCESS;

}