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

creating child process using fork function

P: 4
Hi

I have to develop a c program to create a parent process that spawn three child process. i have written a code but that was not working out.Please help me in getting the desired output of my program. Here is my code:
#include <stdio.h>
int main(void)
{
int pid;
pid = fork();
if(pid > 1)
{
printf("I AM THE PARENT PROCESS\n");
}
else if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
pid = fork();
if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
}
else if(pid > 0)
{

pid = fork();

if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
}
}
}
else
{

printf("couldn't fork");
}
}

Thanks
Sowmya
Apr 2 '07 #1
Share this Question
Share on Google+
5 Replies


arne
Expert 100+
P: 315
Hi

I have to develop a c program to create a parent process that spawn three child process. i have written a code but that was not working out.Please help me in getting the desired output of my program. Here is my code:
#include <stdio.h>
int main(void)
{
int pid;
pid = fork();
if(pid > 1)
{
printf("I AM THE PARENT PROCESS\n");
}
else if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
pid = fork();
if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
}
else if(pid > 0)
{

pid = fork();

if(pid == 0)
{
printf("I AM THE CHILD PROCESS\n");
}
}
}
else
{

printf("couldn't fork");
}
}

Thanks
Sowmya

Your code looks ok to me (in terms of: it should work, please use code tags next time to make it more readable). The only minor issue is that you're forking from the child instead of from the parent, which would be the more usual case.

You may also want to have a look at the getpid() function to verify that all messages are coming from different processes.

Do you have any specific problem?
Apr 2 '07 #2

P: 4
Your code looks ok to me (in terms of: it should work, please use code tags next time to make it more readable). The only minor issue is that you're forking from the child instead of from the parent, which would be the more usual case.

You may also want to have a look at the getpid() function to verify that all messages are coming from different processes.

Do you have any specific problem?

Hi arne,

Thanks for your suggestion..

I will make the required changes .

Thanks ,
Sowmya.
Apr 2 '07 #3

P: 4
Hi,

My program should return value 0 for child process ,but i am getting a bigger value ,can you please help me in this regard.Here is my code:

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<unistd.h>
  4.  main()
  5. {
  6.         int pid,return_value;
  7.         pid=fork();
  8.         if(pid == 0)
  9.         {
  10.                 printf("I AM THE CHILD PROCESS\n");
  11.                 printf("The process id is %d and the return value is %d\n",getpid(),return_value);
  12.         }
  13.         else if(pid > 0)
  14.         {
  15.                 printf("I AM THE PARENT PROCESS\n");
  16.                 printf("The process id is %d \n",getpid());
  17.         pid=fork();
  18.                  if(pid == 0)
  19.                 {
  20.                         printf("I AM THE CHILD PROCESS\n");
  21.                         printf("The process id1 is %d and the return value is %d\n",getpid(),return_value);
  22.                 }
  23.                 else if(pid > 0)
  24.                 {
  25.                 pid = fork();
  26.                 if(pid == 0)
  27.         {
  28.                         printf("I AM THE CHILD PROCESS\n");
  29.                         printf("The process id2 is %d and the return value is %d\n",getpid(),return_value);
  30.                         }
  31.                         else
  32.                         {
  33.                                 printf("couldn't fork");
  34.                         }
  35.                 }
  36.         }
  37. }
Apr 3 '07 #4

arne
Expert 100+
P: 315
Hi,
My program should return value 0 for child process ,but i am getting a bigger value ,can you please help me in this regard.
This is because you actually never assign a value to 'return_value', so it's undefined. (The return value of fork() you already assign to 'pid', no need to use another variable anyway.)

What does getpid() say about the PIDs (process IDs) of your processes? Should work fine, no?
Apr 3 '07 #5

P: 4
Hi arne.

I got the desired output i guess.Here is my output.
I AM THE CHILD PROCESS and return value is 0
I AM THE PARENT PROCESS and return value is 8869 and process id 26585
I AM THE CHILD PROCESS and return value is 0
I AM THE CHILD PROCESS and return value is 0

Thanks,
Sowmya.
Apr 4 '07 #6

Post your reply

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