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){...
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...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT