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 ? 


Apache Beam Learning In Java Tutorials

 Wow  https://youtu.be/9kGETU63rkc