Skip to content

Commit 32cd6e9

Browse files
Add files via upload
1 parent ea5590f commit 32cd6e9

8 files changed

Lines changed: 640 additions & 0 deletions

File tree

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CC=gcc
2+
CFLAGS=-pthread -I. -Wall -Wno-int-conversion -D_GNU_SOURCE
3+
4+
binaries=pcMatrix
5+
6+
all: $(binaries)
7+
8+
pcMatrix: matrix.c pcmatrix.c tasks.c
9+
$(CC) $(CFLAGS) $^ -o $@
10+
11+
clean:
12+
$(RM) -f $(binaries) *.o
13+
14+
15+

matrix.c

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Matrix task processor - matrix module
3+
* Based on Operating Systems: Three Easy Pieces by R. Arpaci-Dusseau and A. Arpaci-Dusseau
4+
*
5+
* Wes J. Lloyd
6+
* University of Washington, Tacoma
7+
* TCSS 422 - Operating Systems
8+
* Spring 2017
9+
*/
10+
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <pthread.h>
14+
#include <assert.h>
15+
16+
int SumMatrix(int ** matrix, const int height, const int width);
17+
18+
// MATRIX ROUTINES
19+
int ** AllocMatrix(int r, int c)
20+
{
21+
int ** a;
22+
int i;
23+
a = (int**) malloc(sizeof(int *) * r);
24+
assert(a != 0);
25+
for (i = 0; i < r; i++)
26+
{
27+
a[i] = (int *) malloc(c * sizeof(int));
28+
assert(a[i] != 0);
29+
}
30+
return a;
31+
}
32+
33+
void FreeMatrix(int ** a, int r, int c)
34+
{
35+
int i;
36+
for (i=0; i<r; i++)
37+
{
38+
free(a[i]);
39+
}
40+
free(a);
41+
}
42+
43+
void GenMatrixType(int ** matrix, const int height, const int width, int type)
44+
{
45+
int i, j;
46+
if (type > 100)
47+
type = 100;
48+
if (type < 1)
49+
type = 1;
50+
for (i = 0; i < height; i++)
51+
{
52+
for (j = 0; j < width; j++)
53+
{
54+
int * mm = matrix[i];
55+
switch (type)
56+
{
57+
case 1:
58+
mm[j] = 1;
59+
break;
60+
case 2:
61+
mm[j] = j;
62+
break;
63+
default:
64+
mm[j] = rand() % type;
65+
}
66+
#if OUTPUT
67+
printf("matrix[%d][%d]=%d \n",i,j,mm[j]);
68+
#endif
69+
}
70+
}
71+
}
72+
73+
void GenMatrix(int ** matrix, const int height, const int width)
74+
{
75+
GenMatrixType(matrix, height, width, 1);
76+
}
77+
78+
// TO DO
79+
// Implement the AvgElement function
80+
int AvgElement(int ** matrix, const int height, const int width)
81+
{
82+
int sum = SumMatrix(matrix, height, width);
83+
int num = height * width;
84+
int avg = sum / num;
85+
86+
return avg;
87+
88+
}
89+
90+
int SumMatrix(int ** matrix, const int height, const int width)
91+
{
92+
int sum=0;
93+
int y=0;
94+
int i, j;
95+
for (i=0; i<height; i++)
96+
{
97+
int *mm = matrix[i];
98+
for (j=0; j<width; j++)
99+
{
100+
y=mm[j];
101+
sum=sum+y;
102+
}
103+
}
104+
return sum;
105+
}
106+
107+
void DisplayMatrix(int ** matrix, const int height, const int width, FILE *stream)
108+
{
109+
int y=0;
110+
int i, j;
111+
for (i=0; i<height; i++)
112+
{
113+
int *mm = matrix[i];
114+
fprintf(stream, "|");
115+
for (j=0; j<width; j++)
116+
{
117+
y=mm[j];
118+
if (j==0)
119+
fprintf(stream, "%3d",y);
120+
else
121+
fprintf(stream, " %3d",y);
122+
}
123+
fprintf(stream, "|\n");
124+
}
125+
}
126+

matrix.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Matrix task processor - matrix header file
3+
* Based on Operating Systems: Three Easy Pieces by R. Arpaci-Dusseau and A. Arpaci-Dusseau
4+
*
5+
* Wes J. Lloyd
6+
* University of Washington, Tacoma
7+
* TCSS 422 - Operating Systems
8+
* Spring 2017
9+
*/
10+
11+
#define ROW 5
12+
#define COL 5
13+
14+
// MATRIX ROUTINES
15+
int ** AllocMatrix(int r, int c);
16+
void FreeMatrix(int ** a, int r, int c);
17+
void GenMatrix(int ** matrix, const int height, const int width);
18+
void GenMatrixType(int ** matrix, const int height, const int width, int type);
19+
int AvgElement(int ** matrix, const int height, const int width);
20+
int SumMatrix(int ** matrix, const int height, const int width);
21+
void DisplayMatrix(int ** matrix, const int height, const int width, FILE *stream);
22+

pcMatrix

14.9 KB
Binary file not shown.

pcmatrix.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Matrix Task Processor
3+
* Based on Operating Systems: Three Easy Pieces by R. Arpaci-Dusseau and A. Arpaci-Dusseau
4+
*
5+
* Assignment 3 code
6+
* Program operates on tasks submitted to the tasks_input directory
7+
* Results are created in the tasks_output directory
8+
*
9+
* A bounded buffer is used to store pending tasks
10+
* A producer thread reads tasks from the tasks_input directory
11+
* Consumer threads perform tasks in parallel
12+
* Program is designed to run as a daemon (i.e. forever) until receiving a request to exit.
13+
*
14+
* This program mimics the client/server processing model without the use of any networking constructs.
15+
*
16+
* Wes J. Lloyd
17+
* University of Washington, Tacoma
18+
* TCSS 422 - Operating Systems
19+
* Spring 2017
20+
*/
21+
22+
#include <stdio.h>
23+
#include <stdlib.h>
24+
#include <pthread.h>
25+
#include <time.h>
26+
#include "matrix.h"
27+
#include "tasks.h"
28+
#include "pcmatrix.h"
29+
30+
int main (int argc, char * argv[])
31+
{
32+
// Uncomment to see example operation of the readtasks() routine
33+
int sleep = 500;
34+
if(argc == 2) {
35+
sleep = atoi(argv[1]);
36+
}
37+
// Uncomment to see example operation of the dotasks() routine
38+
39+
pthread_t p;
40+
pthread_t c, c1;
41+
// To do
42+
// Use pthreads
43+
// Create one pthread for readtasks()
44+
pthread_create(&p, NULL, readtasks, sleep);
45+
// Create one or more pthreads for dotasks()
46+
pthread_create(&c, NULL, dotasks, NULL);
47+
pthread_create(&c1, NULL, dotasks, NULL);
48+
pthread_join(c, NULL);
49+
pthread_join(c1, NULL);
50+
pthread_join(p, NULL);
51+
return 0;
52+
}

pcmatrix.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* matrix task processor pcmatrix header file
3+
* Based on Operating Systems: Three Easy Pieces by R. Arpaci-Dusseau and A. Arpaci-Dusseau
4+
*
5+
* Wes J. Lloyd
6+
* University of Washington, Tacoma
7+
* TCSS 422 - Operating Systems
8+
* Spring 2017
9+
*/
10+
11+
#define OUTPUT 1
12+
13+
14+

0 commit comments

Comments
 (0)