Monday 21 May 2012

Simulating Traffic lights on runtime not a ideal state machine

In this example i am trying to simulate traffic light systems, where the lights will glow based on the sensor update, the threads used are pthreads from posix library and i am using very basic of gcc compiler for compiling. This traffic light manages the four directions of traffic and gives each a time to relay the traffic.

Here we go.... Happy understanding.

#include<iostream>
#include<time.h>
#include<unistd.h>
#include<pthread.h>
#include<cstdlib>
using namespace std;
//class to simulate the timer 

class timer
{
clock_t T1;
clock_t T2;
public:
void start()
{
T1=clock();
}
void stop()
{
T2=clock();
}

int get_time()
{
return ((T2-T1) * 60)/CLOCKS_PER_SEC ;
}

void reset()
{

// use this function to reset the timer
T1=0;
T2=0;
}

~timer() //oops a distructor
{
T1=0;
T2=0;
}
};
 timer t1;

// real time volatile i dont let the compiler use assumptions for me, no //optimizations  for me my dear
volatile bool g_red;
volatile bool g_green;
volatile bool g_yellow;
volatile bool stat=true;
volatile bool g_censor1;
volatile bool g_censor2;
volatile bool g_censor3;
volatile bool g_censor4;
volatile bool g_fyellow=true;

void check()
{
if(g_green && stat)
{
cout<<"Green Light"<<endl;
stat=false;
}
if(g_yellow && g_fyellow)
{
g_yellow=false;
cout<<"Yellow"<<endl;

if(g_censor1)
{
cout<<"++++++North get ready to stop"<<endl;
}
if(g_censor2)
{
cout<<"++++++South Get Ready to stop"<<endl;
}
if(g_censor3)
{
cout<<"++++++East Get Ready stop"<<endl;
}
if(g_censor4)
{
cout<<"++++++West Get Ready stop"<<endl;
}
}
if(g_red)
{
cout<<"=====RED=========="<<endl;
stat=true;
g_yellow=false;
g_green=false;
g_fyellow=true;
}
}



void north()
{
cout<<"\t\tControlling North"<<endl;
t1.start();
while(g_censor1 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=false;
g_censor2=true;
g_censor3=false;
g_censor4=false;
}
void south()
{
cout<<"\t\tControlling South"<<endl;
t1.start();
while(g_censor2 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=false;
g_censor2=false;
g_censor3=true;
g_censor4=false;
}
void east()
{
cout<<"\t\tControlling East"<<endl;
t1.start();
while(g_censor3 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=false;
g_censor2=false;
g_censor3=false;
g_censor4=true;
}
void west()
{
cout<<"\t\tControlling West"<<endl;
t1.start();
while(g_censor4 && t1.get_time() <700)
{
t1.stop();
check();
g_green=true;
g_red=false;
g_yellow=false;
if(t1.get_time() >500)
{
g_green=false;
g_yellow=true;
check();
g_fyellow=false;
}
}
t1.reset();
g_red=true;
check();
g_censor1=true;
g_censor2=false;
g_censor3=false;
g_censor4=false;
}

void *fun(void *arg)
{
cout<<"Censor Update"<<endl;
int num=rand();
if(num%2==0)
{
g_censor1=true;
g_censor2=false;
g_censor3=false;
g_censor4=false;
}
if(num%3==0)
{
g_censor1=false;
g_censor2=true;
g_censor3=false;
g_censor4=false;
}
if(num%5==0)
{
g_censor1=false;
g_censor2=false;
g_censor3=true;
g_censor4=false;
}
if(num%7==0)
{
g_censor1=false;
g_censor2=false;
g_censor3=false;
g_censor4=false;
}
if(g_censor1)
{
cout<<"++++++North Vechicle censor"<<endl;
}
if(g_censor2)
{
cout<<"++++++South Vechicle censor"<<endl;
}
if(g_censor3)
{
cout<<"++++++East Vechicle censor"<<endl;
}
if(g_censor4)
{
cout<<"++++++West Vechicle censor"<<endl;
}
}

int main()
{
//g_censor1=true;

pthread_t t1;
while(1)
{
try
{
//pthread_create(&t1,0,fun,0);
pthread_create(&t1,0,fun,0);
pthread_join(t1,0);
north();
//pthread_create(&t1,0,fun,0);
south();
pthread_create(&t1,0,fun,0);
pthread_join(t1,0);
east();
pthread_create(&t1,0,fun,0);
pthread_join(t1,0);
west();
//pthread_join(t1,0);
throw 1;
}
catch(int)
{
cout<<"exception";
pthread_create(&t1,0,fun,0);
}
}
return 0;
}

Tuesday 1 May 2012

Some intresting questions

Explain the memory layout of a c program ? Explain in detail the role of each segment and type of variables it stores?

Explain the inter process communication mechanism? which all you have used and which one is the fastest?

Whats the need of volatile variable? Explain about the effects in terms of usage of a ordinary variable and a volatile variable?

what does a process control block saves and where does it save the context of the process?

Explain virtual functions ? how they are implemented and how many virtual tables does a derived class has?

Design a macro which reverse a number 1234 as 4321.

Dynamic memory allocation in c and c++? advantages and disadvantages?

How will u implement garbage collection in c++?

Explain the different types of constructors and whats the copy constructor?

How real time operating systems are different from GPOS?What makes a system real time?

How will u restrict a object to be created on stack and heap?

Explain the different types of casting available with c++? Explain with example each one of them and providing situations where they can be used?

what are static libraries and dynamic libraries? are they part of the user code?

why do we prefer using references?

How can you call a function before main ?

Explain the ways u can debug and application?

What approaches will u apply if the controller is not able to connect to the instrument?

Test case to test a scenario where a text box takes a valid value between 0 and 1000? Write test cases ?

Talk about Boundary value analysis?

Types of testing ?

Talk about the configuration management tools requirement management tools ?

Talk about the bug management tool and the different states of the bug tracking ?

Threads synchronisation mechanism ? Design you own semaphore and prove that it solves the design purpose?

Which all features you have used in c++?

Explain abstract classes and the pure virtual functions?

Explain mapping at run time how the run time binding happens in case of virtual functions?

Whats the size  of class if i use a virtual function and if i don't use a virtual function?

Why virtual functions are considered slower? what is the hidden penalty?

Explain why you don't prefer using the heap memory in ur application this is a design specific questions?

how will u set , unset and determine which all bits are set in a value.

Explain big and little endian machines with few examples? where does it matter ?
what are events ? 


Tuesday 3 April 2012

Finite State Machine

#include<iostream>
#include<pthread.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;


char x_buff[1000];
char t_buff[1000];

/*Call setup mode is used to establish SVCs between DTE devices. A PLP uses the X.121 addressing
scheme to set up the virtual circuit. The call setup mode is executed on a per-virtual circuit basis,
which means that one virtual circuit can be in call-setup mode while another is in data-transfer mode.
This mode is used only with SVCs, not with PVCs.
*/

/*  data-transfer  mode is used for transferring data between two DTE devices across a virtual circuit.
In this mode, PLP handles segmentation and reassembly, bit padding, and error and flow control.
This mode is executed on a per-virtual circuit basis and is used with both PVCs and SVCs.

Idle mode is used when a virtual circuit is established but data transfer is not occurring. It is executed
on a per-virtual circuit basis and is used only with SVCs.

Call-clearing mode is used to end communication sessions between DTE devices and to terminate
SVCs. This mode is executed on a per-virtual circuit basis and is used only with SVCs.

Restarting mode is used to synchronize transmission between a DTE device and a locally connected
DCE device. This mode is not executed on a per-virtual circuit basis. It affects all the DTE device’s
established virtual circuits

*/

/* ************************TYPES OF PACKETS */
/*Four types of PLP packet fields exist:
1  General Format Identifier (GFI)—Identifies packet parameters, such as whether the packet
   carries user data or control information, what kind of windowing is being used, and whether
   delivery confirmation is required.
2  Logical Channel Identifier (LCI)—Identifies the virtual circuit across the local DTE/DCE
   interface.
3  Packet Type Identifier (PTI)—Identifies the packet as one of 17 different PLP packet types.
4  User Data—Contains encapsulated upper-layer information. This field is present only in data
   packets. Otherwise, additional fields containing control information are added.
               I bits
GFI               4
LCI              12
PTI              8
User Data       variable

*/
class timer
{
clock_t T1;
clock_t T2;
public:
void start()
{
T1=clock();
}
void stop()
{
T2=clock();
}

int get_time()
{
return ((T2-T1) * 60)/CLOCKS_PER_SEC ;
}

void reset()
{
T1=0;
T2=0;
}

~timer()
{
T1=0;
T2=0;
}
};
enum PLP
{
IDLE=1,
CALL_SETUP,
CALL_CLEAR,
DATA_TRANSFER,
RESTART
};
void *changer(void * arg);
void *FSM(void * arg);
PLP packet;
void change_state(PLP nextstate)
{
packet=nextstate;
}

void call_setup()
{
cout<<"Now in call _setup function "<<endl;
FILE *fp;
char file_name[50];
cout<<"enter file name to open";
gets(file_name);
fp=fopen(file_name,"r");
if(fp==NULL)
{
cout<<"File open Failed"<<endl;
change_state(CALL_SETUP);
}
else
{
fread(t_buff,800,800,fp);


//cout<<"waiting for input"<<endl;
if(t_buff)
{
strcat(t_buff,"+CALL_SETUP_HEADER");
cout<<"-----------------------------------------"<<endl;
cout<<"Input data is "<<t_buff<<endl;
cout<<"-----------------------------------------"<<endl;
}
else
{
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+CALL_SETUP_HEADER");
cout<<"-----------------------------------------"<<endl;
}
change_state(IDLE);

}
}

void idle()
{
cout<<"Now in idle function "<<endl;
strcat(t_buff,"+IDLE_HEADER");
change_state(DATA_TRANSFER);
}

void call_clear()
{
cout<<"Now in call_clear function "<<endl;
strcpy(x_buff,t_buff);
cout<<x_buff<<endl;
strcpy(t_buff,"");
change_state(RESTART);

}

void restart()
{
cout<<"Inside restart function "<<endl;
//change_state(CALL_SETUP);
cout<<"setting value of state machine using thread"<<endl;
pthread_t t1;
pthread_create(&t1,0,changer,0);
pthread_join(t1,NULL);
cout<<x_buff<<endl;

//pthread_create(&t1,0,FSM,0);
//pthread_join(t1,NULL);
}
void data_transfer()
{
static int counter;

counter++;
cout<<"Now in data_transfer function"<<endl;
cout<<"Session =="<<counter<<endl;
if(counter < 6)
{
cout<<"NXT STATE WILL BE IDLE "<<endl;
char *p;
p=t_buff;
if(strlen(t_buff)<32)
{
strcpy(x_buff,p);
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+data_transfer_HEADER");
cout<<"-----------------------------------------"<<endl;
}
else
{
cout<<"-----------------------------------------"<<endl;
strcat(t_buff,"+data_transfer_HEADER");
cout<<"-----------------------------------------"<<endl;
//cout<<"exceed"<<endl;
}
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
change_state(IDLE);
pthread_mutex_unlock( &mutex1 );
}
else
{
counter=0;
cout<<"NEXT STAE WILL BE CALL CLEAR max data _cycle Event"<<endl;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
change_state(CALL_CLEAR);
pthread_mutex_unlock( &mutex1 );
}
}

void *changer(void * arg)
{
cout<<"--------------CHANGER THREAD CALLED---------------------"<<endl;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex1 );
packet=CALL_SETUP;
pthread_mutex_unlock( &mutex1 );
}

void *FSM(void * arg)
{
static int co;
co++;
cout<<" Inside FSM CALL VALUE IS NOW==="<<co<<endl;
switch(packet)
{
case IDLE:
idle();
break;
case CALL_SETUP:
call_setup();
break;
case CALL_CLEAR:
call_clear();
break;
case DATA_TRANSFER:
data_transfer();
break;
case RESTART:
restart();
break;
default:
packet=CALL_SETUP;
break;
}

}

timer tim1; // hu ha global variables timer

int main()
{
pthread_t t;
tim1.start();
int i=0;
while(1)
    {
    try
    {
   
if((tim1.get_time() > 300) && !(tim1.get_time() < 0 ))
        {
//cout<<"Time value 150 elasped"<<tim1.get_time()<<endl;
if(tim1.get_time() < 300)
{
throw 1;
}
tim1.reset();
tim1.stop();
tim1.start();
i++;
pthread_create(&t,0,FSM,0);
pthread_join(t,NULL);


if(i%10 == 0)
{

pthread_t t1;
pthread_create(&t1,0,changer,0);
pthread_join(t1,NULL);
throw 1;
}



        } //edif
else
        {
static int x;
tim1.stop();
x++;
if(x==1)
{
for(int k=0;k<tim1.get_time();i++)
cout<<"--";
cout<<"SYSTEM INTIALIZED "<<endl;
}


    }
    }
    catch(int)
    {
    cout<<"******************************************************************"<<endl;
    cout<<"**********EXCEPTION HANDLED***************************************"<<endl;
    cout<<"Current state is "<<packet<<endl;
    cout<<"Current time is"<< tim1.get_time()<<endl;
    cout<<"TIMER RESET CALLED "<<endl;
    tim1.reset();
    cout<<"TIMER RESTARTED"<<endl;
    tim1.start();
    }
    catch(...)
    {
   
    cout<<"**********EXCEPTION HANDLED***************************************"<<endl;
    cout<<"Current state is "<<packet<<endl;
    cout<<"Current time is"<< tim1.get_time()<<endl;
    cout<<"TIMER RESET CALLED "<<endl;
tim1.reset();
    cout<<"TIMER RESTARTED"<<endl;
    tim1.start();
    }
}


//FSM();
return 0;
}

Quick questions

  1. Why do we use reference and pointers in copy constructor?
  2.  Can exception be thrown from constructor or ~Distrct?
  3.  Main is a entry point of a program how can be call another function  before main?
  4.  Find mid point of linked list only transverse it once?
  5.  What is differences between Mutex and semaphores?
  6. Explain resource sharing in multi threaded environment? 
  7. How static and dynamic libraries are linked ?
  8. Are virtual constructor and distructors possible?
  9. Give syntax for operator overloading for == operator?

==================Level 2========================
Explain Socket binding.
Can two applications have binding on the the same port?
what is the disadvantage of dynamic memory allocation?
how you debug your applications in your current evironment ?
+++++++++++++++++++++++++++++++++++++++++++++++++++
Write down a program to find prime numbers?
Explain why your RTOS is diffrent then a normal kernel?
 

Apache Beam Learning In Java Tutorials

 Wow  https://youtu.be/9kGETU63rkc