Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

OS Lab

Lab 03/4: Processes


3.1 List processes
a. The command to list all processes is ps. i. $ps -option where option can be a : display all processes, not including processes not controlled by terminal x: include processes not controlled by terminal l: long listing. includes more information such as process owner's uid. u: display username of process owner

3.2 Running processes in background and foreground


a. To run a process in background, put a '&' at the end of the process. ii. $mozilla & iii. $cat >a & Program #1 main() { Long int i; for(i=0;i<=400000;i++) printf(l is %d\n,i); } Execute it through the command output file name with &. The & will make background process which is terminated only if the process ends, or is interrupted with a DEL. The UNIX prompt will come back on screen immediately. Load the editor again and wait. After some time a l is 4000000 will be displayed at the top left corner of the screen.

3.3 Changing process priority


a. The nice command is used to schedule a priority for newly created processes. $ nice -n -20 find / -name passwd >>Here -20 is highest priority and 19 is the lowest value. The fpsinding operation is given highest priority.

Compiled by: Pradip Maharjan and Nar kaji Gurung

OS Lab b. The renice command is used to reschedule of the process. $renice -20 -p 4059 >>This will reschedule the priority of process having the process id as 4059 to -20 (highest).

3.4 Creating processes


b. Processes are created via "fork" command. c. Whenever you type any command in shell, it will create new process. fork() Process initiated by us can also create children in the same manner as the Swapper and the process dispatcher did. These children process are created using the fork() function. It is by forking processes that we can exploit the multitasking capability of UNIX. Program #2 main(){ fork(); printf(Hello world\n); } Can you guess what the output of this program will be? Comment on your output. ... Program #3 main() { fork(); fork(); printf(Hello world\n); } Can you guess what the output of this program will be? Comment youre your output. . . . . ............................................................................................................................

Compiled by: Pradip Maharjan and Nar kaji Gurung

OS Lab

3.5 Demonstrate child and parent processes and pid


The new process created by fork is called the child process. This function is called once but returns twice. The only difference in the returns is that the return value in the child is 0 while the return value in the parent process is the process ID of the new child. The reason the childs process ID is returned to the parent is because a process can have more than one child, so there is no function that allows a process to obtain the process Ids of its children. The reason fork returns 0 to the child is because a process can have only a single parent, so that the child can use getppid() to get the process ID of its parent. Both the child and parent continue executing with the instruction that follows the call to fork. The child is a copy of the parent. For example, the child gets a copy of the parents data space, heap and stack. This is a copy- the parent and child do not share these portions of memory.
Program #4 //Program to demonstrate child and parent processes and pids. main(){ int pid;

pid=fork(); if(pid==0){ printf(I am the child, my process ID is %d\n, getpid()); printf(The childs parent process id is %d,getppid()); }else{ printf(I am the parent, my process ID is%d\n,getpid()); printf(the parents parent process ID is %d\n,getppid()); }} Can you guess what the output of this program will be? Comment youre your output.
... ... .. .. ..

3.5 Terminating processes


d. The process can be killed or terminated by using kill signal. e. To kill all jobs, use killall $killall -9 cat >> this will kill all processes with name cat. Compiled by Gurung Nar Kaji @apexcollege 3

OS Lab

Compiled by: Pradip Maharjan and Nar kaji Gurung

You might also like