Shells, System Calls, and Signals

Slides:



Advertisements
Similar presentations
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Advertisements

CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
CS Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.
Process Process: the UNIX abstraction of a stand-along computer that manages resources (memory, CPU, I/O resources) comprising a running program. Processes.
Signals Hua LiSystems ProgrammingCS2690Signals. Topics: Sending Signals -- kill(), raise() Signal Handling -- signal() sig_talk.c -- complete example.
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Signal Signal : - is a notification sent to a process to notify it of some event - interrupts whatever the process is doing and force it to handle a signal.
CSSE Operating Systems
Shells, System Calls, and Signals. What is a Shell? A shell is a command line interface to the operating system – Fetch a command from the user and execute.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
University of Pennsylvania 9/12/00CSE 3801 Multiprogramming CSE 380 Lecture Note 3.
UNIX Signals Bach 7.2 Operating Systems Course The Hebrew University Spring 2010.
Today’s Topics Introducing process: the basic mechanism for concurrent programming –Process management related system calls Process creation Process termination.
1Reference “Introduction To Unix Signals Programming” in the reference material section Man page – sigprocmask, alarm “Understanding the Linux Kernel”
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Creating and Executing Processes
More on UART Interrupts; System Calls Reference on Interrupt Identification Register(IIR) slide 17 of
Agenda  Working with Processes: Purpose Running Programs within same process (execl, execlp, execle, execv, execvp, execve) “Spawning” other process (fork,
Lecture 24CS311 – Operating Systems 1 1 CS311 – Lecture 24 Outline Final Exam Study Guide Note: These lecture notes are not intended replace your notes.
System calls for Process management
Scis.regis.edu ● CS 468: Advanced UNIX Class 5 Dr. Jesús Borrego Regis University 1.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
What is a Process? u A process is an executable “cradle” in which a program may run u This “cradle” provides an environment in which the program can run,
Signals and Signal Processing CIS 370 Lab 7 Umass Dartmouth.
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
1 Signals (continued) CS 241 April 9, 2012 University of Illinois.
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Operating Systems Recitation 4, April th, 2002 Signals.
Process Description and Control Chapter 3. Source Modified slides from Missouri U. of Science and Tech.
Process Management Azzam Mourad COEN 346.
ACCESS CONTROL. Components of a Process  Address space  Set of data structures within the kernel - process’s address space map - current status - execution.
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
The Process CIS 370, Fall 2009 CIS UMassD. The notion of a process In UNIX a process is an instance of a program in execution A job or a task Each process.
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
System calls for Process management Process creation, termination, waiting.
1 Lecture 19: Unix signals and Terminal management n what is a signal n signal handling u kernel u user n signal generation n signal example usage n terminal.
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Signals.
CS241 Systems Programming Discussion Section Week 2 Original slides by: Stephen Kloder.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
The Shell What does a shell do? - execute commands, programs - but how? For built in commands run some code to do the command For other commands find program.
UNIX signals.
Implementation of a simple shell, xssh
Exceptional Control Flow
G.Jyostna.
Implementation of a simple shell, xssh (Section 1 version)
Implementation of a simple shell, xssh
Task Control: Signals and Alarms Chapter 7 and 8
Protection of System Resources
Using Processes.
Unix Process Management
UNIX PROCESSES.
Exceptional Control Flow: System Calls, Page Faults etc.
Lecture 5: Process Creation
Fork and Exec Unix Model
LINUX System Programming with Processes (additional)
CSC Advanced Unix Programming, Fall 2015
Operating Systems Lecture 12.
Tutorial 3 Tutorial 3.
Process Programming Interface
Inter-Process Communication ENCE 360
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Lecture 6: Multiprogramming and Context Switching
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Process Description and Control in Unix
Signals.
Process Description and Control in Unix
Presentation transcript:

Shells, System Calls, and Signals

What is a Shell? A shell is a command line interface to the operating system Fetch a command from the user and execute the command Sometimes the commands are built-in to the shell Other times the commands are external system programs or user programs There are lots of different shells available in UNIX

Bourne Shell Historically the sh language was the first to be created and goes under the name of The Bourne Shell It has a very compact syntax which makes it obtuse for novice users but very efficient when used by experts It also contains some powerful constructs built in

Bourne Shell On UNIX systems, most of the scripts used to start and configure the operating system are written in the Bourne shell It has been around for so long that is virtually bug free

C Shell The C Shell (csh) Similar syntactical structures to the C language The UNIX man pages contain almost twice as much information for the C Shell as the pages for the Bourne Shell, leading most users to believe that it is twice as good

C Shell Actually, there are several compromises within the C Shell which makes using the language for serious work difficult (Check the list of bugs at the end of the man pages!).

C Shell The real reason why the C Shell is so popular is that it is usually selected as the default login shell for most users The features that guarantee its continued use in this arena are aliases and history lists

tcsh – An Enhanced C Shell An enhanced but completely compatible version of the Berkeley UNIX C Shell, csh It is a command language interpreter usable both as an interactive login shell and a shell script command processor Uses a C-like syntax

tcsh – An Enhanced C Shell It includes: Command-line editor Programmable word completion Spelling correction History mechanism Job control

BASH GNU Bourne Again Shell A complete implementation of the IEEE POSIX.2 and Open Group Shell specificaiton with… Interactive command line editing Job control on architectures that support it Csh-like features such as history substitution and brace expansion …and a slew of other features

Korne Shell The ksh was made famous by IBM’s AIX version of UNIX The Korne Shell can be thought of as a superset of the Borne Shell as it contains the whole of the Borne Shell world within its own syntax rules

Processes and the CWD Every process runs in a directory The attribute is called the “current working directory” (cwd) Finding the CWD char *getcwd( char *buf, size_t size ); Returns a string that contains the absolute pathname of the current working directory There are functions that can be used to change the current working directory (chdir)

Other Process Attributes Getting the process id number #include <unistd.h> pid_t getpid( void ); Getting the group id number gid_t getgid( void ); Getting the real user ID of a process uid_t getuid( void );

Creating a Process The only way to create a new process is to issue the fork() system call Fork() splits the current process into 2 processes, one is called the parent and the other is called the child

Parent and Child Processes The child process is a copy of the parent process Same program Same place in the program Almost…. The child process get a new process ID

Process Inheritance The child process inherits many attributes from the parent including… Current working directory User id Group id

The fork() system call #include <unistd.h> Pid_t fork( void ); fork() returns a process id (small unsigned integer) fork() returns twice!!!!!!! In the parent process, fork returns the id of the child process In the child, fork returns a 0

Example #include <unistd.h> #include <iostream> using namespace std; int main( int argc, char *argv[] ) { if( fork() ) cout << "I am the parent" << endl; else cout << "I am the child" << endl; return( 0 ); }

Bad Example (don’t do this) #include <unistd.h> // This is called a #include <iostream> // fork bomb!!!!! using namespace std; // please don’t do this int main( int argc, char *argv[] ) { while( fork() ) cout << "I am the parent" << endl; cout << "I am the child" << endl; return( 0 ); }

Switching Programs fork() is the only way to create a new process This would be almost useless if there was not a way to switch what program is associated with a process The exec() system call is used to start a new program

exec() There are actually a number of exec functions execlp, execl, execle, execvp, execv, execve The difference between these functions is the parameters How the new program is identified and some attributes that should be set

The exec Family When you call a member of the exec family, you give it the pathname of the executable file that you want to run If all goes well, exec will never return!!! The process becomes the new program!!!

Execl() int execl( char *path, char *arg0, char *arg1, …, char *argN, execl( “/home/bin/foobar”, “alpha”, “beta”, NULL );

A Complete execl Example #include <unistd.h> #include <iostream> using namespace std; int main( int argc, char *argv[] ) { char buf[ 1000 ]; cout << "Here are the files in " << getcwd( buf, 1000 ) << endl; execl( "/bin/ls", "ls", "-al", NULL ); cout << "If all goes well, this line will not be printed!!!" << endl; return( 0 ); }

fork() and exec() Together The following program does the following: fork() – results in 2 processes Parent prints out it’s PID and waits for child process to finish (to exit) Child process prints out it’s PID and then exec() “ls” and then exits

execandfork.cpp (1) #include <unistd.h> // exec, fork, getpid #include <iostream> // cout #include <sys/types.h> // needed for wait #include <sys/wait.h> // wait() using namespace std;

execandfork.cpp (2) void child( void ) { int pid = getpid(); cout << "CHILD: Child process PID is " << pid << endl; cout << "CHILD: Child process now ready to exec ls" << endl; execl( "/bin/ls", "ls", NULL ); }

execandfork.cpp (3) void parent( void ) { int pid = getpid(); int stat; cout << "PARENT: Parent process PID is " << pid << endl; cout << "PARENT: Parent waiting for child" << endl; wait( &stat ); cout << "PARENT: Child is done. Parent returning" << endl; }

execandfork.cpp (4) int main( int argc, char *argv[] ) { cout << "MAIN: Starting fork system call" << endl; if( fork() ) parent(); else child(); cout << "MAIN: Done" << endl; return( 0 ); }

execandfork.cpp (output) neptune.cs.kent.edu] {58}% a.out MAIN: Starting fork system call CHILD: Child process PID is 32557 CHILD: Child process now ready to exec ls PARENT: Parent process PID is 32556 PARENT: Parent waiting for child a.out lowcost_DB_NOW.pdf Project 1.pdf asc mail public_html Backup MASC Software bin MPISpawn2 ZephyrDemo hybrid_parallel_system.pdf Parallaxis icp_fall2004_prj4.txt Pictures PARENT: Child is done. Parent returning MAIN: Done neptune.cs.kent.edu] {59}%

A More Concise Example int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0);

System Calls for Files and Directories Description fd = open( name, how ) Open a file for reading and/or writing s = close( fd ) Close and open file n = read( fd, buffer, size ) Read data from a file into a buffer n = write( fd, buffer, size ) Write data from a buffer into a file s = lseek( fd, offset, whence ) Move the “current” pointer for a file s = stat( name, &buffer ) Get a file’s status information( in buffer ) s = mkdir( name, mode ) Create a new directory s = rmdir( name ) Remove a directory (must be empty) s = link( name1, name2 ) Create a new entry (name2) that points to the same object as name1 s = unlink( name ) Remove the name as a link to an object (deletes the object if name was the only link to it)

More System Calls Call Description pid = fork() Create a child process identical to the parent pid = waitpid( pid, &statloc, options ) Wait for a child to terminate s = execve( name, argv, environp ) Replace a process’ core image exit( status ) Terminate process execution and return status s = chdir( dirname ) Change the working directory s = chmod( name, mode ) Change a file’s protection bits s = kill( pid, signal ) Send a signal to a process seconds = time( &seconds ) Get the elapsed time since January 1, 1970

A Simple Shell while( true ) // repeat forever { type_prompt(); // display prompt read_command( command, parameters ); // input from terminal if( fork() != 0 ) // fork off child process { // parent code waitpid( -1, &status, 0 ); // wait for child to exit } else // child code execve( command, parameters, 0 ); // execute command

