fork() problems

S

shellcode

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.
 
J

Jack Klein

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
 
T

Tim Prince

Jack Klein said:
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
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.
 
J

Jack Klein

Jack Klein said:
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
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 <[email protected]>
Subject: fork() problems
Message-ID: <[email protected]>
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.
 
S

shellcode

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.
 
J

junky_fellow

shellcode said:
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.
 
C

CBFalconer

Ben said:
.... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top