如果你所用的发行版没有可用的qt4.5 package那么请安装qt sdk.
亦歌Linux桌面版编译指南
Posted in 1g1g Linux Desktop App.
unix network programming notes
socket()
bind()
listen() ———-blocked <—-unblock SYN i connect() ———>ACK i+1, SYN j
accept()———-blocked <——unblock ACK j+13-way hand-shake
socket(*.21, *.*) — listening socket
accept() returns new socket (server_addr, 21, client_addr.client.port)
tcp write —- blocked until the buffer in APP have already been copied to the buffer in KERNEL
when the buffer in KERNEL is out of free space, APP be suspended.
data resident in the KERNEL buffer until its ACK has been received.
common [sockaddr] struct and [sockaddr_in]
struct sockaddr{ uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; } struct sockaddr_in{ //IPv4 socket address struct }new generic socket address struct for IPv6
struct sockaddr_storage { uint8_t ss_len; /* length of this struct (implementation dependent) */ sa_family_t ss_family; /* address family: AF_xxx value */ /* implementation-dependent elements to provide: * a) alignment sufficient to fulfill the alignment requirements of * all socket address types that the system supports. * b) enough storage to hold any type of socket address that the * system supports. */ };
[wait] and [waitpid] function
#include <sys/wait.h> pid_t wait (int *statloc); pid_t waitpid (pid_t pid, int *statloc, int options);wait: clean a terminated thread and returns its thread id and exit status
when all child threads are executing, blocked until anyone terminates
waitpid: waitpid gives us more control over which process to wait for and whether or not to block. First, the pid argument lets us specify the process ID that we want to wait for. A value of -1 says to wait for the first of our children to terminate. (There are other options, dealing with process group IDs, but we do not need them in this text.) The options argument lets us specify additional options. The most common option is WNOHANG. This option tells the kernel not to block if there are no terminated children.
notice: SIGCHLD is not queued.
the most proper way to handle zombie child threads are using waitpid with options WNOHANG as below :
#include "unp.h" void sig_chld(int signo) { pid_t pid; int stat; while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0) printf("child %d terminated\n", pid); return; } |
notice: slow system calls such as, accept() read() write() connect(), may be interrupted by signal handling, and return EINTR, we must handle them by redoing them.
Posted in Programming.
UML notes
<>—–aggregation
<>——-black diamond: composition
<|——- generalization, inheritance
<———- dependency
+ public
- private
# protected
~package
Posted in Programming.
Intro2Algorithm Notes
Part II Sorting
Ch9 Medians and order statistic
finding the minimum and maximum each = N comparison
finding them both = 3/2* N comparison
finding the second smallest = N + lgN -2 comparison :
construct a comparison tree when finding the smallest, then replace the smallest with negative infinite
and follow the tree again.
finding the kth element = O(n)
O(n) on average : adapted quicksort
O(n) on worst case: first divide the array into groups each of 5 elements. sort each group and select the median , sort all the medians and find the median-of-medians, partition the array around the median-of-medians as the pivot.
Part III Data Structures
Ch10 Elementary Data Structures
representing a tree with arbitrary number of children in a binary tree
struct Node{
Node* parent;
Node* left-child;
Node* right-sibling;
}
ch11 Hash Tables
hash function for ordinary string
“pt” = (’p’ * 128 + ‘t’ ) mod a-big-prime
universal hash methods
division method : k mod N, N should be a prime number not too close to an power of 2
Posted in Programming.
memory alignment
alignment
struct{
char data1;
short data2;
int data3;
char data4;
}
==>
struct{
char data1;
char padding1[1]; // short需要align到2byte
short data2;//不需要padding, int已经align到4byte
int data3;
char data4;
char padding2[3];//虽然已经是最后一个元素 但是为了避免用在数组里的问题时 也不得不加上padding
}
Posted in Programming.
memmove implementation
void* memmove(void* p_dst, void* p_src, size_t size){
assert(!dst && !src);
if(dst==src) return dst;
char* dst=static_cast
char* src=static_cast
if(dst>src & dst
//从后往前拷贝
}
else{
//从前往后拷贝
}
}
Posted in Programming.
c++ primer plus notes
c++ is a superset of c which has extra OOP and generic programming capability.
K&R C is classic C. new standard for C is ANSI/ISO C, a.k.a C99
#include
always use long variable for data longer than 32bit for the sake of good portability.
const vs #define : 1 explicitly declared type 2 used in scopes
losing precision of floating number. e.g. 2.34E+22 + 1
function pointer : int* func() –> int* (*func)()
two-dimension array processing function : void func(int arr[][4]) or void func(int (*arr)[4] )
implicit type conversion: using one argument constructor / use explicit keyword to turn it off
copy-constructor: assignment operator should perform deep copy.
copy-constructor should check whether internal static member is changed in ordinary constructor
assignment-operator should avoid copying from self to self
copy-constructor is invoked when 1. passing argument as value 2. return object as value
3
when doing member function overloading, const keyword is also taken into account.
e.g.
const char & operator[](int i) const {return str[i];}
char & operator[](int i) {return str[i];}
the former is for ordinary object, the later is for const object.
assignment and type conversion:
A a; B b; b= a;
1. a is converted to a temporary B object using conversion constructor: B(const A a)
2. the previous temporary B object is assigned to b using assignment operator
more efficient way:
overload the assignment operator letting it able to handle the assignment between A and B objects directly.
e.g.
B & operator=(A const & a)
deep-copying :
1 constructor : use constructor list
2 destructor: apply memberwise deleting automatically.
3 copy-destructor: apply memberwise copying.
4 assignment-operator : explicitly use base class assignment operator as BaseClass::operator=(obj) to copy the portion of base class. Then do memberwise copying.
STL auto_ptr is a possessive smart pointer, which means it will eliminate the pointer copied to it.
need more discussing http://en.wikipedia.org/wiki/Smart_pointer
problem sovled : 1 memory leaking 2 object slicing
difference between memcpy and memmove
whether can handle memory overlapping
difference between private inheritance and containment
1 private inheritance can redefine virtual functions, containment not
2 private inheritance can access protected members, containment not
3 containment can use several objects, private inheritance uses only one
stl container category :
sequence container
1 vector
2 lists
3 deque (double ended queue)
associative containers
1 map
2 multimap
3 set
4 multiset
stl iterator category
input iterator
output iterator
forward iterator
bidirectional iterator
random_access_iteraotor
forward declaration
when using friend class like this: B::foo() is friend class to A
Class B{
void foo(A& a);
}
Class A{
friend void B::foo(A& a);
}
in A’s declaration, the code refers to class B, therefore, B’s declaration must appear before.
However, in B::foo(), it also references class A. Then, forward declaration of A should be used here.
Notice, in B’s declaration, only function prototypes are allowed, otherwise, if a method definition reference A,
the compiler has to inspect the whole content of A, which is unavailable right now.
By the way, making the entire class B a friend doesnt need a forward declaration, because the statement
friend class B;
already indicates that B is a class.
return type is not involved in generating a member function’s signature. that is to say, you can override a function with a different return type, as long as the new return is derived from the previous one.
the default behavior of meeting a uncaught exception is to call terminate() then abort(); set_unexpected(handler) can override this behavior.
Posted in Programming.
相当重要的决定
from now on:
脱离操作系统的看问题 多了解windows的运行原理
转向分布式系统的工程应用
转向更多的工程应用 不再执着于科学
Posted in Life, Feeling & Story.
virtual memory management
virtual memory address
VMA
|virtual page number n bits|virtual page offset mbits|
VPN is used as index to find an entry in the address table which contains the physical page number
………….
…………
…………
physical page number p bits
…………
…………
then PPN is concatencated with VPO to form the entire Physical Address
in fact, N bits in virtual address is transformed into P bits in physical address.
size of address table is : 2^N * (P+1) extra 1 bit indicating whether the page is loaded in memory
Posted in Linux.
通用状态更新器
我把更新器分成了 http, XMPP, 其他
每个更新器都提供query(username, password) 和 update(username, password, status)方法
每个网站对应有自己的一个更新器
对外提供的是一个update.php页面 输入形如:
action=(query|update)&xml=
<?xml version="1.0" standalone="yes"?> <usu> <site> <sitename>renren.com</sitename> <username>用户名</username> <password>密码</password> <status>test12345</status> </site> <site> <sitename>kaixin001.com</sitename> <username>用户名</username> <password>密码</password> <status>test12345</status> </site> </usu> |
如果action是query 返回值形如:
<?xml version="1.0" standalone="yes"?> <usu> <site> <sitename>renren.com</sitename> <result>状态 或者 login failed 或者 query failed</result> </site> <site> <sitename>kaixin001.com</sitename> <result>状态</result> </site> </usu> |
如果为update 返回形如
<?xml version="1.0" standalone="yes"?> <usu> <site> <sitename>renren.com</sitename> <result>update success或者login failed 或者update failed</result> </site> <site> <sitename>kaixin001.com</sitename> <result>update success或者login failed 或者update failed</result> </site> </usu> |
测试网页地址是 http://portfolio.mrcongwang.com/statusupdater/test.php
关于开发:
添加一个新的http更新器很容易 比如 xiaonei的更新器 如下
<?php require_once('usu-classes.php'); class updater_xiaonei extends http_updater{ //site attributes public function updater_xiaonei(){ $this->sitename = "renren.com"; $this->update_domain = 'status.renren.com'; $this->update_method = 'POST'; $this->update_url = '/doing/update.do?'; $this->update_referer = 'http://'.$this->update_domain.$this->query_url; $this->query_domain = 'status.renren.com'; $this->query_url = '/getdoing.do'; $this->login_domain = 'login.renren.com'; $this->login_url = '/Login.do'; //patterns $this->query_success_pattern = '/<span id="currentStatus">(.+)<\/span>/'; $this->query_fail_pattern=''; $this->login_success_pattern=''; $this->login_fail_pattern='<div class="errors_div">'; $this->update_success_pattern='/\{"updateStatusId":\d+,"code":\d,"msg":".+"\}/'; $this->update_fail_pattern=''; $this->update_post='c=[STATUS]&raw=[STATUS]'; $this->query_post=''; $this->login_post='email=[USERNAME]&password=[PASSWORD]&origURL=&formName=&method=&submit=%E7%99%BB%E5%BD%95'; } } ?> |
kaixin001.com的更新器如下:
<?php require_once('usu-classes.php'); class updater_kaixin001 extends http_updater{ public function updater_kaixin001(){ //site attributes $this->sitename = "kaixin001.com"; $this->update_domain = 'www.kaixin001.com'; $this->update_method = 'POST'; $this->update_url = '/friend/status_submit.php?'; $this->update_post='state=[STATUS]'; $this->query_domain = 'www.kaixin001.com'; $this->query_url = '/home/'; $this->query_post=''; $this->login_domain = 'www.kaixin001.com'; $this->login_url = '/login/login.php'; $this->login_post='email=[USERNAME]&password=[PASSWORD]&url=/home/'; //patterns $this->query_success_pattern = '/<a href="\/home\/status.php" class="c0" style="text-decoration:none;color:#000;">(.+)<\/a>/'; $this->query_fail_pattern=''; $this->login_success_pattern=''; $this->login_fail_pattern='/账号或密码不太对吧!/'; $this->update_success_pattern='/\{"state":".+"\}/'; $this->update_fail_pattern=''; } } ?> |
Posted in Universal Status Updater.
