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.
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
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.
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?
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.!!!
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,786
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.