ISC cron tool code analysis

这周承蒙XDY看得起,让我学着做一个类似于Cron的小工具,我感动不已饿,然后看了会cron的源码,开始有点不淡定了,发现自己的competence可能不足以完成如此重任,况且近期对coding又无很大的兴趣.

不过不管怎么样,不应该让他失望吧,作为一个真正意义上的mentor,能跟着他做东西,学东西是很难得的机会啊


后来XDY又给我看了他写了的一部分,发现还是从中学到了不少,他让我放弃面向对象的思想用纯C写,结果我写出来的代码一看就是面向对象的C,几乎所有的函数第一个参数都是一个结构体,类似于this指针传进去那样.但是他的代码读起来却流畅,易懂简洁无比.


typedef	struct _user {
	struct _user	*next, *prev;	/* links */
	char		*name;
	time_t		mtime;		/* last modtime of crontab */
	entry		*crontab;	/* this person's crontab */
} user;

typedef	struct _cron_db {
	user		*head, *tail;	/* links */
	time_t		mtime;		/* last modtime on spooldir */
} cron_db;

typedef	struct _entry {
	struct _entry	*next;
	struct passwd	*pwd;
	char		**envp;
	char		*cmd;
	int		flags;
} entry;

typedef	struct _job {
	struct _job	*next;
	entry		*e;
	user		*u;
} job;
static job	*jhead = NULL, *jtail = NULL;

1. fork processes to setup the correct process structure
2. close input output stderr
3. load the cron data to the cron database(read from files)
4. calculate time and sleep
5. add the job into job queue
6. run the job from job queue
7. loop to step 3






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值