Added assignment 4 of Operating Systems.

parent 1b4d9e25
function [onb] = norma( v )
onb = v ./ sqrt(dot(v,v));
end
function [Q,R] = obn(V)
[m,n] = size(V)
A = V
Q = zeros(n,n)
for j = 1:n
% take column j of V
%v = V(:,j)
for i = 1:j-1
%a = V(:,i)
%b = v'
%c = a * b
%v = v - dot(v, V(:,i))*v
% v = v - dot(V(:,i), v)*V(:,i)
% v = v - V(:,i)*v' % proj (V(:,i), V(:,j))
% \mathbf{v}_j \leftarrow \mathbf{v}_j - \mathrm{proj}_{\mathbf{v}_{i}}
% \, (\mathbf{v}_j) (remove component in direction vi)
m = (V(:, j)'*V(:, i)) / (V(:, i)'*V(:, i));
V(:, j) = V(:, j) - m * V(:, i);
end
Q(:, j) = V(:, j) / norm(V(:, j));
%v = norma(v)
%V(:,j) = v
end
R = Q' * A
end
%obn([1,1,1;
% 0,1,1;
% 0,0,1])
%function [span, printv2, printw1] = onb2(w1, v2)
% w1 = norma(w1);
% v2 = norma(v2);
% printv2 = v2;
% printw1 = w1;
%
% v2 = cross(w1, v2);
%
% span(:, 1) = w1;
% span(:, 2) = v2;
%end
function span = onb2(w1, v2)
span = [w1, norma(v2 - dot(w1,v2)*w1)];
end
function [onb] = norma( v )
onb = v ./ sqrt(dot(v,v));
end
function [Q,R] = obn(V)
[m,n] = size(V)
A = V
Q = zeros(n,n)
for j = 1:n
% take column j of V
%v = V(:,j)
for i = 1:j-1
%a = V(:,i)
%b = v'
%c = a * b
%v = v - dot(v, V(:,i))*v
% v = v - dot(V(:,i), v)*V(:,i)
% v = v - V(:,i)*v' % proj (V(:,i), V(:,j))
% \mathbf{v}_j \leftarrow \mathbf{v}_j - \mathrm{proj}_{\mathbf{v}_{i}}
% \, (\mathbf{v}_j) (remove component in direction vi)
m = (V(:, j)'*V(:, i)) / (V(:, i)'*V(:, i));
V(:, j) = V(:, j) - m * V(:, i);
end
Q(:, j) = V(:, j) / norm(V(:, j));
%v = norma(v)
%V(:,j) = v
end
R = Q' * A
end
%obn([1,1,1;
% 0,1,1;
% 0,0,1])
%function [span, printv2, printw1] = onb2(w1, v2)
% w1 = norma(w1);
% v2 = norma(v2);
% printv2 = v2;
% printw1 = w1;
%
% v2 = cross(w1, v2);
%
% span(:, 1) = w1;
% span(:, 2) = v2;
%end
function span = onb2(w1, v2)
span = [w1, norma(v2 - dot(w1,v2)*w1)];
end
PROG = ass4
CC = gcc
CFLAGS = -std=c99 -pedantic -Wall -Wextra # -O3 -D_POSIX_SOURCE
LFLAGS = -lpthread
OFILES = main.o
RM = rm -f
$(PROG): $(OFILES)
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $(OFILES)
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
clean:
$(RM) $(OFILES) $(PROG)
File added
#include "stdio.h"
#include "stdlib.h"
#include "pthread.h"
/* temporary include: */
#include <unistd.h>
int forks_len;
pthread_mutex_t *forks;
pthread_cond_t wait_threshold;
int count = 0;
#define COUNT_LIMIT 12
void *philo_start(void *t_id) {
printf("P #%d: hello everybody!\n", (int) t_id);
for(int i = 0; i < 5; i++) {
// Let each philosopher take a random fork.
int f = rand() % forks_len;
pthread_mutex_lock(&forks[f]);
// Check the value of count and signal waiting thread when condition is
// reached. Note that this occurs while mutex is locked.
pthread_cond_signal(&wait_threshold);
// Do something useless
printf("P #%d: I'm eating.\n", (int) t_id);
sleep(1);
pthread_mutex_unlock(&forks[f]);
}
pthread_exit(NULL);
}
void host_start(int philos) {
pthread_attr_t attr;
pthread_t *threads = malloc(philos * sizeof(pthread_t));
forks = malloc(philos * sizeof(pthread_mutex_t));
forks_len = philos;
int rc;
void *status;
// POSIX standard specifies that threads are joinable by default, but
// unfortunately, not all pthread implementations set threads as joinable by
// default. Therefore, set the thread's detach state.
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_cond_init(&wait_threshold, NULL);
// Create the cutlery.
for(int i = 0; i < philos; i++)
pthread_mutex_init(&forks[i], NULL);
// Invite the philosophers.
for(int i = 0; i < philos; i++) {
printf("Host: inviting philo #%d\n", i);
if( (rc = pthread_create(&threads[i], &attr, philo_start, (void *)i)) ) {
fprintf(stderr, "pthread_create() returned: %d\n", rc);
exit(-1);
}
}
printf("Host: invitations done.\n");
// Wait on the other philosophers.
for(int i = 0; i < philos; i++) {
pthread_join(threads[i], &status);
pthread_mutex_destroy(&forks[i]);
}
printf("Host: philosophers are done.\n");
pthread_cond_destroy(&wait_threshold);
pthread_attr_destroy(&attr);
pthread_exit(NULL);
}
int main(int argc, const char **argv) {
host_start( argc > 1 ? atoi(argv[1]) : 2 );
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment