CS 241 Section Week #3 (09/11/08)
CS 241 Section Week #3 (09/11/08)
CS 241 Section Week #3 (09/11/08)
(09/11/08)
Topics This Section
Review of HW1
Lecture review
MP1
Processes
Review of HW1
Function (call by value vs call by reference)
5.
void increment(int x) {
x++;
}
...
for(x=0;x<10;increment(x)) {
printf("x=%d\n",x);
}
10.
char *s;
s=(char *) malloc (100);
s="hello";
free(s);
int **array;
int i,j,10;
array = malloc(10*sizeof(int*));
for(i=0;i<10;i++)
for(j=0;j<10;j++)
array[i][j] = i*j;
27.
char *str,*p;
...
p=str;
while(*p!=0) {
*p++;
p++;
}
30.
#define cube(x) x*x*x
#define double(x) x+x
x = 3;
y = cube(x + 1);
z = 5 * double(x);
HOME
Your home directory (can often be abbreviated as “~”)
TERM
The type of terminal you are running
PWD
Current working directory
PATH
List of directories to search for commands
PATH environment variable
Controls where commands are found
PATH is a list of directory pathnames separated by colons(:)
If a command does not begin with a slash, the shell tries
finding the command in each directory in PATH. The first
match is the command that is run.
Same PATH
PATH=/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin
Some people include “.” in PATH
It may be a security problem
extern
To indicate the existence of, and the type of, a global
variable
To inform the compiler about variables declared outside of
the current scope
Will not have any space allocated for them
To allow data to span the scope of multiple files
MP1
MP1
Part1
Write your code between the markers we provide in the
code. Don't move the comments into one line.
Recall that a char value can be thought of as either a
character or a small integer
‘a’ + 1 is ‘b’
‘Z’ - ‘A’ is 25
Part2
2e we accept both
(s1 & s2) & s3
(s1 & s2) (s2&s3)
Processes
Process Creation with fork()
Process ID (getpid())
Parent ID (getppid())
Return value of fork()
In parent, fork() returns child pid
In child, fork() returns 0
fork() Example 1
What does this do?
fprintf(stdout,”%d\n”,fork());
fork() example 2
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
}
Example 2 cont’d
This exits too quickly; let’s slow it down:
#include <sys/wait.h>
…
…
return -1;
}
if( WIFEXITED(result)) {
fprintf(stdout, ”child %d returned %d\n”,
child_pid, WEXITSTATUS(result));
exec
Before After
env env*
stack exec
free
New
heap
Program
static
code
* The program may
choose to change the
environment
exec variations
There are 6 different ways of calling exec. Which one
to use depends on three conditions:
1. How arguments are passed
2. How the path is specified
3. Whether a new environment is used
exec variations: passing parameters
#include <unistd.h>
#include <stdio.h>
}
return 0;
}