473,387 Members | 1,757 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.

fork() problems

the code:
------fork.c------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
pid_t forkval;
int rv, exitcode;

forkval=fork();
switch(forkval)
{
case -1:
perror("fork");
exit(1);
case 0:
printf("CHILD: This is the child process!!\n");
printf("CHILD: My PID is: %d\n", getpid());
printf("CHILD: My parent's PID is: %d\n", getppid());
printf("CHILD: Enter my exit code: ");
scanf("%d", &exitcode);
printf("CHILD: Exiting with exitcode.. %d\n", exitcode);
exit(exitcode);
default:
printf("PARENT: This is the parent process!!\n");
printf("PARENT: My PID is: %d\n", getpid());
printf("PARENT: My child's PID is: %d\n", forkval);
printf("PARENT: Waiting for child to exit....\n");
wait(&rv);
printf("PARENT: Child died!! Exit code: %d\n",
WEXITSTATUS(rv));
printf("PARENT: Exiting....\n");
break;
}
return 0;
}
------fork.c------

the compilation: gcc -Wall -o fork fork.c

the results:
$ ./fork
CHILD: This is the child process!!
CHILD: My PID is: 1557
CHILD: My parent's PID is: 1556
CHILD: Enter my exit code: PARENT: This is the parent process!!
PARENT: My PID is: 1556
PARENT: My child's PID is: 1557
PARENT: Waiting for child to exit....

im a bit confused at this point. i've looked over the code and the man
pages and i really think i did it right, yet for some reason the CHILD
gets executed before the parent and i'm never given a chance to enter
the exitcode. thank's for help.
Nov 14 '05 #1
6 2368
On Sat, 28 Feb 2004 23:26:50 -0600, shellcode <sh*******@adelphia.net>
wrote in comp.lang.c:
the code:
------fork.c------
#include <stdio.h>
#include <stdlib.h>
The two headers above are standard C.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>


[off-topic code snipped]

These three headers are not part of standard C, and are off-topic
here.

There is no such thing as fork() in standard C, the topic of this
group. It is an extension provided by your particular compiler and
operating system, and that is where it needs to be discussed.

I would suggest news:comp.os.linux.development.apps.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:5b********************************@4ax.com...
On Sat, 28 Feb 2004 23:26:50 -0600, shellcode <sh*******@adelphia.net>
wrote in comp.lang.c:
the code:
------fork.c------
#include <stdio.h>
#include <stdlib.h>


The two headers above are standard C.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>


[off-topic code snipped]

These three headers are not part of standard C, and are off-topic
here.

There is no such thing as fork() in standard C, >
I would suggest news:comp.os.linux.development.apps.

I didn't see any indication that the OP was interested in linux. The
behavior is highly dependent on OS versions, and may be off topic
everywhere, if a general answer is wanted.
Nov 14 '05 #3
On Sun, 29 Feb 2004 07:02:27 GMT, "Tim Prince" <tp*****@computer.org>
wrote in comp.lang.c:

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:5b********************************@4ax.com...
On Sat, 28 Feb 2004 23:26:50 -0600, shellcode <sh*******@adelphia.net>
wrote in comp.lang.c:
the code:
------fork.c------
#include <stdio.h>
#include <stdlib.h>
The two headers above are standard C.
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>


[off-topic code snipped]

These three headers are not part of standard C, and are off-topic
here.

There is no such thing as fork() in standard C, >
I would suggest news:comp.os.linux.development.apps.

I didn't see any indication that the OP was interested in linux. The
behavior is highly dependent on OS versions, and may be off topic
everywhere, if a general answer is wanted.


I did, in the headers of the original post, snippet as follows:
Newsgroups: comp.lang.c
From: shellcode <sh*******@adelphia.net>
Subject: fork() problems
Message-ID: <sl**********************@ph33r.shellcode.net>
User-Agent: slrn/0.9.8.0 (Linux)


Not an absolute guarantee, but the fact that he posted from a computer
running Linux is a strong indication that it is the particular *NIX
variant he is interested in.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
On 2004-02-29, Jack Klein <ja*******@spamcop.net> wrote:
Not an absolute guarantee, but the fact that he posted from a computer
running Linux is a strong indication that it is the particular *NIX
variant he is interested in.


yes. i am running linux. sorry for the off-topic post.
Nov 14 '05 #5
shellcode <sh*******@adelphia.net> wrote in message news:<sl**********************@ph33r.shellcode.net >...
the code:
------fork.c------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
pid_t forkval;
int rv, exitcode;

forkval=fork();
switch(forkval)
{
case -1:
perror("fork");
exit(1);
case 0:
printf("CHILD: This is the child process!!\n");
printf("CHILD: My PID is: %d\n", getpid());
printf("CHILD: My parent's PID is: %d\n", getppid());
printf("CHILD: Enter my exit code: ");
scanf("%d", &exitcode);
printf("CHILD: Exiting with exitcode.. %d\n", exitcode);
exit(exitcode);
default:
printf("PARENT: This is the parent process!!\n");
printf("PARENT: My PID is: %d\n", getpid());
printf("PARENT: My child's PID is: %d\n", forkval);
printf("PARENT: Waiting for child to exit....\n");
wait(&rv);
printf("PARENT: Child died!! Exit code: %d\n",
WEXITSTATUS(rv));
printf("PARENT: Exiting....\n");
break;
}
return 0;
}
------fork.c------

the compilation: gcc -Wall -o fork fork.c

the results:
$ ./fork
CHILD: This is the child process!!
CHILD: My PID is: 1557
CHILD: My parent's PID is: 1556
CHILD: Enter my exit code: PARENT: This is the parent process!!
PARENT: My PID is: 1556
PARENT: My child's PID is: 1557
PARENT: Waiting for child to exit....

im a bit confused at this point. i've looked over the code and the man
pages and i really think i did it right, yet for some reason the CHILD
gets executed before the parent and i'm never given a chance to enter
the exitcode. thank's for help.


In fact, child is still waiting for user to enter the child's exit
code. Neither, the child nor the parent has exited yet.
Nov 14 '05 #6
Ben Peddell wrote:
shellcode wrote:
the code:
------fork.c------
.... snip ...
I know this is Off Topic, but...

In this instance, the fork() call returns to the newly created
(child) process first. This runs until it blocks at the scanf()

.... snip ...

Please do not answer off topic questions other than to reroute
them to a suitable group. There is nobody here (in theory)
qualified to criticize your answer, so it could well propagate
uncorrected misinformation, besides encouraging the off topic
postings.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #7

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

Similar topics

4
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
4
by: marconi | last post by:
hallo there, Can anyone help me to look at my fork problems? I can't seem to work up the piping..the reason i need that for is because i need to make a counter global to client and...
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();...
11
by: ramu | last post by:
Hi All, We know that a c function never returns more than one value. Then how come the fork() function returns two values? How it is implemented? Regards
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 & ...
3
by: thrillseekersforever | last post by:
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...
0
by: rahulthathoo | last post by:
Hi, I have to call a perl program from within PHP, but since that program takes a long time to finish, i fork a process in my php code and then call the perl code, using exec. But I am not able...
5
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.