Docsity

Lab 1: My Shell - Computer Operating Systems | CS 481, Lab Reports of Operating Systems

Toggle sidebar
bg1
1
THE UNIVERSITY of
NEW MEXICO
Lab 1: My Shell
Prof. Dorian Arnold
CS 481: Operating System Principles
Spring ‘09
2
Lab 1 Concepts
• Process creation (fork())
• Process termination (wait())
• Command execution (exec())
• Basic file I/O (open()/close())
• Input/output redirection (dup2())
• Inter-process communication (pipe())
3
Basic Shell Operators
• & →execute command line in the background
• < →redirect standard input of current command
• > →redirect standard output of current command
• >> →redirect standard output of current
command (append if file exists)
• | →redirect standard output of current command
to standard input of following command
4
Major Advice
• Start early!
• Understand and design, then implement
• Develop incrementally
• Partner up, if desired
pf3
pf4

Partial preview of the text

Download Lab 1: My Shell - Computer Operating Systems | CS 481 and more Lab Reports Operating Systems in PDF only on Docsity!

THE UNIVERSITY

of

NEW MEXICO

Lab 1: My Shell

Prof. Dorian Arnold

CS 481: Operating System Principles

Spring ‘

2

Lab 1 Concepts

  • Process creation (

fork()

  • Process termination (

wait()

  • Command execution (

exec()

  • Basic file I/O (

open()

close()

  • Input/output redirection (

dup2()

  • Inter-process communication (

pipe()

3

Basic Shell Operators

execute command line in the background

redirect standard input of current command

redirect standard output of current command

redirect standard output of current

command (append if file exists)

redirect standard output of current command

to standard input of following command

4

Major Advice

  • Start early!• Understand and design, then implement• Develop incrementally• Partner up, if desired

5

Part 1: Bare Bones Shell

  • Print prompt• Read and parse command line• Execute command (with arguments)• Wait for command to complete (or not)• Repeat

6

A Shell of a Shell

while(1) {

char

*cmd

getcmd();

int

retval

forkforkforkfork

if(

retval

//forked

child process

exec exec

exec exec

( cmd

else {//parent

process

int

pid

retval;

wait wait

wait wait

( pid

7

UNIX Command Execution:

exec()

“exec” is a family of functions

Use execvp()

int

execvp(char *path,

char

**argv

)

Search

path

environment

for executable

executable name

command arguments

8

execvp()

example

main( ) {

*char newargv[4];int

i;

newargv[0]

= “ls";

newargv[1]

= “-l";

newargv[2]

= “-a”;

newargv[3]

= NULL;

i

=

execvp

(newargv[0],

newargv

);

perror("execvp() failed");exit(1);

}

This program is equivalent to:

prompt: ls

–l

–a

13

Part 3: Piping

  • prompt:

cmd

foo

cmd

bar

  • execute

cmd

with argument

foo

and pipe

output to

cmd

executed with argument

bar

14

UNIX Pipes

int

main

(

)

{

…int

fd[2];

pipe

(fd);

fork

();

if(

child )

{

close(

fd[1] );

read( fd[0], … );

}

else {

close(

fd[0] );

write(

fd[1], … );

}

}