Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS:414 INTRODUCTION TO UNIX AND LINUX Part 4: Processes, signals and Filters By Dr. Noman Hasany.

Similar presentations


Presentation on theme: "CS:414 INTRODUCTION TO UNIX AND LINUX Part 4: Processes, signals and Filters By Dr. Noman Hasany."— Presentation transcript:

1 CS:414 INTRODUCTION TO UNIX AND LINUX Part 4: Processes, signals and Filters By Dr. Noman Hasany

2 Course Information Prerequisites: CSC 229 Credit Hours : 3 Lecture Hrs:2 Lab Hrs:2 Level:9 Course description: User/kernel interface, fundamental concepts of Unix, user authentication, basic and advanced I/O, file system, signals, process relationships, and inter-process communication. Fundamental development and debugging tools such as "make" and "gdb" will also be covered. Textbook: W. Richard Stevens, Advanced Programming in the UNIX Environment, Addison Wesley Professional, 1992.

3 Topics Distribution Part-1: Unix introduction and simple commands Part-2: File System Part-3: Regular Expressions and vi editor Part-4: Processes, signals and filters Part-5: Shell Scripting Part-6: Programming Tools

4 Topics Shell environment variables Signals Nohup, top and nice commands Pipes/filters Daemon Processes

5 Environment of a Process A set of name-value pairs associated with a process Keys and values are strings Passed to children processes Cannot be passed back up

6 Shell Variables Shells have several mechanisms for creating variables. A variable is a name representing a string value. Shell variables can save time and reduce typing errors, variables Allow you to store and manipulate information – Eg: ls $DIR > $FILE Two types : local and environmental – local are set by the user of the shell itself – environmental come from the operating system and are passed to children processes

7 Shell Variables Syntax varies by shell – name=value # sh, ksh – set name = value # csh To access the value: $var Turn local variable into environment: export variable

8 Environmental Variables NAMEMEANING $HOMEAbsolute pathname of your home directory $PATHA list of directories to search for $MAILAbsolute pathname to mailbox $USERYour user id $SHELLAbsolute pathname of login shell $TERMType of your terminal $PS1Prompt

9 The PATH environment variable Colon-separated (:) list of directories. Print PATH echo "$PATH" Non-absolute pathnames of executables are only executed if found in the list. – Searched left to right Example: $ myprogram sh: myprogram not found $ PATH=/bin:/usr/bin:/acct/s1/dosndonts/bin $ myprogram hello!

10 Modifying PATH In bash, the following command adds /new/path to PATH $ export PATH=$PATH:/new/path $PATH refers to the old path. Note: The earlier entries in the path take precedence over the later ones. If you want the directories you add to your path to take precedence, in the examples above, replace $PATH:/new/path with /new/path:$PATH.

11 Modifying PATH For modifying PATH to be effective for the next login or now, following steps are required to be followed: Edit the bash profile (which is loaded at start of the bourne shell), using one of the following:  vi $HOME/.bash_profile  vi ~/.bash_profile Append the following export command and save the file:  export PATH=$PATH:/new/path Apply the changes for now, using one of the following:  source $HOME/.bash_profile . $HOME/.bash_profile

12 Managing Processes ps : lists the processes running on the machine. ps -u username lists only your processes. ps -a : lists all processes running on the machine. The PID column of the listing, provides the information required by the kill command. top : a more detailed method of observing processes. nice : runs a process with a lower priority. ALWAYS use this if you are running a process that will take a long while (hours or days). nice doesn’t slow down your process much, but allows the interactive aspects of the computer (GUI, etc) to take priority. This makes system administrators and other users happy. nohup : keeps a process running even after you log out

13 ‘top’ command The top command is a very useful tool for quickly showing processes sorted by various criteria. It is an interactive diagnostic tool that updates frequently and shows information about physical and virtual memory, CPU usage, load averages, and your busy processes. Here is simple syntax to run top command and to see the statistics of CPU utilization by different processes:

14 Command: top $top: to view the CPU usage of all processes:

15 nice nice is used to invoke a utility or shell script with a particular priority, thus giving the process more or less CPU time than other processes. The exact mathematical effect of setting a particular niceness value for a process depends on the details of how the scheduler is designed on that implementation of Unix. A niceness of −20 is the highest priority and 19 or 20 is the lowest priority. The default niceness for processes is inherited from its parent process and is usually 0. A process run with nice +15 will receive 25% of the CPU time allocated to a normal-priority process: (20−15)/(20−0) = 0.25

16 nohup nohup is a command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout. The first of the commands below starts the program xyz in the background in such a way that the subsequent logout does not stop it. $ nohup xyz & $ exit

17 Job ID Background and suspended processes are usually manipulated via job number (job ID). This number is different from the process ID and is used because it is shorter. In addition, a job can consist of multiple processes running in series or at the same time, in parallel, so using the job ID is easier than tracking the individual processes.

18 ‘bg’ and ‘fg’ commands bg is a job control command which resumes suspended jobs without bringing them to the foreground. bg %1 The stopped job will resume operation, but remain in the background. It will not receive any input from the terminal while it's in the background, but it will keep running, and you can continue to use the shell from the command line. the fg command continues a stopped job by running it in the foreground.

