?? memcleaner.cpp
字號:
#include <iostream>
#include <assert.h>
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <errno.h>
#include <pwd.h>
#include <sys/wait.h>
int main (int argc,char* argv[])
{
//printf("we have %d param, they are %s %s %s %s.\n", argc, argv[0], argv[1], argv[2], argv[3]);
if (argc != 4)
{
printf("Memory system's cleaner cannot run.\r\n");
exit(1);
}
// the first will be my program name
// second will be the main process id
// third will be queue number
// forth will be the msg file path
setpgrp();
int nExit = 0;
int nMainPid = atoi(argv[1]);
int nMsgQueue = atoi(argv[2]);
while(!nExit)
{
//向被監視進程發送0信號,以探測其生存狀態
int nRtn = kill(nMainPid , 0);
if (nRtn != 0)
{//如果被監視進程已經不存在,則試圖刪除消息隊列和文件標識,并退出
msgctl( nMsgQueue, IPC_RMID, NULL ); // delete the message queue
unlink( argv[3] );
nExit = 1;
}
else
{
sleep(1);
}
}
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -