473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

parent and child processes

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
4 8232
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
jewel87
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
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
jewel87
62
Thank you very much for your help, Arul !!!
Feb 12 '08 #5

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

Similar topics

1
by: ahaideb | last post by:
I have a table (relation) in my database: --------------- | parent | child | --------------- | 1 | 2 | | 1 | 3 | | 2 | 4 | | 2 | 5 ...
2
by: Manisha | last post by:
Hi, I am creating a C++ dll which is used to process data passed to it through one of its exported functions. It should be able to process 160 simultaneous requests. For this reason, I have...
13
by: Jimmy Cracker | last post by:
Is it completely impossible in UNIX to push an environment variable to the parent shell? I would like to do something like this: main(int argc, char *argv) { char *var; var = (char...
0
by: tiger | last post by:
Hi I was working on a project for school, that deals with processes and windows.....here is a sample of the instruction: You are to implement a game of Nim. Your implementation should consist...
0
by: tiger | last post by:
Hi I was working on a project for school, that deals with processes and windows.....here is a sample of the instruction: You are to implement a game of Nim. Your implementation should consist...
4
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as...
4
by: Phil Mc | last post by:
Say for example you have a application running on a windows 2003 server (that is on server, not from). This application needs to start child applications (must be stand alone console applications),...
1
by: Sebouh | last post by:
Hello guys. This time, i need to implement waitpid in order to know if one of the children of a parent has finished executing. The assignment requires me to run a limited number of child processes....
3
by: kneielj | last post by:
How does the shell interpret command substitution ? if its a normal command, the shell would spawn a child process and the parent would wait for the child to execute and would then collect its exit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.