Example #1
0
int main(int argc, char *argv[]) {
	/*
	printf("Argument count: %d\n", argc);
	for (int i = 0; i < argc; i++) {
		printf("Argument vector values:%s at %p memory\n", argv[i], argv[i]);
		for (char *j=argv[i]; *j!='\0'; j++) {
			printf("Another way to print argument vector values: "
                               "%c at %p memory\n", *j, j);
		}
	}
	*/

	bool flag[N];
	memset(flag,1,N*sizeof(bool));

	flag[0] = 0;
	flag[1] = 0;
	int prime = 2;

	while (prime < N) {
		crossOff(flag,prime);
		prime = nextPrime(flag,prime);
	}

	printPrimes(flag);

	return 0;
}
Example #2
0
void readLinesFromFile(const char* filePath) {
  assert(filePath);
  std::string lineBuffer;
  std::ifstream file;
  file.open(filePath);
  std::vector<int> parameters;
  while (std::getline(file, lineBuffer)) {
    if (lineBuffer.empty()) { continue; } 
    parameters.push_back(toInt(lineBuffer));
  }
  printPrimes(parameters);
}
Example #3
0
int main(int argc, char **argv)
{
	if(argc != 4){
		printf("[# of threads] [max prime] [print y/n] \n");
		exit(EXIT_FAILURE);
	}
	int num = atoi(argv[1]);
	if(num > 0)
		num_threads = num;
	else{
		printf("invalid number of threads\n");
		exit(EXIT_FAILURE);
	}
	unsigned long max = atol(argv[2]);
	if(max < 1 || max > 4294967296){
		printf("invalid max number, using 4294967296\n");
		max_num = 4294967296;
	}
	else {
		max_num = max;
	}
	char print;
	if (argv[3][0] == 'y' || argv[3][0] == 'n') {
		print = argv[3][0];
	} else {
		printf("Invalid entry for print primes.\n");
		exit(EXIT_FAILURE);
	}
	printf("hello program\n");
	/*	struct sigaction raven;

	raven.sa_handler = nevermore;
	sigemptyset(&raven.sa_mask);
	raven.sa_flags = 0;

	sigaction(SIGQUIT, &raven, NULL);
	sigaction(SIGINT, &raven, NULL);
	sigaction(SIGHUP, &raven, NULL);
	*/
	printf("good thing we didn't do anything stupid here\n");
	//unsigned long primes_size = max_num / BITS_PER_WORD + 1;
	primes = (char *)malloc(sizeof(char)*((max_num/BITS_PER_WORD)+1));
	printf("Primes has been set\n");
	set_bitmap();
	printf("bitmap has been set\n");
	seed_primes();
	printf("seeding complete\n");
	create_threads();
	printf("threads have been created and died\n");
	printPrimes(print);
	free(primes);
	return 0;
}
Example #4
0
int child(int t,int x){
  int i;

  for(i=1;i<=2;i++){
    pid_t p1=fork();

    if(p1==-1){
        printf("Error in forking in child%d\n",x);
        return;
    }else if(p1==0){
        grandchild(t,x,i);
        return;
    }else{
      wait();
    }
  }

  printf("This is child%d\n",x);
  printPrimes(t);
  return;
}
Example #5
0
int child(int t,int x){
  
  pid_t p1=fork();
  pid_t p2=fork();

  if(p1==-1 || p2==-1){
        printf("Error in forking in child%d\n",x);
        return;
  }

  if(p1==0){
        grandchild(t,x,1);
        return;
  }else if(p2==0){
        grandchild(t,x,2);
        return;
  }else{
        printf("This is child%d\n",x);
        printPrimes(t);
        return;
  }

  return;
}
Example #6
0
int main(int argc, char *argv[])
{
	int i;
	char options;
	int timer = 0;
	unsigned int testMax = UINT_MAX-1;
	int numChildren = 1;
	int verbose = 1;

	while( (options = getopt(argc, argv, "tqc:m:" )) != -1)
	{
		switch(options)
		{
			case 'q':
				verbose = 0;
				break;
			case 'm':
				testMax = atoi(optarg);
				if( (testMax < 2 ) | (testMax >= UINT_MAX) )
					usageError(argv[0]);
				break;
			case 'c':
				numChildren = atoi(optarg);
				if( (numChildren > 10 ) | (numChildren < 1))
					usageError(argv[0]);
				break;
			case 't':
				timer = 1;
				verbose = 0;
				break;
			default:
				usageError(argv[0]);
		}
	}

	//unsigned int maxPrime = UINT_MAX;
	unsigned int maxPrime = testMax;
	unsigned int displayMax = testMax;
	if(maxPrime<32)
		maxPrime = 32;

	unsigned int arrLen = (unsigned int) ceil(maxPrime/numIntBits)+1;

	unsigned int nextPrime;
	unsigned int lastPrime;

	unsigned char *addr;
	unsigned char *primeAddr;
	unsigned char *doneAddr;
	sem_t *semiAddr;

	int children;

	int *bitArray;
	unsigned int *nextPrimeForC;
	int *done;

	time_t totalTime;
	struct timespec startTime;
	struct timespec endTime;

	clock_gettime(CLOCK_REALTIME, &startTime);

	//closes open shares that were improperly closed if they exist
	shm_unlink("/primeBitArray");
	shm_unlink("/primeAddr");
	shm_unlink("/done");
	shm_unlink("/sem");

	//create new shares
	addr = (unsigned char *)openSharedM("/primeBitArray", arrLen*sizeof(BIT_FRAME)+1, 0);
	primeAddr = (unsigned char *)openSharedM("/primeAddr", sizeof(unsigned int), 0);
	doneAddr = (unsigned char *)openSharedM("/done", sizeof(int), 0);
	semiAddr = (sem_t *)openSharedM("/sem", sizeof(sem_t)*3, 0);

	//closes shares on crash
	shm_unlink("/primeBitArray");
	shm_unlink("/primeAddr");
	shm_unlink("/done");
	shm_unlink("/sem");

	bitArray = (int *)(addr);
	done = (int *)(doneAddr);
	nextPrimeForC = (unsigned int *)primeAddr;
	
	//needNextTestSemi = semiAddr[0];
	//readingSemi = semiAddr[1];
	//readyToReadSemi = semiAddr[2];

	//tells children when to die
	*done = 0;

	if(sem_init(&semiAddr[0], 1, 0) == -1)
	{
		perror("Error while init of semi.");
		exit(EXIT_FAILURE);
	}
	if(sem_init(&semiAddr[1], 1, 1) == -1)
	{
		perror("Error while init of semi.");
		exit(EXIT_FAILURE);
	}
	if(sem_init(&semiAddr[2], 1, 0) == -1)
	{
		perror("Error while init of semi.");
		exit(EXIT_FAILURE);
	}

	//set next prime read lock to prevent child deadlock
	if(sem_wait(&semiAddr[1]) == -1)
	{
		perror("Error while waiting on sem.");
		exit(EXIT_FAILURE);
	}

	//set array to 1
	initArray(bitArray, arrLen);

	//create processes for setting sieves
	for(i = 0; i < numChildren ; i++)
	{
		children = fork();
		switch(children)
		{
			case -1:
					perror("Error while forking.");
					exit(EXIT_FAILURE);
			case 0:
		
					while(*done == 0)
					{
						//wait until the next prime has been identified
						if(sem_wait(&semiAddr[0]) == -1)
						{
							perror("Error while waiting on sem.");
							exit(EXIT_FAILURE);
						}

						//quit if no more work
						if(*done == 1)
							break;

						//lock the variable untill read
						if(sem_wait(&semiAddr[1]) == -1)
						{
							perror("Error while waiting on sem.");
							exit(EXIT_FAILURE);
						}

						nextPrime = *nextPrimeForC;
						//unlock the variable after reading
						if(sem_post(&semiAddr[1]) == -1)
						{
							perror("Error while posting on sem.");
							exit(EXIT_FAILURE);
						}

						//tell main to get new value
						if(sem_post(&semiAddr[2]) == -1)
						{
							perror("Error while posting semi.");
							exit(EXIT_FAILURE);
						}

						unmaskSieve(bitArray, arrLen, nextPrime);

					}
					exit(EXIT_SUCCESS);
			default:
					break;
		}
	}

	nextPrime = lastPrime = 3;

	//get first prime
	if(nextPrime < maxPrime)
	{
		*nextPrimeForC = nextPrime;

		//unset next prime read lock to prevent child deadlock
		if(sem_post(&semiAddr[1]) == -1)
		{
			perror("Error while posting sem.");
			exit(EXIT_FAILURE);
		}

		//tell children one value is avalible
		if(sem_post(&semiAddr[0]) == -1)
		{
			perror("Error while posting semi.");
			exit(EXIT_FAILURE);
		}

	}

//	nextPrime = lastPrime = 1;

	//get next prime and post it to nextPrimeForC
	while(nextPrime < maxPrime){

		nextPrime += 2;

		//break if overflow
		if(nextPrime < lastPrime)
			break;

		//check for prime and overflow
		while(!(getBit(bitArray, nextPrime)) & (nextPrime > lastPrime))
		{
			lastPrime = nextPrime;
			nextPrime += 2;
		}

		//break if overflow
		if(nextPrime < lastPrime)
			break;
		
		//wait for a child to read the current value
		if(sem_wait(&semiAddr[2]) == -1)
		{
			perror("Error while waiting on sem.");
			exit(EXIT_FAILURE);
		}

		//set next prime read lock to prevent child reading old info
		//also prevents writing while another is reading
		if(sem_wait(&semiAddr[1]) == -1)
		{
			perror("Error while waiting on sem.");
			exit(EXIT_FAILURE);
		}

		*nextPrimeForC = nextPrime;

		//unset next prime read lock when new info is posted
		if(sem_post(&semiAddr[1]) == -1)
		{
			perror("Error while waiting on sem.");
			exit(EXIT_FAILURE);
		}

		//let children know new info is avalible
		if(sem_post(&semiAddr[0]) == -1)
		{
			perror("Error while posting on sem.");
			exit(EXIT_FAILURE);
		}
	}


	//tell children to die
	*done = 1;
	
	//tell children to stop waiting and die
	for(i = 0; i<numChildren; i++)
	{
		if(sem_post(&semiAddr[0]) == -1)
		{
			perror("Error while posting on sem.");
			exit(EXIT_FAILURE);
		}
	}

	//clean up zombie children 
	//needed before printing primes in case they are still working
	for(i = 0; i<numChildren;i++){	
		wait(NULL);
	}

	clock_gettime(CLOCK_REALTIME, &endTime);
	totalTime = (endTime.tv_sec - startTime.tv_sec);

	if(timer)
		printf("%u \n", (unsigned int)totalTime);

	//print a single colunm list of primes
	if(verbose)
		printPrimes(bitArray, arrLen, displayMax);

	//clean up
	munmap(addr, arrLen*sizeof(BIT_FRAME));
	//free(bitArray); 

	if(sem_destroy(&semiAddr[0]) == -1)
	{
		perror("Error while destroying semi.");
		exit(EXIT_FAILURE);
	}
	if(sem_destroy(&semiAddr[1]) == -1)
	{
		perror("Error while destroying semi.");
		exit(EXIT_FAILURE);
	}
	if(sem_destroy(&semiAddr[2]) == -1)
	{
		perror("Error while destroying semi.");
		exit(EXIT_FAILURE);
	}

	exit(EXIT_SUCCESS);
}
Example #7
0
int main (void)
{
	printPrimes(10);
	return 0;
}
Example #8
0
void main()
{
    char s[100] = "abc2134d3";
    printPrimes(s);
}
Example #9
0
int main(int argc, char **argv){

	//help from: http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
	//and http://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html#Using-Getopt
	
	int c;
	unsigned int mvalue = UINT_MAX;
	unsigned long int cvalue = 1;
	int qflag = 0;
	int pflag = 0;
	unsigned long int numOfBitsInBitArray;

	while((c = getopt(argc, argv, "qm:c:p")) != -1)
		switch(c){
		case 'm':
			mvalue = atoi(optarg);
			break;
		case 'c':
			cvalue = atoi(optarg);
			break;
		case 'q':
        		qflag = 1;
            		break;
        	case 'p':
        		pflag = 1;
        		break;
		case '?':
			usageError();
		}

	if((qflag == 1) && (pflag == 0) && (mvalue > 2)){
		generatePrimes(mvalue, cvalue, &numOfBitsInBitArray);
		computeTwinPrimes(&numOfBitsInBitArray);
		free(bitArray);
	}
	else if((qflag == 1) && (mvalue > 2)){
		generatePrimes(mvalue, cvalue, &numOfBitsInBitArray);
		free(bitArray);
	}
	else if ((qflag == 0) && (pflag == 0) && (mvalue > 2)){
		generatePrimes(mvalue, cvalue, &numOfBitsInBitArray);
		printTwinPrimes(&numOfBitsInBitArray);
		free(bitArray);
	}
	else if((qflag == 0) && (mvalue > 2)){
		generatePrimes(mvalue, cvalue, &numOfBitsInBitArray);
		printPrimes(&numOfBitsInBitArray);
		free(bitArray);
	}
	else if ((qflag == 0) && (mvalue == 2)){  
		printf("Primes:\n"
				"2\n"
				"There are no twin primes.\n");
	}
	else if((qflag == 1) && (mvalue == 2)){
		//do nothing
	}
	else{ //mvalue is less than 2
		printf("There are no primes.\n");
	}
	
	return 0;
}