This is an attempt to use taskDeleteHookAdd() to send a
signal to the desired task as it is being terminated. See the base
notes for details.
Here, load deleter.o and invoke deleter:
ld < deleter.o
deleter
taskDeleteHookShow <-- you should see delete_chain in the list
# now start the work task
taskSpawn ("TEST_1", 100, 0x1e, 0x3000, test, 100)
# now kill it
taskDelete( "TEST_1")
You should see the message from delete_chain() that it is sending
a signal to the correct pid. Unfortunately, the signal doesn't get
delivered!
Why is cleaning up memory so difficult in VxWorks??? Any help???
Thanks in advance. Please email your replies to:
Jeff Burch
Hewlett Packard Co.
---------------------------------------------------------- */
#include <vxWorks.h>
#include <stdio.h>
#include <stdlib.h>
#include <sigLib.h>
#include <taskLib.h>
int delete_chain( WIND_TCB *tcb)
{
fprintf(stderr,"delete_chain: tcb=0x%x, name=%s, signal=SIGABRT\n",
tcb, tcb->name);
sigRaise( (int) tcb, SIGABRT, 1);
return (0);
}
int deleter( void)
{
fprintf(stderr,"adding delete_chain() to the taskDeleteHook list\n");
if (taskDeleteHookAdd( delete_chain) != OK)
{
fprintf(stderr,"deleter: error in taskDeleteHookAdd\n");
}
return (0);
}
I am trying to have a handler called when a task is deleted. Note that
the same code will be run many times and therefore I will have many tasks
running simultaneously. Basically, I want the handler to perform clean-up
operations for me.
The following example code, I trap all possible VxWorks signals.
I run many copyies of this code as follows:
taskSpawn ("TEST_1", 100, 0x1e, 0x3000, test, 100)
taskSpawn ("TEST_2", 100, 0x1e, 0x3000, test, 200)
taskSpawn ("TEST_3", 100, 0x1e, 0x3000, test, 300)
I can send signals to them and verify that things are working:
sigRaise( pid, 31, 100)
But, if I delete the task, my handler is NOT executed!!!
taskDelete( pid)
I am trying the taskDeleteHookAdd() approach, but it seams like a lot
of work and overhead just to do proper clean-up.
Any clues???
Thanks in advance. Please email your replies to:
Jeff Burch
Hewlett Packard Co.
----------------------------------------------------------------- */
#include <vxWorks.h>
#include <stdio.h>
#include <stdlib.h>
#include <sigLib.h>
#include <taskLib.h>
/* example structure that needs to be cleaned up */
typedef struct
{
char *buf;
int len;
} MY_TASK_VAR;
/* global pointer to my task variable */
MY_TASK_VAR *my_task_var = NULL;
void handler( int sig, int code, SIGCONTEXT *sigContext)
{
fprintf(stderr,"handler: sig=%d, code=%d tid=0x%x, len=%d, buf=0x%x\n",
sig, code, taskIdSelf(), my_task_var->len, my_task_var->buf);
/* Here I should clean-up memory */
/* Note: in the real case, this handler would only be called when
the task is exiting */
if (my_task_var->buf)
{
fprintf(stderr,"handler: freeing memory for %d bytes\n",
my_task_var->len);
free( my_task_var->buf);
my_task_var->buf = NULL;
}
}
int test( int len )
{
int i;
SIGVEC my_sigvec;
my_sigvec.sv_handler = handler;
my_sigvec.sv_mask = 0;
my_sigvec.sv_flags = 0;
/* install all signal handlers */
/* in the real program, I probably would need to trap only one handler */
for (i=SIGHUP; i<=SIGUSR2; i++)
{
/* install the same handler for this signal */
if (sigvec( i, &my_sigvec, NULL) != OK)
{
fprintf(stderr,"Error installing handler for %d\n", i);
}
}
/* get the task variable and malloc some memory */
my_task_var = (MY_TASK_VAR *) malloc( sizeof(MY_TASK_VAR));
my_task_var->len = len;
if (!(my_task_var->buf = (char *) malloc( my_task_var->len)))
{
fprintf(stderr,"Error in malloc of %d\n", len);
return (1);
}
if (taskVarAdd( 0, (int *)&my_task_var) != OK)
{
fprintf(stderr,"Error in taskVarAdd\n");
return (1);
}
while (1)
{
fprintf(stderr,"test is pausing, tid=0x%x, len=%d, buf=0x%x\n",
taskIdSelf(), my_task_var->len, my_task_var->buf);
/* in the real program, I would be doing useful stuff */
pause();
}
fprintf(stderr,"test is ending\n");
return (0);
}
~hwajin