Foreground and Background Process in Unix

📌 Summary: 

In this post we are going to learn about Foreground and background process in UNIX. We also know about the steps of process.We cover so many command such as nohugh command, Nice command,how to set/schedule any command,how to create/ set the priority of users.  

➽ In simple words we can say that those process which done by user for interacting with application is called foreground process.

➽ A background process is a process which run without user interaction.

Let we understand by the help of example:

$ ls -l

Output : -r w x  r_ _  _ _ _

$ sort -0 a1 a1      [ sort a1  & store in a1 ]

$ sort -0  a1  a1  &  $ cat  a1 > a2


If we use & (Am percent) than immediately ID of above process ( $ sort -0 a1 a1) will generate and than immediately give  $ prompt. It is foreground process not Background process.

For creating background process in UNIX than write.

$ sort -0 a1 a1 &

Suppose Output: 5122   ( PID Number)

$

$ ls -l

Output  : -rwx r_ _ r _ _

$


⧭ STEPS FOR PROCESS IN UNIX (Foreground Process)



➽ Here parent execute child process and also allocate memory for execution of child process. When child process is executed than parent wash out those memory which is allocated for child process such as


⧭ Note : 

➽ In a foreground process parent create child process and wait until child process is terminated.

➽ In a background process parent create child process but parent does not wait for termination of child process.

For example:

$ sort -0 a1 a1 &

output: suppose  5132  (PID)

$ ls -l


➽ Another condition when you are in user suppose (aniltty05)

$ sort -0 a1 a1 &   (suppose it takes 15 min)

output: suppose  5132  (PID)

Than during this condition we press (ctrl + d) than this will kill the parent process. In this condition child process will also kill and after that we get login.


⧭ COMMANDS:

⧭ nohugh Command for No Hangup

Suppose we are in sh

$ nohugh  sort  -0  a1  a1  &

Output suppose : 5133

After that if we press (ctrl + d ) than child process will be continued.

Here parent sh will kill so init will become the new parent of child process.



Note : 

➽ Only admin can kill the process.

➽ User can not kill the process.

If init will kill than whole process will kill.

If sh parent will kill than init will adopt their child process and that child is called zombi child.


⧭ NICE Command:

➽ This NICE command will decide the priority of process. (changing the priority of process)

    0 →  highest priority

    39  Lowest priority

$ nice  -n  background / Foreground

Example:   $ nice  -5  sort  -0  a1  a1  &

here:    & is background process.

           -5 is 5th priority of command ( sort  -0  a1  a1  & )

Note:

➽ When any user create then admin create / set some priority of that user.

Example : 

In any big organisation when any new employee join then admin create their lowest priority.

        Example:

                            User            Priority

                            amit                0

                            raj                   5

                            anil                  2


➽ User can not change their priority that mean it can not run nice command.


⧭ at Command:

➽ at command is used to set / schedule for any process. That means if we want to execute any process at any particular time automatically than we use at command. 


Suppose we want to start backup of any work at 12.00 PM then  (just like time bomb😃)


➽ If we want to defuse the time then command is:

$  at  -r   982523254

here number (982523254) is not fix it is whatever.

No comments:

Post a Comment