Skip to content

Commit 9574983

Browse files
committed
Multithreading Problems
1 parent 3ee30c4 commit 9574983

23 files changed

+1278
-0
lines changed

Lab6/COE18B061.pdf

970 KB
Binary file not shown.

Lab6/Q10_LongestCommonSubsequence

12.6 KB
Binary file not shown.

Lab6/Q10_LongestCommonSubsequence.c

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<string.h>
4+
#include<pthread.h>
5+
6+
#define MAX 1000
7+
8+
struct length
9+
{
10+
int len1;
11+
int len2;
12+
};
13+
14+
char s1[MAX], s2[MAX];
15+
16+
void *LCS(void *arg);
17+
int max(int a, int b);
18+
19+
int main()
20+
{
21+
printf("\nEnter the string 1: ");
22+
scanf("%s", s1);
23+
24+
printf("\nEnter the string 2: ");
25+
scanf("%s", s2);
26+
27+
struct length *param = (struct length *)malloc(sizeof(struct length));
28+
29+
param->len1 = strlen(s1); //length of string 1
30+
param->len2 = strlen(s2); //length of string 2
31+
32+
pthread_t tid; //returns the thread id of thread created
33+
pthread_attr_t attr; //to define thread attributes
34+
pthread_attr_init(&attr); //initializes the thread attributes
35+
36+
pthread_create(&tid, &attr, LCS, param); //creates a new thread
37+
38+
int *max_len; //define the value from thread
39+
40+
pthread_join(tid, (void *)&max_len); //wait for termination of the thread
41+
42+
printf("\nLength of LCS: %d\n\n", *max_len);
43+
44+
return 0;
45+
}
46+
47+
void *LCS(void *arg)
48+
{
49+
int len1 = ((struct length *)arg)->len1;
50+
int len2 = ((struct length *)arg)->len2;
51+
52+
// Define return variable
53+
int *ret = malloc(sizeof(int));
54+
*ret = 0;
55+
56+
if(len1 == 0 || len2 == 0)
57+
pthread_exit(ret); //to exit a thread
58+
59+
if(s1[len1-1] == s2[len2-1])
60+
{
61+
// Define a new struct copied from the arg
62+
struct length arg_1 = *((struct length *)arg);
63+
--arg_1.len1;
64+
--arg_1.len2;
65+
66+
// Recursively call the thread again
67+
pthread_t tid; //returns the thread id of thread created
68+
pthread_attr_t attr; //to define thread attributes
69+
pthread_attr_init(&attr); //initializes the thread attributes
70+
pthread_create(&tid,&attr,LCS,(void *)&arg_1); //creates a new thread
71+
72+
// Get return value from thread
73+
int *ret_1;
74+
pthread_join(tid,(void*)&ret_1);
75+
*ret = *ret_1 + 1;
76+
}
77+
else
78+
{
79+
// Define new structs copied from the arg
80+
struct length arg_1 = *((struct length*)arg);
81+
--arg_1.len2;
82+
83+
struct length arg_2 = *((struct length*)arg);
84+
--arg_2.len1;
85+
86+
// Recursively call the thread again
87+
pthread_t tid[2]; //returns the thread id of thread created
88+
pthread_attr_t attr; //to define thread attributes
89+
pthread_attr_init(&attr); //initializes the thread attributes
90+
pthread_create(&tid[0],NULL,LCS,(void *)&arg_1);
91+
pthread_create(&tid[1],NULL,LCS,(void *)&arg_2);
92+
93+
// Get return value from both threads
94+
int *ret_1, *ret_2;
95+
pthread_join(tid[0],(void*)&ret_1);
96+
pthread_join(tid[1],(void*)&ret_2);
97+
98+
*ret = max(*ret_1,*ret_2);
99+
}
100+
pthread_exit(ret); //to exit a thread
101+
}
102+
103+
int max(int a, int b)
104+
{
105+
if(a > b)
106+
return a;
107+
return b;
108+
}

Lab6/Q1_ArmstrongNoGeneration

12.7 KB
Binary file not shown.

