Unix Shell background processing


 
Thread Tools Search this Thread
Top Forums Programming Unix Shell background processing
# 22  
Old 09-28-2010
yes, I edited this like 9 times, so reread again please Smilie

---------- Post updated at 04:03 PM ---------- Previous update was at 04:01 PM ----------

Quote:
Originally Posted by Mercfh
Ah that makes sense. I guess it's not a big deal, i might go back through and make some changes. I mean.....the way I have it isn't "wrong" is it?
It is wrong. Try 'echo a &', then 'sleep 60'. It won't wait for sleep to finish because it waited for echo's return instead of sleep's. Just redo wait() with waitpid() and it will wait for the correct process. It will still be incomplete, because you'll still be leaving around zombie processes whenever you background something, but adding a SIGCHLD handler can be done later to complete it.
# 23  
Old 09-28-2010
Ya I noticed literally just now with using sleep 60 right after a ls &......
it still let me type it in.

darnit.
so to use waitpid i'd replace wait with waitpid( pid, &status, WNOHANG);
? or should I use an actual integer for pid

btw I made an integer called status in my execute function, so it now looks like this.
Code:
void Execute(char* cmd[],bool BG)
{
    int status;
    pid_t pid;
    pid = fork();
    switch(pid)
    {
        case -1:  
//Debug     cout << "Fork Failure" << endl;
            exit(-1);
        case  0:
            execvp(cmd[0], cmd);
    
            if(execvp(cmd[0], cmd) == -1)
            {
                cout << "Command Not Found" << endl;
                exit(0);
            }
   
          default:
            if(BG == 0)
            {

                    waitpid( pid, &status, WNOHANG);
//Debug             cout << "DEBUG:Child Finished" << endl;
            }


    }
    
}

the man. pages for waitpid are a bit confusing....

Last edited by Mercfh; 09-28-2010 at 07:21 PM..
# 24  
Old 09-28-2010
From man waitpid:
Code:
WNOHANG     return immediately if no child has exited.

...which doesn't sound like what you want. Give it 0 instead.

Otherwise, looks OK.
# 25  
Old 09-28-2010
It seemed to have worked. The terminal is still messed up, but im ok with that. I tried the sleep and even when it was off kilter it still made it wait.

So as to improve my learning, cause you explain why exactly this worked. wait() as I understand waits for any process to finish before moving on....so waitpid waits for the specific pid process to finish.......but aren't background processes specific to this pid too.

Also...I assume status really has no point except maybe in the sighandler.

I have no clue how you figured this out? i never would've thought of that. But then again im new to processes and such.
# 26  
Old 09-28-2010
Quote:
Originally Posted by Mercfh
So as to improve my learning, cause you explain why exactly this worked. wait() as I understand waits for any process to finish before moving on....so waitpid waits for the specific pid process to finish.......but aren't background processes specific to this pid too.
Every process has its own unique pid. By waiting for the PID you can wait for one specific process and no others.
Quote:
Also...I assume status really has no point except maybe in the sighandler.
You can get the function's return value with WEXITSTATUS(status). This is important to find out if a process returned success(0) or failure(anything else). You might want to make it the return value of your Execute function.
Quote:
I have no clue how you figured this out?
I remembered that this was leaving zombie processes around, so that what you were waiting for probably wasn't what you were getting.
# 27  
Old 09-28-2010
Quote:
Every process has its own unique pid. By waiting for the PID you can wait for one specific process and no others.
Oh so it's waiting for them in order? as they come then, instead of just whatever gets done first? sorta...?

either way you've been a HUGE help. and I can say I've honestly learned alot by doing this....it certainly was interesting..
# 28  
Old 09-28-2010
Quote:
Originally Posted by Mercfh
Oh so it's waiting for them in order as they come then, instead of just whatever gets done first? sorta...?
I'm not sure there's any particular order you can count on if you don't give the pid.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to log file processing details to database table usnig UNIX shell script?

we are getting files on daily basis.we need to process these files. i need a unix shell script where we can count 1-The no of files processed 2-No of data/record processed for each files. The script should log these details into a database table. If there is any error while file... (3 Replies)
Discussion started by: Atul kumar
3 Replies

2. Shell Programming and Scripting

How to run multiple functions in Background in UNIX Shell Scripting?

Hi, I am using ksh , i have requirement to run 4 functions in background , 4 functions call are available in a case that case is also in function, i need to execute 1st function it should run in background and return to case and next i will call 2nd function it should run in background and... (8 Replies)
Discussion started by: karthikram
8 Replies

3. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

4. Shell Programming and Scripting

BASH - Handling background processes - distributed processing

NOTE: I am using BASH and Solaris 10 for this. Currently in the process of building a script that has a main "watcher" daemon that reads a configuration file and starts background processes based on it's global configuration. It is basically an infinite loop of configuration reading. Some of the... (4 Replies)
Discussion started by: dcarrion87
4 Replies

5. Shell Programming and Scripting

background processing in BASH

I have script 3 scripts 1 parent (p1) and 2 children child1 and child2 I have script 3 scripts 1 parent 2 children child1 child2 In the code below the 2 child processes fire almost Instantaneously in the background, Is that possible to know the status of pass/fail of each process... (12 Replies)
Discussion started by: jville
12 Replies

6. Linux

background processing in BASH

I have script 3 scripts 1 parent 2 children child1 child2 In the code below the 2 child processes fire almost Instantaneously in the background, Is that possible to know the status of pass/fail of each process "as it happens" ? In the present scenario although Child2... (5 Replies)
Discussion started by: jville
5 Replies

7. Shell Programming and Scripting

run a shell in the background

How can I run a shell in the background? cat test.sh #!/bin/sh sleep 600 Thank u very much indeed! (2 Replies)
Discussion started by: GCTEII
2 Replies

8. Shell Programming and Scripting

Background shell script

I have a friend at work that asked me a question today and I figured I would seek help here. How does a shell script know whether it is running in the background or not? Thanks in advance for help (5 Replies)
Discussion started by: Moofasa
5 Replies

9. UNIX Desktop Questions & Answers

Unix Background

Hi, I'm new to this forums and to Unix OS... Is this the right place to put this thread? I just need to ask how to set the wallpaper that goes through the x-term windows in a Unix system? It's as if the x-term windows is transparent... I tried modifying the .login file and things got... (5 Replies)
Discussion started by: Gri3v3r7
5 Replies

10. UNIX for Dummies Questions & Answers

background for unix

Hi, I am a newbie learing Unix , I have started with teh book "the Design of the Unix OS" by Bach.After which I plan to read "UNIX Network Programming" by Richard Stevens. What is the background that one needs to learn unix. I know C. But I am not sure about my Operating Systems... (4 Replies)
Discussion started by: ramyar
4 Replies
Login or Register to Ask a Question