0% found this document useful (0 votes)
7 views

Lab3_OS

The document contains three C programs that utilize multithreading to check if given positive integers are Fibonacci numbers, prime numbers, or growing numbers. Each program validates input, creates threads for each number, and prints whether the number belongs to the specified category. The programs handle errors and ensure proper input validation before processing.

Uploaded by

Khang Le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab3_OS

The document contains three C programs that utilize multithreading to check if given positive integers are Fibonacci numbers, prime numbers, or growing numbers. Each program validates input, creates threads for each number, and prints whether the number belongs to the specified category. The programs handle errors and ensure proper input validation before processing.

Uploaded by

Khang Le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: Lê Công Thái Khang

Student ID: ITITIU20224


OPERATING SYSTEMS
LAB 03
I41
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <ctype.h>

// Function to check if a number is a Fibonacci number


int is_fibonacci(int n) {
if (n < 0) return 0;
int a = 0, b = 1, temp;
while (b < n) {
temp = a + b;
a = b;
b = temp;
}
return (b == n || n == 0);
}

// Thread function
void* check_fibonacci(void* arg) {
int num = *(int*)arg;
if (is_fibonacci(num)) {
printf("%d is a Fibonacci member\n", num);
} else {
printf("%d is not a Fibonacci member\n", num);
}
pthread_exit(NULL);
}

// Function to validate if a string represents a positive integer


int is_valid_number(const char *str) {
if (!str || *str == '\0') return 0;
for (int i = 0; str[i] != '\0'; i++) {
if (!isdigit((unsigned char)str[i])) return 0;
}
return 1;
}

int main(int argc, char *argv[]) {


if (argc < 2) {
fprintf(stderr, "Usage: %s <positive integers>\n", argv[0]);
return EXIT_FAILURE;
}

int num_count = argc - 1;


pthread_t threads[num_count];
int numbers[num_count];

for (int i = 0; i < num_count; i++) {


if (!is_valid_number(argv[i + 1])) {
fprintf(stderr, "Invalid input: %s (must be a positive
integer)\n", argv[i + 1]);
return EXIT_FAILURE;
}
numbers[i] = atoi(argv[i + 1]);
}

for (int i = 0; i < num_count; i++) {


if (pthread_create(&threads[i], NULL, check_fibonacci,
&numbers[i]) != 0) {
perror("pthread_create failed");
return EXIT_FAILURE;
}
}

for (int i = 0; i < num_count; i++) {


pthread_join(threads[i], NULL);
}

return EXIT_SUCCESS;
}

I42
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <ctype.h>
#include <math.h>

// Function to check if a number is prime


int is_prime(int n) {
if (n < 2) return 0;
if (n == 2) return 1;
if (n % 2 == 0) return 0;
for (int i = 3; i <= sqrt(n); i += 2) {
if (n % i == 0) return 0;
}
return 1;
}

// Thread function
void* check_prime(void* arg) {
int num = *(int*)arg;
if (is_prime(num)) {
printf("%d is a prime\n", num);
} else {
printf("%d is not a prime\n", num);
}
pthread_exit(NULL);
}

// Function to validate if a string represents a positive integer


int is_valid_number(const char *str) {
if (!str || *str == '\0') return 0;
for (int i = 0; str[i] != '\0'; i++) {
if (!isdigit((unsigned char)str[i])) return 0;
}
return 1;
}

int main(int argc, char *argv[]) {


if (argc < 2) {
fprintf(stderr, "Usage: %s <positive integers>\n", argv[0]);
return EXIT_FAILURE;
}

int num_count = argc - 1;


pthread_t threads[num_count];
int numbers[num_count];

for (int i = 0; i < num_count; i++) {


if (!is_valid_number(argv[i + 1])) {
fprintf(stderr, "Invalid input: %s (must be a positive
integer)\n", argv[i + 1]);
return EXIT_FAILURE;
}
numbers[i] = atoi(argv[i + 1]);
}

for (int i = 0; i < num_count; i++) {


if (pthread_create(&threads[i], NULL, check_prime, &numbers[i]) !
= 0) {
perror("pthread_create failed");
return EXIT_FAILURE;
}
}

for (int i = 0; i < num_count; i++) {


pthread_join(threads[i], NULL);
}

return EXIT_SUCCESS;
}

I43
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <ctype.h>

// Function to check if a number is a growing number


int is_growing(int n) {
int prev_digit = 10; // Start with a digit greater than any single
digit
while (n > 0) {
int current_digit = n % 10;
if (current_digit >= prev_digit) {
return 0;
}
prev_digit = current_digit;
n /= 10;
}
return 1;
}

// Thread function
void* check_growing(void* arg) {
int num = *(int*)arg;
if (is_growing(num)) {
printf("%d is a growing number\n", num);
} else {
printf("%d is not a growing number\n", num);
}
pthread_exit(NULL);
}

// Function to validate if a string represents a positive integer


int is_valid_number(const char *str) {
if (!str || *str == '\0') return 0;
for (int i = 0; str[i] != '\0'; i++) {
if (!isdigit((unsigned char)str[i])) return 0;
}
return 1;
}

int main(int argc, char *argv[]) {


if (argc < 2) {
fprintf(stderr, "Usage: %s <positive integers>\n", argv[0]);
return EXIT_FAILURE;
}

int num_count = argc - 1;


pthread_t threads[num_count];
int numbers[num_count];

for (int i = 0; i < num_count; i++) {


if (!is_valid_number(argv[i + 1])) {
fprintf(stderr, "Invalid input: %s (must be a positive
integer)\n", argv[i + 1]);
return EXIT_FAILURE;
}
numbers[i] = atoi(argv[i + 1]);
}

for (int i = 0; i < num_count; i++) {


if (pthread_create(&threads[i], NULL, check_growing, &numbers[i])
!= 0) {
perror("pthread_create failed");
return EXIT_FAILURE;
}
}

for (int i = 0; i < num_count; i++) {


pthread_join(threads[i], NULL);
}

return EXIT_SUCCESS;
}

You might also like