Question: Please draw the memory layout of the process and the threads created by the following program when the thread-1 is at LINE A and thread-2
Please draw the memory layout of the process and the threads created by the following program when the thread-1 is at LINE A and thread-2 is at LINE B. Explicitly show all the memory regions (text, data, stack and heap) for each process and display the variables and their respective values as stored in each region.Also, what will be printed by this program on the screen?
#include
#include
#include
int raqam=80;
void *printOdd(void *param);
void *printEven(void *param);
// This function must print 5 odd numbers
void *printOdd(void *param) {
int i=0;
int raqam= *((int *)param);
for (i=0; i<5; ) {
if (raqam%2!=0){
printf("Odd=%d ",raqam);
i++;
}
raqam++;
sleep(1);
}
// LINE A
}
// This function must print 5 even numbers
void *printEven(void *param) {
int i=0;
int raqam= *((int *)param);
for (i=0; i<5; ) {
if (raqam%2==0){
printf("Even=%d ",raqam);
i++;
}
raqam++;
sleep(1);
}
// LINE B
}
int main(int argc, char *argv[]) {
pthread_t tid1, tid2;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tid1, &attr, printOdd, &raqam);
pthread_create(&tid2, &attr, printEven, &raqam);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
