Question

In: Computer Science

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?

Solutions

Expert Solution

#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int global;
int main()
{
int local;
pid_t id1 = fork();
if (id1==0)
{
wait(NULL);
global=100;
local=200;
printf("Global:%d\tLocal:%d\n",global,local);
}
else
{
global=10;
local=20;
printf("Global:%d\tLocal:%d\n",global,local);   
wait(NULL);
}
return 0;
}

Explanation:

if (id1==0) // if id1==0 then it is child process, so we make it wait until parent is executed
{
wait(NULL);
global=100;
local=200;
printf("Global:%d\tLocal:%d\n",global,local);
}

// id pid>0 then it is parent process, so we modify the variables, print them and then make the parent wait until child also modifies variables and print them

else

{
global=10;
local=20;
printf("Global:%d\tLocal:%d\n",global,local);   
wait(NULL);
}


Related Solutions

(Write a program in C++) A local instructor wants you to write a program to calculate...
(Write a program in C++) A local instructor wants you to write a program to calculate the average score made on exams by her students. For simplicity, she always has only 12 students in each course she teaches. She teaches multiple subjects so she would like to enter the name of the exam. She wants the program to also determine the highest and lowest scores and the number of students who passed and failed the exam. A score of 60...
Write a C++ program: The local taqueria has decided they need to raise their prices. In...
Write a C++ program: The local taqueria has decided they need to raise their prices. In order to soften the blow to their customers, they also want to rename all their burritos to make them sound more desirable. Your program should create two arrays in main() - one string array with 3 burrito types and one float array with 3 associated prices, defined below: string names[] = {"Carnitas", "Pollo", "Veggie"}; float prices[] = {6.95, 6.25, 5.95}; Now, main should declare...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
Write a C or C++ program that uses pthreads to compute the number of values that...
Write a C or C++ program that uses pthreads to compute the number of values that are evenly divisible by 97 between a specified range of values (INCLUSIVE). The program should accept 3 command-line arguments: low value high value number of threads to create to perform the computation -Specifics for program order of input: 0 9000000000 2 this means 0 to 9 Billion (INCLUSIVE); 2 threads alarm(90); this should be the first executable line of the program to make sure...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
Write a C++ or program that parses (and stores into a variable of the correct type)...
Write a C++ or program that parses (and stores into a variable of the correct type) a string into 4 fields separated by a colon (:): Field 1: A non-empty string value that does not contain a colon Filed 2: An odd integer, or empty Filed 3: One of these values: red, orange, yellow, green, blue, indigo, violet Field 4: Another string value (may contain colon characters) output: Accept: sample:556989:green:Longer example string :) Reject (bad color): sample:556989:purple:Longer example string :)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT