Tuesday, April 26, 2011

pthread

pthread_create(): create a thread, and thread starts to run
phtread_join(): suspend current execution, and wait until thread terminate

pthread_create() is similar with fork(); pthread_join() is similar with wait()

pthread_mutex_lock()
pthread_mutex_trylock()
pthread_mutex_unlock()

Socket Programming in C (3): Socket to Address structure Translation

// Get address structure by sock on current side, E.g. you want to know the client port number which randomly assigned
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

// Get address structure by sock on the other side. E.g. server wants to know client's structure.
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

Socket Programming in C (2): IP address/Hostname Translation

****************************************************************************
//Convert IP addresses from a dots-and-number string to a struct in_addr and back
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

(1) Old way, not support IPv6

char *inet_ntoa(struct in_addr in); // convert binary in network to string
int inet_aton(const char *cp, struct in_addr *inp); // convert dot-number string cp to struct in_addr pointer inp
in_addr_t inet_addr(const char *cp); // convert do-number string cp to in_addr_t

// ntoa: network to ASCII (readable)
// aton: ASCII to network
// atoi: ASCII to integer

(2) Use inet_pton() or inet_ntop(), support for IPv6 
#include <arpa/inet.h>
 

int inet_pton(int af, const char *src, void *dst); // convert string src to network dst. if af is AF_INET(IPv4), dst must be struct addr_in* type.
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); //convert network src to string dst. if af is AF_INET, src pints to a struct addr_in, and dst must be at least INET_ADDRSTRLEN bytes long

*******************************************************************************
//Convert IP addresses from host name string to a struct in_addr and back

#include <netdb.h>
#include <sys/socket.h>       /* for AF_INET */
struct hostent *gethostbyname(const char *name); // convert hostname/dotIP to a host struture, which includes network addr_in
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type); // convert a network addr_in to host structure
struct hostent {
               char  *h_name;            /* full host name*/
               char **h_aliases;         /* alias list */
               int    h_addrtype;        /* host address type */
               int    h_length;           /* length of address */
               char **h_addr_list;      /* list of addresses, often use (struct addr_in*) h_addr */
}
#define h_addr h_addr_list[0]  /* often use this one, need cast to struct addr_in*  */

*******************************************************************************
Example:
/*
 * Converts ascii text to in_addr struct.
 * NULL is returned if the address can not be found.
 * */
struct in_addr *atoaddr(char *address)
{
  struct hostent *host;
  static struct in_addr saddr;

  /* First try it as aaa.bbb.ccc.ddd. */
  saddr.s_addr = inet_addr(address);
  if (saddr.s_addr != -1)
    return &saddr;

  host = gethostbyname(address);
  if (host != NULL)
    return (struct in_addr *) *host->h_addr_list;

  return NULL;
}

Socket Programming in C (1): Address Structure

// this is all the address structure.
// first one is general one, which often declares, and the cast to a specific one
// second part is for IPv4
// third part is for IPv6
// last part is a general structure to store internet address.

include <netinet/in.h>

// All pointers to socket address structures are often cast to pointers
// to this type before use in various functions and system calls:

struct sockaddr {
    unsigned short    sa_family;     // address family, AF_xxx
    char                    sa_data[14];  // 14 bytes of protocol address
};


// IPv4 AF_INET sockets:

struct sockaddr_in {
    short                 sin_family;   // e.g. AF_INET, AF_INET6
    unsigned short   sin_port;     // e.g. htons(3490)
    struct in_addr    sin_addr;     // see struct in_addr, below
    char                  sin_zero[8];  // zero this if you want to
};

struct in_addr {
    unsigned long s_addr;          // load with inet_pton()
};


// IPv6 AF_INET6 sockets:

struct sockaddr_in6 {
    u_int16_t       sin6_family;   // address family, AF_INET6
    u_int16_t       sin6_port;     // port number, Network Byte Order
    u_int32_t       sin6_flowinfo; // IPv6 flow information
    struct in6_addr sin6_addr;     // IPv6 address
    u_int32_t       sin6_scope_id; // Scope ID
};

struct in6_addr {
    unsigned char   s6_addr[16];   // load with inet_pton()
};


// General socket address holding structure, big enough to hold either
// struct sockaddr_in or struct sockaddr_in6 data:

struct sockaddr_storage {
    sa_family_t  ss_family;     // address family
    // all this is padding, implementation specific, ignore it:
    char      __ss_pad1[_SS_PAD1SIZE];
    int64_t   __ss_align;
    char      __ss_pad2[_SS_PAD2SIZE];
};

