Question

In: Computer Science

Consider the following pseudo-code: /* Global memory area accessible by threads */ #define N 100 struct...

Consider the following pseudo-code:

/* Global memory area accessible by threads */
#define N 100
struct frame *emptyStack[N];
struct frame *displayQueue[N];
int main() {
  /*
  ** Initialise by allocating the memory for N frames
  ** And place the N frame addresses into the
  ** empty Stack array
  */
  Initialise();
  thread_t tid1, tid2;
  threadCreate(&tid1, GetFrame);
  threadCreate(&tid2, ShowFrame);
  sleep(300);
}
GetFrame() {
  struct frame *frame;
  struct frame local;
  while (1) {
    CameraGrab(&local);  /* get a frame from the camera store it in local */
    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); /* Modify the image */
    VideoDisplay(&filtered);     /* display the image */
  }

}

This program creates two threads, one calls GetFrame(), which continually grabs frames from a camera, and the other thread callsShowFrame(), which continually displays the frames (after modification). 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.

  1. Without including synchronisation code problems will occur. Discuss what could potentially go wrong?

  2. Identify the critical sections in the above pseudo-code.

  3. Modify the above pseudo-code using semaphores only, to ensure that problems will not occur.

    Hint: this is a variation on the Producer-Consumer problem and will require similar semaphores.

Solutions

Expert Solution

Without including synchronization code problems will occur. Discuss what could potentially go wrong?

Ans: If showFrame() [p1] tries to process the first image from the camera, getFrame() should wait for processing the image. So while trying to get there will be no image in the Queue. So garbage value will be displayed while dequeuing.

So getting and processing should happen simultaneously.

Identify the critical sections in the above pseudo-code.

/* Global memory area accessible by threads */
#define N 100
struct frame *emptyStack[N];
struct frame *displayQueue[N];

int main() {

  /*
  ** Initialise by allocating the memory for N frames
  ** And place the N frame addresses into the
  ** empty Stack array
  */
  Initialise();
  bool flag = FALSE;
  thread_t tid1, tid2;
  threadCreate(&tid1, GetFrame);
  threadCreate(&tid2, ShowFrame);
  sleep(300);
}
GetFrame() {
  struct frame *frame;
  struct frame local;
  while (1) {
    CameraGrab(&local);  /* get a frame from the camera store it in local */

flag= true;

    frame = Pop(); /* pop an empty-frame address from the empty stack */

flag = flase;

    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 */

if(flag == flase)

         Push(frame);          /* push the frame address to the empty stack */
    Solarise(&filtered, &local); /* Modify the image */
    VideoDisplay(&filtered);     /* display the image */
  }

}

Modify the above pseudo-code using semaphores only, to ensure that problems will not occur.

Consider the following pseudo-code:

/* Global memory area accessible by threads */
#define N 100
struct frame *emptyStack[N];
struct frame *displayQueue[N];
int main() {
  /*
  ** Initialise by allocating the memory for N frames
  ** And place the N frame addresses into the
  ** empty Stack array
  */
  Initialise();
  thread_t tid1, tid2;
  threadCreate(&tid1, GetFrame);
  threadCreate(&tid2, ShowFrame);
  sleep(300);
}
GetFrame() {
  struct frame *frame;
  struct frame local;
  while (1) {
    CameraGrab(&local);  /* get a frame from the camera store it in local */
    if (frame = Pop();)
        block();  /* 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 */
    if(Push(frame))
        wakeup();          /* push the frame address to the empty stack */
    Solarise(&filtered, &local); /* Modify the image */
    VideoDisplay(&filtered);     /* display the image */
  }

}


Related Solutions

In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next;...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer Assume that a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member Now write the code that destroys the linked list
Synchronize producer and consumer threads that use 10-item buffer for communication. Write pseudo code for both...
Synchronize producer and consumer threads that use 10-item buffer for communication. Write pseudo code for both threads. Assume that you can use void Buffer::store(Item item) and Item Buffer::get() methods, but you need to explicitly ensure that you never store more than 10 items in the buffer (to prevent overflow).
Translate following pseudo-code to MIPS assembly language cout << “\n Please input a number for $s0”;...
Translate following pseudo-code to MIPS assembly language cout << “\n Please input a number for $s0”; cin >> $s0; cout << “\n Please input a number for $s1”; cin >> $s1; cout << “\n Please input a number for $s2”; cin >> $s2; $t0 = $s0 / 8 - 2 * $s1 + $s2; cout << “\n the Value of the expression “$s0 / 8 - 2 * $s1 + $s2” is ”; cout >> $t0; return;
Consider the following declaration: typedef struct{                                  &nbsp
Consider the following declaration: typedef struct{                                          int A;                                          char B[10];                                          float C;                                          char D;                                  }rectype;                                   typedef rectype matrix[121][4][5];                                   matrix A1; Compute the address of element A1[120][3][3] given the base address at 2000.
Review the following code: Sub RunLoop() For n = 1 to 100 ActiveSheet.Range("A" & n).Value =...
Review the following code: Sub RunLoop() For n = 1 to 100 ActiveSheet.Range("A" & n).Value = n End Sub
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases;...
I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases; }SyncInfo; char buffer[BUFLEN] ; char *phrases[] = {"educated", "educated cat", "educated lion", "serious person" , "serious panda","curious student","curious art student", "obnoxious web developer"}; char localBuffer[BUFLEN]; int allVowelsPresent; void *checker(void *param) { int a=0, e=0, i=0, o = 0, u= 0 ; int* n = (int*)param; // typecasting a void* to int* //printf("%d\n",*n); for (int q=0; q< (*n); ++q) { // dereferencing to get the...
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
Write a C code to let the main thread create N child threads, where each created...
Write a C code to let the main thread create N child threads, where each created thread will randomly generate an integer between 0 to 10 and put it into a global array variable. After that, the main thread will calculate the sum of all the generated integers. N should be input as a command line argument. Complete the following C code by filling all “???”s in the code sketch. NOTE: when you compile the code, you need to add...
#include #include #include #define WORDLENGTH 30 #define MAX 100 struct car { char model[WORDLENGTH]; int year;...
#include #include #include #define WORDLENGTH 30 #define MAX 100 struct car { char model[WORDLENGTH]; int year; int milage; }; typedef struct car Car; Car createCar(char model[], int year, int milage) { Car c; strcpy(c.model, model); c.year = year; c.milage = milage; return c; } void regCars(Car reg[], int *pNrOfCars) { char again[WORDLENGTH] = "yes", model[WORDLENGTH]; int year, milage; while (strcmp(again, "yes") == 0) { printf("Ange model:"); scanf("%s%*c", model); printf("Ange year:"); scanf("%d%*c", &year); printf("Ange milage:"); scanf("%d%*c", &milage); reg[*pNrOfCars] = createCar(model, year,...
Write an inline assembly program that initializes a 100 byte area of memory to 0xFF using...
Write an inline assembly program that initializes a 100 byte area of memory to 0xFF using the STOS instruction with REP using DWORD transfers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT