473,379 Members | 1,270 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,379 software developers and data experts.

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 2521
ro********@yahoo.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.programmer, 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********@yahoo.com <
ro********@yahoo.comwrote in message
<11**********************@w3g2000hsg.googlegroups. 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...@yahoo.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********@yahoo.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
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...
16
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
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 & ...
1
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...
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...
5
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...
3
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.