0% found this document useful (0 votes)
26 views22 pages

Book 12 Oct 2024

for bca students

Uploaded by

Parv Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
26 views22 pages

Book 12 Oct 2024

for bca students

Uploaded by

Parv Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
92 “Understanding The Process ~ simultaneously has to manage several activities at one time and each ‘system's functioning. Therefore, it is very important to understand and appreciate for'a processor. To put’ this another way, you can say that th corresponds to one process. A process will need ‘certain feeeres arc as CPU » - iy salad time, memory, files and V/O devices to accomplish its task. These resources are oe ” x : BC allocated to the process either when it is created, or ee itis executing. Process Management manages the allocation of process (tasks or Jobs) toa processor. The concept of process is central to the understanding of opera this concept fully: - i we In amultiple program environment, a single program does: ne have coe control of the system. This gave rist to the concept of ces The term “Process! has ‘ E saeoosd | (a) been given many definitions. Some of ee defritons follows :_ rogramming system, execu many procedées simultaneous} a name, usually the name of th: }System, more than one process can run at a time. For example p command, a process named grep is created. In se not the shell) is responsible form the management of all thesa mel recognises a process by a number called process identification Pid). determines the time and priorities allocated to them so that multiple e 5 are able to share CPU resources. 1e program being executed. Since ‘ 5 use the memory of a system. When memory is full, the kernel moves the eee data of these processes to the swap area. This swap area is usually a ‘Separate file system on the disk. ee? Management of processes is the responsibilty of the kemel and not the shell. The kere! does CPU. ‘scheduling for these Processes. The kernel frequently requies to store active Processes in the swap area of the disk before calling them , again for running. Bye ior, at . 5 has the following attributes or characterstics : ge) a1 q b Process Identification number or Process ID (PID) Eact process has unique number assigned by the kernal, This unique number [188] [admin@localhos' Try PID 3199 tty 3239 tty. {admin@localhost The parent process ID (PPID) . Every process has a parent process. and a con esp words, PPID is the number of the process that started: all processes share a parent/child relationship. The PP 0 be displayed by ps command with -f option. Example Sps -f [admin@localhost’ ~] $ ps -£° © admin 3199 2875 0 16:27 tevenate IC | UID PID PPID C STIME rye | . | admin 3246 3199, 0 16:31 tty6 I [admin@localhost ~] $ with which it runs. The kemel's process scheduler uses this value | ") ine the process that has to run next. n linux, all processes share a parent-child relationship. ‘Process can create several new Processes, via a create-process system call, ring the course of execution. If it does, the creating process is called the Parent ‘ 4nd the created process is called the child process. The act of creating a is often called spawning a process. Only one parent is needed to | Bchild: Such creation yields a hierarchical process structure like that in fgure.9.1 in which each child has only one parent, but each Parent may 1190) 2 = FIGURE 9.1 Anew process is created because ; itself. This child process has the same oman 1D number is different. This Procedure is “After the forking process, the address space of the the new process data. This done through an exec. 3 Special Processes There are three special processes in almost al (i) Swapper (or scheduler) The process with pid O is called a process in any Linux system, but does nott It is the only process not created by the a Gil) 9.2.4 Process States and Their Transitions When a process is bom, its life in the system begins. During its existence, a process 908s through a series of discrete states or we can Say that as a process executes, it changes state, Each process may be in one of the following states : w Init Process with pid 0 spawns (or forks) a process called init with a pid 1. This the parent of all processes. This process exists on the system as long as Linux is running. Main functions of this process are : © setting up, ie. initializing all subsequent processes 6n the system. © setting up the system in single-user or multiuser mode. ® — Creating or spawing login shells for the users, Pagedaemon A Process with pid 2 is called pagedaemon, This process is invoked in case Of virtual memory implementation. . New (The process is being created.) Wherever a user types a Program to execute, the O/S attempt to find this Program and, if successful, the program code will be loaded and a system call Used to generate a process Corresponding to the execution of the program. [192] (i) Ready : (The process is waiting to be assigned to a processor.) There are many process in the memory which may want fo execute but there is one procesada which can execute only one process at a time. So other processes must have, to wait for the allocation of processer. A process which is ready to execute by the processor Is said to be in ready state. (lily Running (Instructions are being executed.) The C.P.U. is currently allocated to the an and the process is in execution. (iv) Waiting/Blocked State (The process is waiting for some event to occur (such as an /O completion or reception of a signal)). A process comes inthe blocked state if the last event of interest to the process was a request made by it to the system. The request is yet to be fulfilled, hence the process cannot execute even if the C.P.U. is available to it. (v) Terminated The process has finished execution. Figure 9.2 shows state transitions of a process. [193] Some Possible reasons for these transitio 0 qi) (iil) FIGURE 9.2 Ready to Running B F A process is selected out ofthe ready queue (i. scheduled to be run) andis dispatched to the running state. i Running to Waiting yA po VO operation is infact the most common reason for this type of transition, Here, since a process is waiting for /O completion and thus do not need CPU forexecuting any other instructions, CPU isfreedby ttand cPU inthe meantime takes up some other ready process and starts its execution. Walting to Ready ‘The reason for a process to remain in waiting state is no longer applicable eg, VO completes, timer goes off etc. Thus, the process becomes ready for its execution. (194) | (iv) Running to Ready a iat The running process is pre-empted because some other process of higher priority has become ready. rvoenged br 93 . Birth and Death eab Aprocess is said to be born when the program starts execution, and remains alive as long as the program is active. After execution is complete, the process is said to die. Ever process has a parent, just like a file has a parent. This mae ‘another Process, anda process bom from it is said to be its child. When a child process completes, it ore. @ signal to its parent informing it of its death. Control is thus reverted to the parent that can then give birth to other _ processes. Example r ermnotni air ese ist <9" When you run the following command,.; 23 Yi ae § eat adita aio vote bho then a process representing the cat commandis started by the sh process. In this case, sh is parent of cat and catis the eile of sh. The cat. a remains active "as long as the commandis active. 9.4 Mechanism of Process Creation i As you known that a process is just a program that is running. The process contai the binary code to be executed, along with data that would be required forthe program ins wstorun. ‘i aol elnaiiae chest enobemell by the kemel, runs in its own user address space. [195] The process image, as viewed ‘This address space has following segments : (a) (b) () Text Segment This segment contains the executable code of a program. In multiuser environment, same program may be used by many user. Therefore, this area remain fixed. Data Segment This segment contains all variables and arrays that have values assigned to them during program execution. User Segment This segment contains all the process attributes that was discussed earlier. The kemal uses this information to manage all processes. There are three difference phases in the creation of a process using three important system calls, namely, fork( ), exec( ) and wait () () Fork Anew process is created because an existing process makes an exact copy of itself. This child process has the same environmentas its parent, only the process ID numbers different. This procedure is called forking, When a process is forked, it inherits the environment of its parent i.e. it has the same group id, user id, current directory, priority etc. Therefore, the forking mechanism is responsible for the multiplication of processes in the system. [196] (i) EXEC 8 - After the forking process, the address space of the child process is ‘overwritten with the copy of the program that has to be executed. This Is done through an exec call to the system. No additional process is created here but, the existing program text and data areas are simply replaced. with the ones of the new program. This process has the same PID as the child that was just forked. . (ii), WAIT The parent then executes the wait system call to keep waiting for the child process toe complete. The child sends a termination signal to the parent after completion of its execution. The parent is then free to continue with its other functions. 9.5 The init Process The kernel, once itis loaded, finds init in sbin and executes it. The init process has the PID 1. When init starts, it becomes the parent or grandparent of all of the processes that ~ start up automatically on your Linux system. The first thing init does, is reading its initialization file, /etc/inittab. This instruct init to read an initial configuration script for the environment, which sets the path, starts swapping, checks the file systems, and so on. Basically, this step takes care of everything that you system needs to have done at system initialization: setting the clock, initializing serial ports and so forth. [197] ‘Then Init continues to read the /etc/inittab file, which describes how the system ___ should be set up in each run level and sets the default run level. A run level is a * configuration of processes. The Linux system can be run in different process configurations, such as the single user mode or multiuser mode. ‘The sequence of steps or processes leading to the shell are : © Tho ‘init’ process is responsible for spawning or creating a process called ‘getty’on every terminal port. Itis getty that Seis the por’ initial characteristics like speed and parity and provides a login prompt on the screen. ‘¢ Each one of these gettys prints the login'prompt 6n the respective terminal and then goes off to Sleep. ‘ ¢ ~ Auser enters hismher login name at getty's prompt.’ © When a user attempts to log in, getty wakes up and execs the login program to verify the login name and password entered, : N © ~ Login then executes a shell program. The shell executes the appropriate. startup files. ‘ © The shell prints the Linux prompt and waits for input. ©, Suppose a user enters a'grep' command to execute. Shell forks its copy and @xecs ‘grep’ program if it is able to find it on the specified PATH. Init, now the only living ancestors of the shell, goes off to, sleep, waiting for the death of this children. When the user logs out, her shell is killed, and the death is Intimatted to [Link] then wakes up and spawns another getty for that line to monitor the next login, The sequence of event in a complete login can be shown as : : , FIGURE S.3 In summary, the Init process is fesponsible for... neo uoy {8 (a) Setting up 1.6. initializing all subsequent processes on the system. (b) Setting up the system in single-user oF, multiuser mode. » {c) Creating or spawing login shells forthe USCIS, wise rity §, Yu wey wantto terminate a you can terminate a process using the kill aren Yo process because the terminal is hung; pou has gone in an infinte loop ordue to “t00 many background jobs. Syntax : a “s $ kill [-signal] [process Id] [199] Example = fa) TTo terminal the process with process id 97, command is $ kill 97 (©) Thecommand $ kill -9 process 1d gives the guarantee that the process would be killed. Itis stronger signal calleq SIGKILL. (ce) Thecommand $ kill oO terminates all current process ‘except your shell. (d) you can kill your login shell by using any of these commands. @ Skill -9 $$ # $$ stores PID of current (i) $kill -9 0 # kills all processes including login shell (iii) $kill -1 0 # kills all processes including login shell 9.7 _ Resetting the Process Priorities The priority of a process is decided by a number associated with it. This number is called ‘nice’ value of the process, The nice value of a process can range from Oto 39, with 20 as the default nice value of a process. A user can lower the Priority of hig Job using the nice command. Note that higher the nice value of a process means lower is its priority, {200} Example The command $ nice -15 we cat reduce the priority by 15 units, default value is 20, therefore, nice value now becomes g9. You can see the nice values of the various processes running in memory using ps-l. ----=-=-=, | [ndmin@locaihest -] § ps -1 { | \Fs UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME ar los 500 3199 2875 0 75 0 - 1158 wait tty6 [Link] asa} lor 500 3254 3199 0 77.0 - 1046 - tty6 [Link] ps [admin@ localhost ~]$ } EY? In the above output the column with the heading NI denotes the nice value of a process. 9.7.1 Scheduling of processes (or Job control) In a multiprogramming system, at any given time, several processes will be competing for the CPU's time. At any instance, ‘only one process will be in the running state while others will be in ready or waiting state. The operating system determines how to allocate processor time among all the ready processes. This allocation in referred as scheduling. Scheduling is a fundamental operating system, function. [201] ‘Scheduling refers to a set of policies and machanisms built into the operating system that define the order in which the computer system works. Scheduler salecis the next ob to be admitted in the system for processing. Linux provides sophisitiated facilities to schedule a job to run at a specified time of day. If the system load varies greatly throughout the day, it makes sense toschedule less urgent jobs at a time when the system overheads are low. 1 ‘Some main job scheduling commands are at, batch, cron and time. 98 at Command a ee, Purpose : This command is used to execute the Linux commands at the specified time. The at command takes time as its argument. Normally inputhas to be supplied from the standard input. {admin@localhost ~] § at 17:00 at> echo It is backup time \ | | I | | | ! | | at> “d (Ctrl.a) | | Job 2 at 2J12-01-03 17:00 I rr [admin@localhost ~]$ | mS pees = ey Observe that on pressing Ctrid the at command displayed the job-id and the date and time of scheduled execution. Once submitted this way, the message would be @choed on out terminal at 10.00 A.M sharp. [202] Syntax : eal 5 _at’ {-£ '€ilename] (-m] time [date] [-z] I-] Specifies the file that contains the command to run. Sends mail once the command has been run. I Specifies at what time you want the command to be ran. Format hh:mm am / pm or a 24-hour clock is used. The below quick times can also be entered : Options -f filename -m midnight -- indicates the time 12:00 am (00:00). noon - Indicates the time 12:00 pm. now - Indicates the current day and time. ‘Specifies the date you wish it to'be ran on. Format is month, day, year. The following quick days [Link] be. __ entered: today... - Indicates the currant dy. tomorrow -- Indicates the day following the current day. Lists the command that have been settorun. __.. Cancels the command that you have set in the past. . [admin@localhost___-] $ at_-m 11:30 < myfile | job 4 at 2012-01-04 11:30 !%2!/=tr>= ! \ You have new mail in /var/spool/mail/admin | | | This command runs the cornmriands listed in the ‘myfile' at 11:30 AM. when we press enter key a jobnumber, date and time is ‘speciied to this job. 7 [203] Example : This command lists each of the scheduled jobs as shown below : CEA ib... a a I [admin@localhost -] $ at -1 1 ' 2 2012-01-03 17:00 a admin ql | 4 2012-01-06 11:00 a admin i ! | [admin@localhost ~]$ The command to delete job number 4 is [admin@localhost -] § at -r 4 9.9 batch Command a SSSFSSSSSSSSSSSSSSsSsss ‘The batch command is similar to at command. In this command, we do not Specify the exact time to execution of our commands (or job) but the system decides the best time for executing our commands (or job). When we submit our jobs using this command, Linux executes our job when it is felatively free and the system load is light. The batch command does not take any arguments. {admin@localhost ~] $ batch at> [Link] ! | | ! at> *d (Ctrl d) job 5 at 2012-01-03 15:58 | | | | [admin@localhost ~]$ - Note that on pressing (Ctrl d) the batch command displayed the Job Id, date and time of submitted job. g10_crom Command The ‘cron’ is a system deamon that executes commands at Specified times repeatedly. The command and the scheduling information are appended in a file ‘erontabs' available in ‘/var/spool/cron' directory’. ‘cron’ daemon wakes up (every minute) to check for jobs or commands to be executed at this time crontabs file. After executing them, it goes back to sleep, only to wake up the next minute. ach command (or line) in crontab file has the following entries = (2) Minute (0-59) (b) hour (0-23) (c) day of the month (1-31) (d) month of the year (1-12) (e) day of the week (0-6 with 0 means sunday) (f) command A“ can be used in any of the first five fields means run for all values of the field. Example : 0 17 * *. * [Link] This line means run script [Link] at 9.00 p.m everyday. Example : Create a myfile having following contents [205] (( waaineiocaincst =] Teese Shyfties ost t © 10 1 1 © echo Happy New Year‘ F ; ' © 27 + * © [Link] i Pa gc, Denes ct, Ot ! [admin@localhost . ~]§ oF | ee ne I i wi Now, execute the crontab command using the myfile as: admin@localhost . .~] §. crontab myfile This contents of myfile are automatically transferred to the var/spooV/cron/crontabs directory where they are stored in a file which has the same name as your login name. There onwards the eren deamon will read this file automatically and execute the commands present in it on a regular basis. The content of current crontab can be displayed on standard output using the following command at admin@localhost ~] $ crontab:-1'° The current crontab tan be removed using the following command | ° admin@localhost ~-] $ crontab -r This command is mainly used to perform housekeeping tasks, like removing outdated files or collecting data on system perform housekeeping, like removing outdated files or collecting data on system performance. It's also extremely Useful to periodically dial up to an Internet mail server to send and retrieve mail: [206]: 9. jl time:Command : $$$ This command Is used to determine the core no ‘of execution of a particular command, = i To use the command: simply precede any command by the word time, such as: Example : ie [admin@localhost ~] § time 1s | ! abo Desktop’ fruit | Rahul _sumit tree | Bl | go output passwd sachin kavita nitin 1 ! cut cut List Misti prgl—PFg22 } | prgl4 second sort’ ©’ sorti © wel ‘we3 1 real 0m0.18s ' | user 0m0.001s _, : ; 13 | | ays 0n0.0038 Hn | [admin@localhost —18 an : | XN When the command completes, time will report how long it took to. execute the Is command in terms of user CPU time, system CPU time, and real time. Example : i yen a [admin@locaihost ~}] $time 1s sf apple Banana ‘ _ Grapes Pineapple real 0m0.0095 user 0m0.001s sys 0m0.002s [admin@localhost ~] $° eS EE ts ‘The real time shown is the clock elapsed time from the invocation of the command till its termination. The user time shows the time spent by the program in executing itselt, while sys indicates the time used by the Linux system in doing work on behalf of the user. The sum of the user time and the system time actually represents the CPU time. Exercise —$ $< — oo Define Process. Explain the various attributes of a process. Explain spawning and forking process. Explain various process states and their transitions. 5. _ Explain the mechanism of process creation. 6. Explain the init process. What are its responsibilities? - Pp yp nm 7. Which command can you use to terminate a process? Explain it. 8. Which command can you use to reset the process priority? Explain It. 9. Explain various job ‘scheduling commands. 10. Explain at command with its options. 11. Explain batch, cron, and time command by using example. Qaq0 [208 )

You might also like