I'm working with 989 svn version for the example
examples/xenomai/posix/rtt-sender.c
I'm trying to adapt the example to rtai-3.3.
I have a problem compiling with these command lines:
gcc - c -Wall -ggdb -o rtt-sender.o -I/usr/realtime/include -I/usr/src/rtnet/trunk/rtnet/stack/include rtt-sender.c
gcc -o rtt-sender -L/usr/realtime/lib -llxrt -lrtdm -lrt rtt-sender.o -lpthread
The program "rtt-sender" is successfully compiled so but the executable works until the instruction
"mq_open": there it gives Segmentation Fault.
Compiling with:
gcc - c -Wall -ggdb -o rtt-sender.o -I/usr/realtime/include -I/usr/src/rtnet/trunk/rtnet/stack/include rtt-sender.c
gcc -o rtt-sender -L/usr/realtime/lib -llxrt -lrtdm -lrt -lmqueue rtt-sender.o -lpthread
I have
"ld: cannot find -lmqueue
collect2: ld returned 1 exit status
make: *** [sender] Error 1"
Why? I have not got the mqueue dynamic library, is it so?
How can i install it? Can you link a tutorial for this? I've searched for it all day without results!
Do i have to patch the kernel to have mqueue compatibility??
The rtt-sender.c changed by me follows.
Hi, Tery
--
Teresa Noviello
Chiedersi Sempre:"Avro' il tempo di rifarlo?"
--------------------rtt-sender.c--------------------------
#include <errno.h>
#include <pthread.h>
#include <mqueue.h>
#include <signal.h>
#include <stdio.h>
#include < stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <rtai_lxrt.h>
#include <rtnet.h >
#include <time.h>
//my defines
#define DEBUG 1
char *dest_ip_s = "127.0.0.1";
char *local_ip_s = "";
unsigned int cycle = 100000; /* 100 ms */
pthread_t xmit_thread;
pthread_t recv_thread;
#define RCV_PORT 35999
#define XMT_PORT 36000
struct sockaddr_in dest_addr;
int sock;
mqd_t mq;
#define BUFSIZE 1500
union {
char data[BUFSIZE];
struct timespec tx_date;
} packet;
struct station_stats {
struct in_addr addr;
long long last, min, max;
unsigned long count;
};
struct packet_stats {
struct in_addr addr;
long long rtt;
};
#define MAX_STATIONS 100
static struct station_stats station[MAX_STATIONS];
void switch_errno(int err){
switch(err){
case EBADF:
printf("is not a valid descriptor\n");
break;
case EFAULT:
printf("(third argument ) references an inaccessible memory area\n");
break;
case ENOTTY:
printf("(first descriptor) is not associated with a character special device\n");
printf("The specified request does not apply to the kind of object that the descriptor"
"references\n");
break;
case EINVAL:
printf("Request or third argument is not valid\n");
break;
default:
printf("not recognized\n");
break;
}
}
static struct station_stats *lookup_stats(struct in_addr addr)
{
int i;
for (i = 0; i < MAX_STATIONS; i++) {
if (station[i].addr.s_addr == addr.s_addr )
break;
if (station[i].addr.s_addr == 0) {
station[i].addr = addr;
station[i].min = LONG_MAX;
station[i].max = LONG_MIN;
break;
}
}
if (i == MAX_STATIONS)
return NULL;
return &station[i];
}
void *transmitter(void *arg)
{
struct sched_param param = { .sched_priority = 80 };
struct timespec next_period;
struct timespec tx_date;
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
clock_gettime(CLOCK_MONOTONIC, &next_period);
while(1) {
next_period.tv_nsec += cycle * 1000;
if (next_period.tv_nsec >= 1000000000) {
next_period.tv_nsec = 0;
next_period.tv_sec++;
}
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_period, NULL);
clock_gettime(CLOCK_MONOTONIC, &tx_date);
/* transmit the request packet containing the local time */
if (sendto(sock, &tx_date, sizeof(tx_date), 0,
(struct sockaddr *)&dest_addr,
sizeof(struct sockaddr_in)) < 0) {
if (errno == EBADF)
printf("terminating transmitter thread\n");
else
perror("sendto failed");
return NULL;
}
}
}
void *receiver(void *arg)
{
struct sched_param param = { .sched_priority = 82 };
struct msghdr msg;
struct iovec iov;
struct sockaddr_in addr;
struct timespec rx_date;
struct packet_stats stats;
int ret;
msg.msg_name = &addr;
msg.msg_namelen = sizeof(addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = NULL;
msg.msg_controllen = 0;
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
while (1) {
iov.iov_base = &packet;
iov.iov_len = sizeof(packet);
ret = recvmsg(sock, &msg, 0);
if (ret <= 0) {
printf("terminating receiver thread\n");
return NULL;
}
clock_gettime(CLOCK_MONOTONIC, &rx_date);
stats.rtt = rx_date.tv_sec * 1000000000LL + rx_date.tv_nsec;
stats.rtt -= packet.tx_date.tv_sec * 1000000000LL +
packet.tx_date.tv_nsec;
stats.addr = addr.sin_addr;
mq_send(mq, (char *)&stats, sizeof(stats), 0);
}
}
void catch_signal(int sig)
{
mq_close(mq);
}
int main(int argc, char *argv[])
{
struct sched_param param = { .sched_priority = 1 };
struct sockaddr_in local_addr;
int add_rtskbs = 30;
pthread_attr_t thattr;
char mqname[16];
struct mq_attr mqattr;
int max_stations = 0;
int ret;
while (1) {
switch (getopt(argc, argv, "d:l:c::h")) {
case 'd':
dest_ip_s = optarg;
break;
case 'l':
local_ip_s = optarg;
break;
case 'c':
cycle = atoi(optarg);
break;
case -1:
goto end_of_opt;
case 'h':
printf("usage: %s [-d <dest_ip>] [-l <local_ip>] "
"[-c <cycle_microsecs>]\n", argv[0]);
return 0;
default:
printf("usage: %s [-d <dest_ip>] [-l <local_ip>] "
"[-c <cycle_microsecs>]\n", argv[0]);
return 0;
}
}
end_of_opt:
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(XMT_PORT);
if (dest_ip_s[0])
inet_aton(dest_ip_s, &dest_addr.sin_addr);
else
dest_addr.sin_addr.s_addr = INADDR_ANY;
if (local_ip_s[0])
inet_aton(local_ip_s, &local_addr.sin_addr);
else
local_addr.sin_addr.s_addr = INADDR_ANY;
signal(SIGTERM, catch_signal);
signal(SIGINT, catch_signal);
signal(SIGHUP, catch_signal);
mlockall(MCL_CURRENT|MCL_FUTURE);
printf("destination ip address: %s = %08x\n",
dest_ip_s[0] ? dest_ip_s : "SENDER", dest_addr.sin_addr.s_addr);
printf("local ip address: %s = %08x\n",
local_ip_s[0] ? local_ip_s : "INADDR_ANY", local_addr.sin_addr.s_addr);
printf("cycle: %d us\n", cycle);
/* create rt-socket */
//debug
#if !DEBUG
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket cannot be created");
return 1;
}
#else
if ((sock = socket_rt(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket cannot be (_rt) created");
return 1;
}
#endif
/* extend the socket pool */
//debug
#if DEBUG
printf("stampo RTNET_RTIOC_EXTPOOL:\t%d\n",RTNET_RTIOC_EXTPOOL);
#endif
#if !DEBUG
ret = ioctl(sock, RTNET_RTIOC_EXTPOOL, &add_rtskbs);
if (ret != add_rtskbs)
printf("WARNING: ioctl(RTNET_RTIOC_EXTPOOL) = %d\n", ret);
if(ret < 0){
switch_errno(errno);
perror("L'errore nella ioctl e'...\n");
}
#else
ret = ioctl_rt(sock, RTNET_RTIOC_EXTPOOL, &add_rtskbs);
if (ret != add_rtskbs)
printf("WARNING: ioctl_rt(RTNET_RTIOC_EXTPOOL) = %d\n", ret);
if(ret < 0){
switch_errno(errno);
perror("L'errore nella ioctl_rt e'...\n");
}
#endif
#if DEBUG
rt_make_hard_real_time();
#endif
#if !DEBUG
/* bind the rt-socket to local_addr */
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(RCV_PORT);
if ((ret = bind(sock, (struct sockaddr *)&local_addr,
sizeof(struct sockaddr_in))) < 0) {
close(sock);
perror("cannot bind to local ip/port");
return 1;
}
#else
/* bind the rt-socket to local_addr */
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(RCV_PORT);
if ((ret = bind_rt(sock, (struct sockaddr *)&local_addr,
sizeof(struct sockaddr_in))) < 0) {
close(sock);
perror("cannot bind_rt to local ip/port");
return 1;
}
#endif
/* create statistic message queue */
snprintf(mqname, sizeof(mqname), "rtt-sender-%d", getpid());
//debug
printf("Terminata la inizializzazione di nome di coda di messaggi\n");
mqattr.mq_flags = 0;
mqattr.mq_maxmsg = 100;
mqattr.mq_msgsize = sizeof(struct packet_stats);
mqattr.mq_curmsgs = 0;
//debug
printf("Terminata inizializzazione attributi della coda di messaggi\n");
//debug
printf("Momento di chiamata della mq_open\n");
mq = mq_open(mqname, O_RDWR | O_CREAT | O_EXCL, 0600, &mqattr);
//debug
printf("Terminata la mq_open\n");
if (mq == (mqd_t)-1) {
perror("opening mqueue failed");
close(sock);
return 1;
}
/* create transmitter rt-thread */
pthread_attr_init(&thattr);
pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
pthread_attr_setstacksize(&thattr, PTHREAD_STACK_MIN);
ret = pthread_create(&xmit_thread, &thattr, &transmitter, NULL);
if (ret) {
close(sock);
mq_close(mq);
perror("pthread_create(transmitter) failed");
return 1;
}
ret = pthread_create(&recv_thread, &thattr, &receiver, NULL);
if (ret) {
close(sock);
mq_close(mq);
perror("pthread_create(receiver) failed");
return 1;
}
pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
while (1) {
struct packet_stats pack;
struct station_stats *pstat;
int nr;
ret = mq_receive(mq, (char *)&pack, sizeof(pack), NULL);
if (ret < (int)sizeof(pack))
break;
pstat = lookup_stats(pack.addr);
if (!pstat)
continue;
pstat->last = pack.rtt;
if (pstat->last < pstat->min)
pstat->min = pstat->last;
if (pstat->last > pstat->max)
pstat->max = pstat->last;
pstat->count++;
nr = pstat - &station[0];
if (nr > max_stations)
max_stations = nr;
printf("%s\t%.3f us, min=%.3f us, max=%.3f us, count=%ld\r"
"\033[%dA\n", inet_ntoa(pack.addr), (float)pstat->last/1000,
(float)pstat->min/1000, (float)pstat->max/1000,
pstat->count, nr);
}
/* This call also performs the required switch to secondary mode for
socket cleanup. */
printf("\033[%dB\n", max_stations);
/* Important: First close the socket! */
while ((close(sock) < 0) && (errno == EAGAIN)) {
printf("socket busy - waiting...\n");
sleep(1);
}
pthread_join(xmit_thread, NULL);
pthread_kill(recv_thread, SIGHUP);
pthread_join(recv_thread, NULL);
return 0;
}
---------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ RTnet-users mailing list RTnet-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rtnet-users