İşletim Sistemleri (Bahar-2016) Hafta-5/2

Thread ile asal sayılar:

#include<iostream>
#include<unistd.h>
#include<pthread.h>

using namespace std;

void * islem1(void *arg)
{
	cout<<"THREAD BASLADI!!::1"<<endl;

	for(int sayi=100;sayi<2000;sayi++)
	{
		int sayac=0;

		for(int i=2;i<(sayi/2);i++)
		{
			if(sayi%i==0)
			{
				sayac++;
				break;	
			}
		}
		
		if(sayac==0)
			cout<<"::1::"<<sayi<<" asaldir"<<endl;
	}

	pthread_exit(0);
}


void * islem2(void *arg)
{
	cout<<"THREAD BASLADI!!::2"<<endl;

	for(int sayi=3000;sayi<9000;sayi++)
	{
		int sayac=0;

		for(int i=2;i<(sayi/2);i++)
		{
			if(sayi%i==0)
			{
				sayac++;
				break;	
			}
		}
		
		if(sayac==0)
			cout<<"::2::"<<sayi<<" asaldir"<<endl;
	}

	pthread_exit(0);
}

int main()
{
	pthread_t iplik1,iplik2;
		
	cout<<"PROC ID:"<<getpid()<<endl;
	
	pthread_create(&iplik1,NULL,islem1,NULL);
	pthread_create(&iplik2,NULL,islem2,NULL);
	cout<<"THREAD BEKLE"<<endl;
	pthread_join(iplik1,NULL);
	pthread_join(iplik2,NULL);
	cout<<"ANA PROC SONLANDI"<<endl;
}

 

Share