19 Signals Signal: A message a process can send to a process A signal is sent to a process via another process (child to parent), or by kernel. Signals are interrupts that are generated by the kernel and other processes. Signals are used to terminate, suspend, and continue processes. Message type represented by a symbolic name

20 Signals For each signal, the receiving process can: – Explicitly ignore signal – Specify action to be taken upon receipt (signal handler) – Otherwise, default action takes place (usually process is killed) Each signal is associated with a number and a name. 1. SIHGP - Hangup 2. SIGIT - Interrupt 3. SIGQUIT – Quit 9. SIGKILL – Definite kill 15SIGTERM – Software termination

21 Daemon Processes Daemons are system-related background processes that often run with the permissions of root and services requests from other processes. A daemon process has no controlling terminal. It cannot open /dev/tty. If you do a "ps -ef" and look at the tty field, all daemons will have a ? for the tty. A daemon is just a process that runs in the background, usually waiting for something to happen that it is capable of working with, like a printer daemon is waiting for print commands. If you have a program which needs to do long processing then its worth to make it a daemon and run it in background.

22 Pipes Provide a way for interprocess communication (IPC) in shells a '|' symbol used it means that the output of one program (on one side of a pipe) serves as an input for the program on another end. a set of "piped" commands is often called a pipeline It is useful because by combining simple OS utilities one can easily solve more complex tasks

23 Pipes and filters A filter is a program that can remove input given a certain criteria. It, in effect, "filters" out things that either match or do not match, thus giving less information (it has been filtered out). A pipe is a software construct that allows for information to be communicated between processes. (just as a water pipe connecting a community, for example.) Pipes can be utilized as well to "chain" together multiple filter programs to create only the required information. A traditional pipe in Unix connects the output of one process to the input of another.

24 Pipes Output of one program becomes input to another – Uses concept of UNIX pipes Example: $ who | wc -l – counts the number of users logged in Pipelines can be long

25 Pipes (without) Run first program, save output into file Run second program, using file as input Unnecessary use of the disk – Slower – Can take up a lot of space (eg: ls -R followed by wc) Makes no use of multi-tasking process 1process 2

26 Pipes and redirection Pipe is used to send the output to other command whereas to redirect is used to redirect the output to some file. $ who > current_users $ grep “sumair" < current_users

27 Pipes and redirection (An Extra Process) cat command $ cat file | command $ command < file

28 Pipes Most Unix programs can be used to form pipes. Some programs that are commonly used as filters are: cat grep less more head/tail sort Note that these programs aren't used only as filters or parts of pipes. They're also useful on their own.

29 cat: The simplest filter The cat command copies its input to output unchanged (identity filter). When supplied a list of file names, it concatenates them onto stdout. Some options: – -n number output lines (starting from 1) – -v display control-characters in visible form (e.g. ^C ) cat file* ls | cat -n

30 head Display the first few lines of a specified file Syntax: head [-n] [filename...] – -n - number of lines to display, default is 10 – filename... - list of filenames to display When more than one filename is specified, the start of each files listing displays.

31 tee Copy standard input to standard output and one or more files – Captures intermediate results from a filter in the pipeline – Note: tee writes output to both the screen (stdout) and the file. Redirect does only the file. Unix Command Standard output file-list

32 tee Syntax: tee [ -ai ] file-list – -a - append to output file rather than overwrite, default is to overwrite (replace) the output file – -i - ignore interrupts – file-list - one or more file names for capturing output Examples ls | head –10 | tee first_10 | tail –5 who | tee user_list | wc

33 sort: Options Syntax: sort [-dftnr] [-o filename] [filename(s)] -dDictionary order, only letters, digits, and whitespace are significant in determining sort order -f Ignore case (fold into lower case) -n Numeric order, sort by arithmetic value instead of first digit -r Sort in reverse order -ofilename - write output to filename, filename can be the same as one of the input files Lots of more options…

34 uniq: list UNIQue items Remove or report adjacent duplicate lines Syntax: uniq [ -cdu] [input-file] [ output-file] -d Write only the duplicated lines -u Write only those lines which are not duplicated – The default output is the union (combination) of -d and –u -c Supersede the -u and -d options and generate an output report with each line preceded by an occurrence count

35 wc: Counting results The word count utility, wc, counts the number of lines, characters or words Options: -l Count lines -w Count words -c Count characters Default: count lines, words and chars

36 Examples sort file | uniq | wc –l sort file | uniq –d | wc –l sort file | uniq –u | wc -l

37 Examples In the following command with pipes, sorts all files in your directory modified in August by order of size, and prints them to the terminal screen. The sort option +4n skips four fields (fields are separated by blanks) then sorts the lines in numeric order. $ls -l | grep "Aug" | sort +4n -rw-rw-r-- 1 carol doc 1605 Aug 23 07:35 macros -rw-rw-r-- 1 john doc 2488 Aug 15 10:51 intro -rw-rw-rw- 1 john doc 8515 Aug 6 15:30 ch07 -rw-rw-rw- 1 john doc 11008 Aug 6 14:10 ch02 $


Download ppt "CS:414 INTRODUCTION TO UNIX AND LINUX Part 4: Processes, signals and Filters By Dr. Noman Hasany."

Similar presentations


Ads by Google