Consider the following code and answer the following questions Assume a process
ID: 3750232 • Letter: C
Question
Consider the following code and answer the following questions
Assume a process will be successfully created. What would be the value of x and y at (A) & (B) ? Justify your answer.
Can someone help me on this operating system problems ?
#include <stdio.h>
int x, y ; /* a global variable */
int main() {
int pid;
pthread_t tid ;
pthread_attr_t attr ;
x = 16; y =16;
pid = fork();
if (pid == 0) {
pthread_attr_init(&attr);
pthread_creat(&tid,&attr,runner, NULL);
pthrad_join(tid,NULL);
printf("x is %d, y is%d ", x , y); /* (A) */
}
else if (pid >0) { wait(NULL);
printf("x is %d,y is %d ", x, y); /* (B) */
}
} /* end of main */
void *runner(void *param) {
y=y*2;
x=x-y; Pthread_exit(0) ;
}
Explanation / Answer
I made some correction in the code and the result is :-
A- x is 16,y is 16
B- x is -16, y is 32
#include <stdio.h>
#include<pthread.h>
#include <unistd.h>
int x, y ; /* a global variable */
void *runner(void *param) {
y=y*2;
x=x-y; pthread_exit(0) ;
}
int main() {
int pid;
pthread_t tid ;
pthread_attr_t attr ;
x = 16; y =16;
pid = fork();
if (pid == 0) {
pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner, NULL);
pthread_join(tid,NULL);
printf("x is %d, y is%d ", x , y); /* (A) */
}
else if (pid >0) {
printf("x is %d,y is %d ", x, y); /* (B) */
}
} /* end of main */
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.