linux - Implementing background processing -
linux - Implementing background processing -
update: there obvious debugging step forget. happens if seek command ps &
in regular old bash shell? reply see same behavior. example:
[ahoffer@uw1-320-21 ~/program1]$ ps & [1] 30166 [ahoffer@uw1-320-21 ~/program1]$ pid tty time cmd 26423 pts/0 00:00:00 bash 30166 pts/0 00:00:00 ps <no prompt!>
if press enter, command shell reports exit status , console displays exit status , prompt:
[ahoffer@uw1-320-21 ~/program1]$ ps& [1] 30166 [ahoffer@uw1-320-21 ~/program1]$ pid tty time cmd 26423 pts/0 00:00:00 bash 30166 pts/0 00:00:00 ps [1] done ps [ahoffer@uw1-320-21 ~/program1]$
ps: using putty access linux machine via ssh on port 22. original question: working on homework assignment. task implement part of command shell interpreter on linux using functions fork(), exec(). have unusual bug occurs when code executes command background process.
for example, in code below, command ls
correctly executes ls , prints output console. when command finished, the event loop in calling code correctly prints prompt, "% ", console.
however, when ls &
executed, ls
executes correctly , output printed console. however, prompt, " %", never printed!
the code simple. here pseudo code looks like:
int child_pid; if ( (child_pid=fork()) == 0 ) { //child process ...execute command... } else { //parent process if( delim == ';' ) waidpid(child_pid); } //end of function.
the parent process blocks if delimiter semicolon. otherwise function ends , code re-enters event loop. however, if parent sleeps while the background command executes, prompt appears correctly:
...
//parent process if( delim == ';' ) { waidpid(child_pid) } else if( delim == '&' ) { sleep(1); //the prompt, " %", correctly printed // console when parent wakes up. }
no 1 in class knows why happens. os redhat enterprise 5 , compiler g++.
linux process
Comments
Post a Comment