Computer Scientist

Wednesday, 15 December 2010

Discussion on Array size, String length.

This is an revision concentrating two functions, sizeof() and strlen().

There are several manners for a programmer to define a string in C/C++ programs.

  1. char pointer: char *string; 
  2. char array: char string[100];

In order to initialize them, the following steps work.

  • define string immediately if we know what we want to define.
        char string[] = "This is what we want to defined";
        char *string = "This is what we want to defined";
  • define string first and then give the specific number afterwards.
        char string[100];
        string = "This is what we want to defined";
            CAUTION:<<This is not allowed in C++, can not assign an array to another array>>
            INSTEAD: strcpy(string, "This is what we want to defined");

        char *string;
        string = "This is what we want to defined";

In the following part, I give some different defined strings in my code. The print out is the results of two functions, sizeof() and strlen.

Here is the code:

    char *test;
    char test2[100];


    test = "This is what we want to define";
    char buffer []= "This is what we want to define";
    strcpy(test2, "This is what we want to define");
   
    std::cout << "test sizeof " << sizeof(test) << "\n";
    std::cout << "test strlen " << strlen(test) << "\n";
   
    std::cout << "buffer sizeof " << sizeof(buffer) << "\n";
    std::cout << "buffer strlen " << strlen(buffer) << "\n";
   
    std::cout << "test2 sizeof " << sizeof (test2) << "\n";
    std::cout << "test2 strlen " << strlen(test2) << "\n";

The print out is:

   test sizeof 4
   test strlen 30
   buffer sizeof 31
   buffer strlen 30
   test2 sizeof 100
   test2 strlen 30

Another aspect of the difference between strlen() and sizeof() is that strlen needs a function call to determine the string length, however, sizeof is able to give the length during the compile process. The buffer's example demonstrates this argument quite well. But, the prerequisite is that the sizeof() is able to give rather correct string length. The string should be defined and initialized as buffer example dose. In this case, bear in mind that sizeof will include the '\0' but strlent will not.

Hopefully, this makes clear of the usage of string.

Monday, 13 December 2010

Coding works of the Experiment

Here is the TODO list for experiment coding: some of them have long time to achieve and may be implemented later on due to the current works.

  * How to monitor the network bandwidth: the rough idea is to sum the number of packets during a period of time and then to calculate the amount of data per second during this period. At this point, how fine of the records should be concerned.

  * How to write a daemon to let the program running background:

  * How to stop the experiment program at anytime: Investigate how to used signal to tell the experiment process to terminate. Using IPC (Inter process call)??? The main issue is how to used Nagios to spread this instruction across all of the experiment machine.

  * How to monitor the system resources usages:

  * How to use automake to compile my own code together with the shared library of chord or sfslite. For this task, I have get some progresses on nagios snmp plugins. change the Makefile.am in ./src directory and run autoreconf --install in package directory and then automake (not sure if this is required). Then configure it and make it. PROBLEMs to be consider: 1. where is configure.ac? Is it optional for autoconf?
    ANSWER: configure.ac was called configure.in before autoconf 2.50. It can still be found in Chord and nagios snmp plugins distributions. When you modified some places in Makefile.am files or Configure.ac files, don't do anything except make it. All of the .in files will be regenerated.

Tuesday, 7 December 2010

Parsing Long Options

Find this topic in DOCUMENT of GNU C library: libc


Here I conclude some useful tips:


== return values of getopt():
  • successful
    • a character (the option name without argument)
    • a character (the option name), a pointer to char (char *optarg: argument)
  • failed
    • '?' (not included in options OR missing argument) (int optopt keeps the character)
  • -1 complete

== return values of getopt_long ():

  • successful
    • short_options
      • (same with getopt())
    • long_options
      • content of val (flag = NULL) (Tips, put corresponding short option char in val)
      • 0 (flag != NULL, put content of val into *flag)
      • (same with above two) (with argument are stored in optarg)
  • failed
    • (same with getopt())
  • -1 complete
    PS: indexptr record the index of the options in array of struct option.

