Question

In: Computer Science

3. Add mutex locks to tprog2.c to achieve synchronization, and screenshot the output tprog2.c #include <stdio.h>...

3. Add mutex locks to tprog2.c to achieve synchronization, and screenshot the output 

tprog2.c 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void* print_i(void *ptr)
{
  printf("1: I am \n");
  sleep(1);
  printf("in i\n");

}

void* print_j(void *ptr)
{
  printf("2: I am \n");
  sleep(1);
  printf("in j\n");

}

int main() {

  pthread_t t1,t2;

int rc1 = pthread_create(&t1, NULL, print_i, NULL); int rc2 = pthread_create(&t2, NULL, print_j, NULL);

exit(0);

}

Solutions

Expert Solution

Hope you like it and upvote me for the efforts

tprog3.c

#include<stdio.h>

#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>

pthread_mutex_t lock;

void* print_i(void *ptr)

{

pthread_mutex_lock(&lock);
printf("1: I am\n");

sleep(1);

printf("in i\n");

pthread_mutex_unlock(&lock);

}

void* print_j(void *ptr)

{
pthread_mutex_lock(&lock);
printf("2: I am\n");

sleep(1);

printf("in j\n");
pthread_mutex_unlock(&lock);
}

int main()

{

if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init has failed\n");
return 1;
}

pthread_t t1,t2;

int rc1 = pthread_create(&t1, NULL, print_i, NULL);

int rc2 = pthread_create(&t2, NULL, print_j, NULL);
pthread_mutex_destroy(&lock);
exit(0);

}

Output

2: I am

in j

1: I am

in i

Summary: Without mutex,we can observe that, the print message "2: I am" is printed after just printing the message "1: I am". This is because while thread 1(print_i) is processing the scheduler has scheduled thread 2(print_j). And during execution of sleep(1) ie., which means thread 1 is sleeping for 1s, the thread 2 printed the message "2: I am" and it is followed by thread 1 message "in i" as thread 2 is executing sleep(1) during that time. Finally the message "in j" is printed

With mutex lock, thread 2 has locked using mutex and once the thread 2 finished printing all its messages it unlocks using mutex. This locking ensures that eventhough through context switching other threads that gets executed which is trying to lock the same lock(that is already locked by thread 2) will again go to sleep until the thread 2 unlocks it.


Related Solutions

Please in C++ thank you! Please also include the screenshot of the output. I have included...
Please in C++ thank you! Please also include the screenshot of the output. I have included everything that's needed for this. Pls and thank you! Write a simple class and use it in a vector. Begin by writing a Student class. The public section has the following methods: Student::Student() Constructor. Initializes all data elements: name to empty string(s), numeric variables to 0. bool Student::ReadData(istream& in) Data input. The istream should already be open. Reads the following data, in this order:...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main(...
What is the output of the following C program? #include<stdio.h> int fac (int x); void main( ) {                         for (int i=1; i<=2; i++)                                     printf("%d", fac(i)); } int fac(int x) {                         x = (x>1) ? x + fac(x-1) : 100);                         return x; }
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h>...
(12) Explain what will be output of the following program? #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define NUM_THREADS 3 /* 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;   thread_data_t thr_data[NUM_THREADS];   /* create threads */   for (i = 0;...
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include...
Can you write the shell scripts for these C files (code in C): #include <stdio.h> #include <string.h> #include <ctype.h> int main(int argb, char *argv[]){ char ch; int upper=1; if(argb>1){ if(strcmp(argv[1],"-s")==0){ upper=1; } else if(strcmp(argv[1],"-w")==0){ upper=0; } } while((ch=(char)getchar())!=EOF){ if(upper){ printf("%c",toupper(ch)); } else{ printf("%c",tolower(ch)); } } return 0; } ======================================================== #include <stdio.h> #include <stdlib.h> int calcRange(int array[],int size){ int max=array[1]; int min=array[0]; int a=0; for(a=1;a<size;a++){ if(array[a]>max){ max=array[a]; } } for(a=1;a<size;a++){ if(array[a]<min){ min=array[a]; } } printf("%d-%d=%d\n",max,min,max-min); } int main(int arga, char **argv){...
using 2 semaphores and 1 mutex: (solve in simple c++ language for linux terminal and add...
using 2 semaphores and 1 mutex: (solve in simple c++ language for linux terminal and add comments please) The barber shop has one barber (a thread), one barber chair, and n chairs for waiting customers (semaphore), if any, to sit on. If there are no customers (each customer is a thread) present, the barber sits down in the barber chair and falls asleep. When a customer arrives, he has to wake up the sleeping barber. If additional customers arrive while...
PLEASE do the following problem in C++ with the screenshot of the output. THANKS! Assuming that...
PLEASE do the following problem in C++ with the screenshot of the output. THANKS! Assuming that a text file named FIRST.TXT contains some text written into it, write a class and a method named vowelwords(), that reads the file FIRST.TXT and creates a new file named SECOND.TXT, to contain only those words from the file FIRST.TXT which start with a lowercase vowel (i.e., with 'a', 'e', 'i', 'o', 'u'). For example, if the file FIRST.TXT contains Carry umbrella and overcoat...
my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool...
my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool binary[12] = {false,false, false, false,false,false,false,false,false,false,false, false};   int powerTwo[12] = {2048.1028,576, 256, 128, 64, 32, 16, 8 , 4, 2, 1};   int oneZero[9]= {0,0,0,0,0,0,0,0,0};   int tempVal = 0;   double decimal = 0;   int i = 0;   int j = 0;   // Intialization   printf("Starting the CPSC 1011 Decimal to Binary Converter!\n");   printf("Please enter a positive whole number (or EOF to quit): \n");   scanf("%lf", &decimal);   printf("%lf", decimal);...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
3.) Analyze the Java Program LinkRotator RUN the code and add the screenshot of the window...
3.) Analyze the Java Program LinkRotator RUN the code and add the screenshot of the window and output. Describe what code change you would make if you wanted each link to be displayed for 2.5 seconds. _______________________________________________________________ Below is the "LinkRotator" java program: 1: package com.java24hours; 2: 3: import java.awt.*; 4: import java.awt.event.*; 5: import java.io.*; 6: import javax.swing.*; 7: import java.net.*; 8:    9: public class LinkRotator extends JFrame 10: implements Runnable, ActionListener { 11: 12: String[] pageTitle =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT