COMP 3000 Lab 4 2011

From Soma-notes
Revision as of 01:07, 24 October 2011 by Soma (talk | contribs) (→‎Part A)
Jump to navigation Jump to search

A few guidelines:

  • Submit your solutions for both Part A and Part B via WebCT by Friday, Monday, October 28th at 8 PM.
  • Please answer using a single text file. Do not submit doc, docx, pdf, or other formats. Also, please do not submit an archive (zip, tarball, rar) of multiple text files, just upload one. (Please don't just cut and paste your work into a text box on webct.)
  • Show all your work. Short answers are not sufficient; you should list any websites or individuals you consult and should document any experiments you conducted. For any question that you could just answer without any external reference or experiment, write "(no work required)" after your answer.
  • All submitted code should compile and run. Partial code fragments or explanations may not be given credit.

Part A

  1. Compile and run race-demo.c. What does the program output? Describe the pattern. Does the output vary depending upon the system load?
  2. What version of the glibc library is installed on the SCS lambda machines? What command did you use to figure this out?
  3. What does the file /etc/init.d/ssh do on the lambda machines?
  4. What does the file /etc/init/cron do on the lambda machines?
  5. What does the file /etc/init.d/cron do on the lambda machines?

Part B

  1. Modify race-demo.c to create rigged-race.c, a program that is identical in behavior to race-demo.c except that the consumer always wins X races while the producer wins ROUNDS-X races. Synchronize the two threads using the pthread_mutex functions (init, lock, unlock, trylock).

Programs

/* race-demo.c */

/* compile with -pthread flag, e.g.
     gcc -pthread race-demo.c -o race-demo */

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

int value;
int offset = -1;

#define SIZE 10000
#define ROUNDS 100

void *consume_func(void *arg) {
        int i,r;
        int *buffer;
        buffer = (int *) arg;

        for (r = 1; r <= ROUNDS; r++) {                
                for (i = 0; i < SIZE; i++) {
                        if (buffer[i] < r ) {
                                buffer[i] = -1;
                        }
                }
        }
    
        return NULL;
}

void *produce_func( void * arg ) {
        int i, r, iwon;
        int *buffer;
        buffer = (int *) arg;
        
        for (r = 1; r <= ROUNDS; r++) {                
                iwon = 1;
                for( i = 0; i < SIZE; i++ ) {
                        if (buffer[i] == r-1) {
                                buffer[i] = r;
                        } else if ((buffer[i] < r) && (buffer[i] != -1)) {
                                fprintf(stderr, 
                                        "Huh?! Got value %d\n",
                                        (int) buffer[i]);
                                exit(-1);
                        } else if (buffer[i] == -1) {
                                if (iwon) {
                                        printf("Consumer got to %d first on round %d.\n", i,r);
                                        iwon = 0;
                                }
                                buffer[i] = r;
                        }
                }
                
                if (iwon) {
                        printf("Producer won on round %d!\n",r);
                } else {
                        printf("Producer lost on round %d.\n",r);
                }
        }

        return NULL;
}

int main(int argc, char *argv[]) {
        int *buffer;
        pthread_t producer_thread, consumer_thread;
        int r1, r2;

        buffer = (int *) calloc(SIZE, sizeof(int));

        if (buffer == NULL) {
                fprintf(stderr, "Couldn't allocate memory for buffer!");
                exit(-1);
        }

        r1 = pthread_create(&producer_thread, NULL, produce_func, buffer);
        r2 = pthread_create(&consumer_thread, NULL, consume_func, buffer);

        if (r1 != 0 || r2 != 0) {
                fprintf(stderr, "Couldn't create threads.\n");
        }

        pthread_join(producer_thread, NULL);
        pthread_join(consumer_thread, NULL);
        
        return 0;
}