Closed Thread
Results 1 to 10 of 10

Thread: Your Own Shell - forks() and concurrent programs

  1. #1
    sourlemon's Avatar
    sourlemon is offline Programmer
    Join Date
    Nov 2008
    Posts
    101
    Rep Power
    0

    Your Own Shell - forks() and concurrent programs

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

    This is my current execution function:
    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);
    }
    }
    My teacher gave me another waiting statement, but it doesn't work correctly when I do it like that.
    int status = 0;
    wait(&status);
    printf("Child exited with status of %i.\n", status);
    I know that's a lot I'm asking, but if anyone can help me, I'll be greatly appreciated. THANK YOU!!!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Your Own Shell - forks() and concurrent programs

    First of all, I wouldn't call fork() like you did. This is the way I do it:

    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));
    }
    The reason is that if fork() fails, your code won't catch it. Hence my use of a switch statement.
    sudo rm -rf /

  4. #3
    kiddies is offline Programmer
    Join Date
    May 2009
    Posts
    129
    Rep Power
    0

    Re: Your Own Shell - forks() and concurrent programs

    thanks....

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Your Own Shell - forks() and concurrent programs

    That ellipsis leads me to believe it didn't exactly work.
    sudo rm -rf /

  6. #5
    sourlemon's Avatar
    sourlemon is offline Programmer
    Join Date
    Nov 2008
    Posts
    101
    Rep Power
    0

    Re: Your Own Shell - forks() and concurrent programs

    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.

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Your Own Shell - forks() and concurrent programs

    Anytime.
    sudo rm -rf /

  8. #7
    h4x Guest

    Re: Your Own Shell - forks() and concurrent programs

    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));
    }
    this code is flawed.
    Code:
    case 0:
        //child code - execute exec() here
        break;
    what if break will be executed? you assume exec() wont fail.
    Code:
    case -1:
        //failed to fork
        break;
    and here we jump right to
    Code:
    //check to see if child program exited immediately.
    	//if so, it failed.
    	if(WIFEXITED(exit_code))
    wich is uninitilized
    Code:
    waitpid(pid,&exit_code,0);
    cant it fail?
    it might be pedantic, but always check return value unless it return void...

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Your Own Shell - forks() and concurrent programs

    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 /

  10. #9
    h4x Guest

    Re: Your Own Shell - forks() and concurrent programs

    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:

  11. #10
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Your Own Shell - forks() and concurrent programs

    Should I have put this, then?
    Code:
    //insert your code here
    I thought it was fairly obvious, as did the OP...
    sudo rm -rf /

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Concurrent TCP server using Select API in Linux C
    By fayyazlodhi in forum C Tutorials
    Replies: 0
    Last Post: 06-06-2011, 02:11 PM
  2. Pipes and forks issue in C
    By redcameron in forum C and C++
    Replies: 4
    Last Post: 03-24-2009, 12:32 PM
  3. A Concurrent Server
    By anna99 in forum General Programming
    Replies: 1
    Last Post: 07-18-2008, 05:31 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts