Study Guide for Computer Architecture and Operating Systems | ECE 3055, Study notes of Electrical and Electronics Engineering

Material Type: Notes; Professor: Yalamanchili; Class: Computer Arch & Oper Sys; Subject: Electrical & Computer Engr; University: Georgia Institute of Technology-Main Campus; Term: Fall 2007;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-mfq
koofers-user-mfq 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
http://www.cs.nmsu.edu/~jcook/Tools/pthreads/rw.c
1 of 4 7/23/2007 9:26 AM
/*
* File : rw.c
*
* Title : Demo Readers/Writer.
*
* Short : A solution to the multi-reader's, one writer problem.
*
* Long :
*
* Author : Andrae Muys
*
* Date : 18 September 1997
*
* Revised :
*/
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define MAXCOUNT 5
#define READER1 50000
#define READER2 100000
#define READER3 400000
#define READER4 800000
#define WRITER1 150000
typedef struct {
pthread_mutex_t *mut;
int writers;
int readers;
int waiting;
pthread_cond_t *writeOK, *readOK;
} rwl;
rwl *initlock (void);
void readlock (rwl *lock, int d);
void writelock (rwl *lock, int d);
void readunlock (rwl *lock);
void writeunlock (rwl *lock);
void deletelock (rwl *lock);
typedef struct {
rwl *lock;
int id;
long delay;
} rwargs;
rwargs *newRWargs (rwl *l, int i, long d);
void *reader (void *args);
void *writer (void *args);
static int data = 1;
int main ()
{
pthread_t r1, r2, r3, r4, w1;
rwargs *a1, *a2, *a3, *a4, *a5;
rwl *lock;
lock = initlock ();
a1 = newRWargs (lock, 1, WRITER1);
pthread_create (&w1, NULL, writer, a1);
a2 = newRWargs (lock, 1, READER1);
pthread_create (&r1, NULL, reader, a2);
a3 = newRWargs (lock, 2, READER2);
pf3
pf4

Partial preview of the text

Download Study Guide for Computer Architecture and Operating Systems | ECE 3055 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

  • File : rw.c
  • Title : Demo Readers/Writer.
  • Short : A solution to the multi-reader's, one writer problem.
  • Long :
  • Author : Andrae Muys
  • Date : 18 September 1997
  • Revised : */

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

#define MAXCOUNT 5

#define READER1 50000 #define READER2 100000 #define READER3 400000 #define READER4 800000 #define WRITER1 150000

typedef struct { pthread_mutex_t *mut; int writers; int readers; int waiting; pthread_cond_t *writeOK, *readOK; } rwl;

rwl *initlock (void); void readlock (rwl *lock, int d); void writelock (rwl *lock, int d); void readunlock (rwl *lock); void writeunlock (rwl *lock); void deletelock (rwl *lock);

typedef struct { rwl *lock; int id; long delay; } rwargs;

rwargs *newRWargs (rwl *l, int i, long d); void *reader (void *args); void *writer (void *args);

static int data = 1;

int main () { pthread_t r1, r2, r3, r4, w1; rwargs *a1, *a2, *a3, *a4, *a5; rwl *lock;

lock = initlock (); a1 = newRWargs (lock, 1, WRITER1); pthread_create (&w1, NULL, writer, a1); a2 = newRWargs (lock, 1, READER1); pthread_create (&r1, NULL, reader, a2); a3 = newRWargs (lock, 2, READER2);

pthread_create (&r2, NULL, reader, a3); a4 = newRWargs (lock, 3, READER3); pthread_create (&r3, NULL, reader, a4); a5 = newRWargs (lock, 4, READER4); pthread_create (&r4, NULL, reader, a5); pthread_join (w1, NULL); pthread_join (r1, NULL); pthread_join (r2, NULL); pthread_join (r3, NULL); pthread_join (r4, NULL); free (a1); free (a2); free (a3); free (a4); free (a5);

return 0; }

rwargs *newRWargs (rwl *l, int i, long d) { rwargs *args;

args = (rwargs *)malloc (sizeof (rwargs)); if (args == NULL) return (NULL); args->lock = l; args->id = i; args->delay = d; return (args); }

void *reader (void *args) { rwargs *a; int d;

a = (rwargs *)args;

do { readlock (a->lock, a->id); d = data; usleep (a->delay); readunlock (a->lock); printf ("Reader %d : Data = %d\n", a->id, d); usleep (a->delay); } while (d != 0); printf ("Reader %d: Finished.\n", a->id);

return (NULL); }

void *writer (void *args) { rwargs *a; int i;

a = (rwargs *)args;

for (i = 2; i < MAXCOUNT; i++) { writelock (a->lock, a->id); data = i; usleep (a->delay); writeunlock (a->lock); printf ("Writer %d: Wrote %d\n", a->id, i); usleep (a->delay); } printf ("Writer %d: Finishing...\n", a->id); writelock (a->lock, a->id); data = 0; writeunlock (a->lock); printf ("Writer %d: Finished.\n", a->id);

return (NULL); }

void writeunlock (rwl *lock) { pthread_mutex_lock (lock->mut); lock->writers--; pthread_cond_broadcast (lock->readOK); pthread_mutex_unlock (lock->mut); }

void deletelock (rwl *lock) { pthread_mutex_destroy (lock->mut); pthread_cond_destroy (lock->readOK); pthread_cond_destroy (lock->writeOK); free (lock);

return; }