2010. 3. 24.

Real Time Embedded Programming

This is a XModem Protocol written in C++ using QNX Neutrino Operating System Simulator. It sends a file to another location over a network connection, by sending file blocks. Sender and Receiver communicates in real time to ensure correct behaviour of the protocol. The following block of the code opens up two channels for communication, creates 4 threads and establish connection between them. "ackmed" and "sendmed" are "mediums" in-between sender and receiver. "ackmed" sends "acknowledged" from receiver to sender when it is ready to receive the next block. "sendmed" sends file block from sender to receiver upon receiving "acknowledged".



#include <cstdlib>
#include <iostream>
#include <pthread.h>
#include <sys/neutrino.h>

int channelSender;    //Channel for Sender
int channelReceiver;  //Channel for Receiver

void* SenderProc(void *param);
void* ReceiverProc(void *param);
void* KindSendMedium (void *param);
void* KindAckMedia(void *param);

int main(int argc, char *argv[]) {
//std::cout << "Welcome to the Momentics IDE" << std::endl;
pthread_t sender, receiver, kindsendmed, kindackmed;
pthread_attr_t attr;
//Initialize channels
channelSender = ChannelCreate(0);
channelReceiver = ChannelCreate(0);

//Create 4 threads
pthread_attr_init( &attr );
pthread_create( &sender, &attr, &SenderProc, (void *)"XMODEM-Clarified.txt" );
pthread_create( &receiver, &attr, &ReceiverProc, (void *)"XMODEM-Output.dat" );
pthread_create( &kindsendmed, &attr, &KindSendMedium, NULL );
pthread_create( &kindackmed, &attr, &KindAckMedia, NULL );
//Waiting for transmision to be over
pthread_join(sender, NULL);
pthread_join(receiver, NULL);
//The other two threads die out upon exit
//pthread_join(kindsendmed, NULL);
//pthread_join(kindackmed, NULL);
//Destroy channels
ChannelDestroy(channelSender);
ChannelDestroy(channelReceiver);
return EXIT_SUCCESS;
}

댓글 없음: