In: Computer Science
I want a unique c code for the following parts mentioned below: Please try to solve the following. I am not able to solve it.
I have already provideD the code for Part 1 and Part 2. You may only show their working output screenshots. You JUST need to *MODIFY PART 3* and upload the code for it
Please provide details too by highlighting what you modified,
PERFORM ALL THE PARTS SEPARATELY IN SHELL SERVER AND ATTACH CODE.
DO NOT SIMPLY PASTE THE CODE THAT I PROVIDED. PLEASE UPDATE THE PARTS THAT I MENTIONED BELOW
Details are provided to write code.
PART 1:
I have provided a file (threadhello.c that generates 10 threads, each of which simply prints out a "hello world" message before terminating. Examine this code carefully.
#include <pthread.h> #include <stdlib.h> #include <stdio.h> #define NUMBER_OF_THREADS 10 /* This is a structure that is used to pass parameters to the thread. * Currently, the only param is the thread id */ typedef struct { int tid; } paramListType; /* *params should be pointing to a structure of paramListType */ void *print_hello_world(void *params) { paramListType *paramPtr = params; printf("Hello World. Greetings from thread %d\n", paramPtr->tid) ; pthread_exit(NULL); } int main(int argc, char **argv) { pthread_t threads[NUMBER_OF_THREADS]; int status, i; /* parms will be a pointer to a parmListType structure that will * contain the thread id value */ paramListType *params; for(i=0; itid = i; status = pthread_create(&threads[i], NULL, print_hello_world, (void *) params); if(status != 0) { printf("pthread_create returned error code %d\n", status); exit(-1); } }
/* if the program doesn't wait for all the threads to finish, * you may not see the print message from some of them */ for(i=0; i<NUMBER_OF_THREADS; i++) { status=pthread_join(threads[i], NULL); } exit(0); }
Please also show how to Upload this file (you may use an ftp
client or, better yet, look up how to use the scp command (prefered))
Compile this code using the following command: clang threadhello.c -lpthread
Run this command several times. Submit at least 3 screenshots of different outputs that were all a result of running this code
DELIVERABLE #1: >=3 screenshots of this code correctly running
PART 2:
I have also provided a file threadarray.c Upload and run this program. Run the program several times where you have increased the size of the array.
#include <pthread.h> #include <stdlib.h> #include <stdio.h>
#define ARRAY_SIZE 15 /* This is a structure that is used to pass parameters to the thread. * Currently, the only param is the thread id */ typedef struct { int* arr; int tid; } paramListType; void* threadSum(void* p){ paramListType* ptr = (paramListType*)p; int n = ptr->tid; // Declare sum dynamically to return to join: int* thread_sum = (int*) calloc(1, sizeof(int)); //NOTE: uncomment the printf commands below to see behind the scenes if(n == 0){ for(int i = 0; i < ARRAY_SIZE/2; i++){ //printf("Working in thread %d, at position: %d\n", ptr->tid , i ); thread_sum[0] = thread_sum[0] + ptr->arr[i]; } } else{ for(int i = ARRAY_SIZE/2; i < ARRAY_SIZE; i++){ //printf("Working in thread %d, at position: %d\n", ptr->tid , i ); thread_sum[0] = thread_sum[0] + ptr->arr[i]; } } pthread_exit(thread_sum); } int main(int argc, char **argv) { // Declare integer array [1,2,3,4,5,6,7,8,9,10]: int* int_arr = (int*) calloc(ARRAY_SIZE, sizeof(int)); for(int i = 0; i < ARRAY_SIZE; i++) int_arr[i] = i + 1; // Declare arguments for both threads: paramListType thread_params[2]; thread_params[0].tid = 0; thread_params[0].arr = int_arr; thread_params[1].tid = 1; thread_params[1].arr = int_arr; // Declare thread IDs: pthread_t tids[2]; // create threads: pthread_create(&tids[0], NULL, threadSum, &thread_params[0]); pthread_create(&tids[1], NULL, threadSum, &thread_params[1]); // declare sums: int* sum0; int* sum1; // retrieve sum of threads: pthread_join(tids[0], (void**)&sum0); pthread_join(tids[1], (void**)&sum1); printf("Sum of whole array = %i\n", *sum0 + *sum1); return 0; }
DELIVERABLE #2: a screenshot of this code correctly running
PART 3: (REALLY IMPORTANT PART)
Change this program so that it fills the array values with (truly) random numbers between 1 and 100. Then, change the program so that it makes use of five (5) threads to sum the values in the array.
Hint: you may want to check that your five thread version is correctly working on known totals before updating your code to run on random values.
DELIVERABLE #3: your new program file
DELIVERABLE #4: a screenshot of your program running
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 2
/* create thread argument struct for thr_func() */
typedef struct _thread_data_t {
int tid;
double stuff;
} thread_data_t;
/* thread function */
void *thr_func(void *arg) {
thread_data_t *data = (thread_data_t *)arg;
printf("hello from thr_func, thread id: %d\n", data->tid);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t thr[NUM_THREADS];
int i, rc;
/* create a thread_data_t argument array */
thread_data_t thr_data[NUM_THREADS];
/* create threads */
for (i = 0; i < NUM_THREADS; ++i) {
thr_data[i].tid = i;
if ((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))) {
fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
return EXIT_FAILURE;
}
}
/* block until all threads complete */
for (i = 0; i < NUM_THREADS; ++i) {
pthread_join(thr[i], NULL);
}
return EXIT_SUCCESS;
}