Mini Project 1 Tech Shell - Louisiana Tech Universitybox/os/proj1_lecture.pdf · Success Failure...

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

Transcript of Mini Project 1 Tech Shell - Louisiana Tech Universitybox/os/proj1_lecture.pdf · Success Failure...

Mini Project 1 Tech Shell

Dr. Chokchai (Box) Leangsuksun

Louisiana Tech University

1

2 Louisiana Tech University

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 "

3 Louisiana Tech University

General logic"

1.  Read command line input as a string"2.  Tokenize and parse the input "

–  1st 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."

4 Louisiana Tech University

Useful Linux/Unix System Calls"

•  fork(), execvp(), open(), close(), read(), write(), pipe(), dup2(), getcwd(), exit(), chdir(), wait()"

5 Louisiana Tech University

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[]);#

•  5

6 Louisiana Tech University

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);

6

7 Louisiana Tech University

•  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); ## #

7

8 Louisiana Tech University

•  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); # #

8

9 Louisiana Tech University

•  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);  #

9

10

execlp

§  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)

USING PROCESSES

11

execlp

USING PROCESSES

Return

int execlp(const char *file,const char *arg, . . .); Summary

3 Manual Section

<unistd.h> extern char *environ;

Include File(s)

Sets errno Failure Success

Yes -1 Does not return

Table 3.3. Summary of the execlp Library Function.

12

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).

USING PROCESSES

13

14

execlp

Explanation perror Message Constant # • 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.

Operation not permitted

EPERM 1

One or more parts of path to new process file does not exist (or is NULL).

No such file or directory

ENOENT 2

Signal was caught during the system call. Interrupted system call EINTR

4

Input/output error EIO 5

New process argument list plus exported shell variables exceed the system limits.

Argument list too long E2BIG 7

. Table 3.4. exec Error Messages

USING PROCESSES

15

execlp

Explanation perror Message Constant # New process file is not in a recognized format. Exec format error ENOEXEC 8

Total system memory while reading raw I/O is temporarily insufficient.

Resource temporarily unavailable

EAGAIN 11

New process memory requirements exceed system limits.

Cannot allocate memory

ENOMEM 12

• 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.

Permission denied EACCES 13

path references an illegal address. Bad address EFAULT 14

Part of the specified path is not a directory. Not a directory ENOTDIR 20

. Table 3.4. exec Error Messages

USING PROCESSES

16

execlp

Explanation perror Message Constant # An ELF interpreter was a directory. Is a directory EISDIR 21

An ELF executable had more than one interpreter.

Invalid argument EINVAL 22

Process has exceeded the maximum number of files open.

Too many open files EMFILE

24

More than one process has the executable open for writing.

Text file busy ETXTBSY 26

The path value exceeds system path/file name length.

File name too long ENAMETOOLONG

36

The perror message says it all. Too many levels of symbolic links

ELOOP 40

The path value references a remote system that is no longer active.

Link has been severed ENOLINK 67

. Table 3.4. exec Error Messages

USING PROCESSES

17

execlp

Explanation perror Message Constant # The path value requires multiple hops to remote systems, but file system does not allow it.

Multihop attempted EMULTIHOP 72

An ELF interpreter was not in a recognized format.

Accessing a corrupted shared library

ELIBBAD 80

. Table 3.4. exec Error Messages

USING PROCESSES

18

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

19

execvp

Return

Int execvp(const char *file, char *const argv[]); Summary

3 Manual Section

<unistd.h> <extern char *environ;

Include File(s)

Sets errno Failure Success

Yes -1 Does not return

Table 3.5. Summary of the execvp System Call.

USING PROCESSES

20

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.

USING PROCESSES

21

execvp §  If execvp fails, it returns a value of −1 and sets

the value in errno to indicate the source of the error.

USING PROCESSES

22

23

24

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.

USING PROCESSES

25

exit(): Ending a Process

Return

void exit(int status); Summary

3 Manual Section

<stdlib.h> Include File(s)

Sets errno Failure Success

Yes No return Does not return

USING PROCESSES

26

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.

USING PROCESSES

27

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).

USING PROCESSES

28

Waiting on Processes

Return

pid_t wait(int *status); Summary

2 Manual Section

<sys/types.h> <sys/wait.h>

Include File(s)

Sets errno Failure Success

Yes -1 Child process ID or 0

Table 3.9. Summary of the wait System Call.

USING PROCESSES

29

Waiting on 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.

USING PROCESSES

30

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.

USING PROCESSES