Saturday, March 28, 2009

interview questions

e) 一个有10个指针的数组,该指针是指向一个整型数的(An array of 10 pointers to integers)
f) 一个指向有10个整型数数组的指针(A pointer to an array of 10 integers)
g) 一个指向函数的指针,该函数有一个整型参数并返回一个整型数(A pointer to a function that takes an integer as an argument and returns an integer)
h) 一个有10个指针的数组,该指针指向一个函数,该函数有一个整型参数并返回一个整型数( An array of ten pointers to functions that take an integer argument and return an integer )

答案是:
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer



#include
#include

int main(void) {


typedef union {long i; int k[5]; char c;} DATE;
struct date { int cat; DATE cow; double dog;} too;
DATE max;
int b[5];
printf("struct %d\n", sizeof(struct date) );
printf("union %d\n", sizeof(DATE));
printf("double %d\n", sizeof(double));
printf("long %d\n", sizeof(long));
printf("array int %d\n", sizeof(b));
//actual result is 40 24 8 8 20
}

sizeof
long 8, double 8, long long 8, short 2
64位机上测试的结果:
Summary: struct and union has common point, 大小永远为里面最大类型(max=8)的倍数,
struct 按照最大类型的大小来判断是否需要补齐。

Saturday, March 21, 2009

install KVM using QEMU

Insert kvm.ko && (kvm-amd.ko || kvm-intel.ko)

Install qemu-kvm

Creating a vdisk
qemu-img create -f qcow2 vdisk.img 10G

Booting from ISO
qemu-system-x86_64 -hda vdisk.img -cdrom /vmroot/ftp/ubuntu-9.04-desktop-amd64.iso -boot d -m 384 -usbdevice tablet -vnc :1

where -boot [a|c|d|n] boot from floppy (a), hard disk (c), CD-ROM (d), or network (n)
-m is RAM size in MBs

Follow the normal OS installation procedure.

To get the network working
#in file /etc/network/interfaces

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
address 10.128.0.20
netmask 255.255.0.0
broadcast 10.128.255.255
gateway 10.128.0.1
bridge_ports eth0
bridge_fd 0
bridge_maxwait 0
bridge_stp off

Creating a qtap device and adding it to the bridge

sudo modprobe tun
sudo tunctl -b -u root -t qtap0
sudo brctl addif br0 qtap0
sudo ifconfig qtap0 up 0.0.0.0 promisc

Now we will have network ready by configuring /etc/network/interfaces in the guest.

Booting up the VM (We will call the first machine as source and the machine where we are going to migrate the VM as destination)
Make sure you have vncserver installed on both source and destination machines.
qemu-system-x86_64 -hda vdisk.img -net nic,macaddr=52:54:00:39:81:49 -net tap,ifname=qtap0,script=no,downscript=no -vnc :1 -usbdevice tablet (On source machine)
vncviewer :5901 (On your desktop)

To migrate the VM
On target side execute following command to start the blank window.
qemu-system-x86_64 -hda vdisk.img -net nic,macaddr=52:54:00:39:81:49 -net tap,ifname=qtap0,script=no,downscript=no -incoming tcp:0:4444 -vnc :1 -usbdevice tablet

press Ctrl+Alt+2 to switch to qemu console and execute following command to migrate the VM.
migrate -d tcp::

After the migration we can connect to 5901 port of the destination host through vncviewer by executing following command on your desktop machine.
vncviewer :5901