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

fork() problems

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.