Write program in C language using Pthreads API to simulate the following IPC pro
ID: 3711915 • Letter: W
Question
Write program in C language using Pthreads API to simulate the following IPC problem. Refer to Section 5.9.4 for specific instructions on mutex locks and semaphores.
If there are multiple files in your submission, you need to provide a makefile.
Your program should output/display the design of the project, with specific answers to the following questions.
How many threads/processes are there?
What does each thread simulate?
Will each thread be blocked at some time of simulation? If yes, explain the circumstances.
When will a blocked thread be waked up, and by whom?
How many semaphores are there in your project, what is the purpose for having each?
Explanation / Answer
Solution:
code:
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t threadid[4];
struct binop {
int a;
int b;
}BinOp;
pthread_mutex_t lock;
void* add(void *arg)
{
unsigned long i = 0;
printf("Thread 1 has started but waiting ");
pthread_mutex_lock(&lock);
printf("Thread 1 has started running ");
for(i = 0; i < (0xFFFFFFFF); i++);
printf("Addition : %d ", BinOp.a + BinOp.b);
pthread_mutex_unlock(&lock);
return NULL;
}
void* sub(void *arg)
{
unsigned long i = 0;
printf("Thread 2 has started but waiting ");
pthread_mutex_lock(&lock);
printf("Thread 2 has started running ");
for(i = 0; i < (0xFFFFFFFF); i++);
printf("Substraction : %d ", BinOp.a - BinOp.b);
pthread_mutex_unlock(&lock);
return NULL;
}
void* mul(void *arg)
{
unsigned long i = 0;
printf("Thread 3 has started but waiting ");
pthread_mutex_lock(&lock);
printf("Thread 3 has started running ");
for(i = 0; i < (0xFFFFFFFF); i++);
printf("Multiplication : %d ", BinOp.a * BinOp.b);
pthread_mutex_unlock(&lock);
return NULL;
}
void* division(void *arg)
{
unsigned long i = 0;
printf("Thread 4 has started but waiting ");
pthread_mutex_lock(&lock);
printf("Thread 4 has started running ");
for(i = 0; i < (0xFFFFFFFF); i++);
printf("Division : %d ", BinOp.a / BinOp.b);
pthread_mutex_unlock(&lock);
return NULL;
}
int main(void)
{
int i = 0;
int error;
BinOp.a = 4;
BinOp.b = 3;
if (pthread_mutex_init(&lock, NULL) != 0) {
printf(" mutex init has failed ");
return 1;
}
error = pthread_create(&(threadid[0]), NULL, &add, NULL);
if (error != 0)
printf(" Thread can't be created : [%s]", strerror(error));
error = pthread_create(&(threadid[1]), NULL, &sub, NULL);
if (error != 0)
printf(" Thread can't be created : [%s]", strerror(error));
error = pthread_create(&(threadid[2]), NULL, &mul, NULL);
if (error != 0)
printf(" Thread can't be created : [%s]", strerror(error));
error = pthread_create(&(threadid[3]), NULL, &division, NULL);
if (error != 0)
printf(" Thread can't be created : [%s]", strerror(error));
pthread_join(threadid[0], NULL);
pthread_join(threadid[1], NULL);
pthread_join(threadid[2], NULL);
pthread_join(threadid[3], NULL);
pthread_mutex_destroy(&lock);
return 0;
Answer to the quesions:
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.