Thursday, October 29, 2009

Bit masking



As system, kernel and device driver programmer, you, of course use heavy of bitwise operation. This is a simple C code demonstrate bit masking and it's use in the real world ;), see also bit packing/unpacking for more fun.

#include <stdio.h>

enum
{
SANDRA = 1 << 0,
DESSY = 1 << 1,
YANA = 1 << 2,
SUSAN = 1 << 3
};


void
print(unsigned char val)
{

printf("val: %u\n", val);
printf("SANDRA loves me: %i\n", (val & SANDRA) ? 1 : 0);
printf("DESSY loves me: %i\n", (val & DESSY) ? 1 : 0);
printf("YANA loves me: %i\n", (val & YANA) ? 1 : 0);
printf("SUSAN loves me: %i\n", (val & SUSAN) ? 1 : 0);
}


int
main(void)
{

unsigned char
val = 0;
val = SANDRA | DESSY | YANA;
print(val);
printf("\n");
val &= ~SANDRA;
print(val);
printf("\n");
val &= ~(DESSY | YANA);
val |= (SUSAN | SANDRA);
print(val);
printf("\n");
val &= ~(SANDRA | DESSY | YANA | SUSAN);
print(val);
return
0;
}

Tuesday, October 27, 2009

Daemon Process



Linux just like another Unix system, it can create background process to run as daemon, daemonizing is simply a process created by another process as it's child, the parent fork() then exiting after creating the child, the child then detaching itself from the controlling terminal and run in background.

Linux provide daemon() system call, this call was born from BSD Unix, and not a POSIX standard function, this function mostly used by service-based applications such as httpd, sshd, udevd, etc. See daemon() for more informations, here the demonstration code on daemon() usage.
#include <stdio.h>
#include <unistd.h>

int
main(void)
{

int
i = 0;
FILE *fp;

i = daemon(1, 1);
if
(i == -1) {
perror("daemon");
return
-1;
}

fp = fopen("./daemon.log", "w+");
if
(fp == NULL)
exit(EXIT_FAILURE);
while
(1) {
fprintf(fp, "%i\n", ++i);
fflush(fp);
sleep(1);
}

/* never reach here */

fclose(fp);
return
0;
}

Favorite win32 applications



Sometime i use win32 for playing some games, i love game too ;), here my favorite win32 applications:

Development
- MinGW
- Dev-C++

Editor
- OpenOffice.org
- Notepead++

Internet
- GNU Wget
- PuTTY
- Firefox
- FileZilla
- Pidgin

Multimedia
- Winamp
- VideoLAN

Utility
- 7-Zip

Friday, October 23, 2009

Low Back Pain

I got low back pain (LBP) yesterday and still feels it's pain, but no more pain as yesterday, i guess i was exercise too much within this 2 days. Anty call me that night, by following the way of her conversation i though she maybe already change her way, i feels like that. If the situation happen again (my life in 2005 at Bandung) i should give it up, just take your own way because i surrender.

Joker at The Dark Knight says:
Why so seriously?

Thursday, October 15, 2009

Routing

This is a basic routing command, we want to add 192.168.128.0 network with gateway 172.16.1.34, when a client from network 192.168.128.0 request, they will route to 172.16.1.34

route add -net 192.168.128.0 netmask 255.255.240.0 gw 172.16.1.34

Sunday, October 11, 2009

Fedora 11: Leonidas



Tonight i install Fedora 11 (Leonidas) on a 8GB Transcend's IDE SSD. There are previous Fedora 10 (Cambridge) in my SATA disk, i don't need to re-partition my SATA disk, just wipe out and re-partition the SSD. I build the USB media install based on my previous post. I choose Fedora 11 64bit Live-ISO build, i want to taste the Fedora's 64bit version. C'mon.

After creating the USB Live system, boot it up. And the new boot-screen show up, the new playmouth version. Sign-in to it's GNOME desktop, and I'm now in the Live Fedora 11 desktop. Then run /usr/bin/liveinst to start GUI installation.

Problem comes when in partitioning step, the installer wouldn't let me to create ext4 filesystem for boot, switch to ext3 filesystem was same. It complains that the Live system require a ext4 filesystem for root directory. I don't understand with 'Live system require a ext4 filesystem', and confused. Okay, how about creating a ext3 /boot partition and a ext4 / root partition. The installer now run, everything was run as expected.

Finish with installation, configure GRUB to add a new boot entry for Fedora 11 located in the Transcend 8GB SSD. Configure anything, then run yum to update. It's took about 400MiB downloads for updating.

After updating, then i install additional software such as GCC, GTK+ development library and additional non-free packages from rpmfusion repository. Test with play a movie, wow... i got shocked with display error, it displays such broken TV picture, the system was unresponsive, switch to another tty nothing happen. I do manual reset.

After booting, i can login and use the desktop again. Do some additional packages installation such as Devhelp and Liferea. Devhelp could not run, it told me 'Bus error' error. This is a system call related errors, i try to debug then i got the Devhelp's process caught a SIGBUS caused by memset function. I try to run ldconfig, it told me some truncated shared libraries. Okay, do filesystem checking by #touch /forcefsck then reboot the system. It's same, i still got the ldconfig truncated library files. Now i run yum reinstall [packages] to reinstall the truncated shared libraries. Test to run Devhelp again, and it's rock. The problem caused by some broken installation of shared libraries ;).

Here the main Fedora 11 64bit package's version:

- Kernel 2.26.29
- GNOME 2.6.23
- Xorg 1.6.4
- GCC 4.4.1

Friday, October 09, 2009

Mandiri ATM Card

Last day, I was accompanied by Dwi to Pulomas Mandiri branch to get my new ATM card. The card has been lost, i guess it lost when i was charged some moneys at Superindo store in front of my offices. After follow and sign some documents that asked by the Mandiri's officer, now i got the new ATM card. Thanks for you Dwi.

Wednesday, October 07, 2009

GCC -g option

Glynn Clements answer an email in the linux-c-programming mailing list to someone who ask about GCC -g option
Most the time I compile my application without the -g option due to performance reasons.

The -g switch has absolutely no effect upon performance. It simply causes and additional section to be added to the resulting binary. When the program is run normally (i.e. not under gdb), that section won't be mapped. The only downside to -g is that it increases the size
of the file.

However: debug information isn't necessarily much help if you compile with optimisation enabled, as the resulting machine code will bear little resemblance to the original source code. Statements will be re-ordered, many variables will be eliminated, etc.

Problem is that when it hits some bug and dumps core, this is not very useful because there is hardly any information in it. Is there some way to get some useful information out of the core file. For example one of my program crashed and with gdb I see the following: At least I know that the bug is in my function start_process. But is there some way to find out at what line it happened?

It isn't meaningful to talk about a "line" in the source code if you compile with optimisation enabled. However, you can tell gdb to disassemble the machine code for a particular function, and you can print the values contained in registers or at specific memory locations. Working out what that information means in terms of the source code is something which needs to be done manually.

Glynn Clements is an active member in the Linux's kernel mailing list, i love the way he answer a question.

The Linux proc Filesystem

/proc/cpuinfo - CPU informations
/prco/meminfo - Physical and virtual memory informations
/proc/sys/net/ipv4/ip_forward - IP forwarding setting (respect on protocol version)
/proc/sys/vm/overcommit_memory - Virtual memory overcommit setting

Sunday, October 04, 2009

Beer Hacks



Last night im going to Aso's base-camp, i promised him to fix his new Notebook. Going there by a Cab, Aso pay the fares for me - thanks you, because i really low in cash after my ATM card is lost -, there is Luke, Lule and other friends. Wow, Aso show me his new XPS Compaq notebook, wow... it's a stylish notebook, black-silver color and highend one. It's bundled with Microsoft Windows Vista (but I have no intereset with Vista), 3GB physical RAM, slot-in DVD-RAM and Intel Core 2 Duo CPU. Cool and high-end notebook.

The problem is Aso forgot his account's password, he complained that he set the password then he can't login into it again. Wow, he said "i was setting the password when i was drunken", the password is jakarta, mmm... i do a simple mistypo trick for drunken people ;), i try to upcase-ing the password to be JAKARTA, and viola it was unlocked and now sign to locked account. They all asking me "how the hell i did that?", i answer ... "This is a secret way, i can't tell you because you will able to use this to broke a system", they seem accept that.. haha... gotcha...

Friday, October 02, 2009

GCC C Extension

This is the GNU C Compiler extension for the C programming language.

Arithmetic on void- and Function-Pointers

In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.

A consequence of this is that sizeof is also allowed on void and on function types, and returns 1.

The option -Wpointer-arith requests a warning if these extensions are used.

Example:

void *ptr;
/* validate ptr pointer */
func(ptr + 2);



Non-Constant Initializers

As in standard C++, the elements of an aggregate initializer for an automatic variable are not required to be constant expressions in GNU C. Here is an example of an initializer with run-time varying elements:

foo (float f, float g)
{
    float beat_freqs[2] = { f-g, f+g };
    ...
}



Nested Functions

A nested function is a function defined inside another function. (Nested functions are not supported for GNU C++.) The nested function's name is local to the block where it is defined. For example, here we define a nested function named square, and call it twice:

foo (double a, double b)
{
    double square(double z) { return z * z; }

    return square(a) + square(b);
}


The nested function can access all the variables of the containing function that are visible at the point of its definition. This is called lexical scoping. For example, here we show a nested function which uses an inherited variable named offset:

bar (int *array, int offset, int size)
{
    int access(int *array, int index)
      { return array[index + offset]; }
    int i;
    ...
    for (i = 0; i < size; i++)
        ... access(array, i) ...
}



64 Bit Integer

GCC support 64 bit integer value with long long type. Example:

unsigned long long value64 = 23ULL;
unsigned long value32 = 23UL;

printf("sizeof(value64) is %i bit\n", sizeof(value64) << 3);
printf("sizeof(value32) is %i bit\n", sizeof(value32) << 3);



http://gcc.gnu.org