Hello all,

I have been assigned in my class to come up with a program that is written in c and generates prime numbers from 2 to n. I have coded the problem but i have a problem. Any numbers greater than 6479 will not return the results that I would like. I have tried to debug this code many times will not such luck. I am new to multiprocessor coding. Any debugging help will be greatly appriciated. I am sure that I have some stupid coding error that I have not seen because I have been in front of the terminal for so long. Here is my code:

#include <stdio.h>

#include <stdlib.h>

#include <sys/wait.h>

#include <sys/types.h>

#include <unistd.h>





int CalculatePrimes(int startValue, int endValue);



int main(int argc, char *argv[])

{
	//printf("Entering Program\n");

	int i;

	int n;

	int processes;

	int pid;

	int prime;

	int status;

	int totalValue = 0;

	int value = 100;



	switch(argc)

	{

   		case 1: n = 0;
			//printf("Entered Case 1\n"); 

			break;

   		case 2: n = atoi(argv[1]);
			//printf("Entered Case 2\n");

        	        break;

   		default: printf("Too many arguments\n");

        	         exit(-1);

 	}

	
	//printf("The value of n is %d\n", n);


	value = n/4;
	//printf("The value of value is %d\n", value);

	

	processes = 4;

	i = 0;

 	do

	{

   		pid=fork();
		//printf("This is the pid: %d\n", pid);


   		if(pid == 0)  /*child process*/

		{	
			//printf("We have a child!\n");
			//printf("Value is %d\n", value);


			prime = CalculatePrimes(i*value, (i*value) + value); 

			printf("prime %d\n", prime);


     			i = processes;
			//printf("i in the child is %d\n", i);
			
			//printf("Our child has ended. Returned prime thru exit\n");
			printf("Prime is %d prime\n", prime);	

			exit(prime);   		

   		}

   		i = i+1;
		//printf("This is the value of i counter %d\n", i);



 	}while(i < processes);



 	i = 0;

	int tmp;



 	while(i < processes)

	{

	    wait(&status);
	    tmp = WEXITSTATUS(status);
	    printf("This is the tmp variable %d\n", tmp);

	    //printf("status %d\n", status);

	    totalValue = totalValue + tmp;

	    //printf("total %d\n", totalValue);

	    i = i+1;
	    //printf("Value of i when in I < Processes %d\n",i);

	 }

	 printf("Total Primes %d\n", totalValue);
	 //printf("Exiting Program\n");

}





int CalculatePrimes(int startValue, int endValue)

{
	//printf("Entering Calculating Primes\n");


	int prime = 0;

	bool isPrime;



	//printf("start value %d\n", startValue);

	//printf("end value %d\n" , endValue);



	if(startValue < 2)

	{
		//printf("Entering the startValue Check\n");

		startValue = 2;
		//printf("Exiting the startValue check\n");

	}



	for(startValue; startValue< endValue; startValue++)

	{

		isPrime = true;

		

		for(int j=2; j<startValue; j++)

		{

			if (startValue%j ==0)

			{

				isPrime = false;
				//printf("start value after prime is fales = %d\n", startValue);

			}
			//printf("This is j %d\n", j);
			

		}

		if(isPrime)

		{

			//printf("%d is a prime number.\n", startValue);

			prime = prime +1;
			//printf("This is prime: %d\n", prime);

		}
		//printf("Start value at end of loop %d\n",startValue);

	}

	//printf("There are %d primes\n", prime);
	//printf("Exiting Calculating Primes\n");

	return prime;

}

* this code is supposed to be inefficent in finding primes because it is used merely as a bench mark.

Recommended Answers

All 3 Replies

Does anyone have any idea on how to solve this???

bump

commented: Wrong post. -2

> printf("Prime is %d prime\n", prime);
> exit(prime);
Have you read the manual?

The exit() and _Exit() functions terminate a process.

     Before termination, exit() performs the following functions in the order
     listed:

           1.   Call the functions registered with the atexit(3) function, in
                the reverse order of their registration.

           2.   Flush all open output streams.

           3.   Close all open streams.

           4.   Unlink all files created with the tmpfile(3) function.

     The _Exit() function terminates without calling the functions registered
     with the atexit(3) function, and may or may not perform the other actions
     listed.  [B]Both functions make the low-order eight bits of the status[/B] argu-
     ment available to a parent process which has called a wait(2)-family
     function.

You can't return large numbers though the exit status.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.