Ejemplo n.º 1
0
/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{
	int pid = fork();
	if( pid ){
		if( fork() ){
			executeParent();
			printf("Parent waiting...\n");
			int status = 0;
			wait(&status); //wait for child process
			if( shm_unlink(SEM_NAME) == -1 ){
				perror( "sem unlink failed");
				exit(1);
			}
			if( shm_unlink(SHM_NAME) == -1 ){
				perror("shm unlink failed");
				exit(1);
			}
			printf("Parent done\n");
		} else {
			executeChild();
		}
	}else{
		executeChild();
	}
	return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
Archivo: main.c Proyecto: wnr/os13-lab2
void readAndExecute() {
    int readStatus;                     /* An variable to hold the return value of the readStatus function. */
    char input[COMMAND_MAX_LENGTH+2];    /* Array to be used for reading, with 2 extra char for newline and the '\0' char. */
    
    /* Read a line of max COMMAND_MAX_LENGTH command chars from stdin. */
    readStatus = readLine(input, COMMAND_MAX_LENGTH+2, stdin);
    
    /* Check the read line status */
    if(readStatus == 0) {
        /* Read was successful. */
        
        char *args[ARGUMENTS_MAX_LENGTH+2]; /* Array to hold all the arguments. 2 extra chars for command name and '\0' char. */
        
        /* Parse the command line by exploding command string. Args will then contain all arguments of the command. */
        explode(args, ARGUMENTS_MAX_LENGTH+2, input);
        
        /* Check for built-in commands. */
        if(executeCommand(args) != 0) {
            /* The command is not a built in command. The command should be executed externally. */
            
            struct timeval preExecute;      /* Structure to hold time info from pre execution of the command. */
            struct timeval postExecute;     /* Structure to hold time info from post execution of the command. */
            unsigned int elapsed;           /* The number of milliseconds needed for executing the command. */
            
            if(isBackgroundRequested(args, ARGUMENTS_MAX_LENGTH+2, BACKGROUND_REMOVE_CHAR) == 0) {
                /* Command should be executed in foreground. */
                
                /* Get the current time for command execution statistics. */
                CHECK_SAFE(gettimeofday(&preExecute, NULL));
                
                /* Execute the program with the executeChild function with foreground mode. */
                executeChild(args, CHILD_FOREGROUND);
                
                /* Get the current time for command executions statistics. */
                CHECK_SAFE(gettimeofday(&postExecute, NULL));
                
                /* Calculate the elapsed time. */
                elapsed = postExecute.tv_usec - preExecute.tv_usec;
                
                /* Print the statistics. */
                printf("Command '%s' executed in %i milliseconds.\n", args[0], elapsed);
            } else {
                /* Command should be executed in background. */
            
                /* Execute the program with the executeChild function with background mode. */
                executeChild(args, CHILD_BACKGROUND);
            }
        }
    } else if(readStatus == 1) {
        /* Too many characters were read. */
        
        /* Tell user to enter less number of characters. */
        printLine("The character limit of a command is %i", COMMAND_MAX_LENGTH);
    } else if(readStatus == -1) {
        /* An error have occured. */
        
        /* Check the error. */
        if(errno == EINTR) {
            /* Interrupted system call. */
            
            /* Do not treat this like an error, just try again. */
        } else {
            /* Unhandled error. */
        
            /* Force an error. */
            CHECK_SAFE(-1);
        }
    }
}