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

Cs602 Assignment No 1

The document contains a C program that utilizes pthreads to create two threads: one for calculating and printing the squares of an integer array and another for reversing and printing a string. The program prompts the user to enter a string, which is passed to the second thread, while the first thread operates on a predefined array of integers. It includes error handling for thread creation and ensures that the main function waits for both threads to complete before exiting.
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)
4 views

Cs602 Assignment No 1

The document contains a C program that utilizes pthreads to create two threads: one for calculating and printing the squares of an integer array and another for reversing and printing a string. The program prompts the user to enter a string, which is passed to the second thread, while the first thread operates on a predefined array of integers. It includes error handling for thread creation and ensures that the main function waits for both threads to complete before exiting.
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

C language Code:

#include<stdio.h>

#include<pthread.h>

#include<string.h>

void* calculate_squares(void* param)

int* arr = (int*)param;

printf("\nIn First Thread\nPrinting the Actual Contents of Array\n");

for(int i = 0; i < 10; i++)

printf("%\n", arr[i]);

printf("\nPrinting the Squares of Numbers in the Array\n");

for(int i = 0; i < 10; i++)

printf("%d\n", arr[i] * arr[i]);

printf("\n");

return NULL;

void* reverse_string(void* param)

char* str = (char*)param;


int len = strlen(str);

char reversed[len + 1];

for(int i = 0; i < len; i++)

reversed[i] = str[len - i - 1];

reversed[len] = '\0';

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

return NULL;

int main ()

pthread_t t1, t2;

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

char str[100];

printf("Enter a String To Pass T2: ");

scanf("%99s", str);

if(pthread_create(&t2, NULL, reverse_string, (void*)str) != 0)

perror("Failed to Create Thread T2");

return 1;

pthread_join(t2, NULL);

if(pthread_create(&t1, NULL, calculate_squares, (void*)array) != 0)


{

perror("Failed to Create Thread T1");

return 1;

pthread_join(t1, NULL);

return 0;

You might also like