Question

Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int...

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);
}
}
}
}
}

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this...
CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this lab assignment, please use a Word document to include the screenshots and write your answer. 1. Run the following C program, and submit a screenshot of the result. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main( ) { pid_t pid; if ( (pid = fork()) == 0 ) { printf (“I am the child, my pid = %d and my parent pid = %d\n”,...
2. Calculate number of times hello is printed. #include <stdio.h> #include <sys/types.h> int main() { fork();...
2. Calculate number of times hello is printed. #include <stdio.h> #include <sys/types.h> int main() { fork(); fork(); fork(); printf("hello\n"); return 0; }
Using the program below, identify the values of pid at lines A, B, C, and D....
Using the program below, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)   #include <sys/types.h>   #include <stdio.h>   #include <unistd.h>      int main()   {   pid_t pid, pid1;        /* fork a child process */     pid = fork();        if (pid lt; 0) { /* error occurred */       fprintf(stderr, "Fork Failed");       return 1;     }     else if (pid == 0) { /* child process */       pid1 = getpid();       ...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
The following program calls the fork() system call to create a child process. Suppose that the...
The following program calls the fork() system call to create a child process. Suppose that the actual pids of the parent process and child process are as follows: Parent process: 801 Child process:   802 (as returned by fork) Assume all supporting libraries have been included. => Use the answer text field to write the values of the pids printed at lines 1, 2, 3, and 4 (indicate each line number and the value printed). int main() {      pid_t pid,...
Compile and run the following code and presuming your executable is in a.out, run this command...
Compile and run the following code and presuming your executable is in a.out, run this command from the shell prompt: ./a.out ; echo "Parent Terminated Normally with a return value of $?" Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Hint: This has everything to do with how processes “communicate” their exit...
What is output? #include <stdio.h> void CheckValue(int* pointVar1, int* pointVar2) {       if (pointVar1 == NULL...
What is output? #include <stdio.h> void CheckValue(int* pointVar1, int* pointVar2) {       if (pointVar1 == NULL && pointVar2 == NULL) {       printf("Pointer is null\n");    }    else if (*pointVar2 > *pointVar1) {       printf("%p\n", *pointVar2);    }    else if (*pointVar1 > *pointVar2) {       printf("%p\n", *pointVar1);    } } int main() {    int num1 = 5;    int num2 = 9;       CheckValue(&num1, &num2);           return 0; } a. 0 b. 5 c. 9 d. Pointer is null e....
(5 pts.) Consider the C program below. Recall that fflush() just forces the printed value to...
(5 pts.) Consider the C program below. Recall that fflush() just forces the printed value to be output immediately. (For space reasons, we are not checking error return codes, so assume that all functions return normally.) int main () {    if (fork() == 0) {        if (fork() == 0) {           printf("3"); fflush(stdout);        }        else {           pid_t pid; int status;           if ((pid = wait(&status)) > 0) {                  printf("4"); fflush(stdout);           }        }...
What would appear in the console window after the code below executes? #include <stdio.h> int main()...
What would appear in the console window after the code below executes? #include <stdio.h> int main() { char myLetter = 'A'; printf("The value of A is %d", myLetter); }
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT