Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 3 weeks ago

c - creating & joining threads in for loop?

So basically I'm making a simulator in c for vehicles entering in and out of cell phone tower zones. I'm trying to add code in simulator.c that makes the function spawn a new thread for each cell tower as well as one thread to handle the display. The simulator program should continue up until all 7 cell tower threads have completed, and the cell tower threads should continue until they receive a shutdown request. The display thread runs indefinitely but gets killed when the simulator process completes. I'm having trouble understanding what parameters I'm supposed to use to create and join the threads. I posted below what I have so far in the simulator.c program. My attempt is trying to loop the statements to make 7 threads for 7 cell towers. I'm not sure if my first parameter for pthread_create is correct and I'm not really sure what my 4th parameter is supposed to be. I know I'm supposed to pass in some sort of argument but I'm not sure what. Also I'm not too sure what the requirements mean for "the cell tower threads should continue until they receive a shutdown request." Each cell tower thread is supposed to call handleIncomingRequests() from cellTower.c and the display thread should call showSimulation() from display.c

Update:

Any help would be really appreciated.

simulator.c:

 #include <stdio.h>

 #include <stdlib.h>

 #include <pthread.h>

 #include "simulator.h"

 #include "display.c"

 #include "cellTower.c"

 int main() {

   City ottawa;

   // Cell tower data

   short xLocations[7] = {100, 400, 650, 640, 400, 120, 200};

   short yLocations[7] = {100, 200, 150, 450, 500, 350, 500};

   short cellRadii[7] = {110, 200, 150, 180, 160, 120, 100};

   int cellColors[7] = {0xFF8822 /*Orange*/, 0xFF2222 /*Red*/, 0xFFFF44 

Update 2:

/*Yellow*/,

     0x22FF22 /*Green*/, 0xAA66FF /*Purple*/, 0x0099FF /*Blue*/,

     0x999999 /*LightGray*/};

   // Set up the Cell Towers with the above data ... and no connected vehicles to begin

   ottawa.numTowers = 7;

   for (int i=0; i<ottawa.numTowers; i++) {

  ottawa.towers[i].online = 1;

  ottawa.towers[i].id = (unsigned char)i;

  ottawa.towers[i].x = xLocations[i];

  ottawa.towers[i].y = yLocations[i];

 

Update 3:

 ottawa.towers[i].radius = cellRadii[i];

  ottawa.towers[i].color = cellColors[i];

  ottawa.towers[i].numConnectedVehicles = 0;

  for (int j=0; j<MAX_CONNECTIONS; j++)

    ottawa.towers[i].connectedVehicles[j].connected = 0;

   }

   // Remove the line of code below. Add code to spawn the necessary threads and

   // wait for their completion before exiting gracefully with some kind of message

  pthread_t cellTowers[ottawa.numTowers];

  int i;

  for(i = 0; i < ottawa.numTowers; i++){

Update 4:

   pthread_create(cellTowers[i], NULL, handleIncomingRequests, "");

   pthread_join(cellTowers[i], NULL);

  }

  pthread_t displayTower;

  pthread_create(&displayTower, NULL, showSimulation, "");

 }

There are no answers yet.
Be the first to answer this question.