Sample Questions
How is it possible for programs such as GDB to debug a C program, speci�cally to allow the user to step through code and stop executing the program when a segmentation fault occurs. Explain the requirements and mechanisms for this to be possible. (Max 1000 words)
No response
Copyright By PowCoder代写 加微信 powcoder
This question considers a 2D grid of �oating point numbers with dimensions width and height. Find the position of this grid that has the highest magnitude average. This function computes the sum of the neighbouring values in the 2D grid (NorthWest, North, NorthEast, East, SouthEast, South, SouthWest, West) plus the centre and divides this by 9. Neighbours outside the dimensions of the array are treated as value zero. The grid is given as a 1D array which stores the 2D grid positions row- major order. NULL is returned in found_x or found_y if there is an error. The error can arise if the input array is NULL , dimensions are ≤ 0, or found_x or found_y are NULL values themselves.
The 1D array sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9 represents the grid:
f ( x , y ) = 19 ∑ ∑ ∣ a r r a y ( i , j ) ∣ x−1 0 h > 0
// returns the position of the highest magnitude average of the array
// in variables found_x and found_y
void get_hma (float * array, int w, int h, int * found_x, int * found_y)
Write the multi-threaded version for this function prototype to work. You may write additional helper functions if needed.
// pre : w > 0 h > 0
// returns the position of the highest magnitude average of the array
// in variables found_x and found_y
void get_hma_parallel(int nthreads, float * array, int w, int h, int * found_x, int * found_y)
Compile command:
gcc -Wall -Wvla -Werror -std=gnu11 -Werror=format-security -lm -pthread hma.c -o hma
You are to create an interprocess communication mechanism.
A program’s initial process, P0, creates and maintains a stack data structure.
Many child processes are created thereafter and they rely on the same stack data structure of the
parent process P0.
Only the parent process P0 can modify the stack contents. Any child process can operate on the stack by communicating with P0.
There are two types of child processes:
Worker – performs operations with the stack if is is not full or not empty. Cleaner – performs operations with the stack when it is full or when it is empty.
The cleaner aims to make the stack non-empty and non-full.
Cleaner processes cannot operate on the stack while Worker processes operate and vice versa.
The stack operations are as follows:
// places the integer x on top of the stack
// Otherwise , err is set to non – zero value if the stack is full void push ( struct stack * , int x , int * err );
// removes and returns the integer on top of the stack
// Otherwise , err is set to non – zero value if the stack is empty int pop ( struct stack * , int * err );
// returns the integer on top of the stack
// Otherwise , err is set to non – zero value if the stack is empty int peek ( struct stack * , int * err );
// returns non – zero value when empty int is_empty ( );
Write a program with the necessary interprocess communication to keep the stack synchronised for N child
processes, where child processes can be either Worker or Cleaner types. Assume that N and the number of each type are provided upon beginning the program.
Add comments within your code to describe the inter process communication between parent and child.
You do not have to implement the stack functions. Your task is speci�cally to create the communication and make calls to the stack functions where required (assuming they are implemented correctly).
Compile command
gcc -Wall -Wvla -Werror -std=gnu11 -Werror=format-security stack_share.c -o stack_share
Run command (1 Worker, 3 Cleaner)
./stack_share 4 1 3
Useful functions for your reference: Setting the signal handler
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
Sets the disposition of the signal signum to handler, which is address of a programmer defined function (a “signal handler”). signal() returns the previous value of the signal handler, or
Sending the signal
int raise(int sig);
Sends a signal to the calling process or thread. If the signal handler has returned.
raise() returns 0 on success, and nonzero for failure.
Setting up the pipe
int pipe(int pipefd[2]);
creates a pipe, a unidirectional data channel that can be used
The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe. On success, zero is returned. On error, -1 is returned.
either SIG_IGN, SIG_DFL, or the SIG_ERR on error.
causes a handler to be called, raise
for interprocess communication.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com