#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h> 


int stoploop = 0;
void* origMalloc(int x) {
fprintf(stderr,"\nin origMalloc %d\n",x);
void *ret = malloc(x);
return ret;
}
void *inc_x(void *x_void_ptr)
{
	  while(!stoploop)
	  {
		int *ptr = (int*)origMalloc(5); 
		if(!ptr)
		{
			fprintf(stderr,"\nptr NULL\n"); 
		}
		else
		{
			fprintf(stderr,"\nptr not NULL\n"); 
		}

		printf("sleeping --------------\n\n");
		sleep(2);
	}
	fprintf(stderr,"\nstopping process\n"); 
}

void siginthandler(int signum) {
   printf("Caught SIGINT %d shutting down \n", signum);
   stoploop = 1;
}
void sigtermhandler(int signum) {
   printf("Caught SIGTERM %d shutting down \n", signum);
   stoploop = 1;
}

int main(int argc, char *argv[]) {
	fprintf(stderr,"\nin main\n"); 
signal(SIGINT, siginthandler);
signal(SIGTERM, sigtermhandler);
pthread_t inc_x_thread;
if(pthread_create(&inc_x_thread, NULL, inc_x, NULL)) {

fprintf(stderr, "Error creating thread\n");
return 1;

}

if(pthread_join(inc_x_thread, NULL)) {

fprintf(stderr, "Error joining thread\n");
return 2;

}

printf("main end \n");	
	  return 0;
}