Signaling Processes Signal Features of Signal A signal is a notification to a process that an event has occurred. Signals are sometimes called “software interrupts”. Features of Signal Signal usually occur asynchronously. The process does not know ahead of time exactly when a signal will occur. Signal can be sent by one process to another process (or to itself) or by the kernel to a process.

Sources for Generating Signals Hardware A process attempts to access addresses outside its own address space. Divides by zero. Kernel Notifying the process that an I/O device for which it has been waiting is available. Other Processes A child process notifying its parent process that it has terminated. User Pressing keyboard sequences that generate a quit, interrupt or stop signal.

Three Courses of Action Process that receives a signal can take one of three action: Perform the system-specified default for the signal notify the parent process that it is terminating; generate a core file; (a file containing the current memory image of the process) terminate. Ignore the signal A process can do ignoring with all signal but two special signals: SIGSTOP and SIGKILL. Catch the Signal (Trapping) When a process catches a signal, except SIGSTOP and SIGKILL, it invokes a special signal handing routine.

POSIX-Defined Signals (1) SIGALRM: Alarm timer time-out. Generated by alarm( ) API. SIGABRT: Abort process execution. Generated by abort( ) API. SIGFPE: Illegal mathematical operation. SIGHUP: Controlling terminal hang-up. SIGILL: Execution of an illegal machine instruction. SIGINT: Process interruption. Can be generated by <Delete> or <ctrl_C> keys. SIGKILL: Sure kill a process. Can be generated by “kill -9 <process_id>“ command. SIGPIPE: Illegal write to a pipe. SIGQUIT: Process quit. Generated by <crtl_\> keys. SIGSEGV: Segmentation fault. generated by de-referencing a NULL pointer.

POSIX-Defined Signals (2) SIGTERM: process termination. Can be generated by “kill <process_id>” command. SIGUSR1: Reserved to be defined by user. SIGUSR2: Reserved to be defined by user. SIGCHLD: Sent to a parent process when its child process has terminated. SIGCONT: Resume execution of a stopped process. SIGSTOP: Stop a process execution. SIGTTIN: Stop a background process when it tries to read from its controlling terminal. SIGTSTP: Stop a process execution by the control_Z keys. SIGTTOUT: Stop a background process when it tries to write to its controlling terminal.

Sending Signals You may send signals to a process connected to your terminal by typing ^C SIGINT terminate execution ^\ SIGQUIT terminate and core dump ^Z SIGSTOP suspend for later The terminal driver is a program that processes I/O to the terminal can detect these special character sequences and send the appropriate signal to your interactive shell. The shell in turn generates an appropriate signal to the foreground process.

Kill The user can use the csh built-in kill command or use regular UNIX kill command to send a specific signal to a named process. % kill [-sig] process If no signal is specified, then SIGTERM (15)(terminate) is assumed In C/C++ the system call is #include <signal.h> int kill( int pid, int sig_id ); Return values: Success = 0, Failure = -1, Sets errno…YES

Signal Delivery and Processing When an interrupt or event causes a signal to occur, the signal is added to a set of signals that are waiting for delivery to a process. Signals are delivered to a process in a manner similar to hardware interrupts.

Signal Delivery If the signal is not currently blocked by the process, it is delivered to the process following these steps: The same signal is blocked from further occurrence until delivery and processing are finished The current process context is saved and a new one built A handler function associated with the signal is called If the handler function returns, then the process resumes execution from the point of interrupt, with its saved context restored. Among other things, the signal mask is restored. Signals have the same priority But processes can block listening to specific signals via a signal mask

Signal Trapping The system call signal() is used to trap signals #include <signal.h> signal( int sig_id, void * handler() ); Example: Write a C++ program to count the number of times CTRL-C is pressed at the terminal cc_counter.cpp

Alarms Function The alarm API requests the kernel to send the SIGALRM signal after a certain number of real clock seconds. #include <signal.h> int alarm( unsigned int time_interval); Return: Success: the number of CPU seconds left in the process timer; Failure: -1; Sets errno: Yes Argument time_interval: the number of CPU seconds elapse time. After which the kernel will send the SIGALRM signal to the calling process. Example: Write a C++ program to set an alarm for signal 5 seconds after process startup and trap the alarm signal. alarmer.cpp