preload
十一月 15

不知道這個東東是否和我所想像中的一樣.
『A』 process was blocked, and resumed whenl 『B』 process send a message to 『A』
有空時要研究看看
以下這網站連結:
https://sourceforge.net/projects/simpl/

十一月 15

在這個網站中看到這一段程式. 如下


mt.c (節錄自 Pthread Programming 一書)

----------------------------
#include
#include
#include
#include 

#include 

void do_one_thing(int *);
void do_another_thing(int *);
void do_wrap_up(int,int);

int r1=0, r2=0;

void main()
{
  pthread_t thread1, thread2;

  pthread_create(&thread1,NULL,(void*) do_one_thing, &r1);
  pthread_create(&thread2,NULL,(void*) do_another_thing, &r2);

  pthread_join(thread1,NULL);
  pthread_join(thread2,NULL);

  do_wrap_up(r1,r2);
}

void do_one_thing(int *i)
{
  for( *i = 1; *i <= 100; (*i)++)
    write(1,"do one thing.",13);
  /*
  如果用 printf() 或 outs() 這類使用緩衝區的函式,會受到
  緩衝區的共用互斥影嚮,使得執行時顯示的訊息,不會出現交
  叉出現的結果。
  這也是 pthread 及 C Library 配合使用時,一個重要的效率
  考量處:不當的使用 pthread ,反而會因系統呼叫的共用互斥
  動作,折損了程式執行的效率,比使用單一thread的效率還差。
  */
}

void do_another_thing(int *i)
{
  for( *i = 1; *i <= 100; (*i)++)
    write(1,"do anther thing.",26);
}

void do_wrap_up(int i, int j)
{
  printf("%d,%d:%d\n",i,j,i+j);
}

裡面那段中文註解, 讓我猶豫的狀態.....之前我的程式感覺效率很差, 是這個printf()造成的嗎??
仔細想想, 網路上的範例程式, 其printf() 都是放在pthread_mutex_lock() 之後耶....
改天來測測看

[11/19] 更新
程式很慢的原因, 終於找到問題了, 原來是2 個thread 一直用mutex 相互卡住造成的. 尤其是當某一thread 一直polling 某個被mutex 保護的變數時, 往往會造成另一thread 動作很慢. 解決方式是用condition 來讓polling 的thread 先睡覺去, 讓另一個thread 有時間做事, 等作完了, 再把睡覺的thread叫醒.

十一月 15

這幾天, 被thread 搞得頭昏腦脹, 總是有race condition 出現…..看來是自己對於pthread 還不熟….只好上網再多K一些資料.
以下是POSIX threads programming 的網站
http://www.llnl.gov/computing/tutorials/pthreads/

十一月 13

假如在C++ 中要呼叫C 的function,
則需加入如下宣告:


extern "C" {
   void c_function_name(void);
}

而比較好的作法是在c 的header file 中作如下的宣告:


#ifndef     __xxxx_H
#define     __xxxx_H
#ifdef __cplusplus
extern  "C" {
#endif    

/* put your c function definiction here */

void c_function_00(void);
void c_function_01(void);
void c_function_02(void);

#ifdef __cplusplus
}
#endif    

#endif      // __xxxx_H
十一月 08

http://www.cse.buffalo.edu/~milun/unix.programming.html

十月 27

這是我用來寫code 的makefile, 很好用喔


##
## FILE: Makefile
##
## Date: 2006/08/17
##
## Author: Hao Tseng
##
## Descriptio: Makefile for vtssd86 program
##
## MODIFICATION HISTORY:
## Date        By         Version  Change Description
##============================================================
## 2006/08/17   create this file
## 2006/10/25   modify for linux environment
##------------------------------------------------------------------------------

PROGRAM_NAME = vtssd86

# |
# | Include Files
# |
AINCLUDES =
CINCLUDES =  

# |
# | Object files
# |
OBJECTS =  \
	$(OBJ)/source_code_cpp.cpp.o \
	$(OBJ)/source_code_asm.S.o  \
	$(OBJ)/source_code_c.c.o  

# |
# | Put the file name that you want to compile only
# | and generate asm file
ASMFILES =  

# |
# | Tool chain definition
# |

GNU_TOOLS_PREFIX =
AS = $(GNU_TOOLS_PREFIX)as
CC = $(GNU_TOOLS_PREFIX)gcc
CPP = $(GNU_TOOLS_PREFIX)g++
AR = $(GNU_TOOLS_PREFIX)ar
LD = $(GNU_TOOLS_PREFIX)ld
OC = $(GNU_TOOLS_PREFIX)objcopy
NM = $(GNU_TOOLS_PREFIX)nm
OD = $(GNU_TOOLS_PREFIX)objdump
SZ = $(GNU_TOOLS_PREFIX)size

# | A special echo that prints prettily

E = @echo \\\# `date +%Y.%m.%d.%H:%M:%S` ---

# | Silence is golden... put "S=" (s equals nothing) on
# | the command line to get verbose output.

#S =
S = @

# |
# | directory for objects & other output files
# | 

OBJ = ./obj
OUTPUT = ./output
ASMDIR = ./asm_output

# |
# | And the source directory? Is right here.
# |

SRC = .

# |
# | Include paths and such
# |

INCLUDE_PATHS = \
		-I ./inc \
		-I ../inc \

# +------------------------------------
# | Switches for the compiler, the assembler,
# | and the linker
# |

DebugFlags = -g -O2

ASFlags = \
		$(INCLUDE_PATHS) \
		$(DebugFlags) \
		-W -c        

CCFlags = \
		$(INCLUDE_PATHS) \
		$(DebugFlags) \
		-W -c

CPPFlags = \
		$(CCFlags)

# +----------------------------------------
# | Rules
# +----------------------------------------
default : elf

$(OBJ)/%.cpp.o : $(SRC)/%.cpp $(CINCLUDES)
	$(E) Compiling $<
	$(S)$(CPP) $(CPPFlags) $< -o $@

$(OBJ)/%.S.o : $(SRC)/%.S $(AINCLUDES)
	$(E) Assembling $<
	$(S)$(CC) $(ASFlags) $< -o $@

$(OBJ)/%.c.o : $(SRC)/%.c $(CINCLUDES)
	$(E) Compiling $<
	$(S)$(CC) $(CCFlags) $< -o $@

$(ASMDIR)/%.c.s : $(SRC)/%.c $(CINCLUDES)
	$(E) Compiling only $<
	$(S)$(CC) $(CCFlags) $< -S -o $@

$(OBJ) :
	$(E) Making $@/ directory
	$(S)mkdir $@

$(OUTPUT) :
	$(E) Making $@/ directory
	$(S)mkdir $@

$(ASMDIR) :
	$(E) Making $@/ directory
	$(S)mkdir $@

clean : $(OBJ) $(OUTPUT) $(ASMDIR)
	$(E) Removing objects
	$(S)rm -rf $(OBJ)
	$(S)rm -rf $(OUTPUT)
	$(S)rm -rf $(ASMDIR)/*

# +-------------------------------------
# | Linking
# +-------------------------------------
LFLAGS =
#LFLAGS = \
#		-g \
#		-T ./linkleon.ld \
#		-e _hardreset \
#		-g -N -nostdlib

#LIBs declaraction
# "-lrt" is used for posix timer functions.
# "-lstdc++" is used for C++ code.
# "-lpthread" is used for pthread functions.
LIBS = -lgcc -lstdc++ -lm -lrt -lpthread

$(OUTPUT)/$(PROGRAM_NAME).elf : $(OBJ) $(OBJECTS) $(OUTPUT)
	$(E) Linking $@
	$(S)$(CC) $(LFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT)/$(PROGRAM_NAME).elf
	$(E) The Program size
	$(S)$(SZ) $(OUTPUT)/$(PROGRAM_NAME).elf

# |
# | S-Record
# |

$(OUTPUT)/$(PROGRAM_NAME).srec : $(OUTPUT)/$(PROGRAM_NAME).elf
	$(E) Converting $(PROGRAM_NAME).elf to S-Record
	$(S)$(OC) -O srec $(OUTPUT)/$(PROGRAM_NAME).elf $(OUTPUT)/$(PROGRAM_NAME).srec 

# |
# | Handy auxilliary files
# |

$(OUTPUT)/$(PROGRAM_NAME).nm : $(OUTPUT)/$(PROGRAM_NAME).elf
	$(E) Making $(PROGRAM_NAME).nm
	$(S)$(NM) $(OUTPUT)/$(PROGRAM_NAME).elf | sort > $(OUTPUT)/$(PROGRAM_NAME).nm

$(OUTPUT)/$(PROGRAM_NAME).objdump : $(OUTPUT)/$(PROGRAM_NAME).elf
	$(E) Making $(PROGRAM_NAME).objdump
	$(S)$(OD) $(OUTPUT)/$(PROGRAM_NAME).elf -d -S > $(OUTPUT)/$(PROGRAM_NAME).objdump    

$(OUTPUT)/$(PROGRAM_NAME).dat : $(OUTPUT)/$(PROGRAM_NAME).elf
	$(E) Making $(PROGRAM_NAME).dat
	$(S)$(OD) $(OUTPUT)/$(PROGRAM_NAME).elf -s > $(OUTPUT)/$(PROGRAM_NAME).dat    

$(OUTPUT)/$(PROGRAM_NAME).bin : $(OUTPUT)/$(PROGRAM_NAME).elf
	$(E) Converting $(OUTPUT)/$(PROGRAM_NAME).elf to Binary
	$(S)$(OC) -O binary $(OUTPUT)/$(PROGRAM_NAME).elf $(OUTPUT)/$(PROGRAM_NAME).bin 

# +-------------------------------------
# | Shortcut Targets
# |

srec : $(OUTPUT)/$(PROGRAM_NAME).srec

elf : $(OUTPUT)/$(PROGRAM_NAME).elf

bin : $(OUTPUT)/$(PROGRAM_NAME).dat $(OUTPUT)/$(PROGRAM_NAME).bin

aux : $(OUTPUT)/$(PROGRAM_NAME).nm $(OUTPUT)/$(PROGRAM_NAME).objdump

asm : $(ASMDIR) $(ASMFILES)

bootcode :
	$(E) Make the boot code
	$(S)cd boot; make bin

all : elf srec aux bin

help :
	@echo
	@echo Program name: $(PROGRAM_NAME)
	@echo
	@echo Available makefile targets:
	@echo
	@echo "  make clean -- erase intermediate files"
	@echo "  make srec  -- convert elf to S-Record"
	@echo "  make elf   -- only make the .elf file"
	@echo "  make aux   -- generate .nm and .objdump files"
	@echo "  make bin   -- generate .dat and .bin files"
	@echo "  make asm   -- generate .c.s files"
	@echo "  make all   -- do all of above"
	@echo "  make bootcode  -- make boot rom .elf & .bin file(in boot directory)"
	@echo
	@echo Add the option "S=" for a more verbose output
	@echo

# end of file
十月 27

就是這裡啦 http://users.actcom.co.il/~choo/lupg/index.html

十月 27

當發現有函數不知道該怎麼使用時, 用 Google Code Search 來找範例, 是不錯的選擇

十月 27

Introduction To Unix Signals Programming

十月 27

Continue reading »