Theory
Programs जो चल रहे हैं
जब एक program actually server पर चल रहा होता है, इसे एक process कहते हैं। आपका shell एक process है; वह command जो आपने अभी launch किया एक process है; background में humming करता web server एक process है। Kernel हर एक को एक unique Process ID (PID) देता है, इसे refer करने के लिए इस्तेमाल होने वाला एक number।
आप Operating Systems में processes को एक concept की तरह मिले। यहाँ आप इन्हें hands-on control करना सीखते हैं: क्या चल रहा है देखिए, अपना terminal freeze किए बिना लंबे jobs launch कीजिए, और एक misbehaved process को रोकिए। यह lesson वह toolkit cover करता है।
Theory
Foreground और Background
Default रूप से एक command foreground में चलता है: यह आपका terminal ले लेता है, और आप wait करते हैं, type करने में असमर्थ, जब तक यह ख़त्म नहीं होता। quick commands के लिए fine, पर एक job के लिए painful जो minutes तक चलता है।
एक ampersand `&` append कीजिए और command इसकी बजाय background में चलता है: यह शुरू होता है, shell इसका PID print करता है, और आपका prompt तुरंत वापस आता है ताकि आप काम करते रहें। उदाहरण के लिए, sleep 60 & background में एक 60-second timer launch करता है और तुरंत आपको prompt वापस दे देता है। आप फिर ऐसे jobs को jobs से check कर सकते हैं, और kill से एक रोक सकते हैं।
At a glance
| Command | यह क्या करता है |
|---|---|
| ps | Running processes (इनके PIDs के साथ) list करता है |
| command & | command को background में चलाता है; prompt तुरंत वापस आता है |
| jobs | इस shell के background jobs list करता है |
| Ctrl+Z | Current foreground job suspend करता है (pause) |
| bg / fg | background में एक job resume करता है / इसे foreground में लाता है |
| kill PID | उस PID वाली process को रोकने के लिए एक signal भेजता है |
| sleep N | N seconds के लिए pause करता है (scripts में handy) |
Practical
एक Background Job Launch और Stop करना (verified)
$ sleep 60 & # start a 60-second process in the background
[1] 4231 # shell reports job number [1] and PID 4231
$ ps # see it running
PID TTY TIME CMD
4231 pts/0 00:00:00 sleep
$ kill 4231 # politely ask it to stop
$ jobs
[1]+ Terminated sleep 60Formula
Foreground Block करता है, Background Free करता है
Core distinction: एक foreground job आपका terminal तब तक hold करता है जब तक यह ख़त्म नहीं होता; एक background job (& से started) आपको काम करते रहने देता है जबकि यह चलता है।
अगर आपने गलती से एक लंबा job foreground में शुरू किया, इसे suspend करने के लिए Ctrl+Z press कीजिए, फिर इसे background में continue कराने के लिए bg type कीजिए, अपना prompt free करते हुए। बाद में, fg इसे वापस foreground में लाता है। यह foreground/background dance वह तरीका है जिससे आप एक terminal से कई tasks juggle करते हैं बिना नई windows खोले।
Quiz
आप एक लंबी command चलाते हैं और & append करते हैं, ./backup.sh & की तरह। क्या होता है?
- चलने से पहले command delete हो जाती है
- Command background में चलती है और आपका shell prompt तुरंत वापस आता है, तो आप काम करते रह सकते हैं
- Command foreground में चलती है और terminal को block करती है जब तक यह ख़त्म नहीं होती
- & output को alphabetically sort करता है
Show the answer
Command background में चलती है और आपका shell prompt तुरंत वापस आता है, तो आप काम करते रह सकते हैं
& append करना command को BACKGROUND में चलाता है: यह शुरू होती है, shell इसका job number और PID print करता है, और आपका prompt तुरंत वापस आता है ताकि आप इसके काम करते हुए दूसरी commands चला सकें। Option A ग़लत है: & कुछ delete नहीं करता; command normally चलती है, बस background में। Option C बिना & के चलने को describe करता है, यही foreground behaviour है जो terminal को tie up करता है, जो exactly वह है जिससे & बचाता है। Option D invented है; & का sorting से कोई लेना-देना नहीं है। लंबे jobs (backups, downloads) के लिए & इस्तेमाल कीजिए ताकि वे चलें जबकि आप आगे बढ़ें; इन्हें jobs से check कीजिए और ज़रूरत पड़ने पर kill से रोकिए।
Think first
kill और kill -9 में क्या Difference है?
kill PID usually एक process रोकता है, पर कभी-कभी लोग kill -9 इस्तेमाल करते हैं। Difference क्या है, और हमेशा -9 क्यों नहीं इस्तेमाल करते? फिर tap कीजिए।
Show the answer
kill एक process को एक SIGNAL भेजता है, और difference है यह signal कितना forceful है। Plain kill PID default TERM signal (number 15) भेजता है, जो process को politely shut down होने कहता है। यह सही पहली choice है क्योंकि यह program को clean up करने देता है: अपनी files save करता है, database में writing ख़त्म करता है, resources release करता है, और gracefully exit करता है। ज़्यादातर processes इसे obey करती हैं। पर कभी-कभी एक process stuck होती है या TERM ignore करती है, और तब kill -9 PID KILL signal (number 9) भेजता है, जो kernel को process को IMMEDIATELY terminate करने के लिए force करता है, clean up करने का कोई chance नहीं। वह force exactly वजह है आपको पहले -9 की तरफ़ reach नहीं करना चाहिए: क्योंकि process को save या tidy करने का कोई opportunity नहीं मिलता, आप half-written files या corrupted data risk करते हैं। अच्छी habit यह है: पहले plain kill try कीजिए (polite), इसे एक moment दीजिए, और सिर्फ़ तब kill -9 तक escalate कीजिए अगर process genuinely मरने से मना करे। Force करने से पहले politely पूछिए: पहले TERM, KILL सिर्फ़ last resort की तरह।
Summary
Key takeaways
- एक process एक running program है; kernel हर एक को एक unique Process ID (PID) देता है।
- ps running processes और इनके PIDs list करता है; jobs इस shell के background jobs list करता है।
- एक foreground command terminal को तब तक tie up करता है जब तक यह ख़त्म नहीं होता; & append करना इसे background में चलाता है और तुरंत prompt वापस लाता है।
- Ctrl+Z एक foreground job suspend करता है; bg इसे background में resume करता है; fg एक job को foreground में लाता है।
- kill PID एक process को एक signal भेजकर रोकता है।
- Plain kill polite TERM (15) भेजता है ताकि process clean up कर सके; kill -9 KILL भेजता है, immediate termination force करते हुए (last resort)।
- Memory hook: background के लिए &, देखने के लिए ps, रोकने के लिए kill; पहले TERM, -9 सिर्फ़ stuck होने पर।