0% found this document useful (0 votes)
32 views3 pages

Student ID: Bc230217539 Solution: Code: Assignment # 01

Uploaded by

arshman372
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)
32 views3 pages

Student ID: Bc230217539 Solution: Code: Assignment # 01

Uploaded by

arshman372
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/ 3

Operating Systems (Practical) (CS604P) Total marks = 20

Assignment # 01 Deadline Date

Fall 2024 Nov 19, 2024

Student ID: Bc230217539

Solution:

Code

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>

void *calculate_squares(void *arg) {


int *arr = (int *)arg;
int size = 10; // Assuming array size is 10

printf("In First Thread\n");


printf("Printing the actual contents of array\n");
for (int i = 0; i < size; i++) {
printf("%d\n", arr[i]);
}

printf("\nPrinting the squares of numbers in the array:\n");


for (int i = 0; i < size; i++) {
printf("%d^2 = %d\n", arr[i], arr[i] * arr[i]);
}

pthread_exit(NULL);
}

void *reverse_string(void *arg) {


char *str = (char *)arg;
int len = strlen(str);

printf("\nIn Second Thread, Printing the Reverse String...\n");


for (int i = len - 1; i >= 0; i--) {
printf("%c", str[i]);
}
printf("\n");

pthread_exit(NULL);
}
int main() {
int arr[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
char str[100];

printf("Enter string to pass to T2: ");


scanf("%s", str);

pthread_t thread1, thread2;

pthread_create(&thread1, NULL, calculate_squares, (void *)arr);


pthread_create(&thread2, NULL, reverse_string, (void *)str);

pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

printf("Exiting the main function\n");

return 0;
}

Screenshot:

You might also like