Monday, 6 December 2010

Linux GUI system configuration

In order to configure linux by provided GUI, some commands are essential such as "system-cofig-firewall" will open Fedora's GUI firewall configuration window. There are also some other similar command. Using tab to investigate all of them!!!

Friday, 3 December 2010

Dive into more details of sfslite which is used by Chord official implementation

I decided to give up the usage of Openchord because it is quite difficult to use. I will back to Chord Official implementation.

Here I refresh some knowledge of sfslite and provide some useful web pages to understanding its code.








:
    One thing that is a little difficult to understand is in the Lesson 3: more than one callback are in the same procedure.
    Line 27 and 28 can be merged into "delaycb (1, 0, wrap(docallback, wrap(hello)));"
    I think I could understand it like this:
      each wrap() will produce a callback version of a function (type of callback::ref ).
      wrap(hello) provides a callback version of hello() method to function docallback( callcallback::ref ) as docallback(callback::ref)'s parameter.
      wrap(docallback, wrap(hello)) provides a callback version of docallback(callback::ref) to delaycb and asks delaycb() to register docallback(callback::ref) in event queue.

      ========Bear in mind, the return value of wrap() is callback::ref=========

      Here is the graphical expression of this mechanism:
      The usages of callback (callback::ref):
        (1). it can be passed into a callback function like docallback as its parameter and be invoked in this callback function cb();
        (2). it can be registered by a underlaying mechanism like delaycb.


Thursday, 2 December 2010

Problems suffered when I was usign SNMP

1. Set user pass-phrases problem:
    It seems to be infeasible to change pass-phrases directly in configure file. Each time, after I changed pass-phrases in configure file, I can still not make it effect even I have already restart the snmpd.
    (1). When I used snmpusm command to change password, I got a error message. This is because I was failed to assign a correct view to my current account.
    (2). When I used the same command, I got a USM generic error in rpc242. I supposed that I used wrong syntax of snmpusm command. Here is this time's typing (running locally in rpc242):
  snmpusm -v3 -l authPriv -u chenfu -a MD5 -A -x DES -X localhost passwd [ -Ca | -Cx ]

2. Running External Program:
    Actually, I can use several different "things" to run external programs using snmp on remote host.
    (1). "sh" is used to run shell script specially.
    (2). "exec" can used to run almost all of programs. <>
    ========Both "sh" and "exec" should be used by occasions when only one line result is needed. They are under a specific OID. Before, it seems to be feasible to assign aa arbitrary OID to a specific program (It is your responsibility to make sure no conflicts happen.). But now, I have never completed it. Maybe new version of SNMP forbidden this unreliable feature.========
    (3). "extend" is emerge under the situation where the manually assigning of an OID to a program is unsupported by "sh" and "exec".
    (4). "pass" is used to deal with some manually assigned OID. I am not quite familiar with "pass". As far as I know, you are able to decide what operations to do when a specific OID is looked up.

    My problem at the beginning is that I accidentally used the "exec" to run a simple script. I want to use a manually assigned OID to mark this entry. But it failed. When I turned to use "extend", it works quite well.

3. Usage of extend without assigned OID:

  1. On the remote server configure date extension in /etc/snmp/snmpd.conf. Simply add this single line at the end of the config file and reload snmpd:
    extend datecheck /bin/date
    
  2. From any client that has allowed SNMP access to the server query the datecheck with:
    ~$ snmpwalk -v2c -c public remote.server NET-SNMP-EXTEND-MIB::nsExtendOutputFull
    NET-SNMP-EXTEND-MIB::nsExtendOutputFull."datecheck" = STRING: Wed Oct 18 00:01:44 NZDT 2006
    
    That's about it. Easy way to run programs and scripts remotely, isn't it?