#include <pthread.h>
#include <stdio.h>

pthread_t thread1;
pthread_t thread2;

void* function1 (int*);
void* function2 (void*);

int main (void)
{
	printf("Start of main program\n");

	struct sched_param scheduling_parameters;
	pthread_attr_t thread_attributes;

	scheduling_parameters.sched_priority=sched_get_priority_max(SCHED_FIFO);

	if(0!=pthread_setschedparam(pthread_self(),SCHED_FIFO,&scheduling_parameters))
	{
		printf("failed to set scheduling parameters\n");
		return -1;
	}
	
	if(0!=pthread_attr_init(&thread_attributes))
	{
		printf("thread attribute initialization failed\n");
		return -1;
	}
	if(0!=pthread_attr_setinheritsched(&thread_attributes,PTHREAD_EXPLICIT_SCHED))
	{
		printf("failed to set scheduling attributes explicitly\n");
		return -1;
	}

	if(0!=pthread_attr_setschedpolicy(&thread_attributes, SCHED_FIFO))
	{
		printf("failed to set scheduling policy to fifo\n");
		return -1;
	}

	scheduling_parameters.sched_priority=260;

	if(0!=pthread_attr_setschedparam(&thread_attributes,&scheduling_parameters))
	{
		printf("failed to set scheduling parameters (priority)\n");
		return -1;
	}

	if(0!=pthread_create (&thread1, &thread_attributes, function1, NULL))
	{
		printf("could not create thread1\n");
			return -1;
	}

	scheduling_parameters.sched_priority=180;

	if(0!=pthread_attr_setschedparam(&thread_attributes,&scheduling_parameters))
	{
		printf("failed to set scheduling parameters (priority)\n");
		return -1;
	}

	if(0!=pthread_create (&thread2, &thread_attributes, function2, NULL))
	{
		printf("could not create thread2\n");
		return -1;
	}

	if(0!=pthread_join(thread1,NULL))
	{
		printf("error in joining thread1\n");
	}
	
	printf("End of main program!\n");
	return 0;
}

//function for thread 1
void* function1 (void* arg)
{
	string *str = ("Hello World!");
	print("Start of thread1!\n");
	for( i = 0; i < 10;)
	{
		printf("%s\n", str);
	}
	else
	{
		printf("Goodbye!\n");
	}
	if(0!=pthread_join(thread2,NULL))
	{
		printf("error in joining thread2\n");
	}
	printf("End of thread1\n");
}


/*function for thread2*/
void* function2 (void* arg)
{
	int x, y;
	printf("Start of thread2!\n"); 
	for( x =  0; x < 10; x++)
	{
		y /= sin(x);
		printf(".");
	}
	if(0!=pthread_join(thread1,NULL))
	{
		printf("error in joining thread1\n");
	}
	printf("\nEnd of thread2!\n");
}


