Given code:
//Include necessary header files
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wait.h>
#include <signal.h>
#include "csapp.h"
//Initialize the global counter variable as 2
volatile long counter = 2;
//Define a function named handler1()
void handler1(int sig)
{
//Variable declaration
sigset_t mask, prev_mask;
//Call the function sigfillset()
sigfillset(&mask);
//Call the function sigprocmask() and block sigs
sigprocmask(SIG_BLOCK, &mask, &prev_mask);
//Call the function sio_putl()
sio_putl(--counter);
//Call the function sigprocmask() and restore sigs
sigprocmask(SIG_SETMASK, &prev_mask, NULL);
//Exit
_exit(0);
}
//Define a function named main()
int main()
{
//Variable declaration
pid_t pid;
sigset_t mask, prev_mask;
//Print the counter value
printf("%ld ", counter);
//Flush the output buffer using fflush()
fflush(stdout);
//Call the function signal()
signal(SIGUSR1, handler1);
//Check the condition
if ((pid = Fork()) == 0)
{
//True, execute the statement
while(1) {};
}
//Call the function kill()
Kill(pid, SIGUSR1);
//Call the function waitpid()
Waitpid(-1, NULL, 0);
//Call the function Sigfillset()
Sigfillset(&mask);
//Call the function sigprocmask() and block sigs
Sigprocmask(SIG_BLOCK, &mask, &prev_mask);
//Print the counter value
printf("%ld", ++counter);
//Call the function sigprocmask() and restore sigs
Sigprocmask(SIG_SETMASK, &prev_mask, NULL);
//Exit
exit(0);
}
Want to see the full answer?
Check out a sample textbook solutionChapter 8 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- (a) Assume that five generation units with third order cost function (F, (R) = A; P+ B;P+C; P; + D;) are in the circuit. Write a computer program using any abitrary programming longuage (MATLAB, C++, C#, Python,.) to calculate economic load dipatch (ELD) using first order gradient method. Note that all parameters and variables should be defined inside the program (at tirst lines) such that units' characteristics and demand can be changed easily. Neglect grid losses. jusing dynamic programming (DP) methed.arrow_forward(C PROGRAMMING ONLY) 2. Weird Treasure Chestby CodeChum Admin I'm now in the middle of the jungle when I saw this weird treasure chest. It seems that if I properly place the address of a certain number, I get a very weird result! Instructions: In the code editor, you are provided with a treasureChestMagic() function which has the following description:Return type - voidName - treasureChestMagicParameters - an address of an integerDescription - updates the value of a certain integer randomlyYou do not have to worry about how the treasureChestMagic() function works. All you have to do is ask the user for an integer and then call the treasureChestMagic() function, passing the address of that integer you just asked.Finally, print the updated value of the integer inputted by the user.Input 1. An integer Output Enter n: 10Treasure Chest Magic Result = 13arrow_forward(17*) (4x3 marks + 2x4 marks) 1. Give an analysis of the running time (Big-Oh notation) for each of the following 4 program fragments. Note: the running time corresponds here to the number of times the operation sum++ is executed. sqrt is the function that returns the square root of a given number. ) Program Running time (Big-Oh notation) sum 0; for (i=0;iarrow_forward(Chapter 10) Here again is the example used in our lecture to show the difference between scoping with blocks and dynamic scoping; fun g x = let val inc = 1; fun f y = y + inc; fun h z = let val inc = 2; in f z end; in h x end; Annotate it as follows: Draw a circle around every block, and number the blocks. Identify each definition of a name. For each definition, describe its scope in terms of your block numbers. For each occurrence of a name (other than a definition of that name), show which definition is used to bind it. Check that this agrees with your scopes.arrow_forward[Problem 4] Consider the sequence of integers 0, 1, 1, 7, 13, 55, 133, 463.. a) find the recursive function and initial conditions which generates them b) using the solution in part a), find the closed form solution for this sequence c) implement the recursive function in Pythonarrow_forward(c) An electronic rainfall depth measurement device is developed to collect the data from the past 7 days as shown in Table 1 to study the weather condition at UniMAP. Table 1 [Jadual 1] Location: UniMAP Day Rainfall Depth (mm) 5.9 1 2 6.7 3 2.4 7.8 5.2 4 5 1.8 7 3.5 (i) Write a program in C to receive the values of rainfall depth and display the number of days with light rain ( 7.6 mm) from the record in Table 1. (ii) Rewrite the program in (c)(i) to identify and display the day with the lowest value of rainfall depth, the day with the highest value of rainfall depth and the average value of rainfall depth for the past 7 days.arrow_forward(Exhaustive Search: The Assignment Problem) Complete the application of exhaustive search to The following assignment: [9 2 7 8] 64 37 5818 769 4 C = 1, 2, 3, 4 1, 2, 4, 3 1, 3, 2, 4 1, 3, 4, 2 1, 4, 2, 3 1, 4, 3, 2 Complete the remaining cases: Answer: cost = 9+4+1+4 = 18 cost = 9+4+8+9 = 30 cost = 9+3+8+4 = 24 cost = 9+3+8+6 = 26 cost = 9+7+8+9 = 33 cost = 9+7+1+6 = 23arrow_forward(a) Assume that five generation units with third order cost function (F: (P) = A: P²³ + B; P; ² + C; P; +D;) are in the circuit. Write a computer program using any arbitrary programming language (MATLAB, C++, C#, Python,...) to calculate economic. load dispatch (ELD) using first order gradient method. Note that all parameters and variables should be defined inside the program (at first lines) such that units' characteristics and demand can be changed easily. Neglect grid losses.arrow_forward(Pyhton help)For Part a and b,PLS more details.arrow_forward(C Language) Write a recursive function called PrintNumPattern() to output the following number pattern. Given a positive integer as input (Ex: 12), subtract another positive integer (Ex: 3) continually until a negative value is reached, and then continually add the second integer until the first integer is again reached. For this lab, do not end output with a newline.arrow_forward(Greedy Algorithms) Suppose you are given n tasks j={j1,j2,...jn}.Each task jl has two parts -a preprocessing phase which takes pl unites and a main phase which takes fl units of time. There are n machines that can esecute the main phase of the jobs in parallel. However, the preprocessing phases need to be execute sequentially on a special machine. The completion time of any schedule is the earliest time when all tasks have finished execution. Design a greedy algorithm which produces a schedule the minimizes the completion time. Here, you need to give formal proof of correctnessarrow_forward(C PROGRAM ONLY) 1. Undercover Mission Planby CodeChum Admin Hi Programmer, I'm Agent J. I'm preparing for an undercover mission going to the enemy's base. However, it seems that my plans are still missing some few details. Can you help me with this? Instructions: In the code editor, there's a main() function that calls the recursive printPlan() function.The printPlan() function already contains some code but it seems to be missing a base case that makes it stop. Supposedly, this printPlan() function should only print the plan by n / 2 number of times. For example, if n is 10, then this should only print the plan 5 times or if n is 20, then this should only print the plan 10 times.Fix this function by adding the correct condition in its base case.For this problem, assume that the value of n is always divisible by 2.Input 1. Value of n Output Enter n: 6Plan by Agent J.Plan by Agent J.Plan by Agent J.arrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr