Skip to content

Commit 95aee11

Browse files
committed
Files Uploaded
1 parent 5923f89 commit 95aee11

File tree

4 files changed

+119
-3
lines changed

4 files changed

+119
-3
lines changed

Assgn2Report-MA20BTECH11021.pdf

83.8 KB
Binary file not shown.

Assgn2Src-MA20BTECH11021.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <pthread.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <math.h>
5+
// Data structure to store the start value and the no of values in that thread
6+
struct ThreadParams_s
7+
{
8+
int start;
9+
int count;
10+
};
11+
// Data structure to store the final count of Perfect No in that thread and their respective values.
12+
struct ThreadResult_s
13+
{
14+
int *data;
15+
int count;
16+
};
17+
18+
void *thread_runner(void *args)
19+
{
20+
struct ThreadParams_s *params = (struct ThreadParams_s *)args;
21+
struct ThreadResult_s *result = calloc(1, sizeof(struct ThreadResult_s)); //Assigning the memory to store the result of that thread.
22+
result->data = calloc(params->count, sizeof(int));
23+
result->count = 0;
24+
FILE *fp;
25+
char filename[200];
26+
sprintf(filename, "OutFile_%d", params->start); // Since the no of threads vary , hence no of output files vary so we use sprintf to assign names to the files in terms of that thread no.
27+
fp = fopen(filename, "w");
28+
for (int i = params->start; params->count > 0; i++, params->count--)
29+
{
30+
// Running the algoritm to find whether that no is a Perfect No or not.
31+
int j = 1, sum = 0;
32+
while (j < i)
33+
{
34+
if (i % j == 0)
35+
sum += j;
36+
j++;
37+
}
38+
if (sum == i)
39+
{
40+
result->data[result->count] = i;
41+
result->count++;
42+
fprintf(fp, "%d : Is a Perfect Number.\n", i);
43+
}
44+
else
45+
fprintf(fp, "%d : Not a Perfect Number.\n", i);
46+
}
47+
fclose(fp); //Freeing up the memory.
48+
pthread_exit(result);
49+
}
50+
51+
int main()
52+
{
53+
int n, k;
54+
FILE *inp;
55+
inp = fopen("input.txt", "r");
56+
fscanf(inp, "%d %d", &n, &k); // main thread taking input and later freeing that pointer to the input file.
57+
fclose(inp);
58+
if(n<0 || k<0){
59+
printf("The input file contains negative integers."); //If the input is wrong.
60+
return 1;
61+
}
62+
int start = 1, count = n / k;
63+
if (n % k != 0)
64+
{
65+
count++; // The count of numbers to be added in each thread.
66+
}
67+
pthread_t *ptid = calloc(k, sizeof(pthread_t));
68+
struct ThreadParams_s *params = calloc(k, sizeof(struct ThreadParams_s)); //Assigning memory and values to the Threads parameters.
69+
int threadCount = 0;
70+
for (int i = 0; i < k && start <= n; i++)
71+
{
72+
params[i].start = start;
73+
params[i].count = count;
74+
if (start + count > n)
75+
params[i].count = n - start + 1;
76+
start += count;
77+
pthread_create(&ptid[i], NULL, &thread_runner, &params[i]);
78+
79+
threadCount++;
80+
}
81+
FILE *output;
82+
output = fopen("OutMain", "w"); //Creating final output file and storing the data using the result data-structure made above.
83+
for (int i = 0; i < threadCount; i++)
84+
{
85+
void *res = NULL;
86+
pthread_join(ptid[i], &res);
87+
struct ThreadResult_s *result = res;
88+
for (int j = 0; j < result->count; j++)
89+
{
90+
fprintf(output, "Thread%d : %d ", i + 1, result->data[j]);
91+
}
92+
if (result->count == 0)
93+
{
94+
fprintf(output, "Thread%d : \n", i + 1);
95+
}
96+
else
97+
fprintf(output, "\n");
98+
free(result->data);
99+
free(result);
100+
}
101+
fclose(output);
102+
free(ptid); //Freeing up all the pointers.
103+
free(params);
104+
return 0;
105+
}

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
# Computation-using-Multi-THreading
2-
# Computation-using-Multi-THreading
3-
# Computation-using-Multi-Threading
1+
# These are the steps of execution to be followed (make sure you are in a Linux machine):
2+
3+
For the compilation of the code, Enter the input line similar to the one below (check if you are operating in the same directory as the code):-
4+
5+
1) gcc Assgn2Src-MA20BTECH11021.c -o Assgn2Src-MA20BTECH11021
6+
7+
This line will compile the program code and and will create an executable file named Assgn2Src-MA20BTECH11021.
8+
9+
The executable has to be run now:-
10+
11+
2) ./Assgn2Src-MA20BTECH11021
12+
13+
This will execute the program, while reading the input from "input.txt" and generating the OutFile_i's for each thread and 1 MainOut file containing the resultant Perfect No's.

input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10 10

0 commit comments

Comments
 (0)