Lab6/Q1_ArmstrongNoGeneration.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<pthread.h>
4+
#include<math.h>
5+
6+
#define MAX 10000
7+
8+
int start, end;
9+
int store_sum[MAX];
10+
11+
void *runner(void *num);
12+
13+
int main(int argc, char *argv[])
14+
{
15+
//Validate the usage of command line arguments
16+
if(argc < 3)
17+
{
18+
printf("\nSyntax: %s <Starting Number> <Ending Number>\n\n", argv[0]);
19+
exit(EXIT_FAILURE);
20+
}
21+
22+
start = atoi(argv[1]);
23+
end = atoi(argv[2]);
24+
25+
printf("\nSet of Armstrong numbers between %d and %d are { ", start, end);
26+
27+
for(int i=start; i<=end; i++)
28+
{
29+
int *val = (int *)malloc(sizeof(int));
30+
*val = i;
31+
32+
pthread_t tid; //returns the thread id of thread created
33+
pthread_attr_t attr; //to define thread attributes
34+
pthread_attr_init(&attr); //initializes the thread attributes
35+
36+
pthread_create(&tid, &attr, runner, val); //creates a new thread
37+
pthread_join(tid, NULL); //wait for termination of the thread
38+
}
39+
40+
for(int i=0; i<=end-start; i++)
41+
if(store_sum[i] == i+start)
42+
printf("%d ", store_sum[i]);
43+
44+
printf("\b }\n\n");
45+
return 0;
46+
}
47+
48+
void *runner(void *num)
49+
{
50+
int *val = (int *)num;
51+
int temp = *val;
52+
int i = 0;
53+
int sum = 0;
54+
55+
while(temp != 0)
56+
{
57+
i++; // counts the number of digit in the number
58+
temp = temp/10;
59+
}
60+
61+
temp = *val;
62+
while(temp != 0)
63+
{
64+
sum = sum + pow(temp%10, i);
65+
temp = temp/10;
66+
}
67+
68+
store_sum[*val - start] = sum; //stores the value of sum
69+
70+
pthread_exit(0); //to exit a thread
71+
}
72+

Lab6/Q2_Sort

12.7 KB
Binary file not shown.

Lab6/Q2_Sort.c

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<stdbool.h>
4+
#include<pthread.h>
5+
6+
struct array
7+
{
8+
int a[100];
9+
int size;
10+
};
11+
12+
void *runner1(void *arg); //for ascending order sort
13+
void *runner2(void *arg); //for descending order sort
14+
void bubblesort(int *arr, int n, bool(*oprn)(const void *,const void *)); //using function pointer
15+
bool asc(const void *a, const void *b);
16+
bool desc(const void *a, const void *b);
17+
void swap(int *a,int *b);
18+
void print(int *arr,int n);
19+
20+
int main(int argc, char *argv[])
21+
{
22+
//Validate correct number of arguments
23+
if(argc < 2)
24+
{
25+
printf("\nWrong usage.. Syntax: %s <Array elements> \n\n", argv[0]);
26+
exit(EXIT_FAILURE);
27+
}
28+
29+
struct array *arr = (struct array *)malloc(sizeof(struct array));
30+
arr->size = argc - 1;
31+
32+
printf("\nThe size of Array : %d", arr->size);
33+
printf("\nThe Elements of the Array : ");
34+
for(int i=1; i<argc; i++)
35+
{
36+
arr->a[i-1] = atoi(argv[i]);
37+
}
38+
print(arr->a, arr->size);
39+
40+
pthread_t tid1, tid2; //returns the thread id of thread created
41+
pthread_attr_t attr; //to define thread attributes
42+
pthread_attr_init(&attr); //initializes the thread attributes
43+
44+
pthread_create(&tid1, &attr, runner1, arr); //creates a new thread for ascending sort
45+
pthread_create(&tid2, &attr, runner2, arr); //creates a new thread for descending sort
46+
47+
pthread_join(tid1, NULL); //wait for termination of the thread1
48+
pthread_join(tid2, NULL); //wait for termination of the thread2
49+
50+
printf("\n");
51+
return 0;
52+
}
53+
54+
void *runner1(void *arg)
55+
{
56+
struct array *arr = (struct array *)arg;
57+
printf("\nArray Sorted in Ascending order : ");
58+
bubblesort(arr->a, arr->size,asc);
59+
print(arr->a, arr->size);
60+
61+
pthread_exit(0); //to exit a thread
62+
}
63+
64+
void *runner2(void *arg)
65+
{
66+
struct array *arr = (struct array *)arg;
67+
printf("Array Sorted in Descending order : ");
68+
bubblesort(arr->a, arr->size, desc);
69+
print(arr->a, arr->size);
70+
71+
pthread_exit(0); //to exit a thread
72+
}
73+
74+
void bubblesort(int *arr,int n, bool(*oprn)(const void *,const void *))
75+
{
76+
for(int i=0; i<n-1; i++)
77+
{
78+
for(int j=0; j<n-i-1; j++)
79+
{
80+
if(oprn(&arr[j],&arr[j+1]))
81+
swap(&arr[j],&arr[j+1]);
82+
}
83+
}
84+
}
85+
86+
bool asc(const void *a, const void *b)
87+
{
88+
return *(int *)a > *(int *)b;
89+
}
90+
91+
bool desc(const void *a, const void *b)
92+
{
93+
return *(int *)a < *(int *)b;
94+
}
95+
96+
void swap(int *a,int *b)
97+
{
98+
int c = *a;
99+
*a = *b;
100+
*b = c;
101+
}
102+
103+
void print(int *arr,int n)
104+
{
105+
for(int i=0; i<n; i++)
106+
{
107+
printf("%d ",arr[i]);
108+
}
109+
printf("\n");
110+
}

Lab6/Q3_BinarySearch

12.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)