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.
3 messages · Page 1 of 1 (latest)
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.
where should i use individual random arrival time in sleep function. should i use sleep function as soon as i create thread or should i use sleep in customer activity function or should i use it elsewhere
#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
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(®isters_sem, 0, REGISTER_NUMBER);
for (i = 0; i < CUSTOMER_NUMBER; i++) {
customer_num[i] = i;
}
// Create the customer threads
for (id = 0; id < CUSTOMER_NUMBER; id++) {
srand(time(NULL));
arrival_time =
CUSTOMER_ARRIVAL_TIME_MIN + rand() % CUSTOMER_ARRIVAL_TİME_MAX;
sleep(arrival_time);
pthread_create(&customers[id], NULL, Customer_Activity,
(void*)&customer_num[id]);
}
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;
printf("\nCUSTOMER %d IS CREATED %d AFTER SECONDS.", c_id, arrival_time);
}
where should i use individual random arrival time in sleep function. should i use sleep function as soon as i create thread or should i use sleep in customer activity function or should i use it elsewhere