#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep Power
    0

    Miniature shell - Process management shortcuts and Environment variables


    hi,

    I was trying to write a miniature shell (ie, command line interpreter) to implement the features like 'Foreground and Background processing', 'Process management shortcuts' and 'Environment variables'.
    Can anyone help me with sample shell sript to implement the above features.

    cheers
    pongsu
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    I'm not sure what you want to achieve? For background, you can either:
    (a) /path/to/command &
    (b) Run command in foreground, then hit ^Z to suspend the process, and then type bg to resume the process in the background.
    (c) nohup /path/to/command & <-- useful if you want to log off and leave the process running.

    fg brings the last process to foreground. If you have multiple processes in the background, then use fg %1, fg %2 etc. to bring a specific process to the foreground.

    Are you saying you want to put these commands in some kind of shell script?
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep Power
    0

    make it clear


    In the previous problem, i need to write a command line interpretr which implements the following features,
    a) Environment variables
    b) background and foreground processing
    c) process management shortcuts

    thanx for ur help
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Smells a bit like homework, but I'll pitch in . Assuming that you're going to use the C programming language, I'd suggest you first read the man pages for fork, execvp, waitpid, environ, getenv and putenv. Don't read the explanation below till you've read the man pages first, because otherwise the explanation may not make sense. Ok now that we're done reading the man pages, let's outline a few quick methods of doing the following:

    To run a process in the foreground
    First fork() a copy of the currently running program -- this creates a copy of the parent process. Then on the child process, call execvp() to replace the child process image with the new image. Since execvp() and all the exec function family replace the currently running process image with a new image, this is why you'll need to run fork() first, to make a copy of the current program. Thus the child process gets replaced with the program specified to execvp(), while the parent process stays in memory. Meanwhile on the parent process, run waitpid() to wait for the child to finish and get the return status from the child. So how do you know which process is the parent and which process is the child... simple -- if you'd read the man pages for fork(), you'd notice it says that it creates a copy of the process that ran fork(), and the child is at the same point in the program as the parent. However the one difference is that the child gets a return value of 0 from the fork() call, whereas the parent gets the pid of the child, which can be passed to waitpid(). Incidentally, this is EXACTLY the same way that shells do it.

    To run a process in the background
    Similar to running a process in the foreground. First fork() off a copy of the parent. Then on the child, run execvp() to replace the child process image with the new image. The only difference is that on the parent process, you don't execute waidpid() to wait for the child to terminate.

    To get/set environment values
    If you read the man pages for environ, getenv() and putenv(), this is a piece of cake.

    With that said, here's some code that sets an environment value, dumps the environment strings, then executes a program in the foreground, waits for it to finish, then executes the same program in the background and terminates.
    Code:
    #include <sys/types.h>
    #include <unistd.h>
    #include <sys/wait.h>
    #include <stdio.h>
    
    extern char **environ;
    
    int main(void) {
      int ret;
      printf("Adding to environment and dumping it.\n");
      if (add_to_env("prog=foo") == 0)
        show_env();
    
      printf("\n\nRunning program in foreground\n");
      ret = fg("/home/mb/cpp/delay", NULL);
      printf("%d\n", ret);
      printf("\n\nRunning program in background\n");
      ret = bg("/home/mb/cpp/delay", NULL);
    
      return 0;
    }
    
    int fg(char *process, char **args) {
      /* Run *process in the foreground */
      pid_t pid;
      int status = -1;
    
      pid = fork();
      if (!pid) {
        execvp(process, args);
      } else {
        pid = waitpid(pid, &status, 0);
      }
      return status;
    }
    
    int bg(char *process, char **args) {
      /* Run *process in the background */
      if (!fork()) {
        execvp(process, args);
      }
      return 0;
    }
    
    int show_env(void) {
      /* Show existing environment variables */
      char **ep;
      for (ep = environ; *ep != NULL; ep++)
        printf("%s\n", *ep);
      return 0;
    }
    
    int add_to_env(char *envstring) {
      /* Add to environment variables */
      return putenv(envstring);
    }
    Note that the program I'm executing ("/home/mb/cpp/delay") is a program that simply delays for a few seconds and writes a message. Write your own suitable program, so you can see that it is being placed in the background correctly and the environment variables are being passed on correctly to it .

    Hope this helps
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo