Tuesday, March 17, 2009

good code-- traversing process list

#define for_each_process(p) \
for (p=&init_task;(p=list_entry((p)->tasks.next,struct,task_struct,tasks))!= &init_task;)

here i wanted to traverse task list from init_task to last task in circular linux process linked list ---
now i started with p=&init_task -- and if i dio it casually i will put the condition that
p!=&init_task but by doing that it wont go inside the loop even once---

what they have done is -- taken p=&init_task and while checking first condition itself incremented the p and chaged it to the next process and then compared it --Good one --

no doubt we too have a solution that is to start with the 2nd node only that is p=&init_task->tasks.next as p's initial value...

but it was good hmmm!!

No comments: