Skip to content
Snippets Groups Projects
Commit 9f011deb authored by zimko356's avatar zimko356
Browse files

lab 5 TDDE45

parents
No related branches found
No related tags found
No related merge requests found
all: workitems dividearrays fileutil
workitems: workitems.c
$(CC) -g -O0 -o $@ $< -lpthread
dividearrays: dividearrays.cpp
$(CXX) -g -O0 -o $@ $<
fileutil: fileutil.cpp
$(CXX) -g -O0 -o $@ $<
run: fileutil
./fileutil
diff -u bible.txt copy.txt
This diff is collapsed.
File added
#include <random>
int main(int argc, char **argv)
{
std::random_device generator;
std::uniform_int_distribution<int> intdist(0,50);
double res[1024];
for (int i=0; i<1024; i++) {
int nom = intdist(generator);
int denom = intdist(generator);
while(denom == 0){
denom = intdist(generator);
}
res[i] = nom / denom;
}
return 0;
}
File added
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
namespace std {
static void getline(char *line, FILE *fin)
{
int c = '\0', cur = 0;
while (c != '\n' && (c = fgetc(fin)) != EOF) {
line[cur++] = c;
}
line[cur] = '\0';
}
}
using namespace std;
int main(int argc, char **argv)
{
int cur = 0;
char *lines[16536];
char line[80];
// Fix num lines, num columns, strdup-1
FILE *fin = fopen("bible.txt", "r");
if(fin!=NULL){
while (!feof(fin)) {
getline(line, fin);
lines[cur] = (char*) malloc(strlen(line));
strcpy(lines[cur], line);
cur++;
}
fclose(fin);
}
FILE *fout = fopen("copy.txt", "w");
for (int i=0; i<cur; i++) {
fputs(lines[i], fout);
}
fclose(fout);
return 0;
}
Starting program: /home/wakakkakaaa/Documents/lab5-metaprogramming-and-debugging-lab-master/debugging/fileutil
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e51323 in _IO_feof (fp=0x0) at feof.c:35
35 feof.c: No such file or directory.
Quit
Starting program: /home/wakakkakaaa/Documents/lab5-metaprogramming-and-debugging-lab-master/debugging/fileutil
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e51323 in _IO_feof (fp=0x0) at feof.c:35
35 feof.c: No such file or directory.
Starting program: /home/wakakkakaaa/Documents/lab5-metaprogramming-and-debugging-lab-master/debugging/fileutil
Program received signal SIGSEGV, Segmentation fault.
0x00005555555552bd in main (argc=1, argv=0x7fffffffdfb8) at fileutil.cpp:30
30 lines[cur] = (char*) malloc(strlen(line));
File added
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct workitem {
void (*fn)(double *);
int current;
int len;
double *data;
int started;
} workitem;
static void* launchThread(void *in)
{
//pthread_mutex_lock(&mutex);
int n;
workitem *data = (workitem*) in;
//pthread_mutex_lock(&mutex);
volatile int started = 0;
while (!(started = data->started));
while (1) {
pthread_mutex_lock(&mutex);
n = data->current++;
if (n >= data->len) {
pthread_mutex_unlock(&mutex);
break;
}
data->fn(&data->data[n]);
pthread_mutex_unlock(&mutex);
}
pthread_mutex_unlock(&mutex);
return NULL;
}
void launchParallel(int numThreads, double *values, int len, void (*fn)(double *))
{
//pthread_mutex_lock(&mutex);
workitem *data = calloc(1, sizeof(workitem));
pthread_t th[numThreads];
memset(th, 0, numThreads*sizeof(pthread_t));
data->fn = fn;
data->current = 0;
data->len = len;
data->data = values;
data->started = 0;
//thread_mutex_lock(&mutex);
for (int i=0; i<numThreads; i++) {
//pthread_mutex_lock(&mutex);
assert(0 == pthread_create(&th[i], NULL, launchThread, data));
//pthread_mutex_unlock(&mutex);
}
pthread_mutex_lock(&mutex);
data->started = 1;
pthread_mutex_unlock(&mutex);
//pthread_mutex_lock(&mutex);
for (int i=0; i<numThreads; i++) {
assert(th[i] && 0==pthread_join(th[i], NULL));
}
//pthread_mutex_lock(&mutex);
assert(data->current >= len);
//pthread_mutex_unlock(&mutex);
free(data);
//pthread_mutex_unlock(&mutex);
}
void mul2(double *d)
{
*d = *d * 2;
}
int main(int argc, char **argv)
{
double vals[100];
for (int i=0; i<100; i++) {
vals[i] = i*1.5;
}
launchParallel(4 /* Increase this if no error occurs */, vals, 100, mul2);
for (int i=0; i<100; i++) {
if (vals[i] != 1.5*2*i) {
fprintf(stderr, "Expected vals[%d] to have value %.1f, got value %.1f\n", i, (double)1.5*2*i, (double)vals[i]);
}
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment