473,769 Members | 2,331 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fork does not return control back

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;
int i;
char *a="ls";
i=0;
for(i=0;i<600;i ++)
{
pid = fork();
if(pid == 0)
{
/* Child process: */
system(a.exe);
exit(0);
}
else if(pid 0)
{
/* Parent process: */
printf("\n I just invoked group # %d",i);
}
else
{
/* Error handling. */
printf("\n couldn't fork");
exit(1);
}
}
}
NOW THE PROBLEM, the problem is when this code ends it does not give
me prompt back.

Like the output of a.exe is (Lets says "HELLO WORLD")

The screen would look like

HELLO WORLD
HELLO WORLD
HELLO WORLD
HELLO WORLD

I want it to look like

HELLO WORLD
HELLO WORLD
HELLO WORLD
HELLO WORLD
Unix Prompt_>

The output is not stuck, I dont need to hit ctrl c, it is just at
prompt, but does not show. If I do ll -tr I see the contents Eg.

HELLO WORLD
HELLO WORLD
HELLO WORLD
HELLO WORLD ll -tr

I tried using exit(0) , _exit(0)

but still any idea ?

Jul 19 '07 #1
4 2542
ro********@yaho o.com said:
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.
Your code didn't compile here, for the following reasons:

foo.c:4: warning: return type of `main' is not `int'
foo.c: In function `main':
foo.c:5: `pid_t' undeclared (first use in this function)
foo.c:5: (Each undeclared identifier is reported only once
foo.c:5: for each function it appears in.)
foo.c:5: parse error before `pid'
foo.c:7: warning: initialization discards qualifiers from pointer target
type
foo.c:11: `pid' undeclared (first use in this function)
foo.c:11: warning: implicit declaration of function `fork'
foo.c:15: request for member `exe' in something not a structure or union
make: *** [foo.o] Error 1

Once you have a program that can actually be compiled, I suggest you ask
your question (which appears to be about POSIX functions) in a group
such as comp.unix.progr ammer, where such functions are topical and
where, therefore, expertise in their use is widespread.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 19 '07 #2
On Thursday 19 Jul 2007 9:41 pm, ro********@yaho o.com <
ro********@yaho o.comwrote in message
<11************ **********@w3g2 000hsg.googlegr oups.com>:
I want to do fork from a program
This is not topical in news:comp.lang. c. It *is* topical in
news:comp.unix. programmer.

Below, I'll just comment on the Standard C portions of your code.
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()
It is portable and sensible to return a termination status value to
the environment. This is almost always an int value, so:

int main(void) { /* ... */ return status; }

Portable values for 'status' are 0, EXIT_SUCCESS and EXIT_FAILURE.
{
pid_t pid;
int i;
char *a="ls";
i=0;
Why this redundant assignment when the very next thing you do is the
same thing?
for(i=0;i<600;i ++)
{
pid = fork();
if(pid == 0)
{
/* Child process: */
system(a.exe);
exit(0);
}
else if(pid 0)
{
/* Parent process: */
printf("\n I just invoked group # %d",i);
Be aware that unless the output is terminated by a newline character,
it's not guaranteed to appear on the associated device immediately.
}
else
{
/* Error handling. */
printf("\n couldn't fork");
exit(1);
One is not a portable value for abnormal termination. Just use
EXIT_FAILURE.
}
}
}
<snip>

As for your actual problem, post to a UNIX group, since that's where
you'll get the best advice on using fork.
Jul 19 '07 #3
On Jul 20, 4:11 am, rohitsa...@yaho o.com wrote:
printf("\n I just invoked group # %d",i);

printf("\n couldn't fork");

NOW THE PROBLEM, the problem is when this code ends it does not give
me prompt back.
The '\n' goes at the end of the line, e.g.:
printf("couldn' t fork\n");

If you don't end your output with \n then
you might get all sorts of weird effects
with things not appearing.

(NB. Your code may have other problems, I'm
just mentioning this particular one).

Jul 19 '07 #4
ro********@yaho o.com wrote:

# NOW THE PROBLEM, the problem is when this code ends it does not give
# me prompt back.
#
# Like the output of a.exe is (Lets says "HELLO WORLD")
#
# The screen would look like
#
# HELLO WORLD
# HELLO WORLD
# HELLO WORLD
# HELLO WORLD
#
# I want it to look like
#
# HELLO WORLD
# HELLO WORLD
# HELLO WORLD
# HELLO WORLD
# Unix Prompt_>

C, even with Posix, does not guarentee serialization of different
processes doing buffered writes to one file. It might be writing the
message and then it gets coverred up by another process. Serialise
your code and see what happens; this is usually serialized by having
the parent wait for each child in turn.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
But I do believe in this.
Jul 21 '07 #5

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

Similar topics

2
3990
by: Ishwar Rattan | last post by:
Info at http://doc.python.org/ on os.fork() says that it has 'unix' semantics (on a UNIX box) on return values child pid in parent, 0 in child, no mention of failure? So, what does it return on failure? I tried it under Linux with Python-2.3.4, after few thousand forks the system just hangs! (does not return/report fork failure) Another question on module 'mutex' (stumbuled on it while looking for info on threads). What is its...
16
5810
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
5990
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;
11
8279
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
3891
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
1
5081
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 suggested in a faq, but it still doesnt seem to work. Below is what I have done: pid = fork() if (pid == 0){ printf ("http://my.wait.page"); exit (100);
5
12233
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...
5
1904
by: E2CMonkeyBoy | last post by:
Quick Intro: was thumbing through a book on Programming Windows in C# and was wondering if I shouldnt fork my developement: I've written a program in windows script, and now a more robust version in Java (using eclipse). I've hit the wall - in that the UI portions of eclipse are still manual - as VE (visual editor) is now 2 versions out of date (and I subsequently can't get it to work - where it used to on my code). So I picked up a...
3
5687
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 process I launch with execv the pppd to connect an embedded system to internet. The problem is that pppd fails. I have made two test program just to discard bugs (my applications is really big) and the result is that when I call execv from a...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
tracyyun
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...
0
8876
agi2029
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...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
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
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
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...

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.