-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
155 lines (130 loc) · 5.24 KB
/
common.h
File metadata and controls
155 lines (130 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* SMT-Aware Multi-Process Job Scheduling System
* Common Header File
*
* This header defines shared data structures and constants used by
* producers, scheduler, and worker processes.
*/
#ifndef COMMON_H
#define COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include <pthread.h>
#include <sched.h>
/* ============== Configuration Constants ============== */
#define MAX_PRODUCERS 3 /* Number of producer processes */
#define MAX_WORKERS 4 /* Number of worker processes */
#define THREADS_PER_WORKER 4 /* Threads in each worker's pool */
#define MAX_QUEUE_SIZE 100 /* Maximum jobs in scheduler queue */
#define MAX_JOB_NAME 32 /* Maximum job name length */
/* IPC Keys */
#define MSG_QUEUE_KEY 0x1234 /* Message queue key */
#define SHM_KEY 0x5678 /* Shared memory key */
#define SEM_KEY 0x9ABC /* Semaphore key */
/* Message Types */
#define MSG_TYPE_JOB 1 /* Job submission message */
#define MSG_TYPE_DISPATCH 2 /* Job dispatch to worker */
#define MSG_TYPE_COMPLETE 3 /* Job completion notification */
#define MSG_TYPE_SHUTDOWN 99 /* Shutdown signal */
/* Job Priority Levels */
#define PRIORITY_LOW 1
#define PRIORITY_MEDIUM 2
#define PRIORITY_HIGH 3
#define PRIORITY_CRITICAL 4
/* ============== Data Structures ============== */
/* Job structure - represents a unit of work */
typedef struct {
int job_id; /* Unique job identifier */
int producer_id; /* ID of producer that created job */
int priority; /* Job priority (1-4) */
int complexity; /* Simulated work units (ms) */
char job_name[MAX_JOB_NAME]; /* Human-readable name */
struct timespec submit_time; /* Time job was submitted */
struct timespec start_time; /* Time job execution started */
struct timespec end_time; /* Time job completed */
} job_t;
/* Message structure for IPC message queue */
typedef struct {
long msg_type; /* Message type for filtering */
int target_worker; /* Target worker ID (-1 for scheduler) */
job_t job; /* Embedded job data */
} job_message_t;
/* Worker status in shared memory */
typedef struct {
int worker_id;
int active_threads; /* Currently running threads */
int jobs_completed; /* Total jobs finished */
int current_load; /* Current workload estimate */
pthread_mutex_t lock; /* Mutex for thread-safe updates */
} worker_status_t;
/* Shared memory structure */
typedef struct {
int shutdown_flag; /* Global shutdown signal */
int total_jobs_submitted; /* Counter for all jobs */
int total_jobs_completed; /* Counter for completed jobs */
worker_status_t workers[MAX_WORKERS];
pthread_mutex_t global_lock; /* Global mutex */
} shared_state_t;
/* Thread pool job queue entry */
typedef struct job_queue_entry {
job_t job;
struct job_queue_entry *next;
} job_queue_entry_t;
/* Thread pool structure */
typedef struct {
pthread_t threads[THREADS_PER_WORKER];
pthread_mutex_t queue_lock;
pthread_cond_t queue_cond;
job_queue_entry_t *queue_head;
job_queue_entry_t *queue_tail;
int queue_size;
int shutdown;
int worker_id;
int msg_queue_id;
shared_state_t *shared_mem;
} thread_pool_t;
/* ============== Utility Functions ============== */
/* Get current time in milliseconds */
static inline long get_time_ms(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
/* Calculate time difference in milliseconds */
static inline double time_diff_ms(struct timespec *start, struct timespec *end) {
return (end->tv_sec - start->tv_sec) * 1000.0 +
(end->tv_nsec - start->tv_nsec) / 1000000.0;
}
/* Color codes for terminal output */
#define COLOR_RESET "\033[0m"
#define COLOR_RED "\033[31m"
#define COLOR_GREEN "\033[32m"
#define COLOR_YELLOW "\033[33m"
#define COLOR_BLUE "\033[34m"
#define COLOR_MAGENTA "\033[35m"
#define COLOR_CYAN "\033[36m"
/* Logging macros */
#define LOG_PRODUCER(id, fmt, ...) \
printf(COLOR_CYAN "[PRODUCER %d] " fmt COLOR_RESET "\n", id, ##__VA_ARGS__)
#define LOG_SCHEDULER(fmt, ...) \
printf(COLOR_YELLOW "[SCHEDULER] " fmt COLOR_RESET "\n", ##__VA_ARGS__)
#define LOG_WORKER(id, fmt, ...) \
printf(COLOR_GREEN "[WORKER %d] " fmt COLOR_RESET "\n", id, ##__VA_ARGS__)
#define LOG_THREAD(wid, tid, fmt, ...) \
printf(COLOR_MAGENTA "[W%d:T%ld] " fmt COLOR_RESET "\n", wid, tid, ##__VA_ARGS__)
#define LOG_ERROR(fmt, ...) \
fprintf(stderr, COLOR_RED "[ERROR] " fmt COLOR_RESET "\n", ##__VA_ARGS__)
#define LOG_INFO(fmt, ...) \
printf(COLOR_BLUE "[INFO] " fmt COLOR_RESET "\n", ##__VA_ARGS__)
#endif /* COMMON_H */