Question

Write a C program that has a local and a global variable. The program uses a...

Write a C program that has a local and a global variable. The program uses a fork to create a child process. The parent process modifies both variables to be 10 and 20 and prints out the values. Then the child process modifies both variables to be 100 and 200 and prints out the values? Explain the program output?

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h>  
#include <sys/wait.h>

int globalVar = 1;

int main() {
        int localVar = 2;

        if(fork() == 0) {
                // child process
                globalVar = 100;
                localVar = 200;
                printf("Child process: Global: %d, Local: %d\n", globalVar, localVar);
        } else {
                // parent process
                globalVar = 10;
                localVar = 20;
                printf("Parent process: Global: %d, Local: %d\n", globalVar, localVar);
        }
        waitpid(-1, NULL, 0);

        return 0; 
}

saved gcc version 4.6.3 main.c #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include Parent process: Global: 10, Local: 20 Child process: Global: 100, Local: 200 3 <sys/wait.h> 6 int globalVar 1; 7 8 int main) 9 int localVar-2; 10 if(fork()-=0) { 12 13 14 15 // child process globalVar 100; localVar200; printf(child process: Global: globalVar, localVar); %d, Local: %d\n, } else { 16 17 18 19 20 // parent process globalVar - 10; localVar -20; printf(Parent process: globalVar, localvar); Global : %d, Local: %d\n, 21 waitpid(-1, NULL, 0); 23 24 25 return e;

in case of any concerns, please let me know via comments.

Add a comment
Know the answer?
Add Answer to:
Write a C program that has a local and a global variable. The program uses 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
  • 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...

  • Write a program using the fork() system call to do thefollowing. The parent process (main program)...

    Write a program using the fork() system call to do thefollowing. The parent process (main program) forks a process(CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. Have the parent invoke the wait () call to wait for both the child processesto complete before exiting the program. IN A C program

  • a) Write a C-program that creates a chain of 10 processes and prints out their process...

    a) Write a C-program that creates a chain of 10 processes and prints out their process ids and relationships. For example, process 1 is the parent of process 2, process 2 is the parent of process 3, process 3 is the parent of 4 and so on. Each child has to print out all her ancestors identified by the process ids. b) Write a C-program that creates a fan of 10 processes. That is, process 1 is the parent of...

  • Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process...

    Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process (CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. It also forks another process (CHILD 2) that finds the sum of squares of the first n numbers where n is a variable it shares with the parent. Let CHILD 1 print “The sum...

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

  • Q.5. (a) Write a program/ pseudo-code with following specs. The program contains an integer variable x...

    Q.5. (a) Write a program/ pseudo-code with following specs. The program contains an integer variable x initialized with value 20. After initializing x the program creates 07 processes using fork(). Each parent adds 10 to x and prints x. (15)

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

  • Write a C program for Linux called pipes.c that does the following: In the main() function,...

    Write a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with fork(). Child 1 redirects stdout to the write end of the pipe and then executes with execlp() the "ps -aux" command. Child 2 redirects its input from stdin to the read end of the pipe, then it executes the "sort -r -n -k 5" command. After creating both children, the...

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • Write a “C” program that creates a child process that prints out its pid and new...

    Write a “C” program that creates a child process that prints out its pid and new line and then calls pause() system call. Once it returns from the pause system call the child program must exit with code 5. The parent program must wait for the child process and print out the pid of the child process and the exit status with the following format: “childpid=%d,exitstatus=%d\n”.

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