Dear all,
Iam using message queue services to communicate between two xenomai threads.
Iam trying using while loop in each thread to loop that for some 100 or 1000
iterations . But after 3 iterations it showing that the program ended. The
attached file contains the program . Its not hanging but terminating out of
execution.
--
Suresh balijepalli
Rosenehim,Baveria.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <native/task.h>
#include <native/timer.h>
#include <native/sem.h>
#include <native/mutex.h>
#include <native/queue.h>
#include <rtdk.h>
#define NTASKS 2
#define QUEUE_SIZE 1680
#define MAX_MESSAGE_LENGTH 40
RT_TASK task_struct[NTASKS];
#define QUEUE_SIZE 1680
RT_QUEUE myqueue;
RT_QUEUE myqueue1;
double timed_delay = 0;
void calc(void)
{
double megabytes , kilobytes;
float earth_years , martian_years;
for (megabytes=0;megabytes>1000;megabytes++)
{
/*convert megabytes to kilobytes*/
kilobytes = megabytes * 1024;
/*convert earth to martin years*/
martian_years = megabytes* 365 / 687;
}
}
/* dummy function*/
int processing(void)
{
int n,k,i=700000;
char * buffer;
char * buffer1;
char * temp;
char a[]="a" ;
char b[]="b" ;
buffer = (char*) malloc (i+1);
buffer1 = (char*) malloc (i+1);
temp = (char*) malloc (i+1);
if (buffer==NULL) exit (1);
if (buffer1==NULL) exit (1);
for (k=0; k<i; k++)
{
buffer1[k]=b;
buffer[k]=a;
temp[k]=buffer1[k];
buffer1[k]=buffer[k];
buffer[k]=temp[k];
calc();
}
free (buffer);
free (buffer1);
free (temp);
}
/*Task one function*/
void taskOne(void *arg)
{
int k=0;
while (k<100)
{
int retval,retval1,t1;
char message[] = "Message from taskOne";
char msgBuf1[MAX_MESSAGE_LENGTH];
/*Start Timer*/
RTIME start_tsc = rt_timer_tsc();
/*rt_printf("starting tics: %.3f\n",
(start_tsc));*/
/*call to dummy function*/
processing();
/*send message */
retval = rt_queue_write(&myqueue,message,sizeof(message),Q_NORMAL);
if (retval < 0 ) {
/* rt_printf("Sending error\n");*/
} else {
/* rt_printf("taskOne sent message to mailbox\n");*/
}
/*receive message */
printf("\nTaskone : trying to read!");
retval1 =rt_queue_read(&myqueue1,msgBuf1,sizeof(msgBuf1),TM_INFINITE);
if (retval1 < 0 ) {
rt_printf("Receiving error\n");
} else {
/* rt_printf("task1 received message: %s\n",msgBuf1);
rt_printf("with length %d\n",retval1);*/
printf("r\n");
}
printf("\nTaskone!! : %d\n", k);
/*end Timer*/
RTIME tsc=rt_timer_tsc();
/* rt_printf("end tics: %.3f\n",
(tsc));*/
/* calculate the difference */
timed_delay = rt_timer_tsc2ns(tsc-start_tsc);
/*rt_printf(" %.3f \n",
timed_delay );*/
/*start calculating max,Min,Average*/
k++;
}
}
void taskTwo(void *arg)
{
int m=0;
while(m<100)
{
int retval,retval1;
char msgBuf[MAX_MESSAGE_LENGTH];
char message1[] = "Message from tasktwo";
/* receive message */
retval = rt_queue_read(&myqueue,msgBuf,sizeof(msgBuf),TM_INFINITE);
if (retval < 0 ) {
rt_printf("Receiving error\n");
} else {
/* rt_printf("taskTwo received message: %s\n",msgBuf);
rt_printf("with length %d\n",retval);*/
}
/* send message */
printf("\nTasktwo : trying to write!");
retval1 = rt_queue_write(&myqueue1,message1,sizeof(message1),Q_NORMAL);
if (retval1 < 0 ) {
rt_printf("Sending error\n");
} else {
/* rt_printf("tasktwo sent message to mailbox\n");*/
printf("s\n");
}
printf("\nTasktwo!! : %d\n", m);
m++;
}
}
//startup code
void startup()
{
int i,t1;
char str[10] ;
void (*task_func[NTASKS]) (void *arg);
task_func[0]=taskOne;
task_func[1]=taskTwo;
rt_queue_create(&myqueue,"myqueue",QUEUE_SIZE,1000,Q_FIFO);
rt_queue_create(&myqueue1,"myqueue1",QUEUE_SIZE,1000,Q_FIFO);
rt_timer_set_mode(0);// set timer to tick in nanoseconds and not in jiffies
for(i=0; i < NTASKS; i++) {
rt_printf("start task : %d\n",i);
sprintf(str,"task%d",i);
rt_task_create(&task_struct[i], str, 0, 50, 0);
rt_task_start(&task_struct[i], task_func[i], &i);
}
}
void init_xenomai() {
/* Avoids memory swapping for this program */
mlockall(MCL_CURRENT|MCL_FUTURE);
/* Perform auto-init of rt_print buffers if the task doesn't do so */
rt_print_auto_init(1);
}
int main(int argc, char* argv[])
{
// code to set things to run xenomai
init_xenomai();
//startup code
startup();
}
_______________________________________________
Xenomai-help mailing list
[email protected]
https://mail.gna.org/listinfo/xenomai-help