HW3: "Fun" with Memory!

CS 321 Homework, Dr. Lawlor, 2006/03/01.  Due Monday, March 6 at 5pm.

Here's a series of problems regarding the handling of memory.  You're expected to turn in each problem by giving it a particular name inside of netrun, which you'll need to sign up for online if you haven't already.

NetRun has online grading, so you're done once you see this response:

Grading

CS Problem 321_HW3_1 output looks OK!

Note that you MUST give the programs the correct name, or they won't get graded! Also, the LAST program you've run under that name before the deadline is the one I'll take!  

Hint: read the lecture notes.  You'll have to add the correct "#include" line for each problem.

  1. Name as "321_HW3_1".  Make this program respond to the segfault signal by calling the mySignalHandler routine.
    (Executable NetRun link)
    /* Call this routine when a signal happens */
    void mySignalHandler(int signal_number)
    {
    printf("mySignalHandler> hit signal %d! OK!\n",signal_number);
    exit(1);
    }

    int foo(void) {
    int *badPointer=(int *)0;
    /* add code HERE to make mySignalHandler get called on a segfault */
    printf("Now segfaulting (should call mySignalHandler)\n");
    (*badPointer)=0;
    printf("Back from segfault?!\n");
    return 0;
    }

  2. Name as "321_HW3_2".  Make this program NOT give a segfault, by mapping in read/write memory at the bad address in the "notSoBad" routine. You'll have to use the UNIX interfaces to do this.
    (Executable NetRun link)
    /* Puts some memory at this pointer */
    void notSoBad(void *ptr)
    {
    /* Your code here */
    }

    /* You may NOT change the foo routine! */
    int foo(void) {
    int *badPointer=(int *)0;
    notSoBad(badPointer); /* Put memory at the bad pointer */
    printf("Now accessing the pointer %p\n",badPointer);
    (*badPointer)=0;
    printf("I guess that's an OK pointer!\n");
    return 0;
    }

  3. Name as "321_HW3_3".  This program runs out of memory, but it only ever uses about 60MB of memory, and NetRun lets you use 100MB.  Fix the allocateBufs routine to not run out of memory. It may help to read this new lecture note on fragmentation
    (Executable NetRun link)
    /* One linked-list node. Points to an allocated buffer */
    struct llNode {
    llNode *next; /* Points to next entry in the linked list */
    char *buf; /* allocated buffer */
    /* Constructor: create a linked-list node */
    llNode(llNode *next_,char *buf_)
    :next(next_), buf(buf_) {}
    /* Return number of nodes in linked list */
    int count(void) {if (next) return 1+next->count(); else return 1;}
    /* Free all the allocated buffers in this list */
    void freeBuf(void) {
    delete[] buf; buf=0;
    if (next) next->freeBuf();
    }
    };

    /* Allocate number different buffers of this size.
    Return a linked list of the allocated buffers. */
    llNode *allocateBufs(int size,int number) {
    llNode *head=NULL;
    for (int i=0;i<number;i++) {
    char *buf=new char[size];
    llNode *n=new llNode(head,buf);
    head=n;
    if ((i%100)==0)
    printf("Allocated %dth %d bytes at %p, link at %p\n",
    i,size,buf,n);
    }
    return head;
    }

    /* Use the buffers, then throw them away. */
    void processBufs(llNode *l,int size) {
    llNode *cur=l;
    while (cur!=0) {
    cur->buf[size-1]='K';
    cur=cur->next;
    }
    l->freeBuf();
    }

    /* You MAY NOT modify the foo routine... */
    int foo(void) {
    int nLinks=0;

    try {
    llNode *lA=allocateBufs(60000,1000);
    processBufs(lA,60000);
    llNode *lB=allocateBufs(61000,1000);
    processBufs(lB,61000);
    nLinks=lA->count()+lB->count();
    /* don't worry about throwing away the lA and lB links... */
    } catch (...) {
    printf("Error allocating memory!\n");
    }
    return nLinks;
    }

Open-Ended Problems

Half your homework grade is to complete any five open-ended problems (miniprojects) throughout the semester.  I'll try to offer at least one open-ended problem with each week's homework.  If you complete more than five open-ended problems through the semester, I'll take the five with the highest grades. 

Turn in open-ended problems by naming the source code "problem.cpp" and submitting it on blackboard as HW3_O.  HW3_O is due Monday, March 20 (after spring break) at 5pm.
  1. Write a program that does something interesting with memory mapping. For example, it could receive a segfault and map in the *appropriate* memory block. It may help to use the "badmemlib" and "mmap" or "mmapfile" libraries in this (Directory, Zip, Tar-gzip).