G
gooch
I originally posted the following code in a group discussing threads
but after further research I think I have a c question about the code.
I know there are a couple of non standard c includes here and the POSIX
stuff is non standard but this is how I stumbled onto this question.
#include <INTEGRITY.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
timer_t l_timer_2;
int counter =0;
//void Test(union sigval sigval)
void Test (union sigval sigval)
{
printf("Count\n");
counter++;
return;
}
int main()
{
struct itimerspec l_timer_spec1;
struct sigevent l_tevent1;
int *ptr;
int status;
l_tevent1.sigev_notify = SIGEV_THREAD;
l_tevent1.sigev_value.sival_ptr = &l_timer_2;
l_tevent1.sigev_notify_function = Test;
l_tevent1.sigev_notify_attributes = NULL;
l_tevent1.sigev_signo = SIGRTMIN;
l_timer_spec1.it_interval.tv_sec = 2;
l_timer_spec1.it_interval.tv_nsec = 0;
//set subsequent timer intervals
l_timer_spec1.it_value.tv_sec = 2;
l_timer_spec1.it_value.tv_nsec = 0;
//create timer
if (timer_create(CLOCK_REALTIME, &l_tevent1, &l_timer_2) < 0)
{
status = 1;
}
if (status == 0)
{
if (timer_settime(l_timer_2, 0, &l_timer_spec1, NULL) < 0 )
{
status = 1;
}
}
ptr = malloc(1000000);
if (ptr == NULL)
printf("NULL PTR\n");
counter++;
Exit(0);
}
Now I know this code snipet is pretty useless but it was the result of
troubleshooting a problem. When the malloc fails the timer stops
operating as expected. After some research it appears that when malloc
fails, returns NULL, due to not enough resources it actually marks all
the available memory as allocated. Due to this the timer can't start
the thread when it pops. So my question is what, if anything, does the
standard have to say about the behavior of malloc in this situation. Is
this behavior allowed and if so is it common. It seems to me that there
are many more gracefull ways for this to fail.
but after further research I think I have a c question about the code.
I know there are a couple of non standard c includes here and the POSIX
stuff is non standard but this is how I stumbled onto this question.
#include <INTEGRITY.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <time.h>
timer_t l_timer_2;
int counter =0;
//void Test(union sigval sigval)
void Test (union sigval sigval)
{
printf("Count\n");
counter++;
return;
}
int main()
{
struct itimerspec l_timer_spec1;
struct sigevent l_tevent1;
int *ptr;
int status;
l_tevent1.sigev_notify = SIGEV_THREAD;
l_tevent1.sigev_value.sival_ptr = &l_timer_2;
l_tevent1.sigev_notify_function = Test;
l_tevent1.sigev_notify_attributes = NULL;
l_tevent1.sigev_signo = SIGRTMIN;
l_timer_spec1.it_interval.tv_sec = 2;
l_timer_spec1.it_interval.tv_nsec = 0;
//set subsequent timer intervals
l_timer_spec1.it_value.tv_sec = 2;
l_timer_spec1.it_value.tv_nsec = 0;
//create timer
if (timer_create(CLOCK_REALTIME, &l_tevent1, &l_timer_2) < 0)
{
status = 1;
}
if (status == 0)
{
if (timer_settime(l_timer_2, 0, &l_timer_spec1, NULL) < 0 )
{
status = 1;
}
}
ptr = malloc(1000000);
if (ptr == NULL)
printf("NULL PTR\n");
counter++;
Exit(0);
}
Now I know this code snipet is pretty useless but it was the result of
troubleshooting a problem. When the malloc fails the timer stops
operating as expected. After some research it appears that when malloc
fails, returns NULL, due to not enough resources it actually marks all
the available memory as allocated. Due to this the timer can't start
the thread when it pops. So my question is what, if anything, does the
standard have to say about the behavior of malloc in this situation. Is
this behavior allowed and if so is it common. It seems to me that there
are many more gracefull ways for this to fail.