12 Pages

080710-Week9-pipes-4up

Course: CSC 209, Fall 2009
School: Toledo
Rating:
 
 
 
 
 

Word Count: 3200

Document Preview

Exchange Data between Processes Inter-Process Communication (IPC) Lecture 9 After fork() is called we end up with two independent processes We cannot use variables to communicate between processes since they each have separate address spaces, and separate memory locations One easy way to communicate is to use files, where process A writes to a file and process B reads from it Another way is to use pipes 1 2...

Register Now

Unformatted Document Excerpt

Coursehero >> Ohio >> Toledo >> CSC 209

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Exchange Data between Processes Inter-Process Communication (IPC) Lecture 9 After fork() is called we end up with two independent processes We cannot use variables to communicate between processes since they each have separate address spaces, and separate memory locations One easy way to communicate is to use files, where process A writes to a file and process B reads from it Another way is to use pipes 1 2 Acknowledgement: adapted from Karen Reids lecture slides for CSC209 winter 2007 File Objects and Descriptors The stdio library provides FILE structure which handle operations on files (see stdio.h and libio.h) FILE objects are built on top of file descriptors A file descriptor is an index into a per-process table of open file descriptors We will also use file descriptors for other communications such as pipes and sockets File Descriptors Used by low-level I/O open(), close(), read(), write() Declared as an integer int fd; A system call to convert a FILE object to a file descriptor int fileno(FILE *fp); You can also create a FILE object from a file descriptor FILE *fdopen(int fd, const char *mode); 3 4 Example #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <string.h> reading from one file and #define MAX 130 copying to another file int main() { FILE *ifp; /* input file handle */ int ofd; /* output file descriptor */ char buf[MAX]; int i, j, nread=0, nwritten=0; if((ifp = fopen("a.ics", "r")) == NULL) perror("fopen"); if((ofd = open("b.ics", O_WRONLY|O_CREAT|O_TRUNC)) < 0) perror("open"); while((fgets(buf, MAX, ifp)) != NULL) { i = strlen(buf); j = write(ofd, buf, i); nread += i; nwritten += j; } printf("%d read, %d written\n", nread, nwritten); } Process State high address stack Process control block (PCB) sp (stack pointer) pc (program counter) fd table entry file status flags current file offset vnode pointer heap uninit. data init. data text low address 5 fd 0 fd 1 fd 2 fd 3 fd table ... 6 Buffering File Buffering Buffering can be set after stream is opened but before any I/O operation takes place void setbuf(FILE *fp, char *buf); turn buffering off with buf == NULL turn on with buf pointing to buffer of length >=BUFSIZ un-buffered output appears immediately stderr is not buffered line buffered output appears when a full line has been written stdout is line buffered when going to the screen block buffered output appears when a buffer is filled or a buffer is flushed (on close or explicit flush) For greater control: int setvbuf(FILE *fp,char *buf,int mode,size_t size); normally output to a file is block buffered stdout is block buffered when redirected to a file set mode to _IOFBF, _IOLBF, or _IONBF for fully buffered, line buffered, or unbuffered buf points to user buffer, or NULL for system buffer Flush a buffer: int fflush(FILE *fp); 7 8 File Buffering #include <stdio.h> #include <stdlib.h> #define MAX 128 int main(int argc, char **argv) { FILE *ifp, *ofp; char line[MAX]; if(argc != 3) { printf("Usage: myappend infile outfile\n"); exit(1); } if((ifp=fopen(argv[1], "r"))==NULL) { perror("open infile"); exit(2); } Append one file to another file if((ofp=fopen(argv[2], "a"))==NULL) { perror("open outfile"); exit(3); } $ tail f outfile to check output while(fgets(line, MAX, ifp)!=NULL) { block file buffer (default) fprintf(ofp, line); sleep(1); buffer flushed when the file } is closed fclose(ifp); fclose(ofp); return 0; } File Buffering #include <stdio.h> #include <stdlib.h> #define MAX 128 int main(int argc, char **argv) { FILE *ifp, *ofp; char line[MAX]; if(argc != 3) { printf("Usage: myappend infile outfile\n"); exit(1); } if((ifp=fopen(argv[1], "r"))==NULL) { perror("open infile"); exit(2); } no buffer if((ofp=fopen(argv[2], "a"))==NULL) { perror("open outfile"); exit(3); output written } to file immediately setbuf(ofp, NULL); while(fgets(line, MAX, ifp)!=NULL) { fprintf(ofp, line); sleep(1); } fclose(ifp); fclose(ofp); return 0; } 9 10 File Buffering #include <stdio.h> #include <stdlib.h> #define MAX 128 int main(int argc, char **argv) { FILE *ifp, *ofp; char line[MAX]; if(argc != 3) { printf("Usage: myappend infile outfile\n"); exit(1); } if((ifp=fopen(argv[1], "r"))==NULL) { perror("open infile"); exit(2); block file buffer (default) } use fflush to force writing if((ofp=fopen(argv[2], "a"))==NULL) { line by line perror("open outfile"); exit(3); } while(fgets(line, MAX, ifp)!=NULL) { fprintf(ofp, line); File Buffering #include <stdio.h> #include <stdlib.h> #define MAX 128 int main(int argc, char **argv) { FILE *ifp, *ofp; int i=1; char line[MAX], my_line_buf[1024]; if(argc != 3) { printf("Usage: myappend infile outfile\n"); exit(1); } if((ifp=fopen(argv[1], "r"))==NULL) { perror("open infile"); exit(2); set line buffer } if((ofp=fopen(argv[2], "a"))==NULL) { output written perror("open outfile"); exit(3); } to file to file line-by-line fflush(ofp); sleep(1); } fclose(ifp); fclose(ofp); return 0; } 11 } setvbuf(ofp, my_line_buf, _IOLBF, 1024); while(fgets(line, MAX, ifp)!=NULL) { printf("output line %d\n", i++); fprintf(ofp, line); sleep(1); } fclose(ifp); fclose(ofp); return 0; 12 Unix Pipelines Simple example: wc l *.c | egrep 30 Both processes execute concurrently Pipe implemented as an internal OS buffer shared resource - concurrent access by the reader and the writer, so it must be managed carefully producer/consumer problem process A: wc process B: egrep Pipes Anonymous pipe simplex FIFO channel allows one-way communication two pipes needed for two-way communication created by process, closed when process dies created by system call pipe() Named pipe allows two-way communication (full-duplex) created by unix command mkfifo or C function mkfifo() persists as a special device file 13 14 stdin stdout stderr keyboard display pipe() System Call int pipe(int fildes[2]); Functionality creates a pair of file descriptors and stores them into fildes[0] and fildes[1] fildes[0] is used for reading from the pipe fildes[1] is used for writing to the pipe reading with fildes[1] and writing to fildes[0] undefined pipe() Example #include <unistd.h> Send one message through #include <stdlib.h> pipe: half-duplex (one-way) #include <stdio.h> communication #include <string.h> #define MSGSIZE 1024 char msg1[] = "hello, world!"; int main() { char buf[MSGSIZE]; int fildes[2], i, j; if(pipe(fildes) == -1) { perror("pipe call"); exit(1); } if((i=write(fildes[1],msg1,strlen(msg1)))<0){ perror("write call"); exit(2); } if((j= read(fildes[0], buf, MSGSIZE)) < 0) { perror("read call"); exit(3); } user p rocess fl [0 ides ] read fl [1 ides ] wr te i Return value return 0 on success, return -1 on error p ipe kerne l errno EFAULT, EMFILE, ENFILE (see manpage) 15 printf("%d bytes written, %d bytes read: %s\n", i, j, buf); return 0; } 16 Pipes and File Descriptors pipe(int fildes[2]) creates an internal system buffer and two file descriptors, one for reading and one for writing A forked child inherits file descriptors from parent After the pipe call, the parent and child should close the file descriptors for the opposite direction Create two pipes if you want to achieve fullduplex communication 17 What Happens after fork? paren p t rocess ch ld p i rocess f [0 d ] f [1 d ] f [0 d ] f [1 d ] p ipe kerne l 18 Direction of Data Flow paren t ch ld i Direction of Data Flow paren t ch ld i f [0 d ] f [1 d ] f [0 d ] f [1 d ] f [0 d ] f [1 d ] f [0 d ] f [1 d ] child to parent (close fd[1] in parent and fd[0] in child) p ipe p ipe parent to child (close fd[0] in parent and fd[1] in child) kerne l 19 kerne l 20 Full-Duplex Data Flow paren p t rocess ch ld p i rocess f [1 d2 ] f [0 d2 ] This is the sample pipe communication model in A3 f [0 d1 ] f [1 d1 ] f [1 d1 ] f [0 d1 ] f [0 d2 ] f [1 d2 ] p ipe2 fd1 used for parent-to-child communication (close fd1[0] in parent and fd1[1] in child) p ipe1 kerne l fd2 used for child-to-parent communication (close fd2[1] in parent and fd2[0] in child) 21 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/wait.h> #define MSGSIZE 1024 char msg1[] = "hello, world!"; int main() { char buf[MSGSIZE]; int fildes[2], i; pid_t pid; if(pipe(fildes) == -1) { Child process sends one perror("pipe call"); exit(1); } message to parent process pid = fork(); switch(pid) { case -1: perror("fork call"); exit(2); break; case 0: /* child */ close(fildes[0]); if((i=write(fildes[1], msg1, strlen(msg1))) < 0) { perror("write call"); exit(3); } printf("%d bytes written\n", i); break; default: /* parent */ close(fildes[1]); if((i=read(fildes[0], buf, MSGSIZE)) < 0) { perror("read call"); exit(4); } printf("%d bytes read: %s\n", i, buf); wait(NULL); } return 0; } pipe() Example with fork() 22 Size of a Pipe Minimum size of pipe is normally 512 bytes check with int fpathconf(fd[0], _PC_PIPE_BUF); Reading and Writing of a Pipe A read on an empty pipe will block until there is something to read A write on a full pipe will block until there is more space. (Pipes have a finite size) Writing to a pipe that has been closed at the read end will result in a SIGPIPE or Broken Pipe message Reading of pipe will return 0 if the write end of the pipe is closed 23 24 #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int fd[2], pipe_size; if(pipe(fd) == -1) { perror("pipe call"); exit(1); } pipe_size = fpathconf(fd[0], _PC_PIPE_BUF); printf(size of pipe: %d bytes \n", pipe_size); return 0; } Behaviors of Pipe Reading Blocked reading of a pipe returns the number of bytes read by default blocked (waiting) if pipe is empty returns 0 if writing end is closed returns -1 if error happens returns the number of bytes read by default returns -1 with errno EAGAIN if pipe is empty returns 0 if writing end is closed returns -1 if error happens Behaviors of Pipe Writing Blocked writing of a pipe returns the number of bytes written by default blocked (waiting) if pipe is full returns -1 with errno EPIPE if reading end is closed returns -1 if error happens returns the number of bytes written by default returns -1 with errno EAGAIN if pipe is full returns -1 with errno EPIPE if reading end is returns closed -1 if error happens Non-blocked reading of a pipe Non-blocked writing of a pipe 25 26 Non-block Reading/Writing Set the O_NONBLOCK flag using fcntl() #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <fcntl.h> #include <string.h> #define MSGSIZE 130 char msg1[] = "hello", msg2[] = "bye!"; int main() { int fildes[2]; void child(int p[2]); void parent(int p[2]); if(pipe(fildes) == -1) { perror("pipe call"); exit(1); } if(fcntl(fildes[0], F_SETFL, O_NONBLOCK) == -1) { perror("nonblock setting"); exit(2); } switch(fork()) { case -1: perror("fork call"); exit(3); case 0: child(fildes); break; default: parent(fildes); } return 0; } Non-block Reading/Writing void parent(int p[2]) { int nread; char buf[MSGSIZE]; close(p[1]); for(;;) { switch(nread=read(p[0],buf,MSGSIZE)){ case -1: if(errno==EAGAIN) { printf("(pipe empty)\n"); sleep(1); } else { perror("read call"); exit(4); } break; case 0: printf("write end closed\n"); exit(5); default: printf("MSG=%s\n", buf); } } } 27 void child(int p[2]) { int count; close(p[0]); for(count=0; count<3; count++) { write(p[1], msg1, strlen(msg1)+1); sleep(3); } write(p[1], msg2, MSGSIZE); } Child process sends 3 messages, with 3-second break Parent does non-block reading of pipe, and pauses 1 second if pipe empty 28 Implementation of Unix Pipeline $ sort < f1 | uniq We want the stdin of the sort process to be connected to f1 We want the stdout of the sort process to be connected to the stdin of the uniq process Duplicate an open FD using dup() or dup2() dup() and dup2() int dup(oldfd); duplicate oldfd return the duplicated FD, return -1 on error equivalent to: fcntl(oldfd, F_DUPFD, 0); int dup2(oldfd, newfd); 29 newfd and oldfd now refer to the same file if newfd is open, it is first automatically closed return newfd, return -1 on error equivalent to: close(newfd); fcntl(oldfd, F_DUPFD, newfd); 30 Example Process f1 Example Process f1 f d1 f d2 0 1 2 f2 picture after line 2 stdin stdout f d1 f d2 0 1 2 f2 picture after line 4 stdin stdout 1) 2) 3) 4) 5) 6) fd1 = open(f1, O_RDONLY); fd2 = open(f2, O_WRONLY); dup2(fd1, fileno(stdin)); dup2(fd2, fileno(stdout)); close(fd1); close(fd2); stderr 1) 2) 3) 4) 5) 6) 31 fd1 = open(f1, O_RDONLY); fd2 = open(f2, O_WRONLY); dup2(fd1, fileno(stdin)); dup2(fd2, fileno(stdout)); close(fd1); close(fd2); stderr 32 Example Process f1 f2 Pipeline Example #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main() { int fd[2], pid; int ifd = open("f1", O_RDONLY); dup2(ifd, fileno(stdin)); close(ifd); pipe(fd); if((pid=fork()) == 0) { /* child */ dup2(fd[1], fileno(stdout)); close(fd[0]); close(fd[1]); execl("/usr/bin/sort", "sort", (char *)0); } else if(pid > 0){ /* parent */ dup2(fd[0], fileno(stdin)); close(fd[1]); close(fd[0]); execl("/usr/bin/uniq", "uniq", (char *)0); } else { perror("fork"); exit(1); } return 0; } Equivalent to: sort < f1 | uniq 0 1 2 picture after line 6 stdin stdout 1) 2) 3) 4) 5) 6) fd1 = open(f1, O_RDONLY); fd2 = open(f2, O_WRONLY); dup2(fd1, fileno(stdin)); dup2(fd2, fileno(stdout)); close(fd1); close(fd2); stderr 33 34 paren t ifd 0 (stdin) 1 (stdout) 2 (stderr) fd[0] fd[1] #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main() { int fd[2], pid; int ifd = open("f1", O_RDONLY); dup2(ifd, fileno(stdin)); close(ifd); pipe(fd); if((pid=fork()) == 0) { /* child */ dup2(fd[1], fileno(stdout)); close(fd[0]); close(fd[1]); execl("/usr/bin/sort","sort",(char *)0); } else if(pid > 0){ /* parent */ dup2(fd[0], fileno(stdin)); close(fd[1]); close(fd[0]); execl("/usr/bin/uniq","uniq",(char *)0); } else { perror("fork"); exit(1); } return 0; } paren t ifd 0 1 (stdout) 2 (stderr) fd[0] fd[1] #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main() { int fd[2], pid; int ifd = open("f1", O_RDONLY); dup2(ifd, fileno(stdin)); close(ifd); pipe(fd); if((pid=fork()) == 0) { /* child */ dup2(fd[1], fileno(stdout)); close(fd[0]); close(fd[1]); execl("/usr/bin/sort","sort",(char *)0); } else if(pid > 0){ /* parent */ dup2(fd[0], fileno(stdin)); close(fd[1]); close(fd[0]); execl("/usr/bin/uniq","uniq",(char *)0); } else { perror("fork"); exit(1); } return 0; } f1 f1 35 36 paren t ifd 0 1 (stdout) 2 (stderr) fd[0] fd[1] #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main() { int fd[2], pid; int ifd = open("f1", O_RDONLY); dup2(ifd, fileno(stdin)); close(ifd); pipe(fd); if((pid=fork()) == 0) { /* child */ dup2(fd[1], fileno(stdout)); close(fd[0]); close(fd[1]); execl("/usr/bin/sort","sort",(char *)0); } else if(pid > 0){ /* parent */ dup2(fd[0], fileno(stdin)); close(fd[1]); close(fd[0]); execl("/usr/bin/uniq","uniq",(char *)0); } else { perror("fork"); exit(1); } return 0; } paren t ifd 0 1 (stdout) 2 (stderr) fd[0] fd[1] #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main() { int fd[2], pid; int ifd = open("f1", O_RDONLY); dup2(ifd, fileno(stdin)); close(ifd); pipe(fd); if((pid=fork()) == 0) { /* child */ dup2(fd[1], fileno(stdout)); close(fd[0]); close(fd[1]); execl("/usr/bin/sort","sort",(char *)0); } else if(pid > 0){ /* parent */ dup2(fd[0], fileno(stdin)); close(fd[1]); close(fd[0]); execl("/usr/bin/uniq","uniq",(char *)0); } else { perror("fork"); exit(1); } return 0; } pipe f1 f1 37 38 paren t ifd 0 1 (stdout) 2 (stderr) fd[0] fd[1] ch ld i ifd 0 (stdout) 1 (stderr) 2 fd[0] fd[1] #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> int main() { int fd[2], pid; int ifd = open("f1", O_RDONLY); dup2(ifd, fileno(stdin)); close(ifd); pipe(fd); if((pid=fork()) == 0) { /* child */ dup2(fd[1], fileno(stdout)); close(fd[0]); close(fd[1]); execl("/usr/bin/sort","sort",(char *)0); } else if(pid > 0){ /* parent */ dup2(fd[0], filen...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

