ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
say,
there are n-crontab files from n-users ( excluding the user entries those who are restricted from making use of the cron )
the cron daemon parses the files every one minute ( with a minimum granularity of one minute - hope so !!! )
here falls my question,
How is the cron able to parse n-files simultaneously ?
Do cron spawn n-instances to parse the files( i dont think so)?
Does it follow any order in parsing the n-files if not concurrently?
Or
All the cron entries from all the n-users maintained as single entry differentiated by userid?
It is not accurate. There is no such polling at one minute interval.
In fact, cron sets a timer at the time of the next event and then it goes to sleep, waiting the timer to wakeup it again. When this happens it executes the related job.
Every time you edit the cron jobs (by crontab -e) the timer is re-scheduled to reflect any changes in the files.
The user id does not matter in this discussion. Only the cron job time matter.
When the cron process is started at boot time, it will scan all cron jobs, building a chronological list of jobs. The timer is set to the first one in the list.
When this time arrive, the cron job is started (or cron jobs, if there is more than one), the first entry in the list is removed and the timer is set again for the first in the list. This continues forever.
When a new job is created/changed by crontab -e, the chronological list of jobs is rebuild.
I hope this can explain better the process. If not, please, ask again and I will make my best.
Good point !
I did read the cron code in C, several years ago, as part of a College project and we wrote a report about our findings and this was discussed with the rest of class.
I think either the man page does not reflect the current implementation, or the current implementation was changed over the years.
For anyone interested in this subject, reading the code is the only way to be sure how cron works. I already did.
Since bigearsbilly post, I was curious about what is the current
implementation of cron. The things I learned at school are still valid or that one minute polling the cron man pages talk about has something to do with the real implementation ?
Well, looks like the current GNU implementation of cron is a work from Paul Vixie (http://en.wikipedia.org/wiki/Paul_Vixie) and it uses a one minute polling approach. From the cron-4.1-20.src.rpm:
Code:
/* ... wait for the time (in minutes) to change ... */
do {
cron_sleep(timeRunning + 1);
set_time(FALSE);
} while (clockTime == timeRunning);
timeRunning = clockTime;
The "cron_sleep(timeRunning + 1)" is the key here. No doubt about. Vixie's implementation use a one minute polling.
But, if you search the Google for cron code that not belongs to Paul Vixie ("cron.c -vixie") you will find several listings that uses that "time event based queue" I described in my first reply.
None is recent. The most recent is from 2003.
So, I was wondering if there is a good technical reason to this shift or just is an author's preference.
Talking with a few friends about it, someone pointed the Paul Vixie's approach can detects changes in crontab files just by checking its modified time.
The former approach depends on "crontab -e" to signaling the cron process that something has changed. Of course, restarting cron will do the job too. This is the way several modern daemons work today (squid, samba, named etc).
Answering your question, looks like cron builds its internal database AFTER it runs the pending jobs. There is a lot of time to do that before the next minute arrives.
I hope this post makes the things more clear. Sorry all of you, for any mistake and for my poor English.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.