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

Cs604p Assignment 01

The document contains a C language code assignment by Amna Yaseen, which demonstrates the use of multithreading with two threads. One thread calculates and prints the squares of an array of integers, while the other reverses and prints a given string. The code includes error handling for thread creation and uses standard input/output functions.

Uploaded by

narmeenshahid388
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)
11 views

Cs604p Assignment 01

The document contains a C language code assignment by Amna Yaseen, which demonstrates the use of multithreading with two threads. One thread calculates and prints the squares of an array of integers, while the other reverses and prints a given string. The code includes error handling for thread creation and uses standard input/output functions.

Uploaded by

narmeenshahid388
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/ 2

CS604P

ASSIGNMENT NO :01
NAME: AMNA YASEEN
VU ID: BC230429430

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("%d\n", arr[i]);
}
printf("\nPrinting the Squares of Number 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;
}

Output Screenshot:

You might also like