Please explain what will be printed out at LINE1 and LINE2?
#include <stdio.h>
#include <types.h>
int data[4] = {1,3,5,7};
void *runner (void *param);
int main(int argc,char *argv[])
{
pid_t pid;
pthread_t tid;
pid = fork();
if (pid==0){
pthread_create(&tid,NULL,runner,NULL);
pthread_join(tid,NULL);
for(int i=0,i<4,i++)
printf("from child process, the values at i is %d",data[i]);
}
else if (pid>0){
wait(NULL);
for(int i=0;i<4,i++)
printf("from parent process, the values at i is %d",data[i]);
}
}
void *runner(void *param){
for(int i=0;i<4,i++)
data[i]+=3*i;
pthread_exit(0);
}
}
}
}
}
The output with explanation in comments are given below:
data[4]={1,3,5,7}
LINE 1:
from child process, the values at i is 1 // data[0]=data[0]+3*0=1
from child process, the values at i is 6 // data[1]=data[1]+3*1=3+3=6
from child process, the values at i is 11 // data[2]=data[2]+3*2=5+6=11
from child process, the values at i is 16 // data[3]=data[3]+3*3=7+9= 16
LINE 2:
from parent process, the values at i is 1 // data[0]= 1
from parent process, the values at i is 3 // data[1]= 3
from parent process, the values at i is 5 //data[2]= 5
from parent process, the values at i is 7 //data[3]= 7
Output window:
Get Answers For Free
Most questions answered within 1 hours.