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
5 11001 arne 315
Recognized Expert Contributor
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?
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.
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: -
#include<stdio.h>
-
#include<sys/types.h>
-
#include<unistd.h>
-
main()
-
{
-
int pid,return_value;
-
pid=fork();
-
if(pid == 0)
-
{
-
printf("I AM THE CHILD PROCESS\n");
-
printf("The process id is %d and the return value is %d\n",getpid(),return_value);
-
}
-
else if(pid > 0)
-
{
-
printf("I AM THE PARENT PROCESS\n");
-
printf("The process id is %d \n",getpid());
-
pid=fork();
-
if(pid == 0)
-
{
-
printf("I AM THE CHILD PROCESS\n");
-
printf("The process id1 is %d and the return value is %d\n",getpid(),return_value);
-
}
-
else if(pid > 0)
-
{
-
pid = fork();
-
if(pid == 0)
-
{
-
printf("I AM THE CHILD PROCESS\n");
-
printf("The process id2 is %d and the return value is %d\n",getpid(),return_value);
-
}
-
else
-
{
-
printf("couldn't fork");
-
}
-
}
-
}
-
}
arne 315
Recognized Expert Contributor
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Clunixchit |
last post by:
am I lame ??
sounds like i didnt grab something !!!
ive changed my code now!!
whats wrong??
y for 2 loops, it prints 3 loops?
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
|
by: Bo |
last post by:
In my asp.net webservice application, I need to launch a DOS process
as authorized users. To impersonate users, I use <impersonation =
true> in my webconfig. I can't use Diagnostics.Process.Start, because
it creates a child process using parent process token "System", not
the impersonated thread token. I don't want to use
CreateProcessWithLogonW, because my application impersonates different
users and it is not a good idea to handle...
|
by: newgoat |
last post by:
I just wrote a short program to see process switch when sleep() is
invoked within a process. The code is as follows:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main(void){
pid_t f;
|
by: Fred Heida |
last post by:
Hi,
Question on Vista Run As Administrator. If i have exe which creates a child
process, using the Process class, and
this exe is Run As Asministror, is there a way to have the child process
also Run As Asministror with out confirmation for this (as this is already
doen my the main process) ?
Cheers,
|
by: vasu1308 |
last post by:
Hi all,
I want know about writing a program that spwans 3 child processes using fork() function call.
Thanks in advance
Vasu
| |
by: raghav82 |
last post by:
For sending the message in linux environment-
the perl script however uses fork() to release the client process as soon as possible and then in the child process handles the message send.
For sending the message in windows environment-
instead of using fork how to release the child process in the Window fasion? since fork() is not available in windows.
|
by: LH2007 |
last post by:
hi everybody!I'm newbie.I have a exercise about perl like this:
Creating 2 process P1(parent) P2 (child)
P1 get characters from keyboard
If press Ctrl-c P1 send a signal terminate P2
......................................
My ideal is :
$handle=IO::Socket::INET->(proto=>"tcp",
PeerAddr=>$host,
|
by: eso40043 |
last post by:
Hello,
due to a memory leak (bug, I guess) in pyraf(or rather in IRAF) I have to fork an iterative process that goes through hundreds of image frames and does unspeakable things to them. In the child process, this returns a dictionary of image information for each image and is supposed to send it to the parent through pickle.dump. However, the child process hangs at pickle.dump and nothing happens. No error messages. The dictionary that is...
|
by: rparimi |
last post by:
Hello pythoners,
When I create temporary file using the tempfile module, and forkI)
later on in my program, I always see errors when the program exits. Is
this because the child process deletes temp file?
Here's a stripped down version of my script that exhibits this
problem:
#!/usr/bin/python
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |