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);
for (i = 0; i < 6; i++)
sharedVar++;
printf("Thread [%ld] has finished\n",
pthread_self());
pthread_mutex_unlock(&mutex);
}
int main()
{
pthread_t thread_id[10];
int i ;
/* create thread array */
for (i = 0; i < 10; i++)
{
pthread_create(&thread_id[i],
NULL, threadHandler, NULL);
}
/* wait for exit thread */
pthread_join(thread_id[0], NULL);
pthread_join(thread_id[1], NULL);
pthread_join(thread_id[2], NULL);
pthread_join(thread_id[3], NULL);
pthread_join(thread_id[4], NULL);
pthread_join(thread_id[5], NULL);
pthread_join(thread_id[6], NULL);
pthread_join(thread_id[7], NULL);
pthread_join(thread_id[8], NULL);
pthread_join(thread_id[9], NULL);
/* print shared variable */
printf("Shared Variable - %d\n", sharedVar);
}
I need this output
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
#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);
for (i = 0; i < 6; i++)
sharedVar++;
// print the name of the thread here.
printf("Thread ID(%d) has finished\n",(char*)vargp);
pthread_mutex_unlock(&mutex);
}
int main()
{
pthread_t thread_id[10];
int i ;
/* create thread array */
for (i = 0; i < 10; i++)
{
// Here set the name of the thread
char *name = (char*)i;
// pass the name here
pthread_create(&thread_id[i], NULL, threadHandler,
name);
}
/* wait for exit thread */
pthread_join(thread_id[0], NULL);
pthread_join(thread_id[1], NULL);
pthread_join(thread_id[2], NULL);
pthread_join(thread_id[3], NULL);
pthread_join(thread_id[4], NULL);
pthread_join(thread_id[5], NULL);
pthread_join(thread_id[6], NULL);
pthread_join(thread_id[7], NULL);
pthread_join(thread_id[8], NULL);
pthread_join(thread_id[9], NULL);
/* print shared variable */
printf("Shared Variable - %d\n", sharedVar);
}
OUTPUT:
Pleas help I need to create and array to add to my code something like this...
Read given code RaceOrNot1.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or mutex failures. If you believe there is only one possible output, you just need to write that output. #include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t count_mutex3 = PTHREAD_MUTEX_INITIALIZER; int c[2] = {1,0}; void *UpdateC1(void *arg) { int i; for(i=0;i<1000000;i++) { pthread_mutex_lock(&count_mutex); c[0]=(c[0]+1)%2; c[1]=(c[1]+1)%2; pthread_mutex_unlock(&count_mutex); } return NULL; } void *UpdateC2(void *arg) { int...
Compile and run the deadlockFree.c program below and report if this program does go into deadlock or not. Modify this deadlockFree.c program so that you change the lock/unlock order of the mutexes. Experiment with the order of locks (and maybe extend the number of loop iterations) until you find a scenario where you can demonstrate deadlock. //Program: deadlockFree.c A simple threaded program to demonstrate the deadlock condition.Two threads are used to wait on two MUTEXES. Careful ordering of locks should...
In c programming The Consumer Submits processing requests to the producer by supplying a file name, its location and a character. It also outputs the contents of the file provided by the producer to the standard output. The Producer Accepts multiple consumer requests and processes each request by creating the following four threads. The reader thread will read an input file, one line at a time. It will pass each line of input to the character thread through a queue...
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...
//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...
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...
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;...
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...
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...
Read given code and write all possible outputs of the program. Assume there will be no thread creation or joining failures or mutex failures. If you believe there i only one possible output, you just need to write that output. Code: #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> sem_t s1; int c[2] = {0,1}; void *UpdateC1(void *arg) { int i; for(i=0;i<1000000;i++) { sem_wait(&s1); c[0]=(c[0]+1)%2; c[1]=(c[1]+1)%2; sem_post(&s1); } return NULL; } void *UpdateC2(void *arg) { int i; for(i=0;i<2000000;i++)...