Hi guys,
I'm currently having problem editing my code that a child of a process can run concurrently with its parent.
This is the exact statement from my homework.I don't exactly understand how the fork () and shell works together. But after days of browsing, this is what I concluded. When I call fork, it runs another process (supposedly, my shell?). But I'm lost with the parent and the child. Who are they? Why is my child calling the execution? If only the child can call execv, does that mean I have to create another fork to have the process running concurrently? Or can I edit the wait statement?Add functionality to the shell from Problem A so that a user can use the "&" operator as a command terminator. A command terminated with "&" should be executed concurrently with the shell (rather than the shell's waiting for the child to terminate before it prompts the user for another command).
This is my current execution function:
My teacher gave me another waiting statement, but it doesn't work correctly when I do it like that.void exeCmd(char *cmd){
int i = 0;
if(fork() == 0) {
printf("%s\n", cmd);
i = execve(cmd, margv, menvp);
printf("Error: %d\n", errno);
if(i < 0) {
printf("%s: Command not found.\n", cmd);
exit(1);
}
} else {
wait(NULL);
}
}
I know that's a lot I'm asking, but if anyone can help me, I'll be greatly appreciated. THANK YOU!!!int status = 0;
wait(&status);
printf("Child exited with status of %i.\n", status);![]()
First of all, I wouldn't call fork() like you did. This is the way I do it:
The reason is that if fork() fails, your code won't catch it. Hence my use of a switch statement.Code:int exit_code; pid_t pid = fork(); switch(pid) { case 0: //child code - execute exec() here break; case -1: //failed to fork break; default: //parent code waitpid(pid,&exit_code,0); break; } //check to see if child program exited immediately. //if so, it failed. if(WIFEXITED(exit_code)) { //exec() failed printf("Failed with code %d\n",WEXITSTATUS(exit_code)); }
sudo rm -rf /
thanks....
That ellipsis leads me to believe it didn't exactly work.
sudo rm -rf /
thanks dargueta. I forgot about that. I think I solved the background problem by making it an if statement. If the bg is false, I make the parent wait. It looks messy, but I think that's how it is. Thank you for your help.
Anytime.![]()
sudo rm -rf /
this code is flawed.Code:int exit_code; pid_t pid = fork(); switch(pid) { case 0: //child code - execute exec() here break; case -1: //failed to fork break; default: //parent code waitpid(pid,&exit_code,0); break; } //check to see if child program exited immediately. //if so, it failed. if(WIFEXITED(exit_code)) { //exec() failed printf("Failed with code %d\n",WEXITSTATUS(exit_code)); }
what if break will be executed? you assume exec() wont fail.Code:case 0: //child code - execute exec() here break;
and here we jump right toCode:case -1: //failed to fork break;
wich is uninitilizedCode://check to see if child program exited immediately. //if so, it failed. if(WIFEXITED(exit_code))
cant it fail?Code:waitpid(pid,&exit_code,0);
it might be pedantic, but always check return value unless it return void...
H4x...I only put comments because kiddies is supposed to put his/her own code in there. It's called a template. Trust me, I know what to do.
Last edited by dargueta; 10-08-2009 at 02:02 PM. Reason: Fixed grammar
sudo rm -rf /
oh well, perhaps i was misguided by your statment:
First of all, I wouldn't call fork() like you did. This is the way I do it:
Should I have put this, then?
I thought it was fairly obvious, as did the OP...Code://insert your code here
sudo rm -rf /
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks