摘要:
1#ifndef __MEMPOOL_H__ 2#define __MEMPOOL_H__ 3#include "stdio.h" 4#include "stdlib.h" 5#include <string.h> 6 7class CMe...
阅读全文
posted @
2009-07-13 10:48 Super- 阅读(2701) |
评论 (6) |
编辑 收藏
#!/bin/sh
if [ $# -lt 1 ]
then
echo -e "Please Input $1 parameter!\n"
else
while(true)
do
PID=`ps -ef|grep "$1"|grep -v "grep"|awk '{print $1}'`
tmp=${PID:-"aaaa"}
if [ tmp != "aaaa" ]
then
$1 $2
else
echo -e "Current PID is $PID\n"
sleep(10)
fi
done
fi
posted @
2009-07-06 11:34 Super- 阅读(661) |
评论 (0) |
编辑 收藏
#!/bin/sh
PATH=$PATH:./
if [ $# -lt 2 ]
then
echo -e "Please Execute `basename $0` parameter!\n"
fi
if [ -n $2 ]
then
nohup "$1" > "$2" 2>&1 &
fi
posted @
2009-06-22 17:59 Super- 阅读(217) |
评论 (0) |
编辑 收藏
linux中创建进程的方法有fork和exec;
其中fork时,通过复制调用进程来创建新的进程;
exec则是用一个新的程序覆盖原进程的内存空间,来实现进程的转变,
如果系统调用了exec,并且成功,就不会有任何数据返回,如果失败,
则会返回数据,可以通过检查errno来获取错误码,或strerror或perror的方式
打印错误信息;
fork:
如果返回值为0,则表示进入子进程,>0 则父进程,<0则失败;
如果需要子父进程间同步,可以调用wait函数处理,该函数的作用是使一个进程等待,
直到其它进程结束;
如:
pid_t pid = fork();
if(!pid)
{
std::cout<<"I am Child Process!"<<std::endl;
/* do something */
}
else if (pid > 0)
{
wait(NULL);
/* do something */
}
else
{
perror("Fork Error!");
exit(1);
}
exec:
execl,execv
execvl,execvp
如果操作shell 命令:ls -l
execl("/bin/sh","ls","-l",NULL);
或
char * data[] = {"ls","-l",NULL};
execv("/bin/sh",data);
其中NULL定义为 (char *)0;表示串结束。
posted @
2009-06-19 14:29 Super- 阅读(3299) |
评论 (0) |
编辑 收藏
//以下采用函数指针的方法完成command模式
1
#include <iostream>
2
#include <stdio.h>
3
#include <stdlib.h>
4
5
/**//*
6
command模式:将请求放入到一个类A中,将请求的操作放入另一个类B中,
7
将B作为A的一个Private成员,然后在类C中包含一个类A的Private成员,这样
8
可以通过C来完成B的处理。
9
*/
10
11
class CAction
12

{
13
protected:
14
CAction()
15
{
16
17
}
18
public:
19
~CAction()
20
{
21
22
}
23
virtual int Operation() = 0;
24
25
};
26
27
class CExtendAction:public CAction
28

{
29
public:
30
CExtendAction()
31
{
32
33
}
34
~CExtendAction()
35
{
36
37
}
38
int Operation()
39
{
40
std::cout<<"Execute Operation!"<<std::endl;
41
return 1;
42
}
43
};
44
45
typedef int (CAction::*oper)(); //定义类成员指针
46
47
class CCommand
48

{
49
protected:
50
CAction* _act;
51
oper _opt;
52
public:
53
CCommand(CAction* act,oper opt)
54
{
55
_act = act;
56
_opt = opt;
57
}
58
59
~CCommand()
60
{
61
62
}
63
64
int Execute()
65
{
66
return (_act->*_opt)();
67
}
68
};
69
70
71
int main(int argc,char * argv[])
72

{
73
CAction * act = new CExtendAction();
74
CCommand comd(act,&CAction::Operation);
75
76
comd.Execute();
77
}
posted @
2009-06-19 10:00 Super- 阅读(302) |
评论 (0) |
编辑 收藏
摘要: observer推拉模式的区别请参考上一篇observer推模式拉模式源代码如下:
1#ifndef __SUBJECT_H__ 2#define __SUBJECT_H__ 3 4#include <iostream> 5#include <vector> 6 ...
阅读全文
posted @
2009-06-18 19:27 Super- 阅读(1207) |
评论 (0) |
编辑 收藏
摘要: Observer分为推模式和拉模式;区别在于observer是被动还是主动;推模式中,分发者不管observer是否需要信息,都统一的下发相同的更新给各个observer;拉模式中,由observer主动发起,仅仅向分发者请求自己需要的信息。如果分发者中的数据结构体发生的变化,那么每个observer也会要进行相应的改变;但如果用拉模式,observer对象的处理可以不需要变化。以下为observ...
阅读全文
posted @
2009-06-18 18:59 Super- 阅读(1398) |
评论 (0) |
编辑 收藏
Bridge模式将类的定义与实现彻底解耦,采用组合的方式进行处理。
代码如下:
1
#include <iostream>
2
#include <stdio.h>
3
#include <stdlib.h>
4
#include <stdarg.h>
5
6
#define STRLEN 100
7
class CImplement
8

{
9
protected:
10
CImplement()
11
{
12
13
}
14
public:
15
~CImplement()
16
{
17
18
}
19
20
virtual void Operatorion() = 0;
21
virtual void PrintMsg(char * format,
) = 0;
22
23
};
24
25
26
class CExtendImp:public CImplement
27

{
28
public:
29
CExtendImp()
30
{
31
32
}
33
34
~CExtendImp()
35
{
36
37
}
38
39
void Operatorion()
40
{
41
PrintMsg("%s\t %s","Hello" ,"World!");
42
}
43
44
void PrintMsg(char * format,
)
45
{
46
va_list tmp_list;
47
va_start(tmp_list,format);
48
char* c_buffer = new char[STRLEN];
49
vsprintf(c_buffer,format,tmp_list);
50
fprintf(stderr,"\r\n*********\r\n%s\r\n",c_buffer);
51
}
52
};
53
54
class CAbstract
55

{
56
protected:
57
CAbstract()
58
{
59
60
}
61
public:
62
~CAbstract()
63
{
64
65
}
66
67
virtual void operation() = 0;
68
};
69
70
class CExtendAbstract
71

{
72
public:
73
CExtendAbstract(CImplement * data)
74
{
75
_imp = NULL;
76
_imp = data;
77
}
78
~CExtendAbstract()
79
{
80
81
}
82
83
void operation()
84
{
85
_imp->Operatorion();
86
}
87
protected:
88
CImplement * _imp;
89
};
90
91
int main(int argc ,char * argv[])
92

{
93
CImplement * value = new CExtendImp();
94
CExtendAbstract* abstr = new CExtendAbstract(value);
95
abstr->operation();
96
}
posted @
2009-06-18 17:59 Super- 阅读(1123) |
评论 (2) |
编辑 收藏
摘要: 定义一个vector:
typedef struct _SEED_TOP_{ std::string m_Range;//192.168.1.1_192.168.1.255,192.168.2.1-192.168.2.255//每对起始结束ip之间用“,”分隔 &...
阅读全文
posted @
2009-06-18 17:41 Super- 阅读(3671) |
评论 (2) |
编辑 收藏
摘要: 冒似该模式不能彻底解决switch、case的困扰。源码如下:
1#ifndef __CONTENT_H__ 2#define __CONTENT_H__ 3#include "State.h" 4 5class Context 6{ 7public: 8 &nbs...
阅读全文
posted @
2009-06-18 15:36 Super- 阅读(1413) |
评论 (2) |
编辑 收藏