Author(s) Mathieu Xhonneux & Maxime Mawait
Deadline No deadline
Submission limit 1 submissions
every 12 hour(s)
Category tags S3, Malloc, Level 2


Sign in

[S3] Sleepy malloc

The malloc(3) function may fail, for instance if the OS has no free memory remaining. In this case, it may be possible that some other processes free some memory after some time and then we could use it again.

In our program, instead of reporting an error immediately if no free memory is available, we will wait some time to see if the OS has freed memory for us.

Sleepy malloc

Write the body of the function sleepy_malloc. Use the function sleep(3).

/* Wrapper to the malloc system call.
 * If malloc fails, sleep for 5 seconds, and then try again.
 * If malloc failed 10 times in a row, return NULL.
void *sleepy_malloc(size_t s) {