Question

In: Computer Science

I want a unique c code for the following parts mentioned below: Please try to solve...

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

Solutions

Expert Solution

#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;
}

Related Solutions

I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS ....
I want a unique c++ code for the following. PLEASE HIGHLIGHT THESE FUNCTIONS WITH COMMENTS . Add the following functions to the class arrayListType: Then, update the main function to test these new functions. removeAll - which removes ALL of the instances of a value in the list min - which returns the minimum value in the list max - which returns the maximum value in the list arrayListType.h : #ifndef H_arrayListType #define H_arrayListType class arrayListType { public: bool isEmpty()...
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x...
I want a unique c++ code for the following. (Greatest Common Divisor) Given two integers x and y, the following recursive definition determines the greatest common divisor of x and y, written gcd(x,y):     5 5 ± x y x y y x y y gcd( , ) if 0 gcd( , % ) if 0 Note: In this definition, % is the mod operator. Write a recursive function, gcd, that takes as parameters two integers and...
Please use C programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
C++ program, include comments stating what each part of code does please. I want to be...
C++ program, include comments stating what each part of code does please. I want to be able to understand it so I'll be more knowledgeable in the future. The program is multiple files(fibonacci.h file, fibonacci.cpp file, main.cpp file and loops_simple_data_test.cpp). After the directions I also included any starter code or comments left by my professor within the files to aide us. Directions: In folder 04_loops_simple_data write prototype and definition for string value - return function get_fibonacci with an int parameter...
Please solve this problem, in c++ (algoritms) write code and explaination how to solve it! 1)...
Please solve this problem, in c++ (algoritms) write code and explaination how to solve it! 1) N numbers are given. The first K of them are sorted in ascending order, the remaining NK are also sorted in ascending order. Sort numbers in ascending order in O (N). Input data format N - number of numbers, K - number of numbers in the first half (sorted). Example input 10 6 1 2 5 6 8 9 3 4 7 12 Sample...
Please! I want the instructions of how to solve it, not the answer. Write a program...
Please! I want the instructions of how to solve it, not the answer. Write a program that does the following in order: 1. Asks the user to enter a name 2. Asks the user to enter a number “x” 3. Asks the user to enter a number “y” 4. Calculates the sum of “x” and “y” 5. Prints out the number for “x”, “y” and “sum” An example of the program input and output is shown below: Enter your name:...
Need the correct line of code for c program to solve  this equation . i is a...
Need the correct line of code for c program to solve  this equation . i is a value that the program all ready calculates from user imput. t i = (x*1000)+(y*500)+(z*250
I want this code to be written in c++. Take a string of length n as...
I want this code to be written in c++. Take a string of length n as an input from the user such that n>=8 and only lower-case letters (a-z) are allowed as valid string characters. Deleting a letter from the string removes all the occurrences of that letter. The objective is to find the longest possible string such that it is left with only two unique letters and no two consecutive characters in a string are the same. If there...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner (using Stack) in C++(using data structure). Note: you have to solve for N number of disks, 3 Towers (Stacks). Do not use recursion. For better understanding play the game at least once. Link:https://www.mathsisfun.com/games/towerofhanoi.html
Python: I want to make the following code to prompt the user if want to run...
Python: I want to make the following code to prompt the user if want to run the software again. I am new to python, so i do not know if it works like c++. If can explain that would be much appreciated base = float(input("Enter base of the triagle: ")) Triangle_Right = float(input("Enter right side of the triagle: ")) Triangle_Left = float(input("Enter left side of the triagle: ")) height = float(input("Enter the height of the triangle: ")) perimiter = base...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT