0

I want to run a process in the background, using &, and have the process access the data used in the foreground process.

Here is an example:

A() {
  echo "$a"
}
a="abc"
while true; do echo "$(A)"; sleep 10; done &
a="xyz"

The output from the background process is always "abc". It does not matter what value is placed in a after the background task is started. In the example, I want the first iteration to output "abc", and 10 seconds later the output should be "xyz".

I want to have a background process to behave like a thread in Windows. The thread has full access to all data (but must not change data that would affect the UI). Is this possible?

0

The best way to do this is simply to use files.  Other approaches include named pipes and (in some shells) coprocesses.

  • My preference is to have the code in the process be the same whether is it run in the foreground or background. That is how a windows Thread works for the most part. Is this not possible? If not, are you saying that I must pass data between background and foreground code using channels like a file on disk, or a named pipe? – Richard M Saville Aug 23 '17 at 3:34
  • I’m not sure what the first sentence of your comment means.  A() { cat a_file; } will work the same in foreground and background.  And yes, foreground and background processes are not only separate processes, but separate process groups, so, yes, I’m saying that you can’t easily share memory between them. – Scott Aug 23 '17 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.