Background processes in a dummy shell...


 
Thread Tools Search this Thread
Top Forums Programming Background processes in a dummy shell...
# 1  
Old 12-02-2007
Background processes in a dummy shell...

Hey guys,
I am writing a very simple dummy shell in C++ and I am having trouble getting a process to run in the background. First of all, the shell has to recognize when I input a "&" at the end of the command, then it has to stick it in the background of the shell.

I understand that if I want a process to run in the background, then I don't wait on it... but that's the thing. I don't know how to impliment this.

Any help would be awesome. Thanks!Smilie
# 2  
Old 12-02-2007
1. Do it with the shell

Code:
./myprogram &

will run my myprogram in the background

2. Do it in the program

Code:
int main(int argc,char **argv)
{
     pid_t pid=fork();
     if (pid)
     {
            printf("started as %d\n",(int)pid);
            exit(0);
     }

     rest of program
}

# 3  
Old 12-03-2007
I'm sorry I didn't elaborate more.
First of all, my main function take no arguments. The way my program runs is after compile, I type the name of the executable in Unix and it starts the shell with a prompt waiting for the user to enter a command.

I can enter a command for a program in the same file directory and have it run in the foreground fine. But when I enter the same command with "&" after it, it still runs in the foreground.

I don't think I am isolating the "&" correctly. But even if I do, how do I NOT run this process in the foreground?

Thanks for the help.Smilie
# 4  
Old 12-03-2007
How are you running the program?

fork()
execlp()
wait()

or

system()
# 5  
Old 12-03-2007
I am using fork()
execv() and wait()
# 6  
Old 12-03-2007
Quote:
Originally Posted by icer
But even if I do, how do I NOT run this process in the foreground?
What do you think "in the foreground" means?

For example, not calling a blocking wait would help.
# 7  
Old 12-05-2007
One simple way to do background processes is by:

the parent (that is your main program) forks a child, and then the child forks its own child

you'll have something like this:

Loop:
----------------------------
parent
|
child
|
child's child (aka grandChild)
-----------------------------

the parent WILL NOT wait() for its child, this can be a simple condition, test if there is & appended or not

child WILL wait() for its child (grandChild)

you'll be doing this in a loop, and since the parent wont be waiting for its child the parent can fork() other childern, and they will fork() their own childern.

This is me trying to explain it, or here's a simple psuedocode that shows how it is done:

Code:
main()
{
     // parent running
     while(1)
     {
         child = fork();
         if(child==0)
         {
            // child running
            grandChild = fork();

            if(grandChild==0)
            {
                   // grand child running
                   exec();
            }

            wait();         // child must wait for grandChild
       }
            if()              // a condition to test for &
                  wait(); // doesn't exist then wait
   }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

List all background processes

How do I list the process in a Unix based system which are running in background? The following are options that I'm aware of, but they may not be appropiate. a. using ps -ef , and getting records of processes for which STATUS='S'(uninterruptible sleep) b. using jobs -l, and filtering... (5 Replies)
Discussion started by: kumarjt
5 Replies

2. Shell Programming and Scripting

Need help on background processes

Hi, I have a schell script parent.ksh from which I am calling three background processes a.ksh,b.ksh and c.ksh. Once these three processes completes the next step in parent.ksh should execute. How to achieve this? Please help me.... Thanks... (1 Reply)
Discussion started by: ravinunna
1 Replies

3. Shell Programming and Scripting

Background Processes

Ok guys so I have my first dummy shell almost done except for one tiny part: I do not know how to run a process in the background, from the code! I already know how to do that in a normal shell: $ program & However, no clue when it comes to how to program that thing. :eek: A very... (2 Replies)
Discussion started by: Across
2 Replies

4. Solaris

About running processes in background

Hi, I need to establish a procedure that will start an application in background each time my remote Solaris server is (re)started. This would be a kind of daemon. I am no sysadmin expert, so I am looking for pointers. How should I proceed? What are the main steps? Thanks, JVerstry (9 Replies)
Discussion started by: JVerstry
9 Replies

5. UNIX for Dummies Questions & Answers

Disadvantage of background processes

Hi, Inorder to improve the performance, I am trying to execute my command as a background process.. For eg: To zip large numbers of files present in a directory instead of using a single process, i do follow the below method: gunzip -c > / &... (3 Replies)
Discussion started by: unni.raj
3 Replies

6. Shell Programming and Scripting

Keep a certain number of background processes running

I've got a bit of code I'm trying to work on... What i want to happen is ... at all times have four parallel mysql dump and imports running. I found the follow code snippet on the forum and modified it to work by starting four concurrent processes but it waits until all four are done before... (7 Replies)
Discussion started by: dgob123
7 Replies

7. SuSE

oracle background processes

I have installed oracle 10g on suse sles9. I do not see oracle background processes. ps -ef|grep ora_ gives me environment variables junk. ps -ef|grep smon does not show anything however database is up and running. Any idea how to tweak that? (1 Reply)
Discussion started by: vijayasawant
1 Replies

8. Linux

Question about background processes

Hi! First of all, let me warn you I'm quite new to the world of LINUX and Operating Systems understanding, so that's why I pose these newbie and stupid qustions... Anyway, I'm trying to build my own simple shell in C and I'm getting some problems in implementing the background process ('&')... (10 Replies)
Discussion started by: neimaD
10 Replies

9. Shell Programming and Scripting

Running two processes in background

hi there, here's what i need in my korn-shell: ... begin korn-shell script ... nohup process_A.ksh ; nohup process_B.ksh & ... "other stuff" ... end lorn-shell script in plain english i want process A and process B to run in the background so that the script can continue doing... (6 Replies)
Discussion started by: jacob_gs
6 Replies

10. UNIX for Advanced & Expert Users

Background processes

How do you capture the return code from a background process? I am dumping data to a fifo and then processing it in a c program. I need to know that the sql finished successfully to ensure no missing data. Thanks. ex. sqlplus user/password < get_data.sql > data_fifo.txt & bin/process_data... (2 Replies)
Discussion started by: korndog
2 Replies
Login or Register to Ask a Question