Question

Figure 〈 1013 > int main pid t pidfork if (pid0) printt In child process: x*d\n,-x) exit (0) printf (In parent process: xPart A- The Output of fork Lets examine the behavior of fork0 through a simple example. Consider the program in Figure 1. Fi

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

Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)


#include <stdio.h>
#include <ntsid.h>
#include <zconf.h>
#include <stdlib.h>

int x = 4;
int main ()
{
    pid_t pid = fork();
    if (pid == 0){
       printf("In Child process: x = %d\n", --x);
        exit(0);
    }
    printf("In parent processs: x = %d\n", ++x);
    return 0;
}

-------------------------------------------------------------------------------------------------
//output

In Child process: x = 3

In parent processs: x = 5



| Fork [-/CLionProjects/Fork] - ../main.c -Fork main.c * ÷ *ー △CMakeLists.txtー..maine #include <stdio.h> #include <ntsid.h> i

Add a comment
Know the answer?
Add Answer to:
Figure 〈 1013 > int main pid t pidfork if (pid0) printt "In child process: x*d\n",-x)...
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
  • 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.

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

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

  • Q1) Including the initial parent process, how many processes are created by the program shown in...

    Q1) Including the initial parent process, how many processes are created by the program shown in Figure 1? Give an explanation for your answer. You can include an output statement to test and run the program. A sample output statement could be: printf("Testing......"); #include <stdio.h> #include <unistd.h> int main() { /*fork a child process*/ fork(); /*fork another child process*/ fork(); /*fork another child process*/ fork(); return 0; } Figure 1a - How many processes are created? Another version, now displaying...

  • Modify the program in Figure 8.18 so that the following two conditions are met: 1. Each...

    Modify the program in Figure 8.18 so that the following two conditions are met: 1. Each child terminates abnormally after attempting to write to a location in the read-only text segment. 2. The parent prints output that is identical (except for the PIDs) to the following: child 12255 terminated by signal 11: Segmentation fault child 12254 terminated by signal 11: Segmentation fault Modify your solution to Problem 8.24 so that one (and only one) child installs a Segmentation-fault handler which...

  • Consider the following program: 1int main) 2nt count1; 3 int pid 0,pid2-0 4if ((pid - fork))) cou...

    Consider the following program: 1int main) 2nt count1; 3 int pid 0,pid2-0 4if ((pid - fork))) count-count 2; printf("%d ", count); 8 if (count 1) count++ pid2-fork) printf("%d ", 12 13 14 if(pid2){ 15 16 17 18 19 > count); wa i tpid(pid2, NULL, θ); countcount*2; printf("%d ", count); a. How many processes are created during the execution of this program? Explain b. List all the possible outputs of the program c. If we delete line 15 (the waitpid) show...

  • What is the output of the following code: void forkexample() { int x = 1;   ...

    What is the output of the following code: void forkexample() { int x = 1;    if (fork==0) printf(“ Child has x = %d\n”, ++x); Else printf(“ Parent has x = %d\n”, - - x); } int main () { forkexample(); Return 0; }

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

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

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