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.

Slides:



Advertisements
Similar presentations
Recitation 8 (Nov. 1) Outline Process & job control Lab 5 Reminder Lab 5: Due Thursday Minglong Shao Office hours: Thursdays 5-6PM.
Advertisements

Recitation By yzhuang, sseshadr. Agenda Debugging practices – GDB – Valgrind – Strace Errors and Wrappers – System call return values and wrappers – Uninitialization.
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
15-213, Fall 06 Outline Shell Lab Processes Signals.
Operating System Inter-Process Communication. IPC F How does one process communicate with another process? –semaphores -- signal notifies waiting process.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
1 Processes and Pipes COS 217 Professor Jennifer Rexford.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Friday, June 09, 2006 “I think there is a world market for maybe five computers”. - Thomas Watson, Chairman of IBM, 1943.
CSE 451 Section 4 Project 2 Design Considerations.
Fork and Exec Unix Model Tutorial 3. Process Management Model The Unix process management model is split into two distinct operations : 1. The creation.
The Programming Interface. Main Points Creating and managing processes – fork, exec, wait Performing I/O – open, read, write, close Communicating between.
Unix Processes Slides are based upon IBM technical library, Speaking Unix, Part 8: Unix processes Extended System Programming Laboratory (ESPL) CS Department.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Shell (Part 1). Process r A process is an instance of an application running r If there are two instances of an application running then there are two.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Fundamentals CIS 552. Fundamentals Low-level I/O (read/write using system calls)  Opening/Creating files  Reading & Writing files  Moving around in.
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”
Cli/Serv.: procs/51 Client/Server Distributed Systems v Objectives –look at how to program UNIX processes , Semester 1, Processes.
Operating Systems Chapter 2
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Shell (Part 2). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
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.
Concurrent Processes Processes can concurrently run same program. Processes can concurrently run same program. Processes can start other processes. Processes.
Operating Systems Process Creation
Recitation: Signaling S04, Recitation, Section A Debug Multiple Processes using GDB Debug Multiple Processes using GDB Dup2 Dup2 Signaling Signaling.
Process Management Azzam Mourad COEN 346.
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
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)
Tutorial 3. In this tutorial we’ll see Fork() and Exec() system calls.
Dsh: A Devil Shell COMPSCI210 Recitation 14 Sep 2012 Vamsi Thummala.
Process Related System Calls By Neha Hulkoti & Kavya Bhat.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Error handling I/O Man pages
Implementation of a simple shell, xssh
Week 3 Redirection, Pipes, and Background
Implementation of a simple shell, xssh (Section 1 version)
Precept 14 : Ish dup() & Signal Handling
Implementation of a simple shell, xssh
Unix Process Management
Processes in Unix, Linux, and Windows
Processes in Unix, Linux, and Windows
Recitation 9: Tshlab + VM
Pipes A pipe provides a one-way flow of data example: who | sort| lpr
LINUX System Programming with Processes (additional)
תרגול 8 – ק/פ ותקשורת תהליכים ב-Linux
Processes in Unix, Linux, and Windows
Inter-Process Communication
Andy Wang Operating Systems COP 4610 / CGS 5765
Programming Assignment # 2 – Supplementary Discussion
File I/O (1) Prof. Ikjun Yeom TA – Mugyo
IPC Prof. Ikjun Yeom TA – Hoyoun
Inter-Process Communication ENCE 360
Tutorial: The Programming Interface
The Environment of Unix Process
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Section 2 Processes January 27rd, 2017 Taught by Joshua Don.
Intro to the Shell with Fork, Exec, Wait
System Programming: Process Management
Presentation transcript:

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 executable and.... run it. Other features: wildcards, pipes, redirection.

Processes The shell is a process used to start up other processes (parent-child). A process is a program in execution with all virtual objects (files, cpu, memory) associated with it. Process characteristics - code to execute - data to read/write - resources - a pid

System Calls Functions to give user an interface to the kernel. POSIX system calls vs. library functions System calls are documented in section 2 of the manual. - man -s 2 systemcall (Solaris) - man 2 systemcall (Linux) - systemcall(2) – denotes man page in section 2

