Ejemplo n.º 1
0
int main(int argc, char* argv[]) {
	if(argc < 3) {
		printf("Incorrect number of arguments. Example usage: prog1 file.txt a b c\n");
		exit(1);
	}

	// Get all the chars that will be searched for
	char letters[argc - 2]; int i, j = 0;
	for(i = START_LETTERS; i < argc; i++) {
		letters[j++] = argv[i][0];
	}
	int occurences[argc - 2];

	// Prepare structs for time logging
	struct timeval start, finish;

	gettimeofday(&start, NULL); // Get start time
	// Open the file passed to program
	FILE *fp;
	fp = fopen(argv[1], "r");

	// Get a count of each char using stdio file operations
	for(i = 0; i < argc - 2; i++)
		occurences[i] = countOccurences(fp, letters[i]);
	fclose(fp);

	gettimeofday(&finish, NULL); // Get finish time

	// Print off occurences and elapsed time
	for(i = 0; i < argc - 2; i++)
		printf("%c count: %d\n", letters[i], occurences[i]);
	printf("Elapsed time: %f seconds\n", computeElapsedTime(start, finish));

	return 0;
}
Ejemplo n.º 2
0
Archivo: pi.c Proyecto: mingram/CS3305
main(int argc, char **argv)
{
  int niter, nchild, policy;
  double x, y;
  int i, j,k, count = 0; /*# of points in the 1st quadrant of unit circle */
  double z;
  double pi;
  pid_t pid; 
  struct sched_param param; 
  struct timeval start, finish; 
  double elapsedTime; 

  if (argc != 3){
     perror("incorrect arguments");
     exit(0);
  }

  niter = atoi(argv[1]);
  nchild = atoi(argv[2]); 

  /*loop for the number of children*/
  for (j = 0; j < nchild; j++){
       /*set the schedule priority*/    
       k = j; 
       if(k % 2 == 0){
         policy = SCHED_OTHER;
         printf("Policy is sched other \n"); 
       }
       else{
         policy = SCHED_FIFO; 
         printf("Policy is sched fifo \n"); 
       }
       
       param.sched_priority = sched_get_priority_max(policy); 
      
       if(sched_setscheduler(0, policy, &param)){
         perror("Error setting scheduler policy");
         exit(EXIT_FAILURE);
       }
      
       pid = fork(); 
       if (pid < 0){
           perror("Error forking.");
           exit(pid); 
        }
        /*parent process */
        if (pid > 0){
           // exit(0);
        }
        if (pid == 0){
            /* initialize random numbers */
            srand(SEED);
            /*start timer*/
            gettimeofday(&start, NULL);

            count = 0;
            for (i = 0; i<niter; i++) {
                 x = (double)rand() / RAND_MAX;
                 y = (double)rand() / RAND_MAX;
                 z = x*x + y*y;
                 if (z <= 1) count++;
            }
            pi = (double)count / niter * 4;

            /*stop timer*/
            gettimeofday(&finish, NULL);
            /*compute elapsed time*/
            elapsedTime = computeElapsedTime(start, finish); 
            printf("Elapsed time is %f \n", elapsedTime); 
            printf("# of trials= %d , estimate of pi is %g \n", niter, pi);
            exit(0);
        }
     }
           
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
    int niter;
    int nFork;
    double x, y;
    int count = 0; /* # of points in the 1st quadrant of unit circle */
    double z;
    double pi;

    if (argc != 3)
    {
        perror("incorrect arguments\n");
        exit(1);
    }

    niter = atoi(argv[1]);
    nFork = atoi(argv[2]);

    /* initialize random numbers */
    for (int i = 0; i < nFork; i++)
    {
        if (fork() == 0)
        {
            int policy;
            struct sched_param param;

            // if i&1 == true (odd number) set SCHED_FIFO, else (even number) set SCHED_OTHER
            policy = (i&1) ? SCHED_FIFO : SCHED_OTHER;

            param.sched_priority = sched_get_priority_max(policy);

            if (sched_setscheduler(0, policy, &param))
            {
                perror("Error setting scheduler policy");
                exit(EXIT_FAILURE);
            }

            srand(SEED);
            count = 0;

            struct timeval start_time, finish_time;
            double elapsed_time;

            gettimeofday(&start_time, NULL);

            for (int j = 0; j<niter; j++)
            {
                x = (double)rand()/RAND_MAX;
                y = (double)rand()/RAND_MAX;
                z = x*x+y*y;
                if (z<=1) count++;
            }
            pi = (double)count/niter*4;

            gettimeofday(&finish_time, NULL);

            elapsed_time = computeElapsedTime(start_time, finish_time);

            printf("\nchild %d, sched = %s, trials= %d , aprx. pi = %g, time [s] = %f\n",
                   i,
                   (policy == SCHED_FIFO) ? "fifo" : "other",
                   niter, pi, elapsed_time);

            exit(EXIT_SUCCESS);
        }
    }
    printf("completed execution\n");
}