CS 170 Spring 05 JShell Lab - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CS 170 Spring 05 JShell Lab

Description:

error_code = parse(&commands); int cmd_free(CMD *); Specail input - exit or CTRL-D ... Check for the return value from parse(). You have to handle errors and your ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 13
Provided by: lamiay
Category:
Tags: jshell | lab | parse | seg | spring

less

Transcript and Presenter's Notes

Title: CS 170 Spring 05 JShell Lab


1
CS 170 Spring 05JShell Lab
2
What is a Shell?
  • A shell is a command line interpreter it reads
    lines typed by the user and interprets them as
    requests to execute other programs.

Applications
Text editor
Calculator
Shell
Operating System
Machine (Hardware)
3
What is a Shell?
  • Shell is a just another application it has
  • Input
  • Does something (Processing)
  • Output

4
Shell Input
  • Commands typed at the prompt
  • Needs to be parsed ? parse.h
  • int parse(CMD )
  • ex. error_code parse(commands)
  • int cmd_free(CMD )
  • Specail input -gt exit or CTRL-D
  • CTRL-C check signal(SIGINT, SIG_IGN)

5
Shell Input
  • int parse(CMD )
  • CMD is a pointer to a pointer of CMD struct.
  • / command structure /
  • typedef struct command_struct
  • struct command_struct prevCmd
  • struct command_struct nextCmd
  • char file
  • char argv
  • char in_redir
  • char out_redir
  • int appending / flag, non-zero append, zero
    truncate /
  • int background / whether the line terminates
    with an /
  • CMD

6
Shell input
  • Very important
  • Check for the return value from parse(). You have
    to handle errors and your shell must report them.
  • Before accessing any pointer, make sure it is not
    NULL. Using a Null pointer will make your code
    Seg fault.

7
Shell does something (Processing)
  • Calling other applications.

Applications
Text editor
Calculator
Shell
..
fork
dup
exec
Operating System
Machine (Hardware)
8
Shell does something (Processing)
  • First you need to fork a new process to run the
    requested program by the user.
  • ? use fork().
  • How to tell which is which?
  • Use getpid() or fork-return value. 0? Yes
    child, No Parent

include ltstdio.hgtint main()   pid
fork()   printf("\nHello World\n)   return
0
9
Shell does something (Processing)
  • Second Shell asks the OS to run the
    user-requested program
  • ?Use execvp()
  • Recall difference between background processes
    and foreground ones !!
  • Execvp-ed cmd input/output should be directed
    according to user request for that cmd.
  • ? Use dup2()

10
Shell output
  • Your Prompt ? Waiting for a new cmd.

11
General Remarks
  • Use perror() to report the sys-calls error .
  • Zombie processes.
  • ? Use waitpid() or wait4()
  • Test your code on CSIL machines
  • Apply test codes.
  • Provide a README file.
  • Late assignments will receive a ZERO. Submit what
    you have before the deadline.

12
Zombie processes check
  • Command ps a
  • Zombie will show as ltdefunctgt
  • Sample
  • Input
  • cs170_at_ella tmp2 ./jshell
  • jshell sleep 3
  • Output
  • cs170_at_ella tmp2 ps -a
  • 13858 pts/30 000000 jshell
  • 13896 pts/30 000000 sleep ltdefunctgt ?
    Shouldnt show up if waitpid
  • 13905 pts/1 000000 ps
    is correctly implemented.
Write a Comment
User Comments (0)
About PowerShow.com