- Readings:
- Lec 1: 2.1, 2.2, 12.5 [syllabus][vi-basics][unix-basics]
- Lec 2: 12.5(strings), 3.3(stat)
- Lec 3: 3.1-3.3(stat) man ar
- Lec 4: 4.4-4.5, 3.18(chown) man 5 ar
- Lec 5: 12.4, 4.63 man make
- Lec 6: man cc, man cpp, /usr/include/stdio.h
- Lec 7: man cpp again, dumpaddr.c, printargs.c, printenv.c
- Lec 8: man gdb, [gdb quick reference], 5.10.1-5.10.2, 5.1-5.2
- Lec 9:
- Lec 10: 5.3-5.6,
- Test Plan : Closed notes, closed book, function prototypes
- STDIO Review: Some stdio functions
- the FILE struct from stdio.h
- FILE *fopen(char *path, char *mode)
- int fclose(FILE *fp)
- char *fgets(char *s, int n, FILE *stream)
- printf(char *format, ...)
- fprintf(FILE *fp, char *format, ...)
- sprintf(char *buf, char *format, ...)
-
- scanf(char *format, ...)
- fscanf(FILE *fp, char *format, ...)
- sscanf(char *buf, char *format, ...)
-
- getc, putc, getchar, putchar
- gets, fgets, puts, fputs
- fflush(FILE* fp)
- Performance issues with Stdio
- cat with read and write one byte at a time
- cat with read and write one buffer at a time
- cat with getchar and putchar
- Performance
- Buffering I/O
- three types of buffering
- unbuffered
- fully buffered
- line oriented
- int setvbuf(FILE *stream, char *buf, int type, size_t size)
- Memory Layout / Environment Revisted
- Memory layout of running process from last time
- argv style list of strings of the form "name=value"
- extern char **environ;
- Environment manipulation functions Revisited
- 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?
- fork
- Standard fork Skeleton
if((p = fork()) < 0){
}else if (p == 0) {
}else{
}
- Fork example: parentChildren.c
- What's copied?
- all variables, data space, heap stack etc.
- per process open file table
- signal table
- ...
- Properties Inherited:
- open files, IDs, cwd, root directory, umask, signal mask and table, environment, shared memory segments
- Properties Different:
- return value of fork, PID, PPID, times, file locks, pending signals
- Fork and IO example
- Fork example: forkstdio.c
- vfork
- Exec family
#include
int execl(const char *path, const char *arg0, ..., const
char *argn, char * /*NULL*/);
int execv(const char *path, char *const argv[]);
int execle(const char *path, const char *arg0, ..., const
char *argn, char * /*NULL*/, char *const envp[]);
int execve(const char *path, char *const argv[], char *const
envp[]);
int execlp(const char *file, const char *arg0, ..., const
char *argn, char * /*NULL*/);
int execvp(const char *file, char *const argv[]);
- wait
#include
#include
pid_t wait(int *stat_loc);
pid_t waitpid(pid_t pid, int *stat_loc, int options);
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
- 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?
- Fork example: parentChildren.c
- What's copied?
- all variables, data space, heap stack etc.
- per process open file table
- signal table
- ...
- Properties Inherited:
- open files, IDs, cwd, root directory, umask, signal mask and table, environment, shared memory segments
- Properties Different:
- return value of fork, PID, PPID, times, file locks, pending signals
- Fork and IO example
#include
#include
#include
main(){
int fd;
pid_t pid; /* the return value from fork */
char buf[15]; /* the buffer */
char *before,*child,*parent, *common;
before = "Before the fork\n";
child = "child after the fork\n";
parent = "parent after the fork\n";
common = "common code after the fork\n";
if((fd= open("junk",O_WRONLY|O_CREAT, 0777)) == NULL)
fatal("ForkIO Test Open of junk Failed");
write(fd, "Before the fork\n", strlen(before));
if((pid = fork()) < 0){
fatal("ForkStdio Test fork failed");
}else if (pid == 0) {
write(fd, child, strlen(child));
}else{
write(fd, parent, strlen(parent));
}
sleep(1);
write(fd, common, strlen(common));
}
fatal(char *msg)
{
fprintf(stderr,"Fatal Error: %s", msg);
exit(1);
}
- Fork example: forkstdio.c
#include
#include
main(){
FILE *fp;
pid_t pid; /* the return value from fork */
char buf[150]; /* the buffer */
if((fp= fopen("junk","w+")) == NULL)
fatal("ForkStdio Test Open of junk Failed");
setvbuf(fp, buf, _IOFBF, 20);
fprintf(fp, "Before the fork");
if((pid = fork()) < 0){
fatal("ForkStdio Test fork failed");
}else if (pid == 0) {
fprintf(fp, "child after the fork");
sleep(1);
}else{
fprintf(fp, "PARENT AFTER THE FORK");
sleep(1);
}
sleep(1);
fprintf(fp, "Common Code after the fork, pid val=%u\n",(unsigned short) pid);
}
fatal(char *msg)
{
fprintf(stderr,"Fatal Error: %s", msg);
exit(1);
}
- vfork
- Exec family
- wait