Question

OPERATING SYSTEMS QUESTION: Given the code below: int main() { pid_t pid; int value = 10;...

OPERATING SYSTEMS QUESTION:

Given the code below:    
int  main()  
{     
   pid_t  pid;
   int value = 10;
   pid = fork();     
   if (pid == 0) {  /* child */            
      value += 8;             
      printf(“CHILD: value = %d\n”, value);  /* Line A */            
      exit(0);     
   } else { /* parent */           
      value += 5;      
      wait(NULL);           \
      printf(“PARENT: value = %d\n”, value);  /* Line B */           
      exit(0);     
   }
}  
What is printed at Line B? Explain why in a few sentences.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

​​​​​​When we use fork() the new child process was created. so there are two different processes running in a program. Parent process and child process.

If the Child process created successfully it returns the value zero.

Explanation:

We assigned the 10 to the variable named value by using int value = 10 statement.

In the below code we create a single child process by using the statement pid = fork().  

Hence there are two different processes running in the program.

Initially value is 10 for the parent process. The parent process does not have the pid 0. Hence it executes else part the 5 was added with the value variable and it becomes 15. Hence parent value is 15.

Initially value is 10 for the child process. The child process has the pid 0. Hence it executes if part the 8 was added with the value variable and it becomes 18. Hence child value is 18.

Add a comment
Know the answer?
Add Answer to:
OPERATING SYSTEMS QUESTION: Given the code below: int main() { pid_t pid; int value = 10;...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • plz help me

    #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 10; int main() { pid_t pid;  pid = fork();  if (pid == 0) {  value = value + 100;  } else if (pid > 0) { value = value -100;  printf("PARENT: value= %d \n", value); //Line A  wait (NULL); } }  (i) What will be the output in Line A? Justify your answer. (ii) Do you think there is synchronization problem in updating the variable value? Justify your answer.

  • Systems Programming Purpose: Understand the fork() regarding its returning values, global/local variables declared in the parent's...

    Systems Programming Purpose: Understand the fork() regarding its returning values, global/local variables declared in the parent's process, and processing Instruction: Attached is a lab.c file containing a fork() statement. Compile and run it. Explain the output of this program. Thing to submit: Submit a word file to explain the following questions: When fork() is executed, what is happening to the returning values in parent and in child? After fork() is executed, how many more process is created? What is the...

  • Figure 〈 1013 > int main pid t pidfork if (pid0) printt "In child process: x*d\n",-x)...

    Figure 〈 1013 > int main pid t pidfork if (pid0) printt "In child process: x*d\n",-x) exit (0) printf ("In parent process: x return 0 *d\n", ++x) Part A- The Output of fork Let's examine the behavior of fork0 through a simple example. Consider the program in Figure 1. Figure 1) This program will produce two lines of output when executed. What are the values of x in each of these lines? Separate your answers for the first and second...

  • Question 3: [2+2] a)What, to whom and how many values a fork system call returns? b)Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in

    Question 3:                  [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){      pid_t  fork_return;        fork_return = fork(); if (fork_return == 0) {                  execlp("/bin/ls", "ls", NULL);                 printf("Child process ID: %d\n", getpid());      exit(0);        }        else { wait (NULL);                 printf("Parent process ID: %d\n", getpid());         }     return 0;}

  • I wrote the following code to create a child to do something and return. The return...

    I wrote the following code to create a child to do something and return. The return value is to be caught by the parent. Can you see any problem with the code that I wrote? How will you fix it? pid_t pid = fork(); if ( pid < 0 ) exit ( 1 ); if ( pid == 0 ) wait(); exit ( 0 ); (6 points, Operating System question)

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

  • Question 3: [2+2] a)What, to whom and how many values a fork system call returns? b)Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in

    Arid No is 19-arid-898Question 3:                  [2+2]a) What, to whom and how many values a fork system call returns? b) Assuming there is no syntax error, what is the output for the following C program for linux? All of you may need to put screenshot of your PC in which Terminal login must be your arid number.int main(){      pid_t  fork_return;        fork_return = fork(); if (fork_return == 0) {                  execlp("/bin/ls", "ls", NULL);                 printf("Child process ID: %d\n", getpid());      exit(0);        }        else { wait (NULL);                 printf("Parent process ID: %d\n",...

  • I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int...

    I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int main(int argc,char* argv[]) {      if(argc > 1)      {            char* commandName;            for(int i=2;i            {                   forkChild = fork();                   signal(SIGINT,sighandler);                   if(forkChild == 0)                   {                          execlp(commandName,commandName,argv[i],NULL);                          exit(0);                   }                   else                   {                          wait(NULL);                   }       } My problem is that I would like to kill the child with ^C but leave the parent running....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT