473,385 Members | 1,342 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,385 software developers and data experts.

Small Fork Problem

The questions(A&B) are to fine no# of process running from the below
codes. However, I couldn't decipher the solution. Will someone please
throw some light on this? Thanks a lot!!
A]

void main() {
....
pid1 = fork();
if (pid1) pid2 = fork();
.....
}

Solution:

In the first two lines, the parent process forks a child (pid1), then
the same parent (the process holding a nonzero pid1) forks another
child (pid2). Therefore, the effect of the first two lines was to
multiply the number of processes by 3.
B]

void main() {
....
pid1 = fork(); pid2 = fork();
if (pid1 || pid2) execvp(...);
pid3 = fork(); pid4 = fork();
if (pid3 || pid4) execvp(...);
pid5 = fork(); pid6 = fork();
if (pid5 || pid6) execvp(...);
}
In the first line, the two unconditional forks bring the number of
processes to 4. Then, in the second line, 3 out of these 4 processes
are exec'd toward another program: they are the processes that were
playing the parent role either during fork1 or during fork2. So,
conversely, only the fork2-child of the fork1-child will continue
executing this code (i.e., the process with both pid1 and pid2 zero).
Therefore, the next two lines apply only to that remaining process,
which also creates 3 more processes, of which only one remains (the
fork4-child of the fork3-child). Then, the last two lines repeat
exactly the same pattern.
In conclusion, the total number of processes that were created
(whether exec'd or not) is:
1 + 3 + 3 + 3 = 10.

Mar 5 '07 #1
3 2703
th******************@gmail.com wrote:
The questions(A&B) are to fine no# of process running from the below
codes. However, I couldn't decipher the solution. Will someone please
throw some light on this? Thanks a lot!!
Try comp.unix.programmer, fork() is OT here.
--
Ian Collins.
Mar 5 '07 #2
On Mar 5, 2:19 pm, Ian Collins <ian-n...@hotmail.comwrote:
thrillseekersfore...@gmail.com wrote:
The questions(A&B) are to fine no# of process running from the below
codes. However, I couldn't decipher the solution. Will someone please
throw some light on this? Thanks a lot!!

Try comp.unix.programmer, fork() is OT here.

--
Ian Collins.
Thank you _0_

Mar 5 '07 #3
th******************@gmail.com wrote:
The questions(A&B) are to fine no# of process running from the below
codes. However, I couldn't decipher the solution. Will someone please
throw some light on this? Thanks a lot!!
A]

void main() {
...
pid1 = fork();
if (pid1) pid2 = fork();
....
}

Solution:

In the first two lines, the parent process forks a child (pid1), then
the same parent (the process holding a nonzero pid1) forks another
child (pid2). Therefore, the effect of the first two lines was to
multiply the number of processes by 3.
B]

void main() {
...
pid1 = fork(); pid2 = fork();
if (pid1 || pid2) execvp(...);
pid3 = fork(); pid4 = fork();
if (pid3 || pid4) execvp(...);
pid5 = fork(); pid6 = fork();
if (pid5 || pid6) execvp(...);
}
In the first line, the two unconditional forks bring the number of
processes to 4.
No, in the first line you misdeclare the return type of main(), making
everything that follows irrelevant.
Then, in the second line, 3 out of these 4 processes
are exec'd toward another program:
In the lines subsequent to the "..." syntax error, you use functions
that are not part of the standard library and which have no declaration
in scope.
Mar 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gregory Starck | last post by:
Hello, I'm having what seems to be a deadlock in syslog with the code (wich comes from a project I'm working on, but I've reduced the test case to its minimum) I put at the end of this messsage....
1
by: Alexander N. Spitzer | last post by:
I am trying to write a program that will fork a process, and execute the given task... the catch is that if it runs too long, I want to clean it up. this seemed pretty straight forward with a...
16
by: mishra | last post by:
Hi, i thied the following code.. # include<stdio.h> int main() { int a; printf("Hello..."); a=fork(); printf("hi\n"); return(0);
27
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork();...
4
by: rh0dium | last post by:
Hi all, I have a problem with putting a job in the background. Here is my (ugly) script which I am having problems getting to background. There are threads about doing python script.py & ...
1
by: vduber6er | last post by:
Hi I want to have a wait page while the rest of the cgi does its process, but it seems like the wait page waits till everything is complete and never appears. I've tried forking twice already as...
4
by: rohitsagar | last post by:
I want to do fork from a program Code is very simple, below is the code, it just execute a executable called a.exe, I want to run a.exe 600 times. #include<stdio.h> #include<stdlib.h> void...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
1
by: 3srt | last post by:
Hello, I created a simple 'server' script that will accept input from a cgi script. The server will handle multiple connections on the same port, also. In a nut shell, this 'server' script will...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.