2010年9月13日 星期一

pipe @ Linux Kernel

pipe 為一種基於 Parent && Child 之間的 communication Channel, 有點類似 FIFO 的架構, 但跟FIFO 還是有點不同.pipe會先建立起個緩衝區,分別做讀完(pipefd[0]),寫完(pipefd[1])緩衝區的動作. 底下透過 Parent 建立起 Child Process(pipe Read), 等 Child End 之後在還給 Parent 做(pipe write).
#include >sys/wait.h>
#include >stdio.h>
#include >stdlib.h>
#include >unistd.h>
#include >string.h>

int
main(int argc, char *argv[])
{
    int pipefd[2];
    pid_t cpid;
    char buf;

    if (argc != 2) {
     fprintf(stderr, "Usage: %s >string>\n", argv[0]);
     exit(EXIT_FAILURE);
    }

    if (pipe(pipefd) == -1) { // create pipe structure...
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    cpid = fork();  // create child proc
    if (cpid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

   if (cpid == 0) {      /* Child reads from pipe */
       printf("Child...\n");
  close(pipefd[1]); /* Close unused write end */
    // disable write func
        while (read(pipefd[0], &buf, 1) > 0) // read from argv[] one chart by one chart
            write(STDOUT_FILENO, &buf, 1); // write 2 STDOUT

        write(STDOUT_FILENO, "\n", 1);
        close(pipefd[0]);   // disable read func
        _exit(EXIT_SUCCESS);   // child proc end

    } else {                /* Parent writes argv[1] to pipe */
 printf("Parent...\n");
        close(pipefd[0]);          /* Close unused read end */
        write(pipefd[1], argv[1], strlen(argv[1]));
        close(pipefd[1]);          /* Reader will see EOF */
        wait(NULL);                /* Wait for child */
        exit(EXIT_SUCCESS);
    }
}
code ref: Linux Programmer's Manual Refs : Linux Programmer's Manual PIPE(2) pipe(7) - Linux man page Executing programs with C(Linux)

2010年9月12日 星期日

Qtstalker @ finance tool

Qtstalker 為 Open Source 的 project, 有點像TS(trade-station)跟HTS(日盛) 的分析軟體, 可以插入交易策略跟自訂的Indicator. 沒想到我還笨笨的自己在那邊刻.....XD,不過這樣也好. 可以熟知每個交系訊號的觸發機制跟字己進出場的原則. 可參考我過去做的一些理論跟原則 Ref Finance Lists Refs 盤後分析軟體 - qtstalker

特色

  • 支援一打以上的技術指標,像是平滑異同曲線(MACD)、相對強弱指標(RSI)、布林格交易波帶 (MA's Bollinger Bankds)等
  • 六種 K 線圖表現圖形, line, bar, candlestick, point and fingure, paint bars 與 swing
  • 蹩腳的有價証券管理機制。但適合追蹤股價
  • 支援多種市場資訊來源,可以從 Yahoo, CME, NYBOT 中取得股市市價
  • 技術線圖中可以加上買賣指標記號、文字、直線、橫線與斐波納契折線(Fibonacci Retracement)
  • 圖形顯示模式可為日、週與月
  • 投資模式可供 stock, futures, index 與 spreads.
  • 三種圖形縮放模式像是,縮放至螢幕大小,所有資料序列與 log。
  • 可模組化股價與指標,提供未來應用彈性
  • 回測功能,可用實際交易資料來測試指標效能
  • 支援獲利計算功能,可加入特定股票計算買入市值到售出之獲利或損失
Refs 盤後分析軟體 - qtstalker

2010年9月11日 星期六

fork && clone @ Linux Kernel

除了用 fork 的方式來實現多線程的方式外,也可以用 clone 的方式.兩者最大的差別是在 Parent copy or not, fork 會copy Parent 的 space, 而 clone 卻是和 Parent 共用 space, 前者比較多用於對外的多平行執行序如external server/client,後者比較多用於 internal的 Process 如 mm management. fork_smp.c 可發現 Parent 的 data 不會被 Child 改變.
#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[]) {
        int count = 1;
        int child;

        if(!(child = vfork())) {
                printf("This is son, his count is: %d. and his pid is: %d\n", ++count, getpid());
        } else {
                printf("This is father, his count is: %d, his pid is: %d\n", count, getpid());
        }
return 0;
}
clone.c 發現 Parent 的 Data 會被 Child 而改變.
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <signal.h>
#define FIBER_STACK 8192

int a;
void * stack;
int do_something(){
        printf("This is son, the pid is:%d, the a is: %d\n", getpid(), ++a);
        free(stack);
        exit(1);
}
int main() {
        void * stack;
        a = 1;
        stack = malloc(FIBER_STACK);
        if(!stack) {
                printf("The stack failed\n");
                exit(0);
        }

        printf("creating son thread!!!\n");

        clone(&do_something, (char *)stack + FIBER_STACK, CLONE_VM|CLONE_VFORK, 0);
         printf("This is father, my pid is: %d, the a is: %d\n", getpid(), a);
         exit(1);
}
Refs: fork,vfork和clone底层实现 fork, vfork, clone,pthread_create,kernel_thread clone fork及vfork的区别