-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cdba523
commit c8d6a3c
Showing
7 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
main: | ||
gcc -pthread -Wall -Wextra -g main.c -o main | ||
|
||
clean: | ||
rm -f main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <pthread.h> | ||
#include <unistd.h> | ||
|
||
void *worker(void *arg) | ||
{ | ||
(void)arg; // Indicate argument is unused | ||
pthread_t current_thread_id = pthread_self(); | ||
pid_t current_process_id = getpid(); | ||
printf("Thread ID: %lu, Process ID: %d\n", (unsigned long)current_thread_id, current_process_id); | ||
return NULL; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc != 2) | ||
{ | ||
printf("Please provide the number of threads as an argument.\n"); | ||
return 1; | ||
} | ||
|
||
int thread_count = atoi(argv[1]); | ||
if (thread_count <= 0) | ||
{ | ||
printf("Invalid number of threads. Must be greater than 0.\n"); | ||
return 1; | ||
} | ||
|
||
pthread_t threads[thread_count]; | ||
|
||
for (int i = 0; i < thread_count; i++) | ||
{ | ||
if (pthread_create(&threads[i], NULL, worker, NULL) != 0) | ||
{ | ||
printf("Error: Could not create thread %d.\n", i + 1); | ||
return 1; | ||
} | ||
} | ||
|
||
for (int i = 0; i < thread_count; i++) | ||
{ | ||
pthread_join(threads[i], NULL); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
main: | ||
gcc -pthread -Wall -Wextra -g main.c -o main | ||
|
||
clean: | ||
rm -f main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <pthread.h> | ||
|
||
typedef struct | ||
{ | ||
int n; | ||
int result; | ||
} number; | ||
|
||
void *summation(void *args) | ||
{ | ||
number *arguments = (number *)args; | ||
int n = arguments->n; | ||
int sum = 0; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
sum += i; | ||
} | ||
arguments->result = sum; | ||
return NULL; | ||
} | ||
|
||
int main() | ||
{ | ||
int num; | ||
printf("Enter a positive integer: "); | ||
scanf("%d", &num); | ||
|
||
while (num < 0) | ||
{ | ||
printf("Error! Please enter a positive integer: "); | ||
scanf("%d", &num); | ||
} | ||
|
||
number args; | ||
args.n = num; | ||
pthread_t thread; | ||
if (pthread_create(&thread, NULL, summation, &args) != 0) | ||
{ | ||
fprintf(stderr, "Error creating thread\n"); | ||
return 1; | ||
} | ||
pthread_join(thread, NULL); | ||
printf("The summation of integers from 0 to %d is: %d\n", num, args.result); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
main: | ||
gcc -pthread -Wall -Wextra -g main.c -o main | ||
|
||
clean: | ||
rm -f main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <pthread.h> | ||
|
||
typedef struct | ||
{ | ||
int n; | ||
int *fib; | ||
int even_count; | ||
int odd_count; | ||
int sum; | ||
} fib_data; | ||
|
||
void *fibonacci(void *arg) | ||
{ | ||
fib_data *data = (fib_data *)arg; | ||
data->fib = (int *)malloc(data->n * sizeof(int)); | ||
if (!data->fib) | ||
{ | ||
fprintf(stderr, "Memory allocation failed\n"); | ||
pthread_exit(NULL); | ||
} | ||
|
||
data->fib[0] = 0; | ||
if (data->n > 1) | ||
{ | ||
data->fib[1] = 1; | ||
} | ||
|
||
for (int i = 2; i < data->n; i++) | ||
{ | ||
data->fib[i] = data->fib[i - 1] + data->fib[i - 2]; | ||
} | ||
return NULL; | ||
} | ||
|
||
void *countEven(void *arg) | ||
{ | ||
fib_data *data = (fib_data *)arg; | ||
data->even_count = 0; | ||
for (int i = 0; i < data->n; i++) | ||
{ | ||
if (data->fib[i] % 2 == 0) | ||
{ | ||
data->even_count++; | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
void *countOdd(void *arg) | ||
{ | ||
fib_data *data = (fib_data *)arg; | ||
data->odd_count = 0; | ||
for (int i = 0; i < data->n; i++) | ||
{ | ||
if (data->fib[i] % 2 != 0) | ||
{ | ||
data->odd_count++; | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
void *calcSum(void *arg) | ||
{ | ||
fib_data *data = (fib_data *)arg; | ||
data->sum = 0; | ||
for (int i = 0; i < data->n; i++) | ||
{ | ||
data->sum += data->fib[i]; | ||
} | ||
|
||
FILE *file = fopen("sum.txt", "w"); | ||
if (file) | ||
{ | ||
fprintf(file, "Sum: %d\n", data->sum); | ||
fclose(file); | ||
} | ||
return NULL; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc != 2) | ||
{ | ||
printf("Please pass a number as cmd arg\n"); | ||
return 1; | ||
} | ||
|
||
int N = atoi(argv[1]); | ||
if (N <= 0) | ||
{ | ||
printf("Number passed as cmd arg must be positive\n"); | ||
return 1; | ||
} | ||
|
||
printf("Number entered is %d\n", N); | ||
|
||
fib_data data; | ||
data.n = N; | ||
pthread_t threads[4]; | ||
|
||
pthread_create(&threads[0], NULL, fibonacci, &data); | ||
pthread_join(threads[0], NULL); | ||
printf("ID = %lu, Series: ", threads[0]); | ||
for (int i = 0; i < N; i++) | ||
{ | ||
printf("%d ", data.fib[i]); | ||
} | ||
printf("\n"); | ||
|
||
pthread_create(&threads[1], NULL, countEven, &data); | ||
pthread_create(&threads[2], NULL, countOdd, &data); | ||
pthread_create(&threads[3], NULL, calcSum, &data); | ||
pthread_join(threads[1], NULL); | ||
printf("ID = %lu, Even Numbers: %d\n", threads[1], data.even_count); | ||
pthread_join(threads[2], NULL); | ||
printf("ID = %lu, Odd Numbers: %d\n", threads[2], data.odd_count); | ||
pthread_join(threads[3], NULL); | ||
printf("ID = %lu, Sum: %d\n", threads[3], data.sum); | ||
|
||
free(data.fib); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Sum: 7 |