Linux中編譯pthread需要加上參數,參考man pthreads
Compiling on Linux
On Linux, programs that use the Pthreads API should be compiled using cc
-pthread.
星期六, 12月 26, 2009
星期一, 12月 07, 2009
Simple Shell in Unix
用C練習了一個簡單的shell,在這做一下重點筆記。
是收到signal時,會呼叫哪個function來處理。
原始檔案連結:Link
#include
...
#define MAXLINE 80
...
void handle_SIGINT();
...
int hindex;
...
void handle_SIGINT(){
int i = hindex - 1;
char tmp[MAXLINE];
write(STDOUT_FILENO, "\nHistroy:\n", 10);
for(;i > (hindex - 10) && i >= 0;i--){
...
}
signal(SIGINT, handle_SIGINT);
write(STDOUT_FILENO, "\n COMMAND->", 11);
}
void processBuffer(char inputBuffer[], char* args[], int length, int* background){
/* 處理inputBuffer,把他變成一個一個arg儲存在args中 */
}
void readHistory(char* args[], char fletter, int* background){
int i;
for(i = hindex - 1; i >= (hindex-10); i--)
if(*history[i] == fletter) break;
strcpy(history[hindex - 1], history[i]);
processBuffer(history[i], args, strlen(history[i]), background);
}
void setup(char inputBuffer[], char* args[], int* background){
/* 讀入user input並且呼叫processBuffer負責處理,順便紀錄在history中 */
}
int main(void){
char inputBuffer[MAXLINE];
int background; /* indicate if the program run in background when '&' at the end */
char* args[MAXLINE/2 + 1];
hindex = 0; /* initilize the history index which point to the next history storage */
while(1){
background = 0;
printf(" COMMAND->");
fflush(stdout);
signal(SIGINT, handle_SIGINT); /* set up, when program received the control + c
it will invoke the handle_SIGINT function */
setup(inputBuffer, args, &background);
if(!fork()){
execvp(args[0], args);
}else{
if(!background)
wait();
}
}
}
signal(SIGINT, handle_SIGINT);
是收到signal時,會呼叫哪個function來處理。
原始檔案連結:Link
星期日, 12月 06, 2009
Shared Memory Segment's Infomation
每個Segment的相關資料,都可以經由shmctl(segment_id, IPC_STAT, &shmbuffer)來取得
而shmbuffer是struct shmid_ds (在/usr/include/sys/shm.h中),
裏面有些欄位來說明一下:
int shm_segsz: size of the shared memory
short shm_nattch: number of attaches to the segment
struct ipc_perm shm_perm:permission structure
而ipc_perm在/usr/include/sys/ipc.h中,裏面有些欄位是
unsigned short uid: identifier of the user of the segment
unsigned short mode : permission modes
key_t key(on linux is __key) : user-specified key identifier
下面是個簡單的實作範例:
不過unix中有提供一個工具是ipcs就是類似這的output了,只是這裡寫的是極陽春版....
而shmbuffer是struct shmid_ds (在/usr/include/sys/shm.h中),
裏面有些欄位來說明一下:
int shm_segsz: size of the shared memory
short shm_nattch: number of attaches to the segment
struct ipc_perm shm_perm:permission structure
而ipc_perm在/usr/include/sys/ipc.h中,裏面有些欄位是
unsigned short uid: identifier of the user of the segment
unsigned short mode : permission modes
key_t key(on linux is __key) : user-specified key identifier
下面是個簡單的實作範例:
#include
#include
#include
#include
int main(){
int segment_id;
struct shmid_ds shmbuffer;
segment_id = shmget(IPC_PRIVATE, 4096, S_IRUSR | S_IWUSR);
shmctl(segment_id, IPC_STAT, &shmbuffer);
printf("Seg ID\tKey\tMode\tOwner\tSize\tAttaches\n");
printf("%d\t%d\t%o\t%d\t%d\t%d\n", segment_id, shmbuffer.shm_perm.__key,
shmbuffer.shm_perm.mode, shmbuffer.shm_perm.uid, shmbuffer.shm_segsz,
shmbuffer.shm_nattch);
shmctl(segment_id, IPC_RMID, NULL);
return 0;
}
不過unix中有提供一個工具是ipcs就是類似這的output了,只是這裡寫的是極陽春版....
星期六, 12月 05, 2009
Linux IPC (Shared memory)
一個簡單的程式,利用Child算好fabonacci數列後,還回給parent print。
在這之間是利用shared memory來傳遞資料的!
在DEBUG訊息中都有寫上註解了。
簡單說明一下sharedmem的一些system call
segment_id = sgmget(....) //分配shared memory的空間
shared_memory = (char *) shmat(....) //使程式取得該shared mem的pointer
shmdt(shared_memory) //中斷與shared mem的連結
shmctl(...) //清除shared mem region
在這之間是利用shared memory來傳遞資料的!
#include
#include
#include
#include
#include "../lib/string.c"
#define MAX_SEQUENCE 10
#define DEBUG 1
typedef struct {
long seq[MAX_SEQUENCE];
int seq_size;
} shared_data;
int main(int argc, char* argv[]){
int segment_id, pid;
char *shared_memory;
shared_data fib_seq;
const int size = 4096;
if(argc != 2){
fprintf(stderr, "Usage: %s seq_size\n", argv[0]);
return -1;
}
fib_seq.seq_size = atoi(argv[1]);
if(fib_seq.seq_size > MAX_SEQUENCE){
fprintf(stderr, "seq_size overflow.\n");
return -1;
}
segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
if(!(pid = fork())){
shared_memory = (char *) shmat(segment_id, NULL, 0);
fib_seq.seq[0] = 0;
fib_seq.seq[1] = 1;
int i;
if(DEBUG) fprintf(stderr, "Counting Fabonacii...\n");
for(i = 2; i < fib_seq.seq_size; i++)
fib_seq.seq[i] = fib_seq.seq[i-2] + fib_seq.seq[i-1];
if(DEBUG) fprintf(stderr, "Converting each int to string...\n");
char* fib_str[fib_seq.seq_size];
for(i = 0; i < fib_seq.seq_size; i++)
fib_str[i] = itoa(fib_seq.seq[i]);
if(DEBUG) fprintf(stderr, "Join the seq to string...\n");
char* return_str = join_strings(fib_str, fib_seq.seq_size, ',');
sprintf(shared_memory, "%s\n", return_str);
shmdt(shared_memory);
return 0;
}else{
wait(NULL);
if(DEBUG) fprintf(stderr, "Back to parent...\n");
shared_memory = (char *) shmat(segment_id, NULL, 0);
printf("%s", shared_memory);
shmdt(shared_memory);
shmctl(segment_id, IPC_RMID, NULL);
return 0;
}
}
在DEBUG訊息中都有寫上註解了。
簡單說明一下sharedmem的一些system call
segment_id = sgmget(....) //分配shared memory的空間
shared_memory = (char *) shmat(....) //使程式取得該shared mem的pointer
shmdt(shared_memory) //中斷與shared mem的連結
shmctl(...) //清除shared mem region
訂閱:
文章 (Atom)