Unix Shell background processing


 
Thread Tools Search this Thread
Top Forums Programming Unix Shell background processing
# 15  
Old 09-27-2010
That makes sense.
Oh well I mean everything else looks correct, the myshell> printout is a little bit screwy when I use & BUT the & seems to be working correctly regardless (using the sleep 60 & thing to test it)

Last edited by Mercfh; 09-28-2010 at 11:08 AM..
# 16  
Old 09-28-2010
Quote:
Originally Posted by Mercfh
That makes sense.
Oh well I mean everything else looks correct, the myshell> printout is a little bit screwy when I use & BUT the & seems to be working correctly regardless (using the sleep 60 & thing to test it)
You may be fighting buffers because you're using printf(). This keeps it in memory until it decides it's convenient to print it(usually, whenever it finds a newline). And when you fork(), you get a close-to-identical copy... So if you fork while anything's in the buffer it could even get printed twice! You might want to fflush(stdout); after you print, and before you fork(), to guarantee nothing's left in the buffers.

You can also do fprintf(stderr, "hello world\n"); to avoid it, since stderr never buffers. (it might be better to print to stderr anyway.)

Last edited by Corona688; 09-28-2010 at 04:11 PM..
This User Gave Thanks to Corona688 For This Post:
# 17  
Old 09-28-2010
here's an example of what im talking about.
myshell> ls -a
. .. a.out fork.c Hello .hi shell shell.c
myshell> ls -l
total 36
-rwxr-xr-x 1 matt matt 8435 2010-09-26 20:43 a.out
-rw-r--r-- 1 matt matt 943 2010-09-26 20:44 fork.c
drwxr-xr-x 2 matt matt 4096 2010-09-27 02:13 Hello
-rwxr-xr-x 1 matt matt 8322 2010-09-28 16:21 shell
-rw-r--r-- 1 matt matt 1693 2010-09-28 16:21 shell.c
myshell> ls &
myshell> a.out fork.c Hello shell shell.c
ls -a
myshell> . .. a.out fork.c Hello .hi shell shell.c
<---- here is where the "pointer" ends up. i can still type commands, but it doesn't show "myshell>" anymore

I tried the fprintf(stderr, "hello world\n"); thing. but it didn't work, it only happens when the & happens for some reason...

Looking at my code it should go like this.


parse arg. which prints out myshell>
User inputs commands<---
parse arg parses it.

so we have myshell> ls -l
so we execute it. which goes on a newline (apparently) (since BG is 0) and we wait for it to finish.
we then go back in the loop and myshell is printed again.

But with &....it goes back to parse arg (since we can still type things in, and they work) but myshell> isn't printed....which makes zero sense. Im looking at my code, tracing through....and I just dont see why this is happening, even when we aren't "waiting" the the process to finish, it should still loop back around to the parsearg function which has the myshell> printout.

It seems like typing in the & for SOME reason....like...screws all my printouts up.


edit: I think I may know why now.....im not resetting the BG bool to false, so it's treating them all as "background" processes i think........

edit2: CRAP nevermind, i realize im never setting BG permanently, oh well there goes that theory.

Last edited by Mercfh; 09-28-2010 at 06:34 PM..
# 18  
Old 09-28-2010
Please don't PM me in the hope of a faster response. I am not on-call here.

On my system I get:

Code:
myshell> echo a
a
0DEBUG:Child Finished
myshell> echo a &
1myshell> a

It does print 'myshell' again. And then 'echo' stomps on it and moves to the next line. This is normal. Since we're not waiting for it, the background process can't be polite about when it prints what... Also watch where you're cout-ing BG, if the process is backgrounded it kind of just shoves it in there anyway.

In a real shell try
Code:
$ ( sleep 3 ; echo asdf ) &


Last edited by Corona688; 09-28-2010 at 06:49 PM..
# 19  
Old 09-28-2010
Sorry, I just had a bunch of ideas was trying to figure stuff out.
also, i realize that is prints myshell again the first time, but if you try typing in another command, it'll just be a blank cursor (with no myshell printout)
like for instance the * is where the "blinking" cursor is.
myshell> ls
a.out fork.c Hello hello2 shell shell.c
myshell> ls &
myshell > a.out fork.c Hello hello2 shell shell.c
*
^ thats where the blinking cursor will be, i can still type in stuff....but there is no new myshell> printed out.

if I typed something else out again it's like....
ls
myshell>a.out fork.c Hello hello2 shell shell.c
ls &
myshell>a.out fork.c Hello hello2 shell shell.c
ls
myshell>a.out fork.c Hello hello2 shell shell.c


kinda weird...I guess it doesn't hurt anything, just thought it was a bit odd.
# 20  
Old 09-28-2010
I think I've figured it out. Because you let the background process die without wait()-ing for it, there's more than one process left around to wait for, and you get the oldest first, i.e. the already-dead one. so wait() happens instantly, picking up the dead process, while the new process keeps going in the background and messes up your terminal again.

You could try waitpid instead of wait, in order to wait for a specific PID instead of any PID.
# 21  
Old 09-28-2010
Quote:
Originally Posted by Corona688
I think I've figured it out. Because you're not using SIGCHLD, there's more than one process to wait for, and you get the oldest first, i.e. the already-dead one. so it finishes instantly, while the new process keeps going in the background and messes up your terminal again.

You could try waitpid instead of wait, in order to wait for a specific PID instead of any PID.
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's just I guess......messy (well the terminal is at least)
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