//In this assignment, we use multiple threads to calculate the
frequencies of the first digits
//of the numbers in an array of long integers
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
//#define NUM_THREADS 2
#define DIGITS 10
#define MAX 100000000
unsigned long a[MAX];
struct thread_data
{
int thread_num;
int i, j;
//the staring and ending index
unsigned long freq[DIGITS];
// results
};
//initialize the array
void init_array(unsigned long a[], int n)
{
a[0] = 1;
for(int i = 1; i < n; i++)
{
a[i] = a[i-1] + i + 1;
}
}
//get the first digit of the long integer k
int first_digit(unsigned long k)
{
while(k >= DIGITS)
{
k = k/DIGITS;
}
return k;
}
//frequencies of the first digits among the numbers in a[],
//there are total n numbers
void frequencies(unsigned long a[], int n, unsigned long
freq[DIGITS])
{
for(int k=0; k<DIGITS; k++)
freq[k] = 0;
for(int i = 0; i<n; i++)
{
int m = first_digit(a[i]);
freq[m] ++;
}
}
void* thread_freq(void* threadarg)
{
struct thread_data* my_data = (struct thread_data*)
threadarg;
//fill in code below, only line is needed
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
if(argc!=3)
{
printf("Usage: %s n NUM_THREADS\n",
argv[0]);
exit(-1);
}
int n = atoi(argv[1]);
assert(n>=1 && n<=MAX);
int NUM_THREADS = atoi(argv[2]);
assert(NUM_THREADS >=1 && NUM_THREADS
<=10);
init_array(a, n);
pthread_t threads[NUM_THREADS];
struct thread_data thread_data_array[NUM_THREADS];
int rc, t;
for( t=0; t<NUM_THREADS; t++ ) {
thread_data_array[t].thread_num = t;
thread_data_array[t].i = t*n/NUM_THREADS;
thread_data_array[t].j =
(t+1)*n/NUM_THREADS - 1;
rc = pthread_create(&threads[t], NULL, thread_freq,
&thread_data_array[t]);
if (rc) {
printf("ERROR; return code from pthread_create() is %d\n",
rc);
exit(-1);
}
}
unsigned long freq[DIGITS] ={0};
for( t=0; t<NUM_THREADS; t++ ) {
rc = pthread_join( threads[t], NULL );
if( rc ){
printf("ERROR; return code from pthread_join() is %d\n", rc);
exit(-1);
}
//fill in code below
//no more than two lines of code
expected
}
for(int i=1; i<DIGITS; i++)
printf("%d: %8lu\n", i, freq[i]);
return 0;
//Run this code using different number of threads and
see whether multiple threads
//lead to time saving
//Compare the running times with using just 1
thread
}
Please find the following program to find the frequceny of 1st digit in the array.
Note:
I have attached the screenshots and comments inline for better understanding.
Program:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
//Remove the following lines if not needed.
#include <sys/time.h>
//#define NUM_THREADS 2
#define DIGITS 10
#define MAX 100000000
unsigned long a[MAX];
struct thread_data
{
int thread_num;
int i, j; //the staring and ending index
unsigned long freq[DIGITS]; // results
};
//initialize the array
void init_array(unsigned long a[], int n)
{
a[0] = 1;
for(int i = 1; i < n; i++)
{
a[i] = a[i-1] + i + 1;
}
}
//get the first digit of the long integer k
int first_digit(unsigned long k)
{
while(k >= DIGITS)
{
k = k/DIGITS;
}
return k;
}
//frequencies of the first digits among the numbers in
a[],
//there are total n numbers
//COMMENT: We have to pass the start/end index for each thread to
calculate the
//frequency of first digit for numbers in a array
void frequencies(unsigned long a[], int start, int n, unsigned long
freq[DIGITS])
{
for(int k=0; k<DIGITS; k++)
freq[k] = 0;
for(int i = start; i<=n; i++)
{
int m = first_digit(a[i]);
freq[m] ++;
}
}
void* thread_freq(void* threadarg)
{
struct thread_data* my_data = (struct thread_data*)
threadarg;
//fill in code below, only line is
needed
//call the thread function to find the frequency of
1st digit from
//a array
frequencies(a, my_data->i, my_data->j,
my_data->freq);
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
if(argc!=3)
{
printf("Usage: %s n NUM_THREADS\n",
argv[0]);
exit(-1);
}
//Remove the next line if not needed
struct timeval tv1, tv2;
int n = atoi(argv[1]);
assert(n>=1 && n<=MAX);
int NUM_THREADS = atoi(argv[2]);
assert(NUM_THREADS >=1 && NUM_THREADS
<=10);
init_array(a, n);
unsigned long freq[DIGITS] ={0,0,0,0,0,0,0,0,0,0};
pthread_t threads[NUM_THREADS];
struct thread_data
thread_data_array[NUM_THREADS];
int rc, t;
//Remove the next line if not needed
gettimeofday(&tv1, NULL);
for( t=0; t<NUM_THREADS; t++ ) {
thread_data_array[t].thread_num =
t;
thread_data_array[t].i =
t*n/NUM_THREADS;
thread_data_array[t].j =
(t+1)*n/NUM_THREADS - 1;
//uncomment the following line to
find the start/end index for each thread
//printf("thread=%d start=%d
end=%d\n", t, thread_data_array[t].i,
thread_data_array[t].j);
rc =
pthread_create(&threads[t], NULL, thread_freq,
&thread_data_array[t]);
if (rc) {
printf("ERROR;
return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for( t=0; t<NUM_THREADS; t++ ) {
rc = pthread_join( threads[t], NULL
);
if( rc ){
printf("ERROR;
return code from pthread_join() is %d\n", rc);
exit(-1);
}
//fill in code below
//no more than two lines of code
expected
//once a thread is completed its
work, take the frequency values from freq array in
//thread data
structure
for (int j=1; j<DIGITS;
j++)
freq[j]=freq[j] + thread_data_array[t].freq[j];
}
//Remove the next if not needed
gettimeofday(&tv2, NULL);
for(int i=1; i<DIGITS; i++)
printf("%d: %8lu\n", i,
freq[i]);
//Remove the next printf if not
needed
printf ("Total time with %d THREADS = %f seconds\n",
NUM_THREADS,
(double)
(tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double)
(tv2.tv_sec - tv1.tv_sec));
return 0;
//Run this code using different number of threads and
see whether multiple threads
//lead to time saving
//Compare the running times with using just 1
thread
}
Output:
osboxes@osboxes:~/Chegg/C$ ./a.out 10 3
1: 3
2: 2
3: 2
4: 1
5: 1
6: 1
7: 0
8: 0
9: 0
Total time with 3 THREADS = 0.000470 seconds
osboxes@osboxes:~/Chegg/C$ ./a.out 10 1
1: 3
2: 2
3: 2
4: 1
5: 1
6: 1
7: 0
8: 0
9: 0
Total time with 1 THREADS = 0.004904 seconds
Screen Shots:
Sample Output Screen Shot:
//In this assignment, we use multiple threads to calculate the frequencies of the first digits //of...
//In this assignment, we use multiple threads to calculate the sum // 1*1 + 2*2 + 3*3 + 4*4 + ... + n*n // Note we should know from CSE2500 that this sum is // n*(n+1)*(2*n+1)/6 // We a n value, we will create 2*n threads to do the calculation so that // we can have a race condition. // Before you change the code, read the code and run the code and see what // happens. Do we already...
In C using the following 2 files to create a 3rd file that uses multiple threads to improve performance. Split the array into pieces and each piece is handled by a different thread. Use 8 threads. run and compile in linux. #include <stdio.h> #include <sys/time.h> #define BUFFER_SIZE 4000000 int countPrime=0; int numbers[BUFFER_SIZE]; int isPrime(int n) { int i; for(i=2;i<n;i++) if (n%i==0) return 0; return 1; } int main() { int i; // fill the buffer for(i=0;i<BUFFER_SIZE;i++) numbers[i] = (i+100)%100000; //...
Need this in C Code is given below e Dots l lah dit Problem 1. (30 points) Fre bendord.cto obtain the free in decimal representation For ATY. this problem we complete the code in i tive long inte ens (W written the following positive long term 123, 40, 56, 7, 8, 9, 90, 900 the frequencies of all the digits are: 0:4, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7: 1. 8: 1.9: 3 In this example, the free ency of...
so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n) using semaphores implement solution to the critical section problem #include #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* threads call this function */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if...
i want to fix the solution and provide an explanation why the pthread_join is not working as intended? #include <stdio.h> /* standard I/O routines */ #include <pthread.h> /* pthread functions and data structures */ void* PrintHello(void* data) { pthread_t tid = (pthread_t)data; /* data received by thread */ pthread_join(tid, NULL); /* wait for thread tid */ printf("Hello from new thread %u - got %u\n", pthread_self(), data); pthread_exit(NULL); /* terminate the thread */ } /* like any C program, program's execution...
1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...
I am getting the Segmentation fault error on the Ubuntu machine but not on macOS. Any help would be appreciated. /**** main.c ****/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #include <pthread.h> #include <string.h> #define WORD_LEN 6 #define TOP 10 char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r"; struct Word { char word[30]; int freq; }; int threadCount; int fileDescriptor; int fileSize; off_t chunk; struct Word* wordArray; int arrIndex = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...
a multi-threaded producer / consumer program without any locking or signaling. It therefore has synchronization problems. Add locks and signals so that it works correctly. /* Homework 5.X */ /* Robin Ehrlich */ /* compile: gcc Homework5.c -lpthread */ #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <string.h> struct class { struct class *next; int id; int grade; }; #define SLEEP_TIME 1 #define MAX_PRODUCE 10 static struct class *classHead = NULL; static struct class *classTail = NULL; static...
Pleas help I need to create and array to add to my code something like this 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; ::::::::: CODE ::::::::::::: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> int sharedVar = 0; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* thread handler */ void *threadHandler(void *vargp) { int i = 0; pthread_mutex_lock(&mutex); ...
Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...