The code below is for receiving a data from the master via the socket for udp layer. There are some API supported in my project and I created a timer task for calling the task for every 2ms, 10ms and so on.
I am debugging this program on the embedded PC target in eclipse IDE using remote C/C++ application. I am calling the maketimer (alarm) inside the CreateSocket function. But how to check the timer task calling the function TASK1, TASK2 and TASK3 for every 2ms, 10ms and 100ms in the background?
#include "MAIN.h"
#define BILLION 1000000L
timer_t firstTimerID, secondTimerID, thirdTimerID;
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster ;
int connectedSocket, acceptSocket;
struct sockaddr_in addr;
struct sockaddr client, dest;
char buf[128];
long rc, sentbytes;
int port = 18017;
void TASK1(Task2ms_Raster)
{
struct timespec start, stop;
uint32 StartTime, StopTime;
if( (StartTime = clock_gettime( CLOCK_REALTIME, &start)) == -1 )
{
perror("clock gettime");
}
// return EXIT_SUCCESS;
/* Trigger DAQ for the 2ms XCP raster. */
if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_2msRstr( ))
{
++numDaqOverload2ms;
}
/* Update those variables which are modified every 2ms. */
counter32 += slope32;
/* Trigger STIM for the 2ms XCP raster. */
if( enableBypass2ms )
{
if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_2msRstr( ) )
{
++numMissingDto2ms;
}
}
if( (StopTime = clock_gettime( CLOCK_REALTIME, &stop)) == -1 )
{
perror( "clock gettime" );
}
duration2ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration2ms );
}
void TASK2(Task10ms_Raster)
{
struct timespec start, stop;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 )
{
perror( "clock gettime" );
}
/* Trigger DAQ for the 10ms XCP raster. */
if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_10msRstr( ))
{
++numDaqOverload10ms;
}
/* Update those variables which are modified every 10ms. */
counter16 += slope16;
/* Trigger STIM for the 10ms XCP raster. */
if( enableBypass10ms )
{
if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_10msRstr( ) )
{
++numMissingDto10ms;
}
}
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 )
{
perror( "clock gettime" );
}
XCP_FN_TYPE Xcp_CmdProcessor ( );
duration10ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration10ms );
}
void TASK3(Task100ms_Raster)
{
struct timespec start, stop;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 )
{
perror( "clock gettime" );
}
/* Trigger DAQ for the 100ms XCP raster. */
if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_100msRstr( ))
{
++numDaqOverload100ms;
}
/* Update those variables which are modified every 100ms. */
counter8 += slope8;
/* Trigger STIM for the 100ms XCP raster. */
if( enableBypass100ms )
{
if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_100msRstr( ) )
{
++numMissingDto100ms;
}
}
if((clock_gettime( CLOCK_REALTIME, &stop)) == -1 )
{
perror( "clock gettime" );
}
duration100ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration100ms );
}
static void timerHandler( int sig, siginfo_t *si, void *uc )
{
timer_t *tidp;
tidp = si->si_value.sival_ptr;
if ( *tidp == firstTimerID )
TASK1(Task2ms_Raster);
else if ( *tidp == secondTimerID )
TASK2(Task10ms_Raster);
else if ( *tidp == thirdTimerID )
TASK3(Task100ms_Raster);
}
static int makeTimer( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;
/* Set up signal handler. */
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
{
perror("sigaction");
}
/* Set and enable alarm */
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = timerID;
timer_create(CLOCK_REALTIME, &te, timerID);
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = intervalMS * 1000000;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = expireMS * 1000000;
timer_settime(*timerID, 0, &its, NULL);
return 1;
}
int CreateSocket()
{
if(rc!=0)
{
printf("socket failure code: %ld\n",rc);
return 1;
}
else
{
printf("socket started!\n");
}
// Socket creation for UDP
acceptSocket=socket(AF_INET,SOCK_DGRAM,0);
if(acceptSocket==-1)
{
printf("Failure: socket creation is failed, failure code\n");
return 1;
}
else
{
printf("Socket started!\n");
}
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));
if(rc== -1)
{
printf("Failure: listen, failure code:\n");
return 1;
}
else
{
printf("Socket an port %d \n",port);
}
while(rc!=-1)
{
rc= recvfrom(acceptSocket,buf,128,0,(struct sockaddr*)&client, sizeof(client));
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
printf("failure: recv, failure code\n");
break;
}
XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
makeTimer("First Timer", &firstTimerID, 2, 2); //2ms
makeTimer("Second Timer", &secondTimerID, 10, 10); //10ms
makeTimer("Third Timer", &thirdTimerID, 100, 100); //100ms
// buf[rc]='\0';
// printf("Client sendet: %s\n",buf);
// sprintf(buf2,"Du mich auch %s",buf);
// rc=sendto(connectedSocket,buf2,strlen(buf2),0);
}
close(acceptSocket);
return 0;
}
int main()
{
Xcp_Initialize();
CreateSocket();
return 0;
}
void XcpApp_IpTransmit( uint16 XcpPort, Xcp_StatePtr8 pBytes, uint16 numBytes )
{
if ((long)XcpPort==port)
{
sentbytes = sendto(acceptSocket,(char*)pBytes,(long)numBytes,0, (struct sockaddr*) &dest, sizeof(dest));
}
XcpIp_TxCallback(port,(uint16)sentbytes);
}
Read time(7) and signal(7).
BTW, your BILLION is wrong. You probably should have
#define BILLION 1.0e9
(since you are using it only in floating point expressions)
You could have three threads (read a pthreads tutorial), one for TASK1, one for TASK2, one for TASK3 (perhaps using clock_nanosleep(2)...). Or (preferably) you could have a single event loop (around e.g. a poll(2) or ppoll(2) multiplexing call....), perhaps also using timerfd_create(2) to get timer events, and also managing the sockets.
See this example of poll-based event loop. You should adapt it to also use a file descriptor obtained with timerfd_create.. You could also use some event looping library like libevent or libev
Notice that 2 milliseconds is a very small delay. (How reliable do you want that to be?)
Read Advanced Linux Programming
Related
int CreateSocket()
{
//pthread_attr_t attr;
// Socket creation for UDP
acceptSocket = socket(AF_INET,SOCK_DGRAM,0);
if(acceptSocket == -1)
{
printf("Failure: socket creation is failed, failure code\n");
return 1;
}
else
{
printf("Socket started!\n");
}
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));
fcntl(acceptSocket, O_NONBLOCK);
if(rc== -1)
{
printf("Oh dear, something went wrong with bind()! %s\n", strerror(errno));
return -1;
}
else
{
printf("Socket an port %d \n",port);
}
return acceptSocket;
}
int create_timer (void)
{
timer_t timer_id;
int timerfd = timer_create (CLOCK_REALTIME, 0, &timer_id);
if (timerfd < 0) {
perror ("timerfd_create:");
return -1;
}
return timerfd;
}
int start_timer_msec (int fd, int msec)
{
struct itimerspec timspec;
memset (&timspec, 0, sizeof(timspec));
timspec.it_value.tv_sec = 0;
timspec.it_value.tv_nsec = msec * 1000 * 1000;
if (timer_settime(fd, 0, &timspec, NULL) < 0) {
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
Xcp_Initialize();
int fd_2ms = create_timer();
int fd_10ms = create_timer();
int fd_100ms = create_timer();
int rval = 0;
if ((fd_2ms < 0) || (fd_10ms < 0) || (fd_100ms < 0)) {
exit (1);
}
if ((sock = CreateSocket()) < 0) {
perror ("Create_socket");
// fclose (fp);
exit (1);
}
start_timer_msec (fd_2ms, 2);
start_timer_msec (fd_10ms, 10);
start_timer_msec (fd_100ms, 100);
do
{
socklen_t len;
int max_fd = 0;
fd_set rdfds;
FD_ZERO (&rdfds);
GET_MAX_FD (max_fd, sock);
FD_SET (sock, &rdfds);
GET_MAX_FD (max_fd, fd_2ms);
FD_SET (fd_2ms, &rdfds);
GET_MAX_FD (max_fd, fd_10ms);
FD_SET (fd_10ms, &rdfds);
GET_MAX_FD (max_fd, fd_100ms);
FD_SET (fd_100ms, &rdfds);
rval = select (max_fd, &rdfds, NULL, NULL, NULL);
if (rval < 0) {
if (errno == EINTR)
continue;
} else if (rval > 0) {
/*Process CLI*/
if ((FD_ISSET (sock, &rdfds)))
{
FD_CLR(sock, &rdfds);
len = sizeof(client);
printf("NEW DATA ARRIVED\n");
//non blocking mode : MSG_DONTWAIT
rc=recvfrom(sock, buf, 256, 0, (struct sockaddr*) &client, &len);
//I am calculating the time here
//InterruptTime = GetTimeStamp();
//measurements[17] = InterruptTime;
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
if (errno == SIGINT)
continue;
printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
break;
}
ConfigureISR( );
XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
}
if ((FD_ISSET (fd_2ms, &rdfds))) {
FD_CLR(fd_2ms, &rdfds);
TASK1(Task2ms_Raster);
start_timer_msec (fd_2ms, 2);
}
if ((FD_ISSET (fd_10ms, &rdfds))) {
FD_CLR(fd_10ms, &rdfds);
TASK2(Task10ms_Raster);
start_timer_msec (fd_10ms, 10);
}
if ((FD_ISSET (fd_100ms, &rdfds))) {
FD_CLR(fd_100ms, &rdfds);
TASK3(Task100ms_Raster);
start_timer_msec (fd_100ms, 100);
}
}
} while (1);
close(sock);
//fclose (fp);
return 0;
}
I created a socket to receive data from client via the ip address and port number. I created a timer to call the task for every 2ms, 10ms and 100ms. The above code is working fine for linux. But how to convert the above code for QNX RTOS. What are the changes should I make for QNX rtos. could some one please help me ??
IF I use the same code in qnx then it is crashing at int fd_2ms = create_timer(); !!
int CreateSocket()
{
socklen_t len = sizeof(client);
// Socket creation for UDP
acceptSocket=socket(AF_INET,SOCK_DGRAM,0);
if(acceptSocket==-1)
{
printf("Failure: socket creation is failed, failure code\n");
return 1;
}
else
{
printf("Socket started!\n");
}
//non blocking mode
/* rc = ioctl(acceptSocket, FIONBIO, (char *)&flag);
if (rc < 0)
{
printf("\n ioctl() failed \n");
return 0;
}*/
//Bind the socket
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));
if(rc== -1)
{
printf("Failure: listen, failure code:\n")
return 1;
}
else
{
printf("Socket an port %d \n",port);
}
if(acceptSocket == -1)
{
printf("Fehler: accept, fehler code:\n");
return 1;
}
else
{
while(rc!=-1)
{
rc=recvfrom(acceptSocket,buf, 256, 0, (struct sockaddr*) &client, &len);
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
printf("something went wrong with data %s", strerror(errno));
break;
}
// XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
}
}
close(acceptSocket);
return 0;
}
void checkTasks()
{
int i;
for( i =1; i<= 100 ;i++)
{
if(++task1_counter ==2 )
{
task1_counter = 0;
makeTimer("First Timer", &firstTimerID, 2, 2); // creating the timer for 2ms
}
else if(++task2_counter == 10)
{
task2_counter = 0;
makeTimer("Second Timer", &secondTimerID, 10, 10); //creating the timer for 10ms
}
else if(++task3_counter == 100)
{
task3_counter = 0;
makeTimer("Third Timer", &thirdTimerID, 100, 100); //creating the timer for 100ms
}
else
printf("error\n");
}
}
}
int main()
{
CreateSocket(); //initializing all the foreground tasks
while(1)
{
checkTasks();
}
}
CreateSocket : This api is recieving data from the client continuously. I called recvfrom api inside the while loop within the CreateSocket function (which is not shown in the above code). I am calling the checkTasks within the while loop in main function. It is checking for the counter and calling the makeTimer () function call : This function definition is for creating a timer and call the task for every 2ms , 10ms and 100ms by signal.
my Question : once calling a init function in the main() : will it be recieving the data from the client and will in the background the checkTasks will be keep on running ?? Is it right ??
#include "MAIN.h"
#define BILLION 1000000L
timer_t firstTimerID, secondTimerID, thirdTimerID;
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster ;
int connectedSocket, acceptSocket;
struct sockaddr_in addr;
struct sockaddr client, dest;
char buf[128];
long rc, sentbytes;
int port = 18017;
void TASK1(Task2ms_Raster)
{
struct timespec start, stop;
uint32 StartTime, StopTime;
if( (StartTime = clock_gettime( CLOCK_REALTIME, &start)) == -1 ) {
perror("clock gettime");
}
// return EXIT_SUCCESS;
/* Trigger DAQ for the 2ms XCP raster. */
if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_2msRstr( ))
{
++numDaqOverload2ms;
}
/* Update those variables which are modified every 2ms. */
counter32 += slope32;
/* Trigger STIM for the 2ms XCP raster. */
if( enableBypass2ms )
{
if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_2msRstr( ) )
{
++numMissingDto2ms;
}
}
if( (StopTime = clock_gettime( CLOCK_REALTIME, &stop)) == -1 ) {
perror( "clock gettime" );
}
duration2ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration2ms );
}
void TASK2(Task10ms_Raster)
{
struct timespec start, stop;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
}
/* Trigger DAQ for the 10ms XCP raster. */
if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_10msRstr( ))
{
++numDaqOverload10ms;
}
/* Update those variables which are modified every 10ms. */
counter16 += slope16;
/* Trigger STIM for the 10ms XCP raster. */
if( enableBypass10ms )
{
if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_10msRstr( ) )
{
++numMissingDto10ms;
}
}
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
perror( "clock gettime" );
}
XCP_FN_TYPE Xcp_CmdProcessor ( );
duration10ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration10ms );
}
void TASK3(Task100ms_Raster)
{
struct timespec start, stop;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
}
/* Trigger DAQ for the 100ms XCP raster. */
if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_100msRstr( ))
{
++numDaqOverload100ms;
}
/* Update those variables which are modified every 100ms. */
counter8 += slope8;
/* Trigger STIM for the 100ms XCP raster. */
if( enableBypass100ms )
{
if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_100msRstr( ) )
{
++numMissingDto100ms;
}
}
if((clock_gettime( CLOCK_REALTIME, &stop)) == -1 ) {
perror( "clock gettime" );
}
duration100ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration100ms );
}
static void timerHandler( int sig, siginfo_t *si, void *uc )
{
timer_t *tidp;
tidp = si->si_value.sival_ptr;
if ( *tidp == firstTimerID )
TASK1(Task2ms_Raster);
else if ( *tidp == secondTimerID )
TASK2(Task10ms_Raster);
else if ( *tidp == thirdTimerID )
TASK3(Task100ms_Raster);
}
static int makeTimer( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;
/* Set up signal handler. */
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
{
perror("sigaction");
}
/* Set and enable alarm */
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = timerID;
timer_create(CLOCK_REALTIME, &te, timerID);
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = intervalMS * 1000000;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = expireMS * 1000000;
timer_settime(*timerID, 0, &its, NULL);
return 1;
}
int CreateSocket()
{
if(rc!=0)
{
printf("socket failure code: %ld\n",rc);
return 1;
}
else
{
printf("socket started!\n");
}
// Socket creation for UDP
acceptSocket=socket(AF_INET,SOCK_DGRAM,0);
if(acceptSocket==-1)
{
printf("Failure: socket creation is failed, failure code\n");
return 1;
}
else
{
printf("Socket started!\n");
}
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));
if(rc== -1)
{
printf("Failure: listen, failure code:\n");
return 1;
}
else
{
printf("Socket an port %d \n",port);
}
while(rc!=-1)
{
rc= recvfrom(acceptSocket,buf,128,0,(struct sockaddr*)&client, sizeof(client));
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
printf("failure: recv, failure code\n");
break;
}
XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
makeTimer("First Timer", &firstTimerID, 2, 2); //2ms
makeTimer("Second Timer", &secondTimerID, 10, 10); //10ms
makeTimer("Third Timer", &thirdTimerID, 100, 100); //100ms
// buf[rc]='\0';
// printf("Client sendet: %s\n",buf);
// sprintf(buf2,"Du mich auch %s",buf);
// rc=sendto(connectedSocket,buf2,strlen(buf2),0);
}
close(acceptSocket);
return 0;
}
//
//unsigned __stdcall CheckHandler(void* pArguments)
//{
// HANDLE h1,h2,h3;
//
//double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster ;
//
// h1=TimerTask(2,TASK1,&Task2ms_Raster);
// h2=TimerTask(10,TASK2,&Task10ms_Raster);
// h3=TimerTask(100,TASK3,&Task100ms_Raster);
// _endthreadex( 0 );
// return 0;
//}
int main()
{
Xcp_Initialize();
CreateSocket();
return 0;
}
void XcpApp_IpTransmit( uint16 XcpPort, Xcp_StatePtr8 pBytes, uint16 numBytes )
{
if ((long)XcpPort==port){
sentbytes = sendto(acceptSocket,(char*)pBytes,(long)numBytes,0, (struct sockaddr*) &dest, sizeof(dest));
}
XcpIp_TxCallback(port,(uint16)sentbytes);
}
The above is the code for recieving a data from the master via the socket for udp layer. There are some api supported in my project and I created a timer task for calling the task for every 2ms, 10ms and so on. I am debugging this program on the embedded pc target in eclipse ide using remote c/c++ application. when I compile my program there is no error but when i debug my program, it is showing the below errors.
I am recieving error as below:
Warning:
Cannot insert breakpoint -1.
Error accessing memory address 0x2b30: Input/output error.
#include "MAIN.h"
#define BILLION 1000000L
timer_t firstTimerID, secondTimerID, thirdTimerID;
double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster ;
int connectedSocket, acceptSocket;
struct sockaddr_in addr;
struct sockaddr client, dest;
char buf[128];
long rc, sentbytes;
int port = 18017;
void TASK1(Task2ms_Raster)
{
struct timespec start, stop;
uint32 StartTime, StopTime;
if( (StartTime = clock_gettime( CLOCK_REALTIME, &start)) == -1 ) {
perror("clock gettime");
}
// return EXIT_SUCCESS;
/* Trigger DAQ for the 2ms XCP raster. */
if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_2msRstr( ))
{
++numDaqOverload2ms;
}
/* Update those variables which are modified every 2ms. */
counter32 += slope32;
/* Trigger STIM for the 2ms XCP raster. */
if( enableBypass2ms )
{
if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_2msRstr( ) )
{
++numMissingDto2ms;
}
}
if( (StopTime = clock_gettime( CLOCK_REALTIME, &stop)) == -1 ) {
perror( "clock gettime" );
}
duration2ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration2ms );
}
void TASK2(Task10ms_Raster)
{
struct timespec start, stop;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
}
/* Trigger DAQ for the 10ms XCP raster. */
if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_10msRstr( ))
{
++numDaqOverload10ms;
}
/* Update those variables which are modified every 10ms. */
counter16 += slope16;
/* Trigger STIM for the 10ms XCP raster. */
if( enableBypass10ms )
{
if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_10msRstr( ) )
{
++numMissingDto10ms;
}
}
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
perror( "clock gettime" );
}
XCP_FN_TYPE Xcp_CmdProcessor ( );
duration10ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration10ms );
}
void TASK3(Task100ms_Raster)
{
struct timespec start, stop;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
}
/* Trigger DAQ for the 100ms XCP raster. */
if( XCPEVENT_DAQ_OVERLOAD & Xcp_DoDaqForEvent_100msRstr( ))
{
++numDaqOverload100ms;
}
/* Update those variables which are modified every 100ms. */
counter8 += slope8;
/* Trigger STIM for the 100ms XCP raster. */
if( enableBypass100ms )
{
if( XCPEVENT_MISSING_DTO & Xcp_DoStimForEvent_100msRstr( ) )
{
++numMissingDto100ms;
}
}
if((clock_gettime( CLOCK_REALTIME, &stop)) == -1 ) {
perror( "clock gettime" );
}
duration100ms = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "time difference is= %ld\n", duration100ms );
}
static void timerHandler( int sig, siginfo_t *si, void *uc )
{
timer_t *tidp;
tidp = si->si_value.sival_ptr;
if ( *tidp == firstTimerID )
TASK1(Task2ms_Raster);
else if ( *tidp == secondTimerID )
TASK2(Task10ms_Raster);
else if ( *tidp == thirdTimerID )
TASK3(Task100ms_Raster);
}
static int makeTimer( char *name, timer_t *timerID, int expireMS, int intervalMS )
{
struct sigevent te;
struct itimerspec its;
struct sigaction sa;
int sigNo = SIGRTMIN;
/* Set up signal handler. */
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timerHandler;
sigemptyset(&sa.sa_mask);
if (sigaction(sigNo, &sa, NULL) == -1)
{
perror("sigaction");
}
/* Set and enable alarm */
te.sigev_notify = SIGEV_SIGNAL;
te.sigev_signo = sigNo;
te.sigev_value.sival_ptr = timerID;
timer_create(CLOCK_REALTIME, &te, timerID);
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = intervalMS * 1000000;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = expireMS * 1000000;
timer_settime(*timerID, 0, &its, NULL);
return 1;
}
int CreateSocket()
{
if(rc!=0)
{
printf("socket failure code: %ld\n",rc);
return 1;
}
else
{
printf("socket started!\n");
}
// Socket creation for UDP
acceptSocket=socket(AF_INET,SOCK_DGRAM,0);
if(acceptSocket==-1)
{
printf("Failure: socket creation is failed, failure code\n");
return 1;
}
else
{
printf("Socket started!\n");
}
memset(&addr, 0, sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
rc=bind(acceptSocket,(struct sockaddr*)&addr,sizeof(addr));
if(rc== -1)
{
printf("Failure: listen, failure code:\n");
return 1;
}
else
{
printf("Socket an port %d \n",port);
}
while(rc!=-1)
{
rc= recvfrom(acceptSocket,buf,128,0,(struct sockaddr*)&client, sizeof(client));
if(rc==0)
{
printf("Server has no connection..\n");
break;
}
if(rc==-1)
{
printf("failure: recv, failure code\n");
break;
}
XcpIp_RxCallback( (uint16) rc, (uint8*) buf, (uint16) port );
makeTimer("First Timer", &firstTimerID, 2, 2); //2ms
makeTimer("Second Timer", &secondTimerID, 10, 10); //10ms
makeTimer("Third Timer", &thirdTimerID, 100, 100); //100ms
// buf[rc]='\0';
// printf("Client sendet: %s\n",buf);
// sprintf(buf2,"Du mich auch %s",buf);
// rc=sendto(connectedSocket,buf2,strlen(buf2),0);
}
close(acceptSocket);
return 0;
}
//
//unsigned __stdcall CheckHandler(void* pArguments)
//{
// HANDLE h1,h2,h3;
//
//double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster ;
//
// h1=TimerTask(2,TASK1,&Task2ms_Raster);
// h2=TimerTask(10,TASK2,&Task10ms_Raster);
// h3=TimerTask(100,TASK3,&Task100ms_Raster);
// _endthreadex( 0 );
// return 0;
//}
int main()
{
Xcp_Initialize();
CreateSocket();
return 0;
}
void XcpApp_IpTransmit( uint16 XcpPort, Xcp_StatePtr8 pBytes, uint16 numBytes )
{
if ((long)XcpPort==port){
sentbytes = sendto(acceptSocket,(char*)pBytes,(long)numBytes,0, (struct sockaddr*) &dest, sizeof(dest));
}
XcpIp_TxCallback(port,(uint16)sentbytes);
}
The above is the code for recieving a data from the master via the socket for udp layer. There are some api supported in my project and I created a timer task for calling the task for every 2ms, 10ms and so on. I am debugging this program on the embedded pc target in eclipse ide using remote c/c++ application. when I compile my program there is no error but when i debug my program, it is showing the below errors.
I am recieving error as below:
Thread [1] 6988 (Suspended : Signal : SIGILL:Illegal instruction)
main() at MAIN.c:328 0xb784db38
I'm trying to cancel a thread that way :
pthread_cancel(threads[id]);
And I release the mutex before cancelling the thread.
And after that I need to restart it because it was causing a DeadLock
usleep(1);
pthread_create(&threads[id], NULL, aFunction, &id );
usleep(1);
pthread_join(threads[id], NULL);
usleep(1);
I tried to remove pthread_join, but no luck.
This is a big part of the code :
#define LEFT(i) i
#define RIGHT(i) (i+1) % N
#define CreateSemaphore(s,v) sem_init( &s, 0, v)
#define WaitSemaphore(s) sem_wait( &s )
#define SignalSemaphore(s) sem_post( &s )
#define ReleaseSemaphore(s) sem_destroy( &s )
void restart(int id) {
release(id, LEFT(id));
int res;
if (allocated[id][RIGHT(id)] == 1) {
release(id, RIGHT(id));
}
res = pthread_cancel(threads[id]);
if (res != 0) {
perror("Thread cancelation failed");
exit(EXIT_FAILURE);
}
usleep(1);
res = pthread_create(&threads[id], NULL, aFunction, &id );
usleep(1);
if (res != 0 ) {
fprintf( stderr, "Error creating the thread %d \n", id );
exit( 1 );
}
printf("Waiting for thread to finish...\n");
res = pthread_join(threads[id], NULL);
if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
} else
printf("Passed join...\n");
usleep(1);
}
void * aFunction( void *i )
{
int value = *((int *)i);
while ( 1 ){
think( value );
take( value );
eat( value );
drop( value );
}
pthread_exit( NULL );
}
void take( int i ) {
request(i, LEFT(i));
WaitSemaphore( fork[LEFT(i)] );
allocation(i, LEFT(i));
usleep(100);
request(i, RIGHT(i));
WaitSemaphore( fork[RIGHT(i)] );
allocation(i, RIGHT(i));
usleep(100);
}
void drop( int i )
{
SignalSemaphore( forks[LEFT(i)] );
release(i, LEFT(i));
usleep(100);
SignalSemaphore( forks[RIGHT(i)] );
release(i, RIGHT(i));
usleep(100);
}
void release(int id, int f) {
WaitSemaphore(mutex[f]);
beingUsed[id][f] = 0;
currentAvail[f] = 1;
SignalSemaphore(mutex[f]);
}
did you try with pthread_exit(1) inside the thread code? This is other way to finish the running thread when it finishes it work.