420 likes | 564 Views
Learn about Unix session management, terminal control, job control, process groups, foreground and background processes, I/O redirection, signal handling, and shell program customization in Unix and Shell programming.
E N D
Unix and Shell Programming Department of Computer Science and Engineering
Unit-2 Shells- UNIX Session, standard streams, redirection, pipes tee Command, Command Execution andSubstitution, Command-Line Editing, job control, Aliases, Variable Types and options, Shell Customization. Filters and Pipes – related Commands. Commands for Translating Characters, Fileswith duplicate Lines, Counting characters, words and Lines and Comparing files
Process groups • A process group is a collection of (related) processes. Each group has a process group ID. • Each group has a group leader who pid = pgid • To get the group ID of a process: pid_t getpgrp(void) • A signal can be sent to the whole group of processes.
Process groups: • A process may joint an existing group, create a new group. int setpgid(pid_t, pid, pid_t, pgid) • A process can set group ID of itself or its children • _POSIX_JOB_CONTROL must be defined • Most shells with job control create new group for each line of command (job).
Sessions • A session is one or more process groups proc1 | proc2 & proc3 | proc4 | proc5 • results in a session with three groups, see ‘ps –j’ • A login shell is a session in general. proc1 proc3 Login shell proc2 proc4 proc5
Session • A session can have a single controlling terminal • Terminal device for a terminal login • Pseudo-terminal device for a network login • The I/O devices somewhat link to the window and keyboard. • The session leader that establishes the connection to the control terminal is called the controlling process.
Session • Only one I/O device for all processes (and process groups) in a session. Which process should get the input from the keyboard? • Foreground and background process • One foreground group • Many background groups • Input • Only foreground group • Terminals’ interrupt signals are only sent to the processes in the foreground group. • Output • Typically shared
Sessions • To establish a new session: pidsetsid(void); • Process become the session leader • Process become a new group leader of a new group • Process has no controlling terminal (break up the old one) • Each shell is a session. When a shell is created, a terminal must be setup. • Fails if the caller is a group leader. • A process can open file /dev/tty to talk to the controlling terminal regardless how standard IO are redirected. • A way to by pass I/O redirection, see example1.c
How to make a group foreground and background? • So that the terminal device driver knows where to send the terminal input and the terminal-generated signals. pid_t tcgetpgrp(int filedes); Return the process group ID of the foreground process group associated with fieldes. int tcsetpgrp(int filedes, pid_t pgrpid); • If the process has control terminal, set the foreground process group ID to pgrpid. • Pgrpid must be group ID in the same session.
Job control • Allows start multiple jobs from a single terminal and control which job can access the terminal. • Foreground jobs can access terminal • Background jobs may not: • When a backgound job try to read, SIGTTIN signal is sent • A background job must be able to output to the terminal (options may be set by the stty command) • See control.c for an example of switching terminal among groups.
Orphaned process group: • Parent of every member is either in the orphaned group or is not a member of the group’s session. • Happens when a process forks a child and then dies. • The child becomes a member of the orphaned group. • Can have problems: the child may transform from a foreground process to a background process automatically. • Remember in the control.c program, foreground group is set in both the parent and the child. • Can be in an inconsistent state. • If any IO is involved, strange things may happen.
How to make sure that a shell program handles terminal I/O and signals correctly • Create a new group for each job • Both parent and child do setpgid • For foreground job: • After fork, shell set tcsetpgrp to give foreground jobs control over terminal • Shell waits for all foreground processes in the foreground job to finish. After that, shell set tcsetpgrp to itself and print the prompt. • For background job: • Create a separate group so that processes in background jobs do not have access to terminal.
UNIX Processes A PROCESS is a program that is executing. • Processes have Input and Output • There are two types of Processes • Foreground Processes • Background Processes
Foreground Process When running a FOREGROUND process, the shell waits for the program to finish When the process is finished, you will be returned to the command prompt /export/home/morris07>
Background Process When running a BACKGROUND process, the shell starts the process, then leaves • This allows you to execute other commands at the command prompt NOTE: Handy for programs that take a long time to run and do not need to run interactively.
Running Processes in Background Type an ampersand (&) after the command to run it in the background > find . –name java > javaLocations.txt &
Suspending Processes You can SUSPEND a process by typing ^Z This will pause the process temporarily. The process can be resumed in the Foreground or moved to the Background: /export/home/morris07> fg [jobid] /export/home/morris07> bg [jobid]
Displaying List of Suspended Jobs /export/home/morris07> jobs … [1] Stopped vi document [2] Stopped elm -l option gives more information
Displaying Status of Processes Use ps to report the status of a process /export/home/morris07>ps … PID TTY TIME CMD 19834 pts/7 0:05 ksh 19954 pts/7 0:04 ps
Killing a Process Use kill to terminate a process running in the background /export/home/morris07>kill {process id} This sends a signal to the process to end.
That Process Won’t Die!! The kill command waits for the process to perform cleanup: write to files, close files,etc. If the process will not end, use the ‘-9’ or ‘-KILL’ to kill the process immediately > kill –9 19954 > kill –KILL 19954
terminal command standard output Standard Input and Output keyboard command standard input
terminal command standard output redirected output file command Redirection and Pipes Redirecting Standard Output to a File with: ‘>’, ‘>>’ ls –al > mylist(creates or overwrites file) ls –al >> mylist(appends or creates file)
terminal command standard output command command redirected output Redirection and Pipes Redirecting Standard Output to a Command with: ‘|’ ls –al | more(passes output to command)
redirected input Redirection and Pipes Redirecting Standard Input from a File with: ‘<’ cat < file(passes file to command) keyboard command standard input file command
Filters A FILTER is any program that reads from Standard Input and writes to Standard Output. grep uniq look spell sort wc
Filters: grep The grep command searches for the pattern specified and writes these lines to Standard Output. grep [-cilnvw]pattern [file…] >grep ‘Easy’ assignments.txt
Filters: uniq The uniq command examines data, looking for consecutive, duplicate lines. uniq [-cdu][infile [outfile]] • uniq –d , retains one copy of all lines that are duplicated • uniq –u, retains only those lines that are not duplicated. • uniq –c, counts how many times each line is found. >uniq document.txt
Filters: look The look command searches data in alphabetical order and will find lines that begin with a specified pattern (alphabetical characters only). look [-df]pattern [file…] >look Amer * Access the dictionary of correctly spelled words. • Look is not really a filter and cannot be used within a pipeline. • File must be pre-sort file with –dfu options. I.e dictionary, fold, unique.
Filters: spell The spell command will read data and generate a list of all words that look as if they are misspelled. This is a very primitive spell checker. spell [file…] >spell document.txt
Filters: spell • For example, • Mispeel was not flagged • Adds standard prefixes and suffixes to words in dictionary
Filters: sort The sort command sorts data (using ASCII format). sort [-dfnru] [infile…] [-o outfile] >sort names -o sorted_names or >sort names > sorted_names
Filters: wc The wc command counts lines, words, and characters. wc [-lwc][file…] >wc -l document.txt
wc options -c Count bytes. -m Count characters. -C Same as -m. -l Count lines. -w Count words delimited by white space characters or new line characters.