Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using fork

Status
Not open for further replies.

CaptRage61

Programmer
Feb 17, 2005
50
0
0
US
I am using the fork command to run 2 progmrams at the same time, the ls command. I then pass in at the command line 2 args so that the first arg goes to the first ls and the second arg goes to the second ls. That works fine, the problem is I am getting too many program running, all I want to be displayed is the output of one then the output of the other.
For example if I run the command: ./prog -l --color=tty
here is my code anyone know how I can fix it??

Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char* argv[]){
int a = fork();
int b = fork();
int first,second;

if (a !=0){
//parent code

}
else{
//child code

first = execl("/bin/ls","ls",argv[1],NULL);
}

if (b !=0){
//parent code

}
else{
//child code

second = execl("/bin/ls","ls",argv[2],NULL);
}

}
 
Don't you have to wait until the first ls completes? No error checking and probably can be written better:


#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
int pid;

switch(pid=fork())
{
case -1:
fatal("fork error 1");
case 0:
execl("/bin/ls","ls",argv[1],NULL);
fatal("exec call 1 ");
}
/*wait for child process to finish*/
while(wait((int *)0) != pid)
;

switch(pid=fork())
{
case -1:
fatal("fork error 2");
case 0:
execl("/bin/ls","ls",argv[2],NULL);
fatal("exec call 2 ");
}
/*wait for child process to finish*/
while(wait((int *)0) != pid)
;

}
 
Blarg.

Your first fork makes two processes. Then, each of those processes calls the second fork, making four processes.


One of those is the original process (the granddaddy); it's a parent on fork A and fork B, so it does nothing.

The next one is a child on fork A and a parent on fork B, so it runs the first command.

The third one is a child on fork A and on fork B, so it runs both commands.

The fourth one is a parent on fork A and a child on fork B, so it runs the second command.


Worst of all, they're all running at the same time.


You probably just want to fork once.

Have the child process exec the first command.

Have the parent wait for the child and exec the second command when the fist one is done.


At any rate, if you end up needing a second fork, make sure only one of the processes executes it.
 
Code:
int a = fork();
int b = fork();
What chipper is saing that the first line here creates a child process (and the original process (the parent) still exsists). They both execute the next line creating a total of 4 processes... instead of two. These are the types of things that will lead to fork bombs in more compilcated code.

Olded, wait(int *) doesn't need the loop; from the man page
The wait function suspends execution of the current process until a child has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function. If a child has already exited by the time of the call (a so-called "zombie" process), the function returns immediately. Any system resources used by the child are freed.
so
Code:
while(wait((int *)0) != pid);
can be:
Code:
int status;
wait(&status);
if(0 != status)
  //error


[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top