0
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(); !!

  • 1
    Remember that QNX is mostly [POSIX compliant](http://www.qnx.com/developers/docs/6.5.0/topic/com.qnx.doc.neutrino_prog/posix_conformance.html) just like Linux. – Some programmer dude May 05 '14 at 15:10
  • If I use the above code in linux by changing the libraries then it is breaking at create_timer(); function only!!! – user3603553 May 05 '14 at 15:11
  • Can you elaborate on the "breaking"? Do you get compiler errors? Linker errors? Runtime errors? Please ***edit*** *your question* to include that information, and a complete and unedited log of possible compiler errors (if any). – Some programmer dude May 05 '14 at 15:16
  • It is crashing at : int fd_2ms = create_timer(); – user3603553 May 05 '14 at 15:18
  • 2
    To start with, you are using [`timer_create`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_create.html) and [`timer_settime`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html) wrong on ***both*** Linux and QNX (and any other POSIX platform). `timer_create` returns a boolean success/fail indicator only, it's the `timer_id` that you need to use for the call to `timer_settime`. – Some programmer dude May 05 '14 at 15:25
  • @user3603553: please.. edit your answer to only show the part that is related to your problem. – dsu May 05 '14 at 15:28
  • Also, does it really *crash* or do the function just return failure? – Some programmer dude May 05 '14 at 15:31
  • @JoachimPileborg: above code is working fine for linux and it is crashing as i said before – user3603553 May 05 '14 at 15:35
  • 1
    If your program is working for Linux, then you're just lucky! Probably the first timer you create have the id `0` and you're just changing that one single timer (instead of having three different timers). – Some programmer dude May 05 '14 at 15:36

0 Answers0