Fork does not return control back

R

rohitsagar

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 ?
 
R

Richard Heathfield

(e-mail address removed) 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.
 
S

santosh

I want to do fork from a program

This is not topical in It *is* topical in

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

Old Wolf

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).
 
U

Urs Beeli

I want to do fork from a program

If you have questions about fork() you'd best ask them in a unix specific
group as you are likely to get better information there. Others have
suggested such groups.
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()

int main(void)
{
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);
}
}

<ot> if you want the behaviour you describe below, your parent programm must
wait for each of its children to finish before ending main(). on how to do
that I suggest you check your documentation or a unix group
}

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_>

<ot>
You expect your children to finish first and then the parent to exit.
This is not guaranteed (unless you make it so, see my comment above).
What most likely happens is that a few children run, then the parent exits
after spawning the remaining children (thought they have not yet been
executed), printing the unix prompt, after the parent exits, the remainind
children execute. The output would then look something like this:

HELLO WORLD
HELLO WORLD
Unix Prompt_>
HELLO WORLD
HELLO WORLD

As you have 600 "HELLO WORLD" entries, the prompt is easy to miss,
especially as it will be pasted to the end of one of the 600 "I just invoked
group #" messages (due to lacking \n at the end of that string).
The output is not stuck, I dont need to hit ctrl c, it is just at
prompt,

<semi ot>
Yes, the prompt is displayed and then the children mess up your console
(stdout) and you can still type new commands on stdin even without seeing a
prompt.
but still any idea ?

For more (and better) information, try a suitable group.

Cheers
/urs
 
S

SM Ryan

(e-mail address removed) 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.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top