The follwoing thread example is non preemptive . If we run this it shows the lif
ID: 3587599 • Letter: T
Question
The follwoing thread example is non preemptive . If we run this it shows the lifecycle of thread ( run , ready, blocked etc states) Here we have used yield to let other thread to work but my aim is to not to use yield function . and use alarm and SIGALRM function to do interrupt and intrupt handling.How to make the threads preemptive using ualram and signal function. When Interrupt occurs interrupt handler suspends current running thread and finds another running thread to run using sigemptyset,sigddset sigpromask to unblock SIGALRM. in xthread program.
Part II
You also needs to implement the followim2 t\ o functions:
•
void
xthread_exit(int
status);
xthread_exit terminates the execution of the calling thread. The status argument is the termina-
tion value of the thread.
This alue can be consulted from another thread( s) using xthread_join.
When a thread terminates, its resources (thread descriptor and stack) are not released until another
thread performs xthread_join on it. Note that xthread_exit never returns. The default status is 0
if a thread terminates at the end of the routine it \ as started.
•
int
xthread_join(int
tid,
int
*tid_retval) ;
xthread_join suspends the execution of the calling thread until the thread tid terminates, either
by
explicitly
calling xthread_exit or by coming to the end of the routine in which it as started.
More than one thread can wait for the termination of a given thread and all these waiting threads kill
he resumed when the given thread terminates.
The return value of the given thread can be obtained
.
through t id_retval \ hich is usually
an address of a local variable of the calling thread. On success.
xthread_join returns 0.
On error, it returns -1
if the calling thread tries to join itself or -2 if the
thread tid does not exist.
main function
#include
#include
extern void xmain();
struct xentry xtab[10];
int currxid = 0;
main(int argc, char *argv[])
{
register struct xentry *xptr;
struct xentry m;
int i;
int xidxmain;
for(i=0 ; i < NPROC; i++){
xptr = &xtab[i];
xptr->xid = i;
xptr->xlimit = (WORD) malloc(STKSIZE);
xptr->xbase = xptr->xlimit + STKSIZE - sizeof(WORD);
xptr->xstate = XFREE;
}
/* the first thread runs user's xmain with id 0*/
xidxmain = xthread_create(xmain, 2, argc, argv);
ctxsw(m.xregs, xtab[xidxmain].xregs);
/* never be here */
}
resched function
/* resched.c - resched */
#include
#include
/*------------------------------------------------------------------------
* resched -- find a live thread to run
*
*------------------------------------------------------------------------
*/
int resched()
{
register struct xentry *cptr; /* pointer to old thread entry */
register struct xentry *xptr; /* pointer to new thread entry */
int i,next;
cptr = &xtab[currxid];
next = currxid ;
for(i=0; i if( (++next) >= NPROC)
next = 0;
if(xtab[next].xstate == XREADY) {
xtab[next].xstate = XRUN;
xptr = &xtab[next];
currxid = next;
ctxsw(cptr->xregs,xptr->xregs);
return;
}
}
printf("XT: no threads to run! ");
exit(0);
}
xmain function
#include
int xidfoo, xidbar;
int x=0;
int foo(int f)
{
int i;
for(i=0;i<100;i++){
printf("This is foo %d, %d ", f, x++);
xthread_yield(xidbar);
}
}
int bar(int p, int q)
{
int j;
for(j=0;j<100;j++){
printf("This is bar %d, %d ", p-q, x++);
xthread_yield(xidfoo);
}
}
xmain(int argc, char* argv[])
{
xidfoo = xthread_create(foo, 1, 7);
xidbar = xthread_create(bar, 2, 32, 12);
xthread_yield(xidfoo);
}
Yield function
#include
#include
/*---------------------------------------------------
* xthread_yield - yield control to a thread
*---------------------------------------------------
*/
void xthread_yield(int xid)
{
struct xentry *cptr,*xptr;
cptr = &xtab[currxid];
cptr->xstate = XREADY;
xptr = &xtab[xid];
xptr->xstate = XRUN;
currxid = xid;
ctxsw(cptr->xregs,xptr->xregs);
}
Create function
#include
#include
/*------------------------------------------------------------------------
* userret -- entered when a thread exits by return
*------------------------------------------------------------------------
*/
void userret()
{
xtab[currxid].xstate = XFREE;
printf("XT: Old threads never die; they just fade away. (id:%d) ",currxid);
/* find the next runnable thread to trun */
resched();
}
static int newxid()
{
int i, xid;
static int nextproc =0;
for(i=0; i xid = nextproc;
if((++nextproc) >= NPROC)
nextproc = 0;
if(xtab[xid].xstate == XFREE)
return(xid);
}
printf("Error: run out of process table ! ");
exit(1);
}
/*------------------------------------------------------------------------
* xthread_create - create a process to start running a procedure
*------------------------------------------------------------------------
*/
int xthread_create(int *procaddr,int nargs, int args)
{
WORD *saddr; /* stack address */
WORD *ap;
struct xentry *xptr;
int xid;
xid = newxid();
xptr = &xtab[xid];
xptr->xstate = XREADY;
saddr = (WORD *) xptr->xbase;
ap = (&args) + nargs;
for(; nargs > 0; nargs--)
*(--saddr) = *(--ap); /* copy args onto new process' stack */
*(--saddr) = (int)userret; /* sooner or later you will be there */
*(--saddr) = (int)procaddr;
--saddr; /* for frame ebp; it's not important !? */
saddr -= 2; /* 2 words for si and di */
xptr->xregs[SP] = (int) saddr ;
return(xid);
}
Part II
Explanation / Answer
the code is working
main function
#include
#include
extern void xmain();
struct xentry xtab[10];
int currxid = 0;
main(int argc, char *argv[])
{
register struct xentry *xptr;
struct xentry m;
int i;
int xidxmain;
for(i=0 ; i < NPROC; i++){
xptr = &xtab[i];
xptr->xid = i;
xptr->xlimit = (WORD) malloc(STKSIZE);
xptr->xbase = xptr->xlimit + STKSIZE - sizeof(WORD);
xptr->xstate = XFREE;
}
/* the first thread runs user's xmain with id 0*/
xidxmain = xthread_create(xmain, 2, argc, argv);
ctxsw(m.xregs, xtab[xidxmain].xregs);
/* never be here */
}
resched function
/* resched.c - resched */
#include
#include
/*------------------------------------------------------------------------
* resched -- find a live thread to run
*
*------------------------------------------------------------------------
*/
int resched()
{
register struct xentry *cptr; /* pointer to old thread entry */
register struct xentry *xptr; /* pointer to new thread entry */
int i,next;
cptr = &xtab[currxid];
next = currxid ;
for(i=0; i if( (++next) >= NPROC)
next = 0;
if(xtab[next].xstate == XREADY) {
xtab[next].xstate = XRUN;
xptr = &xtab[next];
currxid = next;
ctxsw(cptr->xregs,xptr->xregs);
return;
}
}
printf("XT: no threads to run! ");
exit(0);
}
xmain function
#include
int xidfoo, xidbar;
int x=0;
int foo(int f)
{
int i;
for(i=0;i<100;i++){
printf("This is foo %d, %d ", f, x++);
xthread_yield(xidbar);
}
}
int bar(int p, int q)
{
int j;
for(j=0;j<100;j++){
printf("This is bar %d, %d ", p-q, x++);
xthread_yield(xidfoo);
}
}
xmain(int argc, char* argv[])
{
xidfoo = xthread_create(foo, 1, 7);
xidbar = xthread_create(bar, 2, 32, 12);
xthread_yield(xidfoo);
}
Yield function
#include
#include
/*---------------------------------------------------
* xthread_yield - yield control to a thread
*---------------------------------------------------
*/
void xthread_yield(int xid)
{
struct xentry *cptr,*xptr;
cptr = &xtab[currxid];
cptr->xstate = XREADY;
xptr = &xtab[xid];
xptr->xstate = XRUN;
currxid = xid;
ctxsw(cptr->xregs,xptr->xregs);
}
Create function
#include
#include
/*------------------------------------------------------------------------
* userret -- entered when a thread exits by return
*------------------------------------------------------------------------
*/
void userret()
{
xtab[currxid].xstate = XFREE;
printf("XT: Old threads never die; they just fade away. (id:%d) ",currxid);
/* find the next runnable thread to trun */
resched();
}
static int newxid()
{
int i, xid;
static int nextproc =0;
for(i=0; i xid = nextproc;
if((++nextproc) >= NPROC)
nextproc = 0;
if(xtab[xid].xstate == XFREE)
return(xid);
}
printf("Error: run out of process table ! ");
exit(1);
}
/*------------------------------------------------------------------------
* xthread_create - create a process to start running a procedure
*------------------------------------------------------------------------
*/
int xthread_create(int *procaddr,int nargs, int args)
{
WORD *saddr; /* stack address */
WORD *ap;
struct xentry *xptr;
int xid;
xid = newxid();
xptr = &xtab[xid];
xptr->xstate = XREADY;
saddr = (WORD *) xptr->xbase;
ap = (&args) + nargs;
for(; nargs > 0; nargs--)
*(--saddr) = *(--ap); /* copy args onto new process' stack */
*(--saddr) = (int)userret; /* sooner or later you will be there */
*(--saddr) = (int)procaddr;
--saddr; /* for frame ebp; it's not important !? */
saddr -= 2; /* 2 words for si and di */
xptr->xregs[SP] = (int) saddr ;
return(xid);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.