Emacs, Cygwin, cvs, etc. on Windows 7

When using Emacs 24.3.1 on Windows 7, I found that cygwin-mount.el would not work (it gave a bogus error of being unable to locate mount.exe) or using version control (cvs) within Emacs would fail with “Permission Denied”. Turns out the problem was Windows UAC (User Access Control). By changing the properties on runemacs.exe to “Run as administrator”, I was able to fix this issue.

Share

Windows 7 Emacs Compile Command in Cygwin Bash Shell

To ensure that Windows 7 Emacs uses the Cygwin Bash shell when executing the compile command (M-x compile) and to ensure that Bash reads the .bashrc, do the following:

(setq shell-file-name "bash")
(setq shell-command-switch "-ic")
Share

PuTTY, Linux Terminal, Xterm, Emacs, 256 Colors

The title says it all. This is some documentation to track how to get colors right when using PuTTY from Windows to SSH into a Linux server and then use Emacs in text-mode and edit php or other programming language files in syntax highlighted color.

In PuTTY, first make the following change (away from the default) — On the left-hand side configuration Category tree, select the Data sub-node from the Connection node. In there change, the “Terminal-type string” to “xterm-256color

Once you connect and login to the Linux server, do “echo $TERM” and you should see “xterm-256color” as a result. This allows the terminal emulation to support an xterm-type terminal with 256 colors. Once logged in, if you run “tput colors“, you should see “256” as the result of running this command.

Then run EmacsEmacs will run in text-mode. Within Emacs, run the command ‘list-colors-display‘. This should display 256 colors.

After this, you need to load up the appropriate major mode for the language you are going to be working on to get proper syntax coloring for that language (if you don’t use a suitable mode, you might get some syntax color, but not all the syntax coloring that will be supported in a full-fledged mode that is designed to support the language of your choice).

Share

C — How to check how much time a section of code takes to execute.

This requires use of standard C library support for the clock_gettime() function. This returns time as a data-structure (struct timespec) that includes the current time in seconds and nano-seconds. Call this one time before the section of code to evaluate and once after and then take a difference of the end time versus the start time to figure out the time taken in the section of code.

 

struc timespec timespec_diff(struct timespec end, struct timespec start)

{

}

void testThis()

{

int i;

struct timespec start, end, diff;

 

clock_gettime(0, &start);

for (i = 0; i < 100000; i++)

{

x = i * 45;

}

clock_gettime(0, &end);

diff = timespec_diff(end, start);

printf(“Section of code took %u seconds %d nano-seconds\n”, diff.tv_sec, diff.tv_nsec);

}

Share

C — How to check how much time a section of code takes to execute.

This requires use of standard C library support for the clock_gettime() function. This function returns clock time as a data-structure (struct timespec) that includes the the clock time in seconds and nano-seconds.

Call this function one time before the section of code to evaluate and once after and then take a difference of the end time versus the start time to figure out the time taken in the section of code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <time.h> // for the clock_* APIs and struct timespec

struct timespec timespec_diff(struct timespec end, struct timespec start)

{
    timespec temp;


    if ((end.tv_nsec-start.tv_nsec) < 0)
    { // If this is the case, then the seconds values are different by atleast 1.
        temp.tv_sec = end.tv_sec - start.tv_sec - 1; // Borrow one second from the seconds field...
        temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec; //...and use it in the nano-second field.
    }
    else
    {
        temp.tv_sec = end.tv_sec-start.tv_sec;
        temp.tv_nsec = end.tv_nsec-start.tv_nsec;
    }

    return temp;
}

void testThis()
{
    int i;
    struct timespec start, end, diff;


    clock_gettime(CLOCK_REALTIME, &start);

    for (i = 0; i < 100000; i++)
    {
        x = i * 45;
    }

    clock_gettime(CLOCK_REALTIME, &end);

    diff = timespec_diff(end, start);
    printf("Section of code took %u seconds %d nano-seconds\n", diff.tv_sec, diff.tv_nsec);
}
Share

Async Programming Patterns

Note To Self: In any system where you have multiple processes or tasks handling processing of various events and you provide a mechanism in one process or task for other modules to register a callback, you should also allow for a life-time parameter in the registration that is returned whenever the callback is called. This is so that if callbacks are processed asynchronously by queue-ing messages by the process registering callbacks, there can be stale messages in queue during which time the registration could be deleted and new registrations can happen. So there has to be a mechanism for the callback owner to be able to distinguish the life-time instance of the registration against which the callback was originally made.

Share

Are you unable to use C sizeof operator as expected in pre-processor macros?

If you are trying to use the C programming language’s sizeof operator in a C pre-processor macro and find that you are unable to use it as expected and you see compiler errors (actually C pre-processor (CPP) errors), you are really seeing expected behavior. The sizeof operator works only during compile time and the sizes of structures are only known at compile time and it is not an operator supported by the C pre-processor.

Share