Skip to content
This repository was archived by the owner on Sep 29, 2022. It is now read-only.

Commit 9abaf9d

Browse files
author
Oswaldo Hernandez
authored
Merge branch 'VictorRodriguez:main' into main
2 parents b143c69 + 8aff21a commit 9abaf9d

File tree

10 files changed

+317
-4
lines changed

10 files changed

+317
-4
lines changed

labs/04/README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ Develop the skills to program an application that solves a problem using threads
88
* Linux machine, either a VM or a baremetal host
99
* GCC compiler (at least version 4.8)
1010
* shell scripting
11+
* git send mail server installed and configured on your Linux machine
1112

1213
## Instructions
1314

1415
* Clone the repository
15-
* Go to labs/04
16+
* Go to operating-systems-lecture/labs/04
17+
* Read examples from operating-systems-lecture/labs/04
1618
* Calculate the value of Pi ( 3.141596 ) using this algorithm:
1719

1820
```
@@ -34,7 +36,7 @@ PI = 4.0*circle_count/npoints
3436

3537
more info: https://computing.llnl.gov/tutorials/parallel_comp/ )
3638

37-
Please send the code as Pull request and include in the comments of the code (at
39+
Please send the code as a patch and include in the comments of the code (at
3840
the top), the results in terms of a link to a graph that shows the speed
3941
improvements, how much time does it take to calculate pi with 1 thread, 2
4042
threads 4 threads 8 threads and 16 threads?
@@ -64,9 +66,16 @@ the link could be to a google documents pdf. please create good graphs:
6466
3.151596...
6567
```
6668

67-
## How to do a pull request:
68-
https://docs.github.com/en/github/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request
69+
## Please send the mail as git send mail:
6970

71+
```
72+
$ git add my_terminal.c
73+
$ git commit -s -m <STUDENT-ID>-homework-04
74+
$ git send-email -1
75+
76+
```
77+
Do some tests sending the mail to your personal account, if you get the mail,
78+
then you can be sure I will get the mail
7079

7180
# Time to do the homework:
7281

labs/04/findCircle

8.28 KB
Binary file not shown.

labs/04/findCircle.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <math.h>
4+
5+
int main(){
6+
double r=0.5;
7+
double x=0.5;
8+
double y=0;
9+
10+
double d = pow(r,2) - (pow(x,2) + pow(y,2));
11+
printf("d = %f \n", d);
12+
13+
if(d>0){
14+
printf("inside");
15+
}else if(d==0){
16+
printf("on the circumference");
17+
}else{
18+
printf("outside");
19+
}
20+
}

labs/04/mutex-thread

8.83 KB
Binary file not shown.

labs/04/practice1

9.29 KB
Binary file not shown.