File Management System Calls fd = open(file, how, options); man -s 2 open look for include files and arg types Also for return value meaning Most return -1 on an error (always check for error case and handle it appropriately) Look for what arguments mean and values they can take (constants)

File Management System Calls open(2) some more O_RDONLY – open for reading O_WRONLY – open for writing O_RDWR – open for read and write O_APPEND – to append to a file O_CREAT – to create a file O_TRUNC – to truncate a file Return value is file descriptor to use, -1 on error (Use perror(3C) or strerror(3C).)

File Management System Calls s = close(fd); n = read(fd, buffer, nbytes); n = write(fd, buffer, nbytes); position = lseek(fd, offset, whence); s = stat(name, &buf); // fstat(), lstat() s = mkdir(name, mode); s = link(name1, name2); s = rmdir(name); s = unlink(name); s = chdir(dirname); s = chmod(name, mode);

File Descriptors In POSIX, a file descriptor (fd) is an integer that is assigned when a file is open. There are 3 standard file descriptors which every process (save perhaps a daemon) should expect to have: Integer value Name 0 Standard Input (stdin) 1 Standard Output (stdout) 2 Standard Error (stderr)

System Calls - Errors Most system calls return a -1 on error and set errno – check the man page Always do error checking!!! Use perror(3C) or strerror(3C) to get more meaningful error messages from errno.

Process Mgmt System Calls pid = fork(); - create a child process – copy of parent pid == -1, on error pid == 0, executing in new (child) proc. else pid of child returned to parent pid = waitpid(pid, &statloc, options); exit(status); - check return status of programs * in tcsh 'set printexitvalue' * in csh/tcsh $status, sh/bash $?

Process Mgmt System Calls s = kill(pid,signal); s = execve(name, argv, envp); exec(2) lists 6 functions from the exec() family of system calls. The most general is execve(). waitpid(3C), wait(3C) They can block and wait for a process to finish, or be non-blocking.

How a shell works (to run a program) Create a child process (a copy of parent with a new pid) with fork() system call. In the child process use one of the exec(2) family of system calls to “run” the program. Parent process should then wait for the child to finish. (i.e. use waitpid()).

Using fork(2) to create a new process pid = fork(); if ( pid == -1 ) /* error */ { perror(“fork”); exit(-1); } if ( pid == 0 ) /* child */ { /* execute code for child */ } else /* parent */ { /* execute code for parent */ } /* go over example programs */

How the Shell works more How does the shell find code to run for a command? Some commands are built-in to the shell - so no fork() needed - some are required to be to work (cd, fg, bg) - some are built in for speed gain (avoid costly fork() of new process) (e.g. Tcsh has ls-F) - disadvantage – goes against UNIX - changing the command

How the Shell finds executables Use the PATH environment variable - set path, setenv PATH - what is '.'? problems? Use a loop to find executable file in PATH - use strtok() on PATH - use access(2) with X_OK - construct path to test with snprintf() snprintf(path, n, “%s/%s”, dir, command);

Command line arguments int main(int argc, char **argv) argv[0] – executable name argv[1] – first argument argv[n] – nth argument What about environment variables? int main(int argc, char **argv, char** envp) Go over exec(2) man page.

Signals, Interrupt processing Sent with kill(2) or kill(1) Ctrl-C = SIGINT Ctrl-Z = SIGTSTP SIG{HUP, USR1, USR2, SEGV} catching signals and handling them with signal() or sigset() ignore signals with sigignore() or signal(SIGINT, SIG_IGN)

Signals, Interrupt processing Signal SIGKILL (9) cannot be caught or ignored. kill kill -l will list signals look at /usr/include/sys/iso/signal_iso.h look at sample signal handler code.

Thoughts on project 1 cc, gcc, make and makefiles Allocating memory with malloc(), calloc() pointers, seg faults, bus errors.h files -> /usr/include man pages gets() vs. fgets() sprintf() vs. snprintf() (avoid strcat()) strcpy() vs. strncpy()