You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
structThreadResult_s*result=calloc(1, sizeof(structThreadResult_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
+
charfilename[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 (inti=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
+
intj=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
+
intmain()
52
+
{
53
+
intn, 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
+
return1;
61
+
}
62
+
intstart=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
+
structThreadParams_s*params=calloc(k, sizeof(structThreadParams_s)); //Assigning memory and values to the Threads parameters.
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.
0 commit comments