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. 6 2398
On Sat, 28 Feb 2004 23:26:50 -0600, shellcode <sh*******@adel phia.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.li nux.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.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
"Jack Klein" <ja*******@spam cop.net> wrote in message
news:5b******** *************** *********@4ax.c om... On Sat, 28 Feb 2004 23:26:50 -0600, shellcode <sh*******@adel phia.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.li nux.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.
On Sun, 29 Feb 2004 07:02:27 GMT, "Tim Prince" <tp*****@comput er.org>
wrote in comp.lang.c: "Jack Klein" <ja*******@spam cop.net> wrote in message news:5b******** *************** *********@4ax.c om... On Sat, 28 Feb 2004 23:26:50 -0600, shellcode <sh*******@adel phia.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.li nux.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*******@adel phia.net> Subject: fork() problems Message-ID: <sl************ **********@ph33 r.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.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
On 2004-02-29, Jack Klein <ja*******@spam cop.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.
shellcode <sh*******@adel phia.net> wrote in message news:<sl******* *************** @ph33r.shellcod e.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.
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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 server.(once fork start,
counter will reset). This allow me to communicate with my
counter.....pls help and thanks a lot
|
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();
value = value + 2;
number = number + 2;
|
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
|
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 &
and others
| |
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 main() {
....
pid1 = fork();
if (pid1) pid2 = fork();
.....
|
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 to run the perl
program, but I think i m not able to fork in the first place, in the
sense that I (apache) may not have permissions to fork. Is there any
way to remedy this?
Rahul
|
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 recently
explained it to a friend of mine and at that time decided to see if I
could replicate it in C#. I have created an application that, to me at
least, mimics what fork would/should do on *nix/bsd systems. I know
that fork spawns a new...
|
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 main()
{
pid_t pid;
|
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: 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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |