Command Line Arguments and Processes in CMSC 212 – S05, Exams of Computer Science

Lecture notes for cmsc 212 – s05, covering command line arguments, table-driven parsing, environment variables, creating new processes using fork system call, and learning about other processes using wait and waitpid system calls. It also includes an example of a simple shell program.

Typology: Exams

Pre 2010

Uploaded on 07/29/2009

koofers-user-hl2-1
koofers-user-hl2-1 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 212 – S05 (lect 17)
Announcements
Program #3 due today 8pm
Exam #2
one week from today
April 12
same room as the 1st exam (ARM 0131)
time as the 1st exam (6:00-7:15)
minimally comprehensive
Reading
for today’s lecture
The remaining portions of Chapter 13
for Thursday’s lecture
notes
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Command Line Arguments and Processes in CMSC 212 – S05 and more Exams Computer Science in PDF only on Docsity!

Announcements

l^

Program #3 due today 8pm

l^

Exam #2– one week from today– April 12– same room as the 1

st^ exam (ARM 0131)

  • time as the 1

st^ exam (6:00-7:15)

  • minimally comprehensive l^

Reading– for today’s lecture

  • The remaining portions of Chapter 13
    • for Thursday’s lecture
      • notes

Command Line Arguments

l^

int main(void){

l^

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

l^

Example:% args -file myfile -help– argv[0] = “args"– argv[1] = "-file"– argv[2] = "myfile"– argv[3] = "-help"

Command Line Arguments -- Table Example

int debugFlag, limit;char *inputFileName;option

optionTable[] = {

{ "-debug", Flag, &debugFlag },{ "-file", StrParam, &inputFileName },{ "-limit", IntParam, &limit },

/* possible 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",

Creating New Processes

l^

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

l^

Function Prototype

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)

l^

ps– UNIX command to see the current list of processes

Learning About Other Processes

l^

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

Additional I/O system Calls

l^

Sometimes processes want to communicate– abstraction: pipe - one process writes, other reads what was written– shell example:

ls | wc -l

l^

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

l^

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

l^

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

  • now all standard input comes from myfd

A Simple Shell Program

while (1) {

ret = fgets(line, sizeof(line), stdin);… /* convert line into array of arguments */pid = fork();if (pid == 0) {

ret = execvp(args[0], args); if (ret) {

printf("Command not found\n");exit(-1); } } else if (pid > 0) {

ret = waitpid(pid, &status, NULL); } else { … } }