Question

Complete the following C code by filling all “???”sto write a C program to create a...

Complete the following C code by filling all “???”sto write a C program to create a child process. Before the child process is created, an integer variable A is defined and assigned value 10. Then in the child process, A is reduced by 5. After the child process completes, the parent process increases A by 5. Then what will A’s value be just before the parent process completes? Why? Write the code and run it to verify your answer.

#include <stdio.h>
// include the head files that allow us to use fork()
#include <???                                >
#include <???                                >

// define integer variable A and assign value 10 to it
???


int main()
{
        // define a variable pid that holds the return value of fork() 
        ???


        // call fork() to create a child process
        ???


        // check the returned value of fork() for call failure, being child process or parent process
if (???                 )
        { 
                // call failure, output to stderr ¡°Fork Failed¡±
                ???


                return 1;
        }
        else if (???                    )
        { 
                // this process is the child process, reduce A¡¯s value by 5
                ???

                
                // output to screen ¡°In child process, A=(A)¡±, where (A) is A¡¯s value
                ???

        }
        else
        {
// this process is the parent process, first wait for the child process to complete
                ???


// then increate A¡¯s value by 5
                ???


                // output to screen ¡°In parent process, A=(A)¡±, where (A) is A¡¯s value               
                ???

        }
        
        return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code and output, explanation for the output is after the output and the code

Output: we can see that output is always 15 in the parent process

CODE:

 #include<stdio.h> #include<sys/types.h> #include<unistd.h> int main() { //A=10 int A=10; //create a child process if(fork()==0) { //if control is in child process, decrease A by 5 A=A-5; //print the value printf("Child process A=%d\n",A); } else { //if control is in child process, increase A by 5 A=A+5; //print the value printf("Parent process A=%d\n",A); } return 0; }

Explanation:

fork() returns 0 in child process and returns the PID(process id) of child process inside parent process.

Assuming that we have an OS that uses virtual memory(any modern OS), the child process gets the exact copy of the virtual memory that parent has.

In simple terms, child process will have exact replica of all variables of parent process in a separate location.

If we print the address of A in both child and parent, we will have same address, but in reality, physically A of child and parent will be in completely different locations.

So, even though we use same variable A in child and parent, they are actually in different memory locations

So, in parent process, A=A+5; 15 will be printed.

in child process, A=A-5;  5 will be printed.

Extras:

Having multiple processes is very different than having multiple threads.

When we have multiple threads in a process, all the threads share same memory space, the output will become complecated in that case.

Add a comment
Know the answer?
Add Answer to:
Complete the following C code by filling all “???”sto write a C program to create a...
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...

  • Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also,...

    Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also, include a screen shot of the output and terminal window of each command you used. Read carefully to do this task please. shell.c code given below. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <string.h> int main() { int PID; char lineGot[256]; char *cmd; while (1){ printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes \n) cmd = strtok(lineGot, "\n");...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • 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()...

  • C program To develop a C program to implement a process system call. PROBLEM You are...

    C program To develop a C program to implement a process system call. PROBLEM You are to use the Ubuntu operating system to write a C program that creates a process to determine the identification of the current user of your computer. I mean if joe is the login of the current user of your computer your solution should return joe. Your solution must also provide the pid of both the parent and the child processes. You may use the...

  • 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)

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • Write a C or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the...

    Write a C or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the fork function to produce two processes. In the child process print out the lower case version of the string. In the parent process print out the reversed upper case version of the string (e.g. child: “abc”; parent: ”CBA”). You may call the toupper and tolower functions in the header <ctype.h> if you wish. Specify in the output whether the parent or child process is...

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
ADVERTISEMENT