Question: WRITE A CODE FOR void setAffinity(pthread_t thread, int coreId); FOR THE CODE GIVEN BELOW pthread_setaffinity_np for setting CPU affinity on a per- thread basis. Each

WRITE A CODE FOR void setAffinity(pthread_t thread, int coreId); FOR THE CODE GIVEN BELOW

pthread_setaffinity_np for setting CPU affinity on a per- thread basis. Each thread in the parallel version will be mapped to a specificCPU core using pthread_setaffinity_np i. implement the mapping of threads to specific CPU cores using the CPU affinity concept, exploiting sched_setaffinity function calls. ii. pthread_setaffinity_np to set CPU affinity for each thread, the CPU cores =2.

#include

#include

#include

#include

// Define the structure for a node in the linked list

typedef struct Node {

int data;

struct Node* next;

} Node;

// Global variable

Node* head = NULL;

pthread_mutex_t lock;

// Function to add data to the linked list

void insert(int data) {

Node* new_node = (Node*)malloc(sizeof(Node));

if (new_node == NULL) {

fprintf(stderr, "Memory allocation failed ");

exit(EXIT_FAILURE);

}

new_node->data = data;

new_node->next = head;

head = new_node;

}

// Function to merge two sorted linked lists

Node* merge(Node* left, Node* right) {

if (left == NULL)

return right;

if (right == NULL)

return left;

Node* result = NULL;

if (left->data <= right->data) {

result = left;

result->next = merge(left->next, right);

} else {

result = right;

result->next = merge(left, right->next);

}

return result;

}

// Function to perform merge sort on a linked list

Node* merge_sort(Node* head) {

if (head == NULL || head->next == NULL)

return head;

Node* slow = head;

Node* fast = head->next;

while (fast != NULL && fast->next != NULL) {

slow = slow->next;

fast = fast->next->next;

}

Node* mid = slow->next;

slow->next = NULL;

Node* left = merge_sort(head);

Node* right = merge_sort(mid);

return merge(left, right);

}

// Thread function to perform merge sort on a subset of the linked list

void* merge_sort_thread(void* arg) {

Node* sublist_head = (Node*)arg;

sublist_head = merge_sort(sublist_head);

pthread_exit(sublist_head);

}

int main() {

pthread_t tid1, tid2; // Two threads for two halves of the linked list

// Generate and read roll numbers from a file

FILE *file = fopen("roll_numbers.txt", "w");

if (file == NULL) {

fprintf(stderr, "Error opening file for writing. ");

return 1;

}

// Generate 1000 random roll numbers and write them to the file

srand(time(NULL));

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

int roll_number = rand() % 9000 + 1000; // Generate a 4-digit roll number

fprintf(file, "%d ", roll_number);

}

fclose(file);

// Read roll numbers from the file and insert them into the linked list

file = fopen("roll_numbers.txt", "r");

if (file == NULL) {

fprintf(stderr, "Error opening file for reading. ");

return 1;

}

int roll_number;

while (fscanf(file, "%d", &roll_number) != EOF) {

insert(roll_number);

}

fclose(file);

pthread_mutex_init(&lock, NULL);

// Split the list into two halves

Node* slow = head;

Node* fast = head->next;

while (fast != NULL && fast->next != NULL) {

slow = slow->next;

fast = fast->next->next;

}

Node* second_half_head = slow->next;

slow->next = NULL;

// Create two threads to sort each half of the list

pthread_create(&tid1, NULL, merge_sort_thread, (void*)head);

pthread_create(&tid2, NULL, merge_sort_thread, (void*)second_half_head);

Node* sorted_first_half;

Node* sorted_second_half;

// Join threads and get sorted halves

pthread_join(tid1, (void**)&sorted_first_half);

pthread_join(tid2, (void**)&sorted_second_half);

// Merge the sorted halves

Node* sorted_list = merge(sorted_first_half, sorted_second_half);

pthread_mutex_destroy(&lock);

// Print the sorted list

printf("Sorted linked list: ");

Node* current = sorted_list;

while (current != NULL) {

printf("%d -> ", current->data);

current = current->next;

}

printf("NULL ");

// Free memory allocated for the sorted list

current = sorted_list;

while (current != NULL) {

Node* temp = current;

current = current->next;

free(temp);

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!