Process Scheduling in Linux
Hi all,
I am actually studying process scheduling in Linux but I have a lot of problems trying to work out a programme. I am supposed to write a program on 1) How to keep track of whether the child or parent process runs first after fork() is called. One way is to write to a file but how does it work? If I don't use this, does only printing meet the requirement as well? 2) When the system needs to run a number of processes how can we keep track of the sequence wat is being run. Eg, if there are 3 processes, the round robin scheduling policy will give you "p1 p2 p3 p1 p2 p3..." How can I display the sequence or ID or anyhtin ghta can help me identify what programme is running? I hope someone out there would be able to help me out, needed a little guidance here. Thanks a million! Cheers, Mandy |
Welcome to LQ!
Probably the best way is to turn off output buffering and simply write out who is executing. E.g. write "parent" in the parent and "child" in the child, or something like that. As for printing the ID of the currently running process, getpid is your friend. To be truly portable, your code shouldn't depend on one process or another running first. Vfork will guarantee that the child runs first, but it will block the parent until the child calls either an exec or exits. |
hi there
to the parent process the fork() will return the the value of the child's pid .whereas in th the child process the value of the PID will always be 0 so do ike tis int pid; pid=fork() if(pid==0) printf("child process"); else if(pid<0) printf("fork failed"); else printf("parent process"); regards |
Quick reminder of the LQ rules you signed ...
"Do not expect LQ members to do your homework - you will learn much more by doing it yourself." And to the responding gents ... The fact that it's a Mandy asking doesn't really change the rules ;) Cheers, Tink |
Hi all,
Thanks masand and btmiller... I just needed a little guide, not expecting an A+ answer. Yup Tinkster I know I cant expect LQ to do my homework. I actually got a similar code for the child or parent first part. When I tried running the program it seems that its always parent first, any pearticular reason? I searched the web but I cant find any alogorithm of the Linux scheduler. Parent and child should have the same priority right? So who decides what? Cheers, Mandy |
hi there
it depends on the who gets the time slice !!!! u can make ur child process to work if u out ur parent process to sleep if it is started first. and to the "moderators", it does not matter to me who is asking the question, i am not like the one who likes to keep a part of the answer reserved for the next reply.!!! regards |
By the way, I see no real point in determining which one of the parent or the child run first after a fork. They will run concurrently anyway, this is what a time-sharing O/S is all about..
Here are some other comments: If you are on a multi-processor H/W, they could both run at the very same time. Using a file is probably not the best approach to detect the winner process, what I would do is to retrieve the current time with a high resolution in each process just after the fork, and later compare the values. |
All times are GMT -5. The time now is 05:12 AM. |