-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreading_Server.c
More file actions
139 lines (114 loc) · 3.9 KB
/
MultiThreading_Server.c
File metadata and controls
139 lines (114 loc) · 3.9 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PROC_DIR "/proc"
#define STAT_FILE "/stat"
#define MAX_PROCESSES 1024
#define PORT 8081
#define MAX_CLIENTS 10
#define MAX_BUFFER_SIZE 1024
typedef struct {
int pid;
char comm[256];
unsigned long utime;
unsigned long stime;
unsigned long total_time;
} ProcessInfo;
int read_process_stat(const char *pid, ProcessInfo *pinfo) {
char stat_path[256];
snprintf(stat_path, sizeof(stat_path), PROC_DIR "/%s" STAT_FILE, pid);
FILE *stat_file = fopen(stat_path, "r");
if (stat_file == NULL) {
return -1;
}
fscanf(stat_file, "%d %s %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u %lu %lu",
&pinfo->pid, pinfo->comm, &pinfo->utime, &pinfo->stime);
pinfo->total_time = pinfo->utime + pinfo->stime;
fclose(stat_file);
return 0;
}
int compare_process(const void *a, const void *b) {
ProcessInfo *p1 = (ProcessInfo *)a;
ProcessInfo *p2 = (ProcessInfo *)b;
return (p2->total_time - p1->total_time);
}
void get_top_cpu_processes(char *result) {
DIR *dir = opendir(PROC_DIR);
if (dir == NULL) {
perror("opendir");
snprintf(result, MAX_BUFFER_SIZE, "Error opening /proc directory");
return;
}
struct dirent *entry;
ProcessInfo processes[MAX_PROCESSES];
int process_count = 0;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR && atoi(entry->d_name) > 0) {
if (read_process_stat(entry->d_name, &processes[process_count]) == 0) {
process_count++;
}
}
}
closedir(dir);
qsort(processes, process_count, sizeof(ProcessInfo), compare_process);
snprintf(result, MAX_BUFFER_SIZE, "Top CPU-consuming processes:\n");
for (int i = 0; i < 2 && i < process_count; i++) {
char temp[1024];
snprintf(temp, sizeof(temp), "PID: %d, Name: %.255s, User Time: %lu, Kernel Time: %lu, Total CPU Time: %lu\n",
processes[i].pid, processes[i].comm, processes[i].utime, processes[i].stime, processes[i].total_time);
strncat(result, temp, MAX_BUFFER_SIZE - strlen(result) - 1);
}
}
void *handle_client(void *client_socket) {
int sock = *(int *)client_socket;
char buffer[1024] = {0};
int valread;
valread = read(sock, buffer, 1024);
printf("%s\n", buffer+valread);
char process_info[MAX_BUFFER_SIZE] = {0};
get_top_cpu_processes(process_info);
send(sock, process_info, strlen(process_info), 0);
close(sock);
free(client_socket);
pthread_exit(NULL);
}
int main() {
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server is listening on port %d\n", PORT);
while (1) {
new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen);
if (new_socket < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("Connection accepted from client IP: %s, Port: %d\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
int *client_socket = malloc(sizeof(int));
*client_socket = new_socket;
pthread_t thread_id;
pthread_create(&thread_id, NULL, handle_client, (void *)client_socket);
pthread_detach(thread_id);
}
close(server_fd);
return 0;
}