LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2004, 09:47 AM   #1
Newbiegal
LQ Newbie
 
Registered: Oct 2004
Posts: 2

Rep: Reputation: 0
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
 
Old 10-03-2004, 02:02 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
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.
 
Old 10-03-2004, 02:10 PM   #3
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 69
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

Last edited by masand; 10-03-2004 at 02:12 PM.
 
Old 10-03-2004, 02:13 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
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
 
Old 10-04-2004, 11:35 AM   #5
Newbiegal
LQ Newbie
 
Registered: Oct 2004
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 10-04-2004, 11:52 AM   #6
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 69
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
 
Old 10-04-2004, 12:08 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,784

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.

Last edited by jlliagre; 10-04-2004 at 12:09 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scheduling in Linux asurya Linux - General 5 08-31-2005 08:34 AM
linux scheduling problem lordofring Programming 2 08-30-2005 09:08 AM
switching Linux kernel I/O scheduling algorithms irfanhab Slackware 1 03-17-2005 11:53 AM
Process scheduling (CPU) kiwi_bloke *BSD 1 08-29-2004 09:07 PM
Linux Scheduling Kumar Programming 3 06-14-2004 04:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration