Question: Please write a Real-Time Event Scheduler in C. Instructions and related files: There should be NO DEADLOCKS and NO RACE CONDITIONS in your code Your

Please write a Real-Time Event Scheduler in C. Instructions and related files: Please write a Real-Time Event Scheduler in C. Instructions and related files:

There should be NO DEADLOCKS and NO RACE CONDITIONS in your code

There should be NO DEADLOCKS and NO RACE CONDITIONS in your code Your program should shut down gracefully after all requests have been performed.

Files: minheap.h: https://pastebin.com/H5yj2ffg minheap.c: https://pastebin.com/saYDUA9V Also pasted those below

//minheap.h

#ifndef MinHeap_h #define MinHeap_h typedef int (*Comparator)( const void *, const void * ); typedef struct MinHeap { Comparator comparator; void * block; // extensible block, moveable. void * temp; // temp space for swap() unsigned int elemsize; unsigned int elemcount; } MinHeap; int initializeMinHeap( MinHeap *, Comparator, unsigned int elemsize ); int addElement( MinHeap *, void * ); void * removeMin( MinHeap *, void * buffer ); void * getMin( MinHeap * ); void destroyMinHeap( MinHeap * ); #endif

//minheap.c

#include "MinHeap.h" #include  #include  #include  int initializeMinHeap( MinHeap * self, Comparator comparator, unsigned int elemsize ) { if ( self == 0 ) { fprintf( stderr, "\x1b[2;31mzero arg passed to initializeMinHeap() file %s line %d\x1b[0m ", __FILE__, __LINE__ ); return ~0; } else if ( (self->temp = malloc( elemsize )) == 0 ) { fprintf( stderr, "\x1b[2;31mmalloc() failed in initializeMinHeap() file %s line %d\x1b[0m ", __FILE__, __LINE__ ); return ~0; } else { self->comparator = comparator; self->block = 0; self->elemsize = elemsize; self->elemcount = 0; return 0; } } static void swap( MinHeap * self, unsigned int i, unsigned int j ) { unsigned int elemsize = self->elemsize; unsigned char * block = self->block; unsigned char * temp = self->temp; memcpy( temp, block + i * elemsize, elemsize ); memcpy( block + i * elemsize, block + j * elemsize, elemsize ); memcpy( block + j * elemsize, temp, elemsize ); } static void siftUp( MinHeap * self, int pos ) { int i, parent; unsigned int elemsize = self->elemsize; Comparator comparator = self->comparator; unsigned char * block = self->block; i = pos; while ( i > 0 ) { parent = ( i - 1 )/2; if ( comparator( block + parent * elemsize, block + i * elemsize ) > 0 ) { swap( self, parent, i ); } else { break; } i = parent; } } static void siftDown( MinHeap * self, int lo, int hi ) { int i, child, rchild; unsigned int elemsize = self->elemsize; Comparator comparator = self->comparator; unsigned char * block = self->block; i = lo; child = 2*i + 1; while ( child  0 ) { child = rchild; } if ( comparator( block + i * elemsize, block + child * elemsize ) > 0 ) { swap( self, i, child ); } else { break; } i = child; child = 2*i + 1; } } int addElement( MinHeap * self, void * element ) { unsigned int elemsize = self->elemsize; self->elemcount += 1; self->block = realloc( self->block, self->elemcount * elemsize ); memcpy( self->block + (self->elemcount - 1) * elemsize, element, elemsize ); siftUp( self, self->elemcount - 1 ); return 0; } void * removeMin( MinHeap * self, void * buffer ) { unsigned char * block = self->block; unsigned int elemcount = self->elemcount; unsigned int elemsize = self->elemsize; if ( block == 0 ) { return 0; } else { swap( self, 0, elemcount - 1 ); siftDown( self, 0, elemcount - 2 ); memcpy( buffer, block + (elemcount - 1) * elemsize, elemsize ); self->elemcount -= 1; self->block = realloc( block, self->elemcount * elemsize ); return buffer; } } void * getMin( MinHeap * self ) { if ( self == 0 ) return 0; else return self->block; } void destroyMinHeap( MinHeap * self ) { if ( self == 0 ) { fprintf( stderr, "\x1b[2;31mzero arg passed to destroyMinHeap() file %s line %d\x1b[0m ", __FILE__, __LINE__ ); return; } else { if ( self->block != 0 ) free( self->block ); self->block = 0; self->elemcount = 0; if ( self->temp != 0 ) free( self->temp ); self->temp = 0; } }

l Introduction This assignment requires the design and implementation of a multithreaded event scheduler. This will give you an opportunity to write a multithreaded program that reads in and stores event requests in any order and then executes the requests in order in real time. You will gain experience in writing a multithreaded program that uses timers, signmals, signal handlers and semaphores to manage the use of a shared data structure Your program will have two threads. The first thread will read event requests from stdin. An event request will consist of a time span and an event string. Tie spans are expressed in a non-negative number of seconds. The incoming event requests may be entered in any order. The first thread will reject any event request with a negative time span and add any future request to a MinHea,p shared with the second thread. The second thread will respond to timer events "timeouts"). The second thread will block until a timeout and then respond to the timeout by updating now from the current time witlh clock-gettime(2). It will then extract and perform all events prior to the updated now 2 Event Requests An event request has two parts: A time span and the request string. The time span is a non-negative integer number of seconds untl the event is to be performed. The request string is just a simple string. The string contents take the place of an encoded re- quest that might be arbitrarily complicated. As far as this program is concerned, "performing a request" is nothing more than printing out the request string and the associated time. In a more realistic implementation, performing the request would involve various forms of application-specific activities. 3 The MinHeap The data structure of choice for string and retrieving ordered event requests is the MinHeap. The MinHeap allows you to add data elements in any order and later extract them in increasing order The lowest element in the minheap is always at the top of the heap structure, and thus readily and inexpensively available The MinHeap data structure will be extensible and be able to hold any number of event requests, ordered by absolute time

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!