Wrote this to give it the gorilla test and isolate the problem but no luck in crashing it.....
Code:
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include "queue.h"
void* task(void* vp);
void* writer(void* vp);
void* status(void* vp);
queue_t Q;
int main(int argc, char* argv[])
{
char answer[50];
system("clear");
while(1)
{
printf("Would you like to add (a), remove (r), auto (au)? ");
scanf("%s", answer);
if((strcmp(answer, "a") == 0) || (strcmp(answer, "a") == 0))
{
queue_init(&Q, task, 0);
}
else if(strcmp(answer, "au") == 0)
{
queue_init(&Q, task, 10);
}
if(strcmp(answer, "a") == 0)
{
int items;
printf("How many items would you like to add? ");
scanf("%d", &items);
int x;
int y;
for(x=0; x<items; x++)
{
char* stuff = malloc(1000);
for(y=0; y<1000; y++)
{
stuff[y] = 'a';
}
queue_add(&Q, (void*)stuff, 1000);
}
printf("There are %d items in the queue and a total of %d bytes in the queue.\n", queue_length(&Q), queue_mem(&Q));
}
else if(strcmp(answer, "r") == 0)
{
int items;
printf("How many items would you like to remove? ");
scanf("%d", &items);
int x;
for(x=0; x<items; x++)
{
char* item = queue_remove(&Q);
free(item);
}
printf("There are %d items in the queue and a total of %d bytes in the queue.\n", queue_length(&Q), queue_mem(&Q));
}
else if(strcmp(answer, "au") == 0)
{
pthread_t thread_id;
pthread_create(&thread_id, NULL, status, NULL);
pthread_t thread_id2;
pthread_create(&thread_id2, NULL, writer, NULL);
sleep(2);
while(queue_length(&Q))
{
sleep(1);
}
pthread_cancel(thread_id);
pthread_join(thread_id, NULL);
system("clear");
status((void*)1);
queue_destroy(&Q);
}
}
return 0;
}
void* writer(void* vp)
{
int x;
for(x=0; x<3000000; x++)
{
u_char* stuff = malloc(1000 * sizeof(u_char));
int y;
for(y=0; y<1000; y++)
{
stuff[y] = 'a';
}
queue_add(&Q, (void*)stuff, 1000);
}
return NULL;
}
void* status(void* vp)
{
while(1)
{
pthread_testcancel();
printf("There are %d items in the queue and a total of %d bytes in the queue.\n", queue_length(&Q), queue_mem(&Q));
fflush(stdout);
usleep(100000);
if((int*)vp)
break;
system("clear");
}
return NULL;
}
void* task(void* vp)
{
usleep(100);
free(vp);
return NULL;
}