⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 real-time experiment #8 signals.htm

📁 该文档是学习在Vxworks上进行编程开发的入门教材。
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0066)http://www.rt.db.erau.edu/experiments/vx/signals/Experiment-8.html -->
<HTML><HEAD><TITLE>Real-Time Experiment #8: Signals</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.2873" name=GENERATOR></HEAD>
<BODY>
<CENTER>
<H1>Embry-Riddle Real-Time Laboratory Experiment<BR>Experiment #8<BR>Signals 
</H1></CENTER>
<HR SIZE=3>

<H2>Introduction</H2>A signal is a software notification to a task or a process 
of an event. A signal is <B>generated</B> when the event that causes the signal 
occurs. A signal is <B>delivered</B> when a task or a process takes action based 
on that signal. The <B>lifetime</B> of a signal is the interval between its 
generation and its delivery. A signal that has been generated but not yet 
delivered is <B>pending</B>. There may be considerable time between signal 
generation and signal delivery.
<P></P>VxWorks supports a software signal facility. The signals asynchronously 
alter the control flow of task. Any task can raise a signal for a particular 
task. The task being signaled immediately suspends its current thread of 
execution and the task specified signal handler routine is executed the next 
time the task is scheduled to run. The signal handler gets invoked even if the 
task is blocked on some action or event. The signal handler is a user supplied 
routine that is bound to a specific signal and performs whatever actions are 
necessary whenever the signal is received. Signals are most appropriate for 
error and exception handling, rather than general intertask communication.
<P></P>The <E>Wind</E> kernel has both BSD 4.3 and POSIX signal interface. The 
POSIX interface provides a standardized interface which is more functional than 
BSD 4.3 interface. Your application should use only one interface and not mix 
the two.
<P></P>
<HR SIZE=3>

<H2>Objectives</H2>The following are the primary objectives of this experiment: 
<UL>
  <LI>To demonstrate VxWorks' implementation of POSIX signal routines. </LI></UL>
<P></P>
<HR SIZE=3>

<H2>Description</H2>The signal facility provides a set of 31 distinct 
signals(see VxWorks Manual). A signal can be raised by calling 
<B><E>kill()</E></B>, which is analogous to an interrupt or hardware exception. 
A signal is bound to a particular signal with <B><E>sigaction()</E></B>. While 
the signal handler is running, other signals are blocked from delivery. Tasks 
can block the occurence of certain signals with <B><E>sigprocmask()</E></B>; if 
a signal is blocked when it is raised, its handler routine will be called when 
the signal becomes unblocked. 
<P></P>Signal handlers are typically defined as: <PRE>void sigHandlerFunction(int signalNumber)
{
.............. /* signal handler code */
..............
..............
}
</PRE>
<P></P>where <B>signalNumber</B> is the signal number for which 
<B>sigHandlerFunction</B> is to be invoked for.
<P></P>The <B>sigaction</B> function installs signal handlers for a task: 
<UL>
  <LI><I>int sigaction(int signo, const struct sigaction *pAct, struct sigaction 
  *pOact)</I> </LI></UL>
<P></P>A data structure of type <B>struct sigaction</B> holds the handler 
information. The <B>sigaction</B> call has three parameters: the signal number 
to be caught, a pointer to the new handler structure(of type <B>struct 
sigaction</B>), and a pointer to the old structure(also of type <B>struct 
sigaction</B>). If the program does not need the value of the old 
handler(*pOact), pass a NULL pointer for *pOact.
<P></P>To direct a specific signal to a specific task, the <B>kill(int, int)</B> 
call is made where the first argument the task id to send signal to, and the 
second argument is the signal to send to the task .
<P></P><B>1. Example: </B>
<P></P>In the example below, the "sigGenerator" function generates the SIGINT or 
Ctrl-C signal, and directs the signal to the "sigCatcher" task. When 
"sigCatcher" receives the signal, it suspends its normal execution and branches 
to a signal hander that it has installed(catchSIGINT function).
<P></P><PRE>------------------------------------------------------------------------------------
/* includes */
#include "vxWorks.h"
#include "sigLib.h"
#include "taskLib.h"
#include "stdio.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(30); /* allow time to get sigCatcher to run */

for (i=0; i &lt; 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  /* sigCatcher is dead */
    		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(&amp;newAction.sa_mask); /* no other signals blocked */
newAction.sa_flags = NO_OPTIONS;	/* no special options */

if(sigaction(SIGINT, &amp;newAction, NULL) == -1)
	printf("Could not install signal handler\n");

for (i=0; i &lt; ITER1; i++)
  	{
    	for (j=0; j &lt; 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); 
}

------------------------------------------------------------------------------------
</PRE>
<P></P>
<HR SIZE=3>

<H2>Procedures</H2>1. Copy the source code in the example and compile it. 
<P></P>2. Load the object file onto the target machine.
<P></P>3. Run the examples by executing the main routine("sigGenerator") of the 
example on WindSh terminal. 
<P></P>Note: Make sure you have redirected I/O, otherwise you won't see the 
results of the <EM>printf</EM> commands. 
<HR SIZE=3>

<H2>Follow On Experiment</H2>Experiment 1. Modify the program above so that 
there is no signal handler associated with "sigCatcher." What happens when the 
SIGINT signal is now delivered to "sigCatcher?"
<P></P>Experiment 2. Modify the program above so that the SIGINT signal is 
blocked(i.e. "sigCatcher" is prevented from receiving the SIGINT signal). 
<HR SIZE=3>

<H2>Additional Information</H2>Refer to VxWorks User's Manual and Reference 
Manual.
<P></P>
<HR SIZE=3>

<CENTER>
<H4><A 
href="http://www.rt.db.erau.edu/experiments/vx/toc/TableOfContents.html">Return 
to Primary Table of Contents </A></H4></CENTER>
<HR SIZE=3>

<CENTER>Last Updated: 4 April 1997<BR><EM>Created by: Dan Eyassu</EM><BR><A 
href="mailto:eyassud@db.erau.edu">eyassud@db.erau.edu</A><BR></CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -