Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

c - Child process in background (ls &)

I am making my own shell using C, if I use & at the end of the command then it will run in the background.. So to make it run in the background I don't use a wait() in the parent.

My code runs like this when I am not using &

-->ls
File1.c file2.c file.txt
-->

where I am printing --> before taking the user input for the next command.

But if I use & at the end, I have just tried this with ls

My code runs like this:

-->ls &
-->File1.c file2.c file.txt

I don't know why the --> symbol is not printing on the next line in this case.. Is there something with ls that executes quickly or a code issue??

I want the output to look like this when & is used

-->ls &
File1.c file2.c file.txt
-->
question from:https://stackoverflow.com/questions/65902247/child-process-in-background-ls

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Although I have neither seen your code nor the platform you use, it seems that the processes are scheduled in a different order depending on running it in background or in foreground.

If you want to synchronize the processes, you can either establish some kind of inter-process communication or use a deterministic scheduler. The later is most certainly an overkill and infeasible.

I'd recommend to wait for the process to end or use pipes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...