Friends,
Today we are going to have a look in why we run ns2 program as "ns program.tcl"?
Before that we have to go through basic C++ programmings in ns2.Consider a basic C++ instruction only program i .e for example; we are considering a program to calculate time taken for a packet travel from uppermost node to lower most node with the time taken to travel packet between each nodes (t) are constant (say t=1ms) and the number of nodes as n. (say n=10). And we have to find out overall time taken to reach packet to lowermost node. The C++ program new.cc is given below and after compiling we get a executable file "new".
//new.cc
main(){
float time = 0 , t = 1;
int i, n = 10;
for(i = 1; i < n; i++)
time = time+i ;
printf("Overall time is %f seconds.\n",time);
}
Now we execute the program from terminal, we can see the following results;
>> ./new
Overall time is 10.0 seconds.
But this programming style is not perfect. If the time to reach packet from one node to another is different and the number of nodes are changed then every time we have to change C++ program. To avoid this we can use input arguments. If we have more that one or two inputs we can provide it from terminal by using input arguments. For that normally we use argc and argv commands. The variable argc shows the number of input arguments and the variable argv contains all the inputs needed for programmer. The following C++ program shows it.
//new.cc
int main(int argc, char* argv[]) {
float time = 0, t = atof(argv[0]);
int i, n = atoi(argv[1]);
for(i = 1; i < n; i++)
time = time + i ;
printf("Overall time is %f seconds\n",time);
}
Now we execute the program from terminal, we can see the following results;
>> ./new 1 10
Overall time is 10.0 seconds.
>> ./new 3 8
Overall time is 24.0 seconds.
This programming scheme also have flexibility problem and which is inconvenient as input increases. So we went for most convenient form of programming using configuration file. In this scheme the input data is used to pick input arguments. Consider a C++ program new.cc with configuration file con.txt and Function readArgFromFile(fp,a) reads the configuration file associated with a file pointer “fp,” and sets variables.
//new.cc
int main(int argc, char* argv[]) {
float time = 0, t[9];
FILE* fp = fopen(argv[1],"w");
int i, n = readArgFromFile(fp,a);
for(i = 1; i < n; i++)
time = time + i ;
printf("Overall time is %f seconds\n",time);
fclose(fp);
}
//con.txt
Number of node = 10
Time = 1 4 3 5 2 7 9 5 3
Now we execute the program from terminal, we can see the following results;
>> ./new con.txt
Overall time is 30.0 seconds.
By modifying that con.txt file, we get various results.
Now we are looking towards the ns2 and i hope you all get some clue about what i am talking too. In ns2, we know that program is run by using command "ns programname.tcl". The TCL file programname.tcl is used as the input argument providing configuration file and “ns” is a C++ executable file that generate after compiling using make and install function. The input configuration file specifies system parameters and all other configurations related to it.
I hope you all get it.
Stay tuned for more updates........!!!!!!!!!
Comments
Post a Comment