UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old October 7th, 2003, 07:21 PM
pongsu pongsu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 pongsu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old October 7th, 2003, 07:42 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 54 m 7 sec
Reputation Power: 797
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

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #3  
Old October 7th, 2003, 07:49 PM
pongsu pongsu is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 pongsu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #4  
Old October 8th, 2003, 12:52 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 54 m 7 sec
Reputation Power: 797
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Miniature shell - Process management shortcuts and Environment variables


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway