Mini Project 1 Tech Shell

Similar documents
How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

5. Processes and Memory Management

Lecture 25 Systems Programming Process Control

Process definition Concurrency Process status Process attributes PROCESES 1.3

Outline. Review. Inter process communication Signals Fork Pipes FIFO. Spotlights

Unix System Calls. Dept. CSIE

ARUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS. If you have two

NDK: NOVELL NSS AUDIT

Libmonitor: A Tool for First-Party Monitoring

TFTP Usage and Design. Diskless Workstation Booting 1. TFTP Usage and Design (cont.) CSCE 515: Computer Network Programming TFTP + Errors

Sources: On the Web: Slides will be available on:

Lecture 24 Systems Programming in C

HP-UX Essentials and Shell Programming Course Summary

A Brief Introduction to the Use of Shell Variables

System Calls and Standard I/O

Shared Memory Segments and POSIX Semaphores 1

Leak Check Version 2.1 for Linux TM

Unix Scripts and Job Scheduling

RECOVER ( 8 ) Maintenance Procedures RECOVER ( 8 )

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

System calls. Problem: How to access resources other than CPU

Networks. Inter-process Communication. Pipes. Inter-process Communication

LECTURE-7. Introduction to DOS. Introduction to UNIX/LINUX OS. Introduction to Windows. Topics:

Set-UID Privileged Programs

Programming A PLC. Standard Instructions

public static void main(string[] args) { System.out.println("hello, world"); } }

OPEN APPLICATION INTERFACE (OAI) INSTALLATION GUIDE NEC

Programmation Systèmes Cours 4 Runtime user management

Command Line Crash Course For Unix

Semantic Analysis: Types and Type Checking

Automatic Script Generation Based on User-System Interactions

Interlude: Process API

LOW LEVEL FILE PROCESSING

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

System Calls Related to File Manipulation

C++ INTERVIEW QUESTIONS

Lecture 22: C Programming 4 Embedded Systems

How to test and debug an ASP.NET application

Basics of I/O Streams and File I/O

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

System Security Fundamentals

static void insecure (localhost *unix)

CS10110 Introduction to personal computer equipment

Using C to Access Data Stored in Program Space Memory on the TMS320C24x DSP

CS355 Hw 3. Extended Shell with Job Control

2 SYSTEM DESCRIPTION TECHNIQUES

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

SQLITE C/C++ TUTORIAL

Chapter 7: Unix Security. Chapter 7: 1

Informatica e Sistemi in Tempo Reale

Introduction. What is an Operating System?

Linux Kernel Architecture

Traditional Rootkits Lrk4 & KNARK

OS: IPC I. Cooperating Processes. CIT 595 Spring Message Passing vs. Shared Memory. Message Passing: Unix Pipes

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Process Monitor HOW TO for Linux

Hands-On UNIX Exercise:

Chapter 10 Case Study 1: LINUX

PHP Debugging. Draft: March 19, Christopher Vickery

CIS 551 / TCOM 401 Computer and Network Security

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Answers to Even-numbered Exercises

Have both hardware and software. Want to hide the details from the programmer (user).

Project 2: Bejeweled

Moving from CS 61A Scheme to CS 61B Java

Cleo Host Loginid Import Utility and Cleo Host Password Encryption Utility

Appendix. Web Command Error Codes. Web Command Error Codes

Grundlagen der Betriebssystemprogrammierung

About the File Manager 2

sys socketcall: Network systems calls on Linux

CPSC 226 Lab Nine Fall 2015

OPERATING SYSTEM SERVICES

Lab 4: Socket Programming: netcat part

Lecture 5: Java Fundamentals III

File Transfer Protocol (FTP) Chuan-Ming Liu Computer Science and Information Engineering National Taipei University of Technology Fall 2007, TAIWAN

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

C++ Programming Language

CS Unix Tools & Scripting Lecture 9 Shell Scripting

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

An Introduction to the Linux Command Shell For Beginners

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Java Interview Questions and Answers

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

LC-3 Assembly Language

Thirty Useful Unix Commands

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

CSC230 Getting Starting in C. Tyler Bletsch

CSC 2405: Computer Systems II

1 Abstract Data Types Information Hiding

