偶爾發現了調試函數需要的一個功能:根據反饋的資訊,調整一些簡單的參數。有兩個要求:1、不中斷程式,又不會每次都需要暫停等待輸入;2、不需要重新編譯,程式運行。這樣需求基本上就是多進程、監聽加IPC就ok。
首先想到的是使用環境變數。基本使用的是getenv()函數,獲得相應的環境變數即可。在使用過程中,發現根本就不可能。因為環境變數是使用者層級的,而每個console都相當於一個使用者,不能進行環境變數溝通。
隨後看到了IPC這個概念,遂查了一下,發現有管道、socket、訊號、共用記憶體、訊息佇列等。
用到的管道和共用記憶體都是基於共用記憶體的。不得不說linux設計的太聰明了,膜拜這種抽象能力。程式中的檔案抽象起來就是一塊記憶體嘛,而檔案又天生有共用的功能,偉大啊。
首先就是用shm,因為號稱速度快。是在/dev/shm/下建立一個檔案,這個檔案夾是完全存在於記憶體中(有時會用到swap)的。不會有太多磁碟IO,所以會比較快。問題在於需要使用鎖,才能保證讀寫的原子性。甚至是讀到不完整的資料。所以不愛啊。主要就是shmget()、shmat()、shmctl()和shmdt()。鎖則使用很相似的semget()、semctl()、semop()等。效果不錯但是邏輯很複雜。
PV操作:
int p(int semid){struct sembuf op;op.sem_num = 0;op.sem_op = -1;return semop(semid, &op, 1);}int v(int semid){struct sembuf op;op.sem_num = 0;op.sem_op = 1;return semop(semid, &op, 1);}
Sender:
#include<stdio.h>#include<sys/ipc.h>#include<sys/shm.h>#include<sys/types.h>#include<unistd.h>#include<string.h>#include<sys/sem.h>#include"statics.h"#include"pv.h"int main(){int shmid, semid;key_t key;message *msg;if((key = ftok(PATH,ID))==-1){printf("ftok error");return -1;}if((shmid = shmget(key, 4096, IPC_CREAT)) == -1){printf("shm error");return -1;}if((msg = shmat(shmid,NULL,0)) == (message*)-1){printf("shmat error");return -1;}if((semid = semget(key, 1, IPC_CREAT)) == -1){printf("sem error");return -1;}union semun option;option.val = 1;semctl(semid, 0, SETVAL, option);p(semid);msg->type = 0;msg->length = 1;((int*)msg->data)[0] = 2;v(semid);shmdt(msg);return 0;}
Reciever:
#include<stdio.h>#include<sys/ipc.h>#include<sys/shm.h>#include<sys/types.h>#include<unistd.h>#include<string.h>#include<sys/sem.h>#include"statics.h"#include"pv.h"int main(){int shmid, semid;key_t key;message *msg;if((key = ftok(PATH,ID))==-1){perror("ftok error");return -1;}if((shmid = shmget(key, 4096, IPC_CREAT)) == -1){perror("shm error");return -1;}if((msg = shmat(shmid,NULL,0)) == (message*)-1){perror("shmat error");return -1;}if((semid = semget(key, 1, IPC_CREAT)) == -1){perror("sem error");return -1;}union semun option;option.val = 1;semctl(semid, 0, SETVAL, option);while(1){p(semid);if(0 == msg->type){int *arr = (int*)msg->data;if(msg->length == 1){int val = arr[0];if(2 == val)break;}}v(semid);sleep(1);printf(".");}shmdt(msg);printf("exit\n");return 0;}還存在另一個問題,就是sem的釋放時機和位置,因為雙方的存在時間不一定,所以會有問題。
隨後就是使用具名管道,具名管道是一個可見的檔案,在運行結束後,管道是空的,檔案大小也是0,。應該也是純記憶體行為。具名管道主要是mkfifo()、open()、close()、read()、write()了,是檔案的使用模式。雖然操作沒有原子性(可能會寫一部分,再寫一部分資料),但是檔案的鎖機制應該是生效的。不會有讀入舊資料的危險。使用起來也沒顯簡單太多了。
Sender:
#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<errno.h>#include<stdio.h>#include"statics.h"int main(){message msg;int file, count;if((mkfifo(PATH, O_CREAT) < 0)&&(EEXIST != errno)){perror("can't create fifo");return -1;}file = open(PATH, O_WRONLY, 0);if(-1 == file){perror("file wrong");}msg.type = 0;msg.length = 1;((int*)msg.data)[0] = 2;count = write(file, &msg, sizeof(msg));if(-1 == count){perror("can't write");}printf("count: %d\n", count);close(file);return 0;}
Reciever:
#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<errno.h>#include<stdio.h>#include"statics.h"int main(){message msg;int file, count;if((mkfifo(PATH, O_CREAT) < 0)&&(EEXIST != errno)){perror("can't create fifo");return -1;}file = open(PATH, O_RDONLY|O_NONBLOCK, 0);while(1){sleep(1);printf(".");count = read(file, &msg, sizeof(msg));if(-1 == count && EAGAIN == errno){continue;}else{if(0 == msg.type){int *arr = (int*)msg.data;if(msg.length == 1){int val = arr[0];if(2 == val)break;}}}}printf("exit\n");return 0;}問題又在於檔案系統中會真的出現一個管道檔案,大小比較美好。
過程中遇到了一些很小又有點意思的問題:
0.fork()函數,這個函數複製了棧的內容,也就是說把自己啟動並執行空間複製了,導致會產生一次調用了兩個函數的效果。很有意思,所以會返回兩個值0子線程,val(>0)主線程。很有意思。而且整個的運行方式和MPI極像,終於明白MPI為什麼會有那種不合理設計了。
1.printf()不輸出,中間使用了printf('.')表徵接受端的運行情況。但是基本不會有輸出,直到進程結束。原因是printf是按行flush的,填滿一行的點時間太長,所以會看不到一個點。解決方案有兩個:使用stderr輸出;fflush(stdout)強制flush。
2.標頭檔在make的時候多次include,出現多次定義的情況。解決辦法是:在標頭檔中僅聲明extern變數,另建一個c檔案,專門定義變數。不需要加extern,但注意const等修飾要一致。
明天做一下速度測試,還沒想好方法。思考ing...