/* includes */
#include "vxWorks.h"
#include "sigLib.h"
#include "taskLib.h"
#include "stdio.h"
#include "errnoLib.h"
#include "logLib.h"
/* function prototypes */
void catchSIGINT(int);
void sigCatcher(void);
/* globals */
#define NO_OPTIONS 0
#define ITER1 100
#define LONG_TIME 1000000
#define HIGHPRIORITY 100
#define LOWPRIORITY 101
int ownId;
void sigGenerator(void) /* task to generate the SIGINT signal */
{
int i, j, taskId;
STATUS taskAlive;
if ((taskId = taskSpawn("signal", 100, 0x100, 20000, (FUNCPTR)sigCatcher, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)
printf("taskSpawn sigCatcher failed\n");
ownId = taskIdSelf(); /* get sigGenerator's task id */
taskDelay(50); /* allow time to get sigCatcher to run */
for (i = 0; i < ITER1; i++)
{
if((taskAlive = taskIdVerify(taskId)) == OK)
{
printf("+++++++++++++++++++++++++++++++SIGINT sinal generated\n");
kill(taskId, SIGINT); /* generate signal */
/* lower sigGenerator priority to allow sigCatcher to run */
taskPrioritySet(ownId, LOWPRIORITY);
}
else
break;
}
printf("\n***************sigGenerator Exited***************\n");
}
void sigCatcher(void) /* task to handle the SIGINT signal */
{
struct sigaction newAction;
int i, j;
newAction.sa_handler = catchSIGINT; /* set the new handler */
sigemptyset(&newAction.sa_mask); /* no other signals blocked */
newAction.sa_flags = NO_OPTIONS; /* no special options */
signal(SIGINT, catchSIGINT);
/* printf("Could not install signal handler\n");*/
for (i = 0; i < ITER1; i++)
{
for (j = 0; j < LONG_TIME; j++)
;
printf("Normal processing in sigCatcher\n");
}
printf("\n+++++++++++++++sigCatcher Exited+++++++++++++++\n");
}
void catchSIGINT(int signal) /* signal handler code */
{
printf("-------------------------------SIGINT signal caught\n");
/* increase sigGenerator priority to allow sigGenerator to run */
taskPrioritySet(ownId, HIGHPRIORITY);
}
运行上述程序,输出结果为:
Normal processing in sigCatcher
… //大量的Normal processing in sigCatcher
Normal processing in sigCatcher
+++++++++++++++sigCatcher Exited+++++++++++++++
想问下,为什么
printf("+++++++++++++++++++++++++++++++SIGINT sinal enerated\n");以及
printf("-------------------------------SIGINT signal caught\n");不会被输出,改为logMsg依然不行。在catchSIGINT下断点发现该函数根本没有被运行,
我想第一个printf没有被运行大概是因为被signal这个任务阻塞了吧,可是程序运行完了也没有输出一次,而且改为logMsg输出也不行,就应该不是被阻塞了吧。(是不是函数不存在阻塞一说-_-!!)
小弟刚开始接触vxworks,望各位DX多多指教!