By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,609 Members | 1,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,609 IT Pros & Developers. It's quick & easy.

parent and child processes

P: 62
Hi everyone!
I'm writing some code in C under UNIX, which should give some output like this:
PARENT: pid = 10063
CHILD: my pid = 10064
CHILD: my parent's pid = 10063
CHILD: Sleeping...
PARENT: my child's pid = 10064
PARENT: Waiting for the child to exit...
CHILD: Done sleeping...
PARENT: child is dead.

The output I am getting looks like this:

CHILD: my pid = 3891
CHILD: my parent's pid = 3890
PARENT: my pid = 3890
PARENT: my child's pid = 3891
PARENT: Waiting for the child to exit...PARENT: the child is dead.
loki.brunel.ac.uk% CHILD: Sleeping...CHILD: Done sleeping...

Here is the code I have done:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4.  
  5. int main(){
  6.  
  7. pid_t pid;
  8.  
  9. switch (pid = fork())
  10. {
  11.      case -1:
  12.              perror("couldn't fork");
  13.              break;
  14.      case 0:
  15.              printf("CHILD: my pid = %d\n", getpid());
  16.              printf("CHILD: my parent's pid = %d\n",getppid());
  17.              printf("CHILD: Sleeping...");
  18.              sleep(10);
  19.              printf("CHILD: Done sleeping...");
  20.              break;
  21.  
  22.      default:
  23.              printf("PARENT: my pid = %d\n", getpid());
  24.              printf("PARENT: my child's pid = %d\n", pid);
  25.              printf("PARENT: Waiting for the child to exit...");
  26.              waitpid(pid);
  27.              printf("PARENT: the child is dead.\n");
  28.              break;
  29.  }
  30.  return 0;
  31.  
  32. }
  33.  

What is wrong here, why it goes to the parent process in the middle of the childs process commands? How do I change this?
Feb 10 '08 #1
Share this Question
Share on Google+
4 Replies

P: 90
Hi everyone!
I'm writing some code in C under UNIX, which should give some output like this:
PARENT: pid = 10063
CHILD: my pid = 10064
CHILD: my parent's pid = 10063
CHILD: Sleeping...
PARENT: my child's pid = 10064
PARENT: Waiting for the child to exit...
CHILD: Done sleeping...
PARENT: child is dead.

The output I am getting looks like this:

CHILD: my pid = 3891
CHILD: my parent's pid = 3890
PARENT: my pid = 3890
PARENT: my child's pid = 3891
PARENT: Waiting for the child to exit...PARENT: the child is dead.
loki.brunel.ac.uk% CHILD: Sleeping...CHILD: Done sleeping...

Here is the code I have done:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4.  
  5. int main(){
  6.  
  7. pid_t pid;
  8.  
  9. switch (pid = fork())
  10. {
  11.      case -1:
  12.              perror("couldn't fork");
  13.              break;
  14.      case 0:
  15.              printf("CHILD: my pid = %d\n", getpid());
  16.              printf("CHILD: my parent's pid = %d\n",getppid());
  17.              printf("CHILD: Sleeping...");
  18.              sleep(10);
  19.              printf("CHILD: Done sleeping...");
  20.              break;
  21.  
  22.      default:
  23.              printf("PARENT: my pid = %d\n", getpid());
  24.              printf("PARENT: my child's pid = %d\n", pid);
  25.              printf("PARENT: Waiting for the child to exit...");
  26.              waitpid(pid);
  27.              printf("PARENT: the child is dead.\n");
  28.              break;
  29.  }
  30.  return 0;
  31.  
  32. }
  33.  

What is wrong here, why it goes to the parent process in the middle of the childs process commands? How do I change this?
Hi jewel,
This is synchronization problem, as we know unix is multiprocessing OS when quantum time completes for one process the OS scheduler can schedules other process. This is reason you are getting a messed output.

So, you have to choose a synchronization method to get your perfect output what you needed. If you tell me, why do you want this?, it would be great for me to understand for you problem and help further more.

Regards
Arul
Feb 11 '08 #2

P: 62
Thank you very much for your answer, Arul.
Actually, the wanted output I gave in the beginning, is what the task set by the teacher required. Would this code be the right answer or I can improve it somehow to ensure best marks? What do you think??

P.S. Here is what the rest task is saying:

Write a program that uses fork() to create two processes.
In the child process, do the following:
print it's own PID
print it's parent's PID
sleep for 10 seconds using the sleep() syscall.
call the exit function to terminate.
In the parent process do the following:
print it's own PID
print it's child's PID
wait for the child to die using the wait() syscall.
when the child is dead, print a message that says so.
Feb 11 '08 #3

P: 90
Thank you very much for your answer, Arul.
Actually, the wanted output I gave in the beginning, is what the task set by the teacher required. Would this code be the right answer or I can improve it somehow to ensure best marks? What do you think??

P.S. Here is what the rest task is saying:

Write a program that uses fork() to create two processes.
In the child process, do the following:
• print it's own PID
• print it's parent's PID
• sleep for 10 seconds using the sleep() syscall.
• call the exit function to terminate.
In the parent process do the following:
• print it's own PID
• print it's child's PID
• wait for the child to die using the wait() syscall.
• when the child is dead, print a message that says so.
Hi Jewel,
If it is a assignment, i think your o/p would be ok.But one thing for better o/p aligmnment put all your "\n" at the beginning of the printf.(eg printf("\nHello");.

Any have if you need more input on process syn or IPC you can find something from the following link.
http://www.advancedlinuxprogramming.com/alp-folder
http://www.cs.cf.ac.uk/Dave/C/
Regards
Arul
Feb 12 '08 #4

P: 62
Thank you very much for your help, Arul !!!
Feb 12 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.