- Test Sept 27 (old versions available next time?)
- gdb - Gnu Debugger
- Unix debugger: breakpoints, stepping through code
- compile with -g option
- gdb program [core]
- b line
- b function
- run [arglist]
- bt
- c
- n
- s
- help
- quit
- tsearch trees
- Example: tsearch.ex.c
- Processes: What exactly is a process?
- current status of execution (context)
- resources allocated
- credentials - which user and group
- current working directory
- memory space of executing program
- UIDs, GIDs
- /etc/password
- /etc/group
- root = user ID 0 ( superuser)
- man -k userid
- man -k groupid
- man -k group | grep name
- Process IDs
- pid
- ps
- fixed PIDs
- process 0 - scheduler
- process 1 - init
- process 2 -
- PID syscalls
- Process Terminology
- parent, child
- orphan
- exit status of a child check by parent using system call wait
- zombie
- Group IDs: getgid(), getpgid()
- supplementary groups
- setgroups(int ngroups, int *gidset)
- int getgroups(int gidsetsize, int grouplist[])
- Effective, real, saved UID
- set uid,gid on exec
- Memory layout
- arguments to main
- environment
- stack
-
-
- heap
- bss -
- initialized data
- text segment
- Dumping pointers
dumpaddr(char *label, void *address){
fprintf(stdout,"%p %s\n", address, label);
}
dumpaddr("Global addresses global", &global);
- Environment
- argv style list of strings of the form "name=value"
- extern char **environ;
- Example: printargs.c
/* This example shows how to access command line arguments */
#include
main(int argc,char *argv[]) {
int i;
for(i=0; i < argc; ++i)
printf("argv[%d]=""%s""\n", i, argv[i]);
}
- Example: printenv.c
/* This example shows how to access environment variables */
#include
extern char **environ;
main(){
char **p;
for(p=environ; *p != NULL; ++p){
fprintf(stderr,"%s\n", *p);
}
}
- Environment manipulation functions
- char *getenv(name)
- setenv(name, value, overwrite)
- void unsetenv(name)
- int putenv (string)
- Notes on Environment Functions
- putenv- removes old entry if it exists
- setenv-does not remove old entry if overwrite = 0
- What happens when we try to stuff in a longer new value?
- What happens when we try to add a new name?
- int getrusage(int who, struct rusage *usage)
- struct rusage sys/reource.h page 106
- Creating new processes: fork
- Standard fork Skeleton
if((p = fork()) < 0){
}else if (p == 0) {
}else{
}
- Fork example: parentChildren.c
#include
#include
main(){
int pid, pid2, status;
if ((pid = fork()) < 0)
fatal("Cannot fork");
else if (pid == 0){ /* Child code */
printf("Child code\n");
if ((pid = fork()) < 0)fatal("Cannot fork");
else if (pid == 0){ /* Child of Child code */
printf("Child of Child code\n");
sleep(1);
exit(30);
}else { /* Parent of Child code */
printf("Parent of Child code\n");
sleep(5);
exit(31);
}
}else{ /* Parent Code */
while( pid2 = wait(&status) > 0){
printf("\n pid=%d \n return value %d \n status = %4x, status shifted=%d\n",
pid, pid2, status, status>>8);
}
}
}
fatal(char *s){
fprintf(stderr,"Fatal Error %s\n", s);
exit(0);
}