Tuesday, April 19, 2011

asl compiler and DSDT

ASL(ACPI Source language) compiler: translate ACPI source language into ACPI machine language.
http://www.acpi.info/toolkit.htm

DSDT (Differentiated System Description Table): part of the ACPI specification and it supplies configuration information about a base system
https://wiki.archlinux.org/index.php/DSDT

tool: acpidump
dump the DSDT table from BIOS to a file

Thursday, April 14, 2011

ftrace trace-cmd for Ubuntu

Why do we need trace-cmd:
http://stackoverflow.com/questions/5658170/how-ther-linux-kernel-process-the-write-to-sys-power-state

Download Place:
http://packages.ubuntu.com/maverick/trace-cmd

Linux Device Drivers Book online version

http://lwn.net/Kernel/LDD3/

sysfs 
Chapter 14: The Linux Device Model

Linux Kernel Cross Reference Site

1. http://lxr.linux.no/linux/
2. http://www.linux-m32r.org/lxr/http/source/?a=i386

Compile new kernel on old kernel

http://jianggmulab.blogspot.com/2010/06/install-new-kernel-on-old-distros-such.html

Tuesday, April 12, 2011

TCP header flags

PUSH flag: When you type "ls\n", client want to send package immediately even only has three bytes data. this flag help you to do that

SYN flag, ACK flag: for tcp 3 way handshake.
SYN->
               <-SYN ACK
SYN->

FIN flag, ACK flag: for tcp termination
FIN->
              <-not FIN ACK
              <- FIN ACK
ACK->

Interview website

Good interview website:
http://www.careercup.com/
http://www.glassdoor.com/

Shortest Path Algorithm: Bellman-ford V.S. Dijkstra's

Dijkstra's: Given a vertex(node), it can find the shortest cost between vertex and very other vertex in the graph.

1. dist[start] =0 and dist[i] = infinity for other vertices i
2. nhop[start] = start and  nhop[i] = -1 for any vertex i (nhop: next hop from start)
3. empty set = {} store marked router
4. while not all routers have been marked
       1). let u be the vertex is unmarked and has smallest dist[u], and mark u
            // In the beginning, start will be marked because dist[start] is 0
            // how to get the smallest dist[u] is not easy: u canot be in the marked set, and u has to be the smallest. skip all the node is marked, then choose the vertex with smallest distance from start to this vertex
       2). for all neighbors v of u, dist[v] = min{dist[v], dist[u]+cost[u,v]}
       3). if dist[v] > (dist[u]+cost[u,v]), 
            nhop[v] = v (u=start) or nhop[u] otherwise

Run time of this algo. O(V^2), if you use heap to store the dist[i], run time O(E+VlogV)

Bellman-ford: the basic idea is very similar to Dijkstra's, but instead of selecting the shortest distance neighbour edges, it select all the neighbour edges.
Run time of this algo. O(|V|*|E|)

Routing Protocol Example:
Bellman-ford: distance vector routing(RIP)
Dijkstra's: link state routing(OSPF)

more info:
wiki dijkstra's bellman-ford

How does Suspend work on Linux (CentOS)


How does suspend work on Linux:

Step1: suspend on Userspace
pm-suspend is shell script, which attempts to prepare the system for suspend. For example, it will tell NetworkManager to shut down networking, and use vbetool (run code at video option ROM) to save VGA state. Finally, it wil echo string “mem” to /sys/power/state. This jumps to kernel, and the userspace is stopped.
Detailed info:
pm-suspend: /usr/lib/pm-utils/bin/pm-action
functions: /usr/lib/pm-utils/functions
10NetworkManager: /usr/lib/pm-utils/sleep.d/10NetwrokManager
20video: /usr/lib/pm-utils/sleep.d/20video

Step2: suspend in kernel
After run “echo -n mem > /sys/power/state”, the kernel goes through the device tree and calls suspend method on each bound driver. In the mean while, kernel execute couple of ACPI methods acpi_enter_sleep_state_prep() and acpi_enter_sleep_state(). In this method, kernel will set the SLP_TYP register at south-bridge, which tell BIOS to wakeup from sleep state next time.
Detailed info:
acpi_enter_sleep_state_prep(): /linuxsrc/drivers/acpi/hardware/hwsleep.c
acpi_enter_sleep_state(): /linuxsrc/drivers/acpi/hardware/hwsleep.c
main.c /linux/src/drivers/acpi/sleep/main.c

Suspend Time measurements:
Userspace: 1.5 seconds
Kenerl: 1 second

