//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; //...
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...
Read the following code, threaded recursive calculation of Fibonacci number of n: #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *fib(void *arg); int main(int argc, char **argv){ int n = atoi(argv[1]); printf("%d\n", (int)fib(n)); } void *fib(void *arg){ int n; pthread_t thread1; pthread_t thread2; void *a; void *b; int c; n = (int)arg; if (n <= 0) return 0; if (n == 1) return 1; pthread_create(&thread1, NULL, fib, n-1); ...
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...
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...
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);
...
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;...
can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <string.h> #include <dirent.h> #include <sys/wait.h> #include <time.h> #include <sys/stat.h> #include <unistd.h> char *pathLog; char *pathRep; struct stat attr; int fileNum, curNum; char *create_logPath(char *directory); void *funcChecker(void *pathSub); void *funcprintTimeAndChanges(void *pathSub); void main(int argc, char *argv[]) { if(argc < 3) { printf("%s must be executed with exactly two additional arguments (pathRep, pathSub)!\n", argv[0]); printf("Typed count is %d\n", argc); printf("Aborted...
I am supposed to write documentation and report for the code below but I am new to operating system concepts I will appreciate if someone can help make a detailed comment on each line of code for better understanding. Thanks #include <pthread.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <ctype.h> #define handle_error_en(en, msg) \ do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) struct thread_info...