最近寫了乙個多程序的程式。 建立
n個程序,主程序檢視哪個程序空閒,如果空閒,則把任務通過管道的方式把任務分發給這個程序來完成。
只是簡單測試程式。所以還有以下地方需要補充:
1. 父程序對每個子程序沒有新增
等待終止的訊號。
2. 沒有新增
ctrl+c
的訊號控制。
3. 記憶體有些沒有釋放,另外函式入口處引數沒有檢測。
4. 管道都沒有關閉,釋放什麼的。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define process_busy 1
#define process_not_busy 0
/* process managementstructure */
typedef struct _processlistprocesslist;
//max 10 process
#define max_process_number 10
processlist *pprocesslist = null;
sem_t *semsharememory;
char acsharefilename[1024] = ;
int iprocessnumber = max_process_number;
processlist *pstsharedmemory = null;
#define share_memory_sem "sharememory"
int pfd[max_process_number][2];
/******************************************
* functionname:pcreatsem
* description:create the sem
* input:the posix name of the sem
* return:ok, pointer of sem
* fail, null
*******************************************/
sem_t * pcreatsem(const char *psemname)
;semtmp = sem_open(psemname, o_rdwr | o_creat | o_excl, s_irusr | s_iwusr, 1);
if (-1 == semtmp)
}else
semtmp = sem_open(psemname, o_rdwr | o_creat | o_excl, s_irusr | s_iwusr, 1);
if (-1 == semtmp)
}if (-1 == sem_unlink(psemname))
return semtmp;}
/*init processlist */
int iinitshareprocesslist( int maxprocessnum)
ifd = open(acsharefilename,o_rdwr | o_creat,s_irwxu);
if(ifd == -1)
pstprocessrecord = (processlist *)malloc(sizeof(processlist) * maxprocessnum);
if(pstprocessrecord == null)
memset(pstprocessrecord,0,sizeof(processlist) * maxprocessnum);
for(i = 0;iipid = -1;
(pstprocessrecord+i)->iindex = i;
(pstprocessrecord+i)->istatus = process_not_busy;
}write(ifd,pstprocessrecord,(sizeof(processlist)*maxprocessnum));
free(pstprocessrecord);
pstsharedmemory = (processlist *)mmap(null,sizeof(processlist) * maxprocessnum,
prot_read | prot_write,map_shared,ifd,0);
if(pstsharedmemory == (void *)(-1))
/* v-sem operate */
if (-1==sem_post(semsharememory))
return 0;}
int isetpid(int iindex,int ipid)
(*(pstsharedmemory+iindex)).ipid = ipid;
/***v-sem operate***/
if (-1==sem_post(semsharememory))
return 0;}
int isetprocessbusy(int iindex)
(*(pstsharedmemory+iindex)).istatus = process_busy;
/***v-sem operate***/
if (-1==sem_post(semsharememory))
return 0;
}int isetprocessnotbusy(int iindex)
(*(pstsharedmemory+iindex)).istatus = process_not_busy;
/***v-sem operate***/
if (-1==sem_post(semsharememory))
return 0;
}int igetindex(int *piindex,int ipid)
*piindex = -1;
for(i=0; i< iprocessnumber;i++)
}/***v-sem operate***/
if (-1==sem_post(semsharememory))
return 0;}
int igetnotbusyprocess(int *piindex)
*piindex = -1;
for(i=0; i< iprocessnumber;i++)
}/***v-sem operate***/
if (-1==sem_post(semsharememory))
return 0;}
int iprocesstask(int iindex)
;int n = 0;
int iret = 0;
printf(" child %d is in /n",iindex );
//init oci
while(1)
// close oci
return 0;
}int icreateprocess()
isetprocessbusy(iindex);
inumber++;
sprintf(acbuffer,"%s%d","tp",inumber);
write(pfd[iindex][1],acbuffer,sizeof(acbuffer));}}
自己手動寫乙個簡單的bs結構
拋去web框架,自己手寫乙個bs請求響應過程 自己建立乙個資料夾test,包含乙個hello.html 和乙個webserver.py 自己在html檔案裡面寫一些標籤 下面是webserver.py的主要內容 server obj.serve forever webserver.py主要是在本地啟...
自己寫的乙個Vue
下面這裡是我自己寫的乙個小型的vue,原理就是proxy proxy天生沒有prototype,因此要加上,不然extends會報錯 proxy.prototype proxy.prototype object.prototype class myvue extends proxy super da...
自己寫乙個scheme測試工具
端午在家休息了好幾天,基本上就是帶孩子和睡覺了。荒廢了好幾天之後,今天重操舊業。the little scheme看到第99頁了。要寫乙個函式numbered?來判斷乙個表示式是否是數字表示式。上 define atom?lambda a not or null?a pair?a define op...