Full Transcript

**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (7) Date 04-05-2024** **Part 1:** **[Process Management:]** **Description:** The students take from this exercise what is a process, how to manage Foregr...

**University of Science and Technology** **Faculty of Computer Science and Information Technology** **Open Source Operating Systems** **Lab (7) Date 04-05-2024** **Part 1:** **[Process Management:]** **Description:** The students take from this exercise what is a process, how to manage Foreground and background processes, and the the job control commands. **Lab Learning Outcomes:** - The ps command - Managing LNUIX processes - Killing processes - job control commands. **Lab Instructions:** The lab instructor has to follow the steps below to demonstrate to the students the following:. 1. **What is a Process:** A **process** is a program in execution. Every time you invoke a system utility or an application program from a shell, one or more \"child\" processes are created by the shell in response to your command. All LINUX processes are identified by a unique process identifier or **PID**. An important process that is always present is the init process. **Process types:** 1. Interactive processes. 2. Automatic processes. 3. Daemons. 2. **Displaying process information:** The **ps** command is one of the tools for visualizing processes. This command has several options which can be combined to display different process attributes. **Example:** Use **ps command** only to give information about the current shell and eventual processes: \$ **ps** **Output:** PID TTY TIME CMD 4245 pts/7 00:00:00 bash 5314 pts/7 00:00:00 ps 3. **Controlling processes associated with the current shell:** Most shells provide sophisticated job control facilities that let you control many running jobs (i.e. processes) at the same time. This is useful if, for example, you are editing a text file and want of interrupt your editing to do something else. With job control, you can suspend the editor, go back to the shell prompt, and start work on something else. When you are finished, you can switch back to the editor and continue as if you hadn\'t left. A process will have one of three different statuses: \(1) It may be in the foreground \(2) It may be in the background \(3) Or it may be stopped (suspended There can be only one job in the foreground at any time. The foreground job has control of the shell with which you interact - it receives input from the keyboard and sends output to the screen. Jobs in the background do not receive input from the terminal, generally running along quietly without the need for interaction (and drawing it to your attention if they do). The foreground job may be suspended, i.e. temporarily stopped, by pressing the **Ctrl-Z key**. A suspended job can be made to continue running in the foreground or background as needed by typing \"**fg**\" or \"**bg**\" respectively. Note that suspending a job is very different from interrupting a job (by pressing the interrupt key, usually **Ctrl-C**); interrupted jobs are killed off permanently and cannot be resumed. Background jobs can also be run directly from the command line. **Example:** 1. by appending a \'&\' character to the command line run the command : **    find / -print 1\>output 2\>errors\ **    \$ find / -print 1\>output 2\>errors &   **output:\ **    \[1\] 27501 \     \$ 2. Here the \[1\] returned by the shell represents the job number of the background process, and the 27501 is the PID of the process. 3. If you have more than one job you can refer to the job as %n where n is the job number. **Example:** \$  fg %3  resumes job number 3 in the foreground. 4. To find out the process ID\'s of the underlying processes associated with the shell and its jobs, use ps (process show):     \$ ps  \       PID TTY          TIME CMD \     17717 pts/10   00:00:00 bash \     27501 pts/10   00:00:01 find \     27502 pts/10   00:00:00 ps So here the PID of the shell (bash) is 17717, the PID of find is 27501 and the PID of ps is 27502. 5. To terminate a process or job abruptly, use the kill command. Kill allows jobs to refer to in two ways - by their PID or by their job number. So \     \$ kill %1 \ or \     \$ kill 27501 would terminate the find process. Actually kill only sends the process a signal requesting it shutdown and exit gracefully (the SIGTERM signal), so this may not always work. To force a process to terminate abruptly (and with a higher probability of success), use a -9 option (the SIGKILL signal):      \$ kill -9 27501 4. **Job control commands:** To allow managing jobs shells provides a set of job control commands as shown below: Command Effect Ps process show & running command in background CTRL-Z Suspend current process CTRL-C Terminate a job running in foreground bg Resume stoped job in background fg Resume stoped job in foreground Jobs List all jobs (stopped&in background) Kill Terminate a job running in background stop Stop execution of a job **Examples**: 1. To see a list of all the jobs associated with the current shell, type jobs:     \$ jobs  \     \[1\]+  Running  find / -print 1\>output 2\>errors &  \$ kill %1 Terminate job ID1. \$ fg %2 Resume job ID2 in foreground. \$ stop %find Stop find command 2. Re execute jobs command     \$ jobs   **Part 2:** **[Shell variables]** **Description:** The students take from this exercise the variables that When you start a shell (by logging in or opening a Terminal window), are already set. Here are some variables that are either set when you use a bash shell or that can be set by you to use with different features. **Lab Learning Outcomes:** The Common Shell Variables. **Lab Instructions:** The lab instructor has to follow the steps below to demonstrate to the students the following:. 1. **Types of shell Enviromental variables:** The shell variables are in uppercase characters by convention. Bash keeps a list of two types of Environment variables: - **Global variables:** Global variables or environment variables are available in all shells. The **env** or **printenv** commands can be used to display environment variables. These programs come with the *sh-utils package.* **Syntax:** \$ env - **Local variables:** Local variables are only available in the current shell. Using the set built-in command without any options will display a list of all variables (including environment variables) and functions. displayed by the **set** command: **Syntax:** \$ set 2. **Getting the value of the shell variable:** To Print out the value of a shell variable use the echo command. And by preceding a \$ to the variable name. **example**: \$ echo \$PATH The dollar (\$) tells the shell to insert the variable's value into the command line. 3. **Defining your own variables:** Although most of the environmental variables are set automatically by the system you can define a shell variable by typing its name followed by = and its value **example**: \$ echo \$PS1 (before defining) \$ PS1 = + \$ echo \$PS1 (after defining) Variables are case sensitive and capitalized by default. Giving local variables a lowercase name is a convention which is sometimes applied. However, you are free to use the names you want or to mix cases. Variables can also contain digits, but a name starting with a digit is not allowed: prompt\> export 1number=*1* bash: export: \`1number=1\': not a valid identifier To set a variable in the shell, use \$ VARNAME=\"value\" - Putting spaces around the equal sign will cause errors. - Shell variables can be used to store temporary values Set a shell variable's value as follows: \$ PROJ = "/home/you/docs" \$ echo \$OROJ You can move files to that directory by typing from any where : \$ mv file \$PROJ 4. **Single and Double Quotes:** When assigning character data containing spaces or special characters, the data must be enclosed in either single or double quotes. Using double quotesto show a string of characters will allow any variables in the quotes to be resolved \$var="test string" \$newvar="Value of var is \$var" \$echo \$newvar Value of var is test string Using single quotes to show a string of characters will not allow variable resolution \$var='test string' \$newvar='Value of varis \$var' \$echo \$newvar Value of var is \$var 5. **Exporting variables:** In Bash, use export to export a shell variable into the environment:( to be used by the shell and the sub shell). \$ FILES=\"notes.txt report.txt\" \$ export FILES Or combine those into one line: \$ export FILES =\"notes.txt report.txt" A variable created like the ones in the example above is only available to the current shell. It is a local variable: child processes of the current shell will not be aware of this variable. In order to pass variables to a subshell, we need to *export them using the export built-in command. Variables that are exported are referred* to as environment variables. Setting and exporting is usually done in one step: export VARNAME=\"*value\"* A subshell can change variables it inherited from the parent, but the changes made by the child don\'t affect the parent. 6. **Where Programs are Found?** **The location of a program can be specified explicitly:**./sample runs the sample program in the current directory. /bin/ls runs the ls command in the */bin* directory. Otherwise, the shell looks in standard places for the program. The variable called PATH lists the directories to search in. Directory names are separated by colon, for example: \$ echo \$PATH /bin:/usr/bin:/usr/local/bin So running whoami will run */bin/whoami* or */usr/bin/whoami* or */usr/local/bin/whoami* (whichever is found first) 7. **Bash Configuration Variables** Some variables contain information which Bash itself uses. The variable called PS1 (Prompt String 1) specifies how to display the shell prompt. Use the echo command with a \$ sign before a variable name to see its value, **Example**: \$ echo \$PS1 \[\\u@\\h \\W\]\\\$ The special characters \\u, \\h and \\W represent shell variables containing, respectively, your user/login name, machine's hostname and current working directory, i.e., \$USER, \$HOSTNAME, \$PWD 8. **Changing Environment Variables:** The value of an environment variable can be set on the command line or in a configuration file as follows: \$ export VARIABLE=VALUE To see the current value of a variable: \$ echo \$VARIABLE The shell searches for programs to run in a list of directories in the variable \$PATH, which are separated by the ':' character If you want to run programs which aren't in */bin* or */usr/bin* then you might want to add them to your \$PATH. **example**: \$ export PATH=\"\$PATH:/usr/local/bin:/usr/games" **Changing the Prompt:** Setting PS1 will change the shell prompt, for example: \$ export PS1=':\\w\\\$ ' Characters preceded by \\ are given special interpretations, for example: \\t and \\d display the time and date in the prompt \\w or \\W show the current working directory \\\$ shows up as either \$ or \#, depending on whether you are a normal user or root \\u displays your username \\h displays the hostname of the machine PS2 is an alternative prompt, displayed when bash needs more input before it can run a complete command. **Removing an Environment Variable:** ------------------------------------- By removing an environment variable we can remove all existing component of particular variable. To remove an environment variable, execute the **unset** command followed by variable name: \$ unset variable name  \$ unset new\_variable    \$echo new\_variable The above command will delete the new\_variable variable and its components from the system. ** Common Environment Variables** --------------------------------- Some standard environment variables are as follows: - **PATH** This variable contains a list of directories in which our system looks for files. It separates directories by a (:) colon. - **USER** This variable holds the username. - **HOME** This variable holds the default path to the user\'s home directory. - **EDITOR** This variable contains the path to the specified editor. - **UID** This variable contains the path to the user\'s unique id. - **TERM** This variable contains the path to the default terminal emulator. - **SHELL** This variable contains the path to the default shell that is being used by the user. - **ENV** This variable displays all the environment variable. - **Exercise (To be solved in Classroom):** Use echo \$ var to display the above shell variables values.

Use Quizgecko on...
Browser
Browser