Friday, April 8, 2011

Linux Suspend Info

http://www.linux.com/archive/feed/54610

http://www.advogato.org/article/913.html

Thursday, April 7, 2011

Compile Linux Kernel

$ wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.24.tar.gz
$ tar -xzf linux-2.6.24.tar.gz
$ cd linux-2.6.24
$ make menuconfig
$ make -j4
$ make modules_install /* Remember to do this */
$ sudo make install

FAQ
1. remember to install ncurses-devel for make menuconfig
2. install qt-devel for make xconfig

useful link about kernel compile:
http://www.cs.columbia.edu/~junfeng/10sp-w4118/hw/hw2.html

Some problems I had:
1. Compile kernel on Ubuntu with gcc version 4.6
Because gcc-4.6 doesn't support soemthing, it need to downgrade to gcc (e.g version 4.4)
How to downgrade gcc, please see here

Option ROM concept

Definition from wiki:
An Option ROM typically consists of firmware that is called by the system BIOS.

Give an example of option ROM. BIOS want to initialize video card, but BIOS vendors don't have the data sheet of the video card. They have to call some functions in the Option ROM of video card, and initialize the VGA card.
Usually, functions in Option ROM need to be executed during the real mode.
Like coreboot, most of code are executing at protected mode. So it need to switch back to real mode in order to initialize the VGA. 

Wednesday, April 6, 2011

where is the kernel source file on Ubuntu

/usr/src
but these files are not full version of the Linux source
For writing kernel module development:
Ubuntu install kernel-dev
CentOS install kernel-devel

please see another article about kernel module development

Tuesday, April 5, 2011

University network Topology


This is the University network topology
Suppose I am typing cnn.com, it will send the package from my laptop to cnn.com server
1. package from my laptop to 4th level switch
2. 4th level switch go to Research I building switch (note: package hasn't hit router yet)
3.  Research I switch send the package to Innovation hall build router (first router, there only about 25 routers at the GMU campus)
4. Innovation Hall router send the package to the cloud at GMU network.
5. the cloud will deliver the package to Core Router,
6. core router send the package to fusion, do checking
7.  fusion send package back to core router.
8. core router send package to cloud GMU network
9, GMU network route package to the Edge System
10. Edge system send package to outside (cnn.com)

Monday, April 4, 2011

Find out VGA memory size

http://www.cyberciti.biz/faq/howto-find-linux-vga-video-card-ram/

Friday, April 1, 2011

Vim Tips

1. How to slip two window
$vim file1
:vsp file2(or do tab)  vertical split
:sp file2(or do tab) horizontal split
use Ctr+w to switch between windows

2. go the end of line and beginning of line
VI go to the end of line
$
VI go the beginning of the line
0
go to a specific line when your file is very large
$  vim filename 99999



3. another website taks about VIM tip


http://edwinux.blogspot.com/search/label/vim

4.To re-format a paragraph in VIM, type gqap in command mode

5. How to search
/search-keyword
 n: forward
shift+n: back

DMA

Here is the definition from wiki:
Direct memory access (DMA) is a feature of modern computers and microprocessors that allows certain hardware subsystems within the computer to access system memory for reading and/or writing independently of the central processing unit

The basic idea is that: DMA could move data from one device to another device without CPU.
For example, it would move data from memory to harddisk. If you use Peripheral Input/Output PIO to move data, it would access CPU for each single byte. (copy 0x80 to register, and copy register to hard disk.)
Another example, video capture card send data to network card(Video Chat), DMA helps a lot. DMA is a feature of Bus, not necessary to be a device.

Today, I just had a experience about DMA. The coreboot doesn't support the DMA enable for SATA on Windows XP, so the XP is very slow. Every single byte of data reading/writing to disk need to be done by CPU.
If we go to device manage and click Primary IDE Channel Properties, we would see Current Transfer Mode as PIO.
We could fix this problem by install the south-bridge SATA driver(VIA SATA on ASUS M2V-MX_SE ). After we installed the SATA driver, the Current Transfer Mode changed to Ultra DMA Mode 6

SVN tips

1. find current version of the project: svn info


2. $ svn checkout --username fengwei --password hello123 URL


3.  check out to a new name 
$ svn co svn+ssh://username@servername.com/home/username/svnrepos/file newfilename
find

4. man page of svn co: svn check -h

5.  $ svn diff --help

6. 
cd into a working directory (doesn't need to be latest version)
$ svn diff -r 4 > diff.txt
this will generate the difference between current version 10 and version 4