Illustration 1: Diagram of program function and data flow

Acronis Backup & Recovery: Events in Application Event Log of Windows

Real SQL Programming. Persistent Stored Modules (PSM) PL/SQL Embedded SQL

Introduction to Programming II Winter, 2014 Assignment 2

Grandstream Networks, Inc.

Operating System Structure

13 File Output and Input

Transcription:

Mini Project 1 Tech Shell Dr. Chokchai (Box) Leangsuksun Louisiana Tech University 1

Requirements What is a shell? A system program that provides interfaces to system resources and execute other programs" Handles executions of commands & supports built-in commands (cd, pwd, exit, set). Supports piping. I/O redirection Supports new built-in commands set and list that will allow user to set a shell variable and its value as well as list all variables with their values Error Condition " Louisiana Tech University 2

General logic" 1. Read command line input as a string" 2. Tokenize and parse the input " 1 st scan: by looking for to break up input into command substrings. " 3. see whether the command is built-in or not. " built-in ones are cd, pwd, exit, set, don t fork but just call appropriate functions in the parent process. For a regular command, fork and exec the command. " Make sure you handle redirections" 4. If there are multiple command substrings, fork a new process for each regular command and connect them with pipe." Louisiana Tech University 3

Useful Linux/Unix System Calls" fork(), execvp(), open(), close(), read(), write(), pipe(), dup2(), getcwd(), exit(), chdir(), wait()" Louisiana Tech University 4

A series of execxx functions execxx() system call used after a fork to replace the process memory space with a new program" extern char **environ; int execl(const char *path, const char *arg0,... / *, (char *)0 */); int execle(const char *path, const char *arg0,... / *, (char *)0, char *const envp[]*/); int execlp(const char *file, const char *arg0,... / *, (char *)0 */); int execv(const char *path, char *const argv[]); int execve(const char *path, char *const argv[], char *const envp[]); int execvp(const char *file, char *const argv[]);# Louisiana Tech University 5 5

Using of execxx calls Using execl() The following example executes the ls command, specifying the pathname of the executable (/bin/ls) and using arguments supplied directly to the command to produce single-column output. #include <unistd.h> int ret;... ret = execl ("/bin/ls", "ls", "-1", (char *)0); Louisiana Tech University 6 6

Using execle() The following example is similar to Using execl(). In addition, it specifies the environment for the new process image using the env argument. #include <unistd.h> int ret; char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 }; #...# # ret = execle ("/bin/ls", "ls", "-l", (char *)0, env); # # # Louisiana Tech University 7 7

Using execlp() The following example searches for the location of the ls command among the directories specified by the PATH environment variable. #include <unistd.h> int ret; # #... # # ret = execlp ("ls", "ls", "-l", (char *)0); # # Louisiana Tech University 8 8

Using execvp() The following example searches for the location of the ls command among the directories specified by the PATH environment variable, and passes arguments to the ls command in the cmd array. #include <unistd.h> int ret; # char *cmd[] = { "ls", "-l", (char *)0 }; #... # ret = execvp ("ls", cmd); # Louisiana Tech University 9 9

execlp USING PROCESSES The execlp library function is used when the number of arguments to be passed to the program to be executed is known in advance. (Table 3.3) 10

USING PROCESSES execlp Include File(s) <unistd.h> extern char *environ; Manual Section 3 Summary int execlp(const char *file,const char *arg,...); Return Success Failure Sets errno Does not return -1 Yes Table 3.3. Summary of the execlp Library Function. 11

USING PROCESSES execlp The initial argument, file, is a pointer to the file that contains the program code to be executed. For the execlp call to be successful, the file referenced must be found and be marked as executable. If the call fails, it returns a 1 and sets errno to indicate the error. (Table 3.4). 12

13

USING PROCESSES execlp # Constant perror Message Explanation 1 EPERM Operation not permitted The process is being traced, the user is not the superuser, and the file has an SUID or SGID bit set. The file system is mounted nosuid, the user is not the superuser, and the file has an SUID or SGID bit set. 2 ENOENT No such file or directory One or more parts of path to new process file does not exist (or is NULL). 4 EINTR Interrupted system call Signal was caught during the system call. 5 EIO Input/output error 7 E2BIG Argument list too long New process argument list plus exported shell variables exceed the system limits.. Table 3.4. exec Error Messages 14

USING PROCESSES execlp # Constant perror Message Explanation 8 ENOEXEC Exec format error New process file is not in a recognized format. 11 EAGAIN Resource temporarily unavailable Total system memory while reading raw I/O is temporarily insufficient. 12 ENOMEM Cannot allocate memory New process memory requirements exceed system limits. 13 EACCES Permission denied Search permission denied on part of file path. The new file to process is not an ordinary file. No execute permission on the new file to process. 14 EFAULT Bad address path references an illegal address. 20 ENOTDIR Not a directory Part of the specified path is not a directory.. Table 3.4. exec Error Messages 15

USING PROCESSES execlp # Constant perror Message Explanation 21 EISDIR Is a directory An ELF interpreter was a directory. 22 EINVAL Invalid argument An ELF executable had more than one interpreter. 24 EMFILE Too many open files Process has exceeded the maximum number of files open. 26 ETXTBSY Text file busy More than one process has the executable open for writing. 36 ENAMETO OLONG File name too long The path value exceeds system path/file name length. 40 ELOOP Too many levels of symbolic links The perror message says it all. 67 ENOLINK Link has been severed The path value references a remote system that is no longer active.. Table 3.4. exec Error Messages 16

USING PROCESSES execlp # Constant perror Message Explanation 72 EMULTIHOP Multihop attempted The path value requires multiple hops to remote systems, but file system does not allow it. 80 ELIBBAD Accessing a corrupted shared library An ELF interpreter was not in a recognized format.. Table 3.4. exec Error Messages 17

execvp If the number of arguments for the program to be executed is dynamic, then the execvp call can be used. (Table 3.5). USING PROCESSES 18

USING PROCESSES execvp Include File(s) <unistd.h> <extern char *environ; Manual Section 3 Summary Int execvp(const char *file, char *const argv[]); Return Success Failure Sets errno Does not return -1 Yes Table 3.5. Summary of the execvp System Call. 19

USING PROCESSES execvp As with the execlp call, the initial argument to execvp is a pointer to the file that contains the program code to be executed. Unlike execlp, there is only one additional argument that execvp requires. The second argument specifies that a reference to an array of pointers to character strings should be passed. 20

execvp USING PROCESSES If execvp fails, it returns a value of 1 and sets the value in errno to indicate the source of the error. 21

22

23

USING PROCESSES terminating a Process A process normally terminates in one of three ways. In order of preference, these are Ø It issues (at any point in its code) a call to either exit or _exit. Ø It issues a return in the function main. Ø It falls off the end of the function main ending implicitly. Programmers routinely make use of the library function exit to terminate programs. This function does not return a value. 24

USING PROCESSES exit(): Ending a Process Include File(s) <stdlib.h> Manual Section 3 Summary void exit(int status); Return Success Failure Sets errno Does not return No return Yes 25

USING PROCESSES Ending a Process The exit function accepts a single parameter, an integer status value that will be returned to the parent process. By convention, a 0 value is returned if the program has terminated normally; other wise, a nonzero value is returned. 26

USING PROCESSES Waiting on Processes More often than not, a parent process needs to synchronize its actions by waiting until a child process has either stopped or terminated its actions. The wait system call allows the parent process to suspend its activity until one of these actions has occurred. (Table 3.9). 27

USING PROCESSES Waiting on Processes Include File(s) <sys/types.h> <sys/wait.h> Manual Section 2 Summary pid_t wait(int *status); Return Success Failure Sets errno Child process ID or 0-1 Yes Table 3.9. Summary of the wait System Call. 28

Waiting on Processes USING PROCESSES The wait system call accepts a single argument, which is a pointer to an integer, and returns a value defined as type pid_t. 29

USING PROCESSES Waiting on Processes While the wait system call is helpful, it does have some limitations: Ø It will always return the status of the first child process that terminates or stops. Thus, if the status information returned by wait is not from the child process we want, the information may need to be stored on a temporary basis for possible future reference and additional calls to wait made. Ø Another limitation of wait is that it will always block if status information is not available. 30