The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
CS4315 Operating Systems
Lab 3: Basic Synchronization using Mutex
In this lab work we will do some practice about basic synchronization.
Question 1 is about creating threads and each thread runs a different function.
Question 2 and 3 are about mutex lock (acquiring a lock and releasing a lock).
1. Run the following C program (tprog1.c) and observe the result on the screen.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
// These two functions will run concurrently
void* print_i(void *ptr)
{
printf("I am in in");
}
void* print_j(void *ptr)
{
printf("I am in jn");
}
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);
}
2. Run the following C program (tprog2.c) and observe the result on the screen. Briefly explain why we may get the interleaving messages from these two processes.
#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 in");
}
void* print_j(void *ptr)
{
printf("2: I am n");
sleep(1);
printf("in jn");
}
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);
}
3. Add mutex locks to tprog2.c to achieve synchronization, and then observe the result on your screen.
Submit the following on Blackboard: 1) a text file (e.g. q2.txt) that includes your answer to question 2.
2) tprog3.c (i.e., tprog2.c with mutex lock) 3) a screenshot of running tprog3
If you like you can include 1-3 in one document, e.g. a word file.
-----------