labs/04/practice1.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Link a grafica:
3+
https://docs.google.com/spreadsheets/d/193QjARyWOg6bN4Dingg4aaYqNGzAMB6qSqdAcy1Kqfw/edit?usp=sharing
4+
5+
Autor:
6+
Andres Remis Serna A00829053
7+
8+
Con apoyo de los siguientes repositorios:
9+
https://github.com/VictorRodriguez/operating-systems-lecture/blob/master/labs/04/pi.c
10+
https://github.com/VictorRodriguez/operating-systems-lecture/blob/master/labs/04/mutex-thread.c
11+
*/
12+
13+
#include <pthread.h>
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
#include <math.h>
17+
#include <time.h>
18+
19+
#define NUM_THREADS 75
20+
21+
int npoints = 100000;
22+
int circle_count = 0;
23+
double radius=1;
24+
pthread_mutex_t lock;
25+
int pointsThread;
26+
27+
28+
/*https://github.com/VictorRodriguez/operating-systems-lecture/blob/master/labs/04/pi.c */
29+
30+
unsigned int seed;
31+
32+
double r2(){
33+
return (double)rand_r(&seed) / (double)((unsigned)RAND_MAX + 1);
34+
}
35+
36+
/*fin github*/
37+
38+
int find_circle(double x, double y){
39+
40+
double d = pow(radius,2) - (pow(x,2) + pow(y,2));
41+
42+
if(d>=0){
43+
return 1;
44+
}else{
45+
return 0;
46+
}
47+
}
48+
49+
void * Count(void * a){
50+
int cont=0;
51+
pthread_mutex_lock(&lock);
52+
for (int j=1; j<=pointsThread; j++){
53+
//generate 2 random numbers between 0 and 1
54+
double xcoordinate = r2();
55+
double ycoordinate = r2();
56+
int circ=find_circle(xcoordinate,ycoordinate);
57+
if (circ==1) {
58+
circle_count = circle_count + 1;
59+
}else{
60+
cont++;//end do
61+
}
62+
}
63+
pthread_mutex_unlock(&lock);
64+
return NULL;
65+
}
66+
67+
int main(int argc, char *argv[]){
68+
clock_t start, end;
69+
double cpu_time_used;
70+
71+
start = clock();
72+
if (pthread_mutex_init(&lock, NULL) != 0){
73+
printf("\n mutex init failed\n");
74+
return 1;
75+
}
76+
//do j = 1,npoints
77+
seed = time(NULL);
78+
79+
pthread_t threads[NUM_THREADS];
80+
pointsThread= npoints/NUM_THREADS;
81+
82+
83+
for(int i=0; i<NUM_THREADS; i++){
84+
pthread_create(&threads[i], NULL, Count, NULL);
85+
}
86+
87+
for(int i=0; i<NUM_THREADS; i++){
88+
pthread_join(threads[i], NULL);
89+
}
90+
91+
double PI = 4.0*(double)circle_count/(double)npoints;
92+
end = clock();
93+
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
94+
printf("PI = %f\nTime used=%f",PI, cpu_time_used);
95+
pthread_mutex_destroy(&lock);
96+
}

labs/04/raceCondition

8.77 KB
Binary file not shown.

labs/04/raceCondition.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <pthread.h>
2+
#include <semaphore.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
#define NITER 1000000
7+
8+
int cnt = 0;
9+
10+
pthread_mutex_t lock;
11+
12+
void * Count(void * a){
13+
pthread_mutex_lock(&lock);
14+
int i, tmp;
15+
for(i = 0; i < NITER; i++)
16+
{
17+
tmp = cnt; /* copy the global cnt locally */
18+
tmp = tmp+1; /* increment the local copy */
19+
cnt = tmp; /* store the local value into the global cnt */
20+
}
21+
pthread_mutex_unlock(&lock);
22+
return NULL;
23+
}
24+
25+
int main(int argc, char * argv[])
26+
{
27+
pthread_t tid1, tid2;
28+
29+
if (pthread_mutex_init(&lock, NULL) != 0){
30+
printf("\n mutex init failed\n");
31+
return 1;
32+
}
33+
34+
if(pthread_create(&tid1, NULL, Count, NULL))
35+
{
36+
printf("\n ERROR creating thread 1");
37+
exit(1);
38+
}
39+
40+
if(pthread_create(&tid2, NULL, Count, NULL))
41+
{
42+
printf("\n ERROR creating thread 2");
43+
exit(1);
44+
}
45+
46+
if(pthread_join(tid1, NULL)) /* wait for the thread 1 to finish */
47+
{
48+
printf("\n ERROR joining thread");
49+
exit(1);
50+
}
51+
52+
if(pthread_join(tid2, NULL)) /* wait for the thread 2 to finish */
53+
{
54+
printf("\n ERROR joining thread");
55+
exit(1);
56+
}
57+
58+
if (cnt < 2 * NITER)
59+
printf("\n BOOM! cnt is [%d], should be %d\n", cnt, 2*NITER);
60+
else
61+
printf("\n OK! cnt is [%d]\n", cnt);
62+
63+
pthread_mutex_destroy(&lock);
64+
}

labs/04/race_condition.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <pthread.h>
2+
#include <semaphore.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
#define NITER 1000000
7+
8+
int cnt = 0;
9+
10+
void * Count(void * a)
11+
{
12+
int i, tmp;
13+
for(i = 0; i < NITER; i++)
14+
{
15+
tmp = cnt; /* copy the global cnt locally */
16+
tmp = tmp+1; /* increment the local copy */
17+
cnt = tmp; /* store the local value into the global cnt */
18+
}
19+
}
20+
21+
int main(int argc, char * argv[])
22+
{
23+
pthread_t tid1, tid2;
24+
25+
if(pthread_create(&tid1, NULL, Count, NULL))
26+
{
27+
printf("\n ERROR creating thread 1");
28+
exit(1);
29+
}
30+
31+
if(pthread_create(&tid2, NULL, Count, NULL))
32+
{
33+
printf("\n ERROR creating thread 2");
34+
exit(1);
35+
}
36+
37+
if(pthread_join(tid1, NULL)) /* wait for the thread 1 to finish */
38+
{
39+
printf("\n ERROR joining thread");
40+
exit(1);
41+
}
42+
43+
if(pthread_join(tid2, NULL)) /* wait for the thread 2 to finish */
44+
{
45+
printf("\n ERROR joining thread");
46+
exit(1);
47+
}
48+
49+
if (cnt < 2 * NITER)
50+
printf("\n BOOM! cnt is [%d], should be %d\n", cnt, 2*NITER);
51+
else
52+
printf("\n OK! cnt is [%d]\n", cnt);
53+
54+
pthread_exit(NULL);
55+
}
56+
57+
58+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
#
3+
# Round Robin calculator
4+
#
5+
# Simulate the Round Robin mechanism based on a queue of processes
6+
# Print the Avg turn around time
7+
#
8+
# It can be used from command line
9+
#
10+
# process = [] might be a random list of elapsed times
11+
#
12+
# Experiment with this and make 3D graphs for:
13+
#
14+
# avg_turn_time vs quantum vs max process size
15+
#
16+
17+
import sys, getopt
18+
19+
def main(argv):
20+
21+
quantum = 4
22+
context_switch = 2
23+
execution_time = 0
24+
avg_turnaround_tm = 0.0
25+
context_switchs = 0
26+
27+
processes =[2,3,4,12,8,5,6,1]
28+
num_processes = len(processes)
29+
30+
try:
31+
opts, args = getopt.getopt(argv,"hq:",["quantum_time="])
32+
except getopt.GetoptError:
33+
print("round_robin.py -q <quantum time>")
34+
sys.exit(-1)
35+
for opt, arg in opts:
36+
if opt == '-h':
37+
print ("test.py -q <quantum time>")
38+
sys.exit()
39+
elif opt in ("-q", "--quantum_time"):
40+
quantum = int(arg)
41+
42+
for process in processes:
43+
if process <= quantum:
44+
print("process executed")
45+
execution_time+=process
46+
print("execution time " + str(execution_time))
47+
else:
48+
print("process interrupted")
49+
remain_time = process - quantum
50+
context_switchs+=1
51+
print("time process executed: "+ str(quantum))
52+
execution_time+=quantum
53+
execution_time+=context_switch
54+
print("execution time " + str(execution_time))
55+
processes.append(remain_time)
56+
57+
print(processes)
58+
print()
59+
60+
avg_turnaround_tm = float(execution_time)/float(num_processes)
61+
print ("Number of processes : " + str(num_processes))
62+
print ("Number of context switch : " + str(context_switchs))
63+
print ("Avg turn around time : " + str(avg_turnaround_tm))
64+
65+
if __name__ == "__main__":
66+
main(sys.argv[1:])

0 commit comments

Comments
 (0)