Docsity

Lecture Notes for Command Line Arguments - Slides | CMSC 212, Study notes of Computer Science

Partial preview of the text

Download Lecture Notes for Command Line Arguments - Slides | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

Announcements

z^

Program #2 due today

z^

Midterm #2 is next Tuesday– same time and place as last time

Command Line Arguments

z^

int main(int argc, char *argv[])– argc - number of arguments (including program name)– argv - array of command line arguments

  • argv[0] - command invoked to start program• argv[n] - n'th command line argument

z^

Example:– ./foo -file myfile -help– argv[0] = "./foo"– argv[1] = "-file"– argv[2] = "myfile"– argv[3] = "-help"

Command Line Arguments Continued

int debugFlag, limit;char *inputFileName;optionTable = [] {

{ "-debug", Flag, &debugFlag },{ "-file", StrParam, &inputFileName },{ "-limit", IntParam, &limit },…. /* other options go here */

}; int optionCount = sizeof(optionTable)/sizeof(option);

Project 1B Using Table Driven Parsing

typedef struct {

char *name;int opCode;int numRegisters;int usesMem; } opInfo;

static opInfo opTable[] = {

{ "Load",

{ "Move",

{ "Store", 3, 1, 1 },{ "Add",

{ "Halt",

{ "Negate", 6, 1, 0 },{ "Branch", 7, 2, 1 },{ "Bnn",

{ "Input", 10, 1, 0 },{ "Output", 11, 1, 0 },{ "Data",

Environment Variable Example

z^

Shell Commands to Setup Environement:– C Shell:

setenv FOO_CONFIG ~/foo

  • Bash:

FOO_CONFIG=~/foo

main(int argc, char *argv[], char *envp[]){

char *configDirectory;configDirectory = getenv("FOO_CONFIG");

Creating New Processes

z^

Use fork system call– UNIX (and LINUX) specific– creates a new copy of the current process

z^

Syntax int fork();–

returns twice

  • once from initial call• second time in a new process
    • return value indicates which process is which

the "child" (new) process

  • 0

the "parent" (original) process

  • < 0

the "parent", but not child was created (error case)

Learning About Other Processes

z^

Wait and waitpid system call– #include <sys/types.h>– #include <sys/wait.h>– pid_t wait(int *status);

  • wait until a child process terminates
    • pid_t waitpid(pid_t pid, int *status, int options);
      • wait until the passed process terminates
        • return is the id the the terminated process– status fills out a status variable
          • WIFEXITED(status) - true if the child terminated normally• WEXITSTATUS(status) - low 8 bits of parameter to exit• WTERMSIG(status) - signal number that terminated process

Invoking a new program

z^

exec system calls– int execv(const char *prog, char *const argv[]);

  • run the program in the passed prog parameter• pass the new program argv
    • int execvp(const char *file, char *const argv[]);
      • like execv, but used the PATH environment variable

z^

On success,– new program launched, the system call does not return

z^

On failure,– returns -1, sets global errno to indicate cause of the error

Additional I/O system Calls

z^

Sometimes process want to communicate– abstraction: pipe - one process writes, other reads– shell example: ls | wc

z^

int pipe(int filedes[2]);– create a pipe between two processes– write and read system call can use these

z^

Stndard I/O– fd 0 is standard input to a process– fd 1 is standard output from a process– fd 2 is standard error output from

z^

int dup2(int oldfd, int newfd);– change output of one fd to another– dup2(0, myfd)

  • now all standard input comes from myfd

Changing Standard Input/Output

z^

Useful to combine pipe, dup2, and exec….pipe(pipefds);pid = fork();if (pid == 0) {

dup2(0, pipefd[0]);close(pipefd[1]);ret = execvp("ls", args);…. else if (pid > 0) {

dup2(1, pipefd[1]);close(pipefd[0]); } else {….}