Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Consider the following pseudo-code: /* in global memory area accessible by threads */ #define N 100 struct frame *emptyStack[N]; struct frame *displayQueue[N]; int main() {
Consider the following pseudo-code:
/* in global memory area accessible by threads */
#define N 100
struct frame *emptyStack[N];
struct frame *displayQueue[N];
int main() {
/*
** Allocate memory for N frames
** Place the frames' addresses into the empty Stack
*/
InitialiseStackMemory();
thread_t tid1, tid2;
thread_create(&tid1, GetFrame);
thread_create(&tid2, ShowFrame);
sleep(300);
}
GetFrame() {
struct frame *frame;
struct frame local;
while (1) {
CameraGrab(&local); /* get a frame from the camera */
frame = Pop(); /* pop an empty frame address
from the empty stack */
CopyFrame(&local, frame); /* copy data from the local frame
to the frame address*/
Enqueue(frame); /* push the frame address to
} the display queue */
}
ShowFrame() {
struct frame *frame;
struct frame local;
struct frame filtered;
while (1) {
frame=Dequeue(); /* pop the leading full frame from
the full queue */
CopyFrame(frame, &local); /* copy data to the local frame */
Push(frame); /* push the frame address to the
empty stack */
Solarise(&filtered, &local);
VideoDisplay(&filtered);
}
}
This program creates two threads, one calls GetFrame(), which continually grabs frames from a camera, and the other calls ShowFrame(), which continually displays them (after modifying the frame). When the program starts the emptyStack contains the addresses of N empty frames in memory.
The process runs for 5 minutes displaying the contents from the camera.
The procedures Pop() and Push() are maintaining the list of frame addresses on the empty stack. Pop() removes a frame memory address from the empty stack, and Push() adds a memory address to the empty stack.
The procedures Dequeue() and Enqueue() are maintaining the list of frame memory addresses in the display queue in display order. Dequeue() removes the memory address of the next frame
to display from the display queue, and Enqueue() adds a memory address to the end of the display queue.
The stack and the queue are the same size, and are large enough to contain all available frame memory addresses.
/* in global memory area accessible by threads */
#define N 100
struct frame *emptyStack[N];
struct frame *displayQueue[N];
int main() {
/*
** Allocate memory for N frames
** Place the frames' addresses into the empty Stack
*/
InitialiseStackMemory();
thread_t tid1, tid2;
thread_create(&tid1, GetFrame);
thread_create(&tid2, ShowFrame);
sleep(300);
}
GetFrame() {
struct frame *frame;
struct frame local;
while (1) {
CameraGrab(&local); /* get a frame from the camera */
frame = Pop(); /* pop an empty frame address
from the empty stack */
CopyFrame(&local, frame); /* copy data from the local frame
to the frame address*/
Enqueue(frame); /* push the frame address to
} the display queue */
}
ShowFrame() {
struct frame *frame;
struct frame local;
struct frame filtered;
while (1) {
frame=Dequeue(); /* pop the leading full frame from
the full queue */
CopyFrame(frame, &local); /* copy data to the local frame */
Push(frame); /* push the frame address to the
empty stack */
Solarise(&filtered, &local);
VideoDisplay(&filtered);
}
}
This program creates two threads, one calls GetFrame(), which continually grabs frames from a camera, and the other calls ShowFrame(), which continually displays them (after modifying the frame). When the program starts the emptyStack contains the addresses of N empty frames in memory.
The process runs for 5 minutes displaying the contents from the camera.
The procedures Pop() and Push() are maintaining the list of frame addresses on the empty stack. Pop() removes a frame memory address from the empty stack, and Push() adds a memory address to the empty stack.
The procedures Dequeue() and Enqueue() are maintaining the list of frame memory addresses in the display queue in display order. Dequeue() removes the memory address of the next frame
to display from the display queue, and Enqueue() adds a memory address to the end of the display queue.
The stack and the queue are the same size, and are large enough to contain all available frame memory addresses.
a. Without synchronisation code problems will occur. Discuss what could potentially go wrong?
b. Identify the critical sections in the above pseudo-code.
c. Modify the above pseudo-code using semaphores only, to ensure that problems will not occur.
Step by Step Solution
★★★★★
3.41 Rating (157 Votes )
There are 3 Steps involved in it
Step: 1
part a Problem 1 Lets suppose thread2showFrame method runs first then it will try to dequeue a element from the empty displayQueue because thread1 get...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started