#output is segmentation fault

14 messages · Page 1 of 1 (latest)

woeful echoBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

serene acorn
#

!f

woeful echoBOT
#
#include <pthread.h>    //Create POSIX threads.
#include <semaphore.h>  //To create semaphores
#include <stdio.h>      //Input Output
#include <stdlib.h>
#include <time.h>    //Wait for a random time.
#include <unistd.h>  //Thread calls sleep for specified number of seconds.

#define CUSTOMER_NUMBER 25
#define CUSTOMER_ARRIVAL_TIME_MIN 1
#define CUSTOMER_ARRIVAL_TİME_MAX 3
#define REGISTER_NUMBER 5
#define COFFEE_TIME_MIN 2
#define COFFEE_TIME_MAX 5

int arrival_time = 0;

int customer_num[CUSTOMER_NUMBER];  // The array that holding customers id

void* Customer_Activity(void* customer_id);

int main(int argc, char* argv[]) {
  int id;
  int i;

  pthread_t customers[CUSTOMER_NUMBER];

  sem_init(&registers_sem, 0, REGISTER_NUMBER);

  for (i = 0; CUSTOMER_NUMBER; i++) {
    customer_num[i] = i;
  }

  // Create the customer threads

  for (id = 0; id < CUSTOMER_NUMBER; id++) {
    pthread_create(&customers[id], NULL, Customer_Activity,
                   (void*)&customer_num[i]);
  }

  for (i = 0; i < CUSTOMER_NUMBER; i++) {
    pthread_join(customers[i], NULL);
  }
  return 0;
}

void* Customer_Activity(void* customer_id) {
  int c_id = *(int*)customer_id;
  srand(time(NULL));
  arrival_time = CUSTOMER_ARRIVAL_TIME_MIN + rand() % CUSTOMER_ARRIVAL_TİME_MAX;
  sleep(arrival_time);
  printf("CUSTOMER %d IS CREATED %d AFTER SECONDS.", c_id, arrival_time);
yildiz
serene acorn
#

for(i = 0; CHSTOMER_NUMBER; i++) is an infinite loop

#

I'm assuming you meant i < CUSTOMER_NUMBER

wicked glacier
#

where

#

i can not find

serene acorn
wicked glacier
#

thanks

#

The segmentation fault has been fixed, but where should sleep be, so each customer should be generated at random times, how should I do this?

woeful echoBOT
#

@wicked glacier Has your question been resolved? If so, run !solved :)

#

@wicked glacier

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. You can use !solved to close a post and mark it as solved.

wicked glacier
#

!solved