Duke - ECON - 343
Econometrics III, Econ 343 (395), Tauchen, Fall 20087 Tauchen Fall 20087Econometrics III: Econ 343 (395)Description: This is the foundational course for the asymptotic theory of parametric models with some nonparametric topics considered as the ne
Duke - ECON - 139
Economics 139D/239: Introduction to Econometrics Fall Quarter 2005 Prerequisites: Economics 2, 2A, 2D, 52D or 55D; Statistics 101 or 103. Lectures: Tuesday-Thursday, 8:30-9:45 AM, 136 Social Sciences Instructor: Paul Ellickson, 210 Social Sciences (p
Duke - ECON - 343
Economics 343 Tauchen Problem Set 4 Problems are due Wednesday, September 27Fall 20061. This problem refers to the handout on the Laplace Likelihood distributed on Monday, September 18. A typo-corrected version will posted on Blackboard. A. Show
Duke - ECON - 343
Economics 343 Tauchen Problem Set 4 Due Wednesday, October 18Fall 2006The first three questions pertain to the midterm. It has been posted on Blackboard. 1. Regarding Question 1. ^ a In reference to 1-D, why does /n have to be used for the upper
Duke - ECON - 343
Economics 343 Tauchen Problem Set 7 Due Wednesday, November 1 1. Consider the non-linear least squares model yt = f (xt , 0 ) + utFall 2006where 0 is p1, E(ut |Ft ) = 0, xt is in Ft , but Var(ut |Ft ) = 2 (xt ) is not necessarily constant. For s
Duke - ECON - 343
Economics 343 Tauchen Problem Set 8 (Corrected) Due Wednesday, November 8Fall 2006WARNING: Start early. This project entails substantial computer work. You may consult with each other, but each student must write his/her own programs and answers
Duke - ECON - 343
Economics 343 Tauchen Midterm Examination Three Questions, 100 points, plus a 10 point bonus problem.Fall 2006 Note: For all questions on the following pages, full credit requires clear explanations and derivations. Point allocations are shown i
Duke - ECON - 158
Economics 158 Tauchen Problem Set 1 Assigned Problems are Due Wednesday, January 28Spring 1998I Assigned due January 28.I-1 Elton and Gruberpp. 67 68: 1 I-2 Elton and Gruber pp. 67 68: 3 Hint: Apply the formula at the bottom of p. 60. I-3 Elto
Duke - ECON - 158
Economics 158 Tauchen Problem Set 2 Assigned Problems are Due Wednesday, February 4Spring 1998I Assigned due February 4.I-1 Suppose random returns r1; r2; : : : ; rN are generated according to the modelri = ai + V + Ui ;1where ai is a const
Duke - ECON - 158
Economics 158 TauchenSpring 1998 Problem Set 4 Assigned Problems are Due Wednesday, February 18I Assigned due February 18.WARNING: The assigned problems require using the computer and a package like Excellor a regression package. You may work
Duke - ECON - 158
Economics 158 TauchenSpring 1998Problem Set 6 Assigned Problems are Due Wednesday, March 11 Note on terminology: We have been calling the line in ; space that de nes the e cient frontier the securities market line. This line is = rf + m , rf
Duke - ECON - 158
Economics 158 TauchenSpring 1998 Problem Set 8 Assigned Problems are Due Wednesday, April 1I Assigned due April 1.I-1Elton and Gruber, pp. 399 400 1. I-2 Elton and Gruber, pp. 399 400 3. I-3 Elton and Gruber, pp. 399 400 6.II Workout Problem
Duke - ECON - 158
Economics 158 TauchenTo see the consistency between the CAPM and the APT, we again use a K = 2 factor model for the returns: ri = i + bi1F1 + bi2F2 + i; i = 1; 2; : : : ; N: 1 We want to show that under the APT the mean return i satis es i = rf + m
Duke - ECON - 158
Economics 158 TauchenSpring 1998Notes on the CAPMThe capitalization of the largest stocks in the S&amp;P 500 Index in June 1997:FIRM NAME GENERAL ELECTRIC CO COCA-COLA CO MICROSOFT CORP EXXON CORP INTEL CORP MERCK &amp; CO PHILIP MORRIS COS INC ROYAL
Duke - ECON - 158
Economics 158 TauchenInterest Rate RisksSpring 1998 April 22, 1998Let denote the price at time t of a pure discount bond that pays $1 in period t + n: According to the expectations theory, 1 ,1 = =0 1 1 + r + :tn n s tn e f t;t swhere r +
Duke - ECON - 158
Economics 158 TauchenNotes on Roll's CritiqueSpring 1998 March 13, 1998The essence of Roll's critique is that given historical data on means and variances of returns, we can always nd a portfolio that plays the role of a market&quot; portfolio. For e
Duke - ECON - 158
Economics 158, Tauchen, Spring 1998Computing Statistics in a Simple CaseReturn (%) Conditions Bad Ave Good Asset #1 7 8 20 Asset #2 6 7 8 Probability 0.25 0.50 0.25mu1 var1 sigma1 mu2 var2 sigma2 cov12 rho1210.75 28.69 5.36 7.00 0.50 0.71 3.25
Duke - ECON - 158
Economics 158 TauchenNotes on Yields and Bond PricesSpring 1998 April 15, 1998BondsA typical coupon bond has coupon payment C; principle V and terminal payment V: At issue, a 10-year bond with y = 6 percent yield is priced as 1000 1000 = 1 +6
Duke - ECON - 158
Economics 158 TauchenSpring 1998Midterm Examination Four Questions, 100 points Note: For all questions, full credit requires clear a explanations or derivations. Very little credit partial will be given for unsubstantiated statements, correct or
Duke - ECON - 158
Economics 158 TauchenSpring 1998Final Examination Seven Questions, 200 points Note: For all questions, full credit requires clear explanations or derivations. Very little partial credit will be given for unsubstantiated statements, correct or not
Duke - ECON - 343
Econometrics III Tauchen Candidate Midterm ProblemsFall 20071. Let {Xn } be a sequence of random variables such that Xn = 0 with probablility 1 n an with probablility nwhere 0 &lt; n &lt; 1 for all n, and an as n . 1-A. Show that n 0 Xn 0. 1-B
Duke - ECON - 342
%!PS-Adobe-2.0 %Creator: dvips(k) 5.86 Copyright 1999 Radical Eye Software %Title: 342_03.dvi %Pages: 1 %PageOrder: Ascend %BoundingBox: 0 0 596 842 %EndComments %DVIPSWebPage: (www.radicaleye.com) %DVIPSCommandLine: dvips 342_03.dvi -o 342_03.ps %DV
Duke - ECON - 157
Economics 157 TauchenFall 1999Midterm Examination Four Questions, 100 points Note: For all questions, full credit requires clear explanations or derivations. Very little credit partial will be given for unsubstantiated statements, correct or not.
Duke - ECON - 200
%!PS-Adobe-2.0 %Creator: dvips(k) 5.86 Copyright 1999 Radical Eye Software %Title: 02_trade.dvi %Pages: 2 %PageOrder: Ascend %BoundingBox: 0 0 596 842 %EndComments %DVIPSWebPage: (www.radicaleye.com) %DVIPSCommandLine: dvips 02_trade.dvi -o 02_trade.
Duke - ECON - 200
%!PS-Adobe-2.0 %Creator: dvips(k) 5.86 Copyright 1999 Radical Eye Software %Title: 03_yldf.dvi %Pages: 2 %PageOrder: Ascend %BoundingBox: 0 0 596 842 %EndComments %DVIPSWebPage: (www.radicaleye.com) %DVIPSCommandLine: dvips 03_yldf.dvi -o 03_yldf.ps
Duke - ECON - 200
%!PS-Adobe-2.0 %Creator: dvips(k) 5.86 Copyright 1999 Radical Eye Software %Title: 06_cpr.dvi %Pages: 2 %PageOrder: Ascend %BoundingBox: 0 0 596 842 %EndComments %DVIPSWebPage: (www.radicaleye.com) %DVIPSCommandLine: dvips 06_cpr.dvi -o 06_cpr.ps %DV
Duke - ECON - 200
%!PS-Adobe-2.0 %Creator: dvips(k) 5.86 Copyright 1999 Radical Eye Software %Title: 08_fixd.dvi %Pages: 2 %PageOrder: Ascend %BoundingBox: 0 0 596 842 %EndComments %DVIPSWebPage: (www.radicaleye.com) %DVIPSCommandLine: dvips 08_fixd.dvi -o 08_fixd.ps
Duke - ECON - 200
%!PS-Adobe-2.0 %Creator: dvips(k) 5.86 Copyright 1999 Radical Eye Software %Title: 09_blks1.dvi %Pages: 3 %PageOrder: Ascend %BoundingBox: 0 0 596 842 %EndComments %DVIPSWebPage: (www.radicaleye.com) %DVIPSCommandLine: dvips 09_blks1.dvi -o 09_blks1.
Duke - ECON - 342
%!PS-Adobe-2.0 %Creator: dvipsk 5.58f Copyright 1986, 1994 Radical Eye Software %Title: 342fin00.dvi %Pages: 3 %PageOrder: Ascend %BoundingBox: 0 0 612 792 %EndComments %DVIPSCommandLine: dvips 342fin00.dvi -o 342fin00.ps %DVIPSParameters: dpi=300, c
Duke - ECON - 342
%!PS-Adobe-2.0 %Creator: dvipsk 5.58f Copyright 1986, 1994 Radical Eye Software %Title: 342mid00.dvi %Pages: 2 %PageOrder: Ascend %BoundingBox: 0 0 612 792 %EndComments %DVIPSCommandLine: dvips 342mid00.dvi -o 342mid00.ps %DVIPSParameters: dpi=300, c
Duke - ECON - 157
Economics 157 TauchenSpring 1999Final Examination Seven Questions, 200 points Note: For all questions, full credit requires clear explanations or derivations. Very little partial credit will be given for unsubstantiated statements, correct or not
Duke - ECON - 342
%!PS-Adobe-2.0 %Creator: dvipsk 5.86 p1.5d Copyright 1996-2001 ASCII Corp.(www-ptex@ascii.co.jp) %based on dvipsk 5.86 Copyright 1999 Radical Eye Software (www.radicaleye.com) %Title: 342_03.dvi %Pages: 1 %PageOrder: Ascend %BoundingBox: 0 0 596 842
Duke - ECON - 342
%!PS-Adobe-2.0 %Creator: dvipsk 5.86 p1.5d Copyright 1996-2001 ASCII Corp.(www-ptex@ascii.co.jp) %based on dvipsk 5.86 Copyright 1999 Radical Eye Software (www.radicaleye.com) %Title: 342_04.dvi %Pages: 1 %PageOrder: Ascend %BoundingBox: 0 0 596 842
Duke - ECON - 157
Economics 157 TauchenFall 1999Notes on Expected UtilityExpected Utility:0Let U W denote the utility function of end-of-period wealth. It is reasonable to assume U W 0; and, for a risk averter, U 00W 0: According to expected utility theory
Duke - ECON - 157
Economics 157 TauchenFall 1999Notes on the Portfolio Possibility Curve PPC2 2 Suppose there are two risky assets with returns r1 and r2 such that 1 2 and 1 2 : Let w 0 denote the proportion invested in portfolio 2: Then p = 1 , w1 + w2 2 2 2 2
Duke - ECON - 157
Economics 157 TauchenFall 1999Diversi cationLet rp = PN wiri denote a portfolio return and recall that the portfolio variance Bodie, i=1 Kane, Marcus p. 213 isRisksp=2N X w2 i=1i i+2N N XXw w i=1 j =i6i j ij :Consider equal inves
Duke - ECON - 157
Economics 157 TauchenNotes on Telser's CriterionFall 1999Telser's criterion is to maximize subject top = Erp ;Prrp rL ; where rp is the portfolio return, is a small number, e.g., = 0:05 and rL is some predetermined lower limit. If returns
Duke - ECON - 157
Economics 157 TauchenFall 1999Suppose the returns generating process isriFirst Look at APT= i + biF +rpi;i= 1; 2; : : : ; Ni;2where EF = 0; VarF = is whereF;2E i = 0; Var i =CovF; i = 0: The portfolio returnPN p=i=1 w
Duke - ECON - 157
Economics 157 TauchenFall 1999The APTThe Returns Process:For simplicity, we use a J = 2 factor model for the returns generating process: ri = i + bi1 F1 + bi2 F2 + i ; i = 1; 2; : : : ; N 1 where F1 and F2 are two common random risk factors s
Duke - ECON - 157
Economics 157 TauchenFall 1999The gure below shows below shows the Standard and Poor's Composite Price Index The market&quot; from January 1, 1971 April 8, 1999.Levels Series 1200Standard and Poor's Composite Index10008006004002000 1970
Duke - ECON - 157
Economics 157 Tauchen The recent history of U.S. data is shown below:20 15 10 5 0 1960 20 15 10 5 0 1960 20 15 10 5 0 1960 1965 1970 1975 1980 1985 1990 1965 1970 1975 1980 1985 US 10Year Rate 1990 1965 1970 1975 1980 1985 US 1Year Rate 1990Fall 1
Duke - ECON - 157
Economics 157 TauchenFall 1999Fixed Income and DurationT T TValuationTConsider a portfolio of bonds with cash ows G1; G2; : : : :; G ; in periods j = 1; 2; : : : ; J: The value of the portfolio is X G V0 =J JLet denote the price of a pu
Duke - ECON - 157
Economics 157 TauchenFall 1999We want to derive the relationship between asset prices and payouts. Let Y denote the random payout of an asset with mean EY and variance Var Y. Think of Y as the total pro t one period hence of a particular investm
Duke - ECON - 157
Economics 157 TauchenFall 1999Valuation FormulasThe Constant Growth ModelWe assume, for now, that g k else the sum is in nite, which is totally unrealistic. Recall the formula for a geometric series 1 1 ; 2 ai = 1 , a i=0 which gives the valu
Duke - ECON - 157
Economics 157 Consider a European call option with the following characteristics: r = continuously compounded interest rate C0 = current value of the option S0 = current price of the stock T = time remaining until expiration, 30 365, 60 365 = stan
Duke - ECON - 157
Balance Sheet of ABC Corporation12/31/97 12/31/986/7/2009AssetsCurrentCash Receivables Inventories 45 ($Million) 8 6 55 ($Million) 12 5Long TermPlants Land 240 100 73 472 ($Million) 255 110 81 518 ($Million)Intangible TOTAL ASSETSLiabil
Duke - ECON - 344
%!PS-Adobe-2.0 %Creator: dvipsk 5.86 p1.5d Copyright 1996-2001 ASCII Corp.(www-ptex@ascii.co.jp) %based on dvipsk 5.86 Copyright 1999 Radical Eye Software (www.radicaleye.com) %Title: 342fin02.dvi %Pages: 3 %PageOrder: Ascend %BoundingBox: 0 0 596 84
BYU - ID - CHEM - 461
Chemistry 461 Homework Assignment #1. Due 8 September 2004 Work as many of the problems from Chapter 1 as you can, but turn in the following: 3, 5, 7, 11, 12, 14, 16, 19, 22, 23, 25, 34, 35, 37, 39 Notes: #5: Use the solver in Excel or another spread
BYU - ID - CHEM - 461
Chemistry 461 Homework Assignment #2. Due 15 September 2004 A. Work as many of the problems from Chapter 2 as you can, but turn in the following: 1, 2, 3, 4, 5, 7, 12 Try to do number 9 also. You will be judged on your attempt, not your success. B. R
BYU - ID - CHEM - 461
Chemistry 461 Homework Assignment #3. Due 23 September 2005 Work as many of the problems from Chapter 3 as you can, but turn in the following: 1, 3, 5, 8, 9, 14, 16, 23, 24, 29 Notes: All problems should be done BY HAND. Do not use a graphing calcula
BYU - ID - CHEM - 461
Chemistry 461 Homework Assignment #4. Due 30 September 2005 Work as many of the problems from Chapter 4 as you can, but turn in the following: 1, 2, 3, 5, 8, 9, 10, 14, 16, 17, 20, 23, 26, 29, 32, 35, 36 Notes: 1, 5: Use a graphing calculator or spre
BYU - ID - CHEM - 461
Chemistry 461 Homework Assignment #6 Due 24 October 2005 Work as many of the problems from Chapter 6 as you can, but turn in the following: 1, 5, 8, 10, 13, 17, 20, 21, 26, 29, 34, 39, 34-47 Notes and adjustments: #13-you've already done this once in
Duke - ECON - 201
Preliminary Work on the Effect of a Flagged Market Jump on an Equity's BetaJunior Research Seminar Economics 201FSOutline Review Beta Estimate Time Horizon Leads/Lags Shifting Objective/TimelineCapital Asset Pricing ModelReturn of Equity
Duke - ECON - 201
Analyzing the Effect of a Market Jump on an Equitys ReturnsJunior Research Seminar Economics 201FSOutline Objective Procedure Summation of Results TimelineCapital Asset Pricing ModelReturn of Equity = Risk-free rate + (Beta * Market Premium
Duke - ECON - 201
Forecasting Realized Variance Using JumpsAndrey Fradkin Econ 201 4/18/2007Outline Theoretical Background The HARRVCJ Model Is the HARRVCJ model better than the HAR RV model? Does IV contain more information than RV, C, J? How does market ris
Duke - ECON - 201
Comparing Realized and BiPower Variation in Lee-Mykland StatisticWarren Davis April 11 PresentationOutline Discussion of Lee-Mykland Change of Statistic Simulation Set-Up Simulation Results Future DirectionsLee and Mykland (2006)The Bi-Po
Duke - ECON - 342
Economics 342 Tauchen Problem Set 1 Assigned Problems are due Wednesday, March 23Spring 2005I Assigned (Due March 23).I-1 Greene [19] pp. 606607 #1 I-2 Greene [19] pp. 606607 #2 I-3 Suppose the distributed lag model is4yt = +k=0k xt-k + t
Duke - ECON - 342
%!PS-Adobe-2.0 %Creator: dvips(k) 5.86 Copyright 1999 Radical Eye Software %Title: 342_01.dvi %Pages: 2 %PageOrder: Ascend %BoundingBox: 0 0 612 792 %DocumentFonts: CMR12 CMBX12 CMMI12 CMMI8 CMR8 CMEX10 CMSY8 CMSY10 %EndComments %DVIPSWebPage: (www.r
Duke - ECON - 342
Economics 342 Tauchen Problem Set 2 Assigned Problems are due Wednesday, March 30Spring 2005The purpose of this assignment is to estimate a small scale VAR for the US economy. You may work in groups of 2 or 3 (maximum) to estimate the models. Eac
Duke - ECON - 342
%!PS-Adobe-2.0 %Creator: dvips(k) 5.86 Copyright 1999 Radical Eye Software %Title: 342_02.dvi %Pages: 2 %PageOrder: Ascend %BoundingBox: 0 0 612 792 %DocumentFonts: CMR12 CMBX12 CMMI12 CMSY10 CMMI8 CMEX10 CMR8 CMSY8 %+ CMTI12 %EndComments %DVIPSWebPa
Duke - ECON - 342
Economics 342 Tauchen Problem Set 3 Assigned Problems are due Wednesday, April 6Spring 2005I Assigned (Due April 6)The folder unemployment contains data on the U.S. unemployment rate, monthly 1960:01 2005:02. The purpose of questions 13 is to ad