Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C, develop a user-level SRTF thread scheduler for UNIX POSIX threads using

ID: 3828879 • Letter: U

Question

Using C, develop a user-level SRTF thread scheduler for UNIX POSIX threads using an interrupt handler for the system's real-time clock (i.e., "SIGALM"). Since the default thread scheduling algorithm by UNIX POSIX is the Round-Robin scheduling algorithm, you need to override the POSIX default RR scheduling by your custom-made user-level SRTF thread scheduling algorithm.

The first thread (called "the parent thread") creates ten child threads, using "pthread_create" POSIX function (a total of eleven (11) threads should be active, including the parent thread).

Explanation / Answer

#define _REENTRANT
#include <pthread.h>
#include <thread.h>

thread_t user_threadID;
sigset_t new;
void *handler(), interrupt();

main( int argc, char *argv[] ){
test_argv(argv[1]);

sigemptyset(&new);
sigaddset(&new, SIGINT);
switch(*argv[1]) {
case '0':
pthread_sigmask(SIG_BLOCK, &new, NULL);
pthread_create(&user_threadID, NULL, handler, argv[1]);
pthread_join(user_threadID, NULL);
break;

case '1':
thr_sigsetmask(SIG_BLOCK, &new, NULL);
thr_create(NULL, 0, handler, argv[1], 0, &user_threadID);
thr_join(user_threadID, NULL, NULL);
break;
}

printf("thread handler, # %d, has exited ",user_threadID);
sleep(2);
printf("main thread, # %d is done ", thr_self());
} /* end main */

struct sigaction act;

void *
handler(char argv1[])
{
act.sa_handler = interrupt;
sigaction(SIGINT, &act, NULL);
switch(*argv1){
case '0':
pthread_sigmask(SIG_UNBLOCK, &new, NULL);
break;
case '1':
thr_sigsetmask(SIG_UNBLOCK, &new, NULL);
break;
}
printf(" Press CTRL C to deliver SIGINT signal to the process ");
sleep(8);

void
interrupt(int sig)
{
printf("thread %d caught signal %d ", thr_self(), sig);
}

void test_argv(char argv1[]) {
if(argv1 == NULL) {
printf("use 0 as arg1 to use thr_create();
or use 1 as arg1 to use pthread_create() ");
exit(NULL);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote