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 i;
for(i=0;i<2000000;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 *UpdateC3(void *arg)
{
int i;
for(i=0;i<3000000;i++)
{
pthread_mutex_lock(&count_mutex3);
c[0]=(c[0]+1)%2;
c[1]=(c[1]+1)%2;
pthread_mutex_unlock(&count_mutex3);
}
return NULL;
}
int main(int argc, char *argv[])
{
int rt,i;
pthread_t t[3];
rt=pthread_create( &t[0], NULL, &UpdateC1,
NULL);
if( rt!=0 )
fprintf(stderr,"Thread %d creation
failed: %d\n", 0,rt);
rt=pthread_create( &t[1], NULL, &UpdateC2,
NULL);
if( rt!=0 )
fprintf(stderr,"Thread %d creation
failed: %d\n", 1,rt);
rt=pthread_create( &t[2], NULL, &UpdateC3,
NULL);
if( rt!=0 )
fprintf(stderr,"Thread %d creation
failed: %d\n", 2,rt);
for(i=0;i<3;i++)
{
rt=pthread_join( t[i], NULL);
if( rt!=0 )
fprintf(stderr,"Wait for thread %d failed: %d\n", i,rt);
}
printf ("\t%c\t%c\n",'a'+c[0],'a'+c[1]);
return 0;
}
Possible outputs are:
a a
a b
b a
exaplaination:
at any time one thread will be running as there is a mutex lock wheneven there is a updation in each thread execution. So 3 possible values for the array of C are (0,0) , (0,1) , (1,0)
After the all theards completes their execution , there is printf which prints the a character
a + c[0] a+c[1] ie a + 0 a+ 0=> a a
a + c[0] a+c[1] ie a +0 , a+1 => a b
a + c[0] a+c[1] ie a +1 a+0 => b a
Read given code RaceOrNot1.c and write all possible outputs of the program. Assume there will be...
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++)...
Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or semaphore 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> #include <semaphore.h> #include <errno.h> sem_t s1; int c=0,x=0; void *UpdateC1(void *arg) { int i; for(i=0;i<2000000;i++) { sem_wait(&s1); c++; x++; sem_post(&s1); } } void *UpdateC2(void *arg) { int i,x=0; for(i=0;i<2999999;i++) { sem_wait(&s1); c++; x++; ...
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...
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);
...
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...
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 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...
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; //...
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...
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...