Skip to content

Commit 0b167a8

Browse files
Emmanuel MassaquoiEmmanuel Massaquoi
authored andcommitted
Merge branch 'master' of github.com:Maxxquoi/Introduction-to-Operating-Systems
2 parents 298ac2d + 6644b10 commit 0b167a8

File tree

8 files changed

+550
-4
lines changed

8 files changed

+550
-4
lines changed

p1/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
ReadWrite: ReadWrite.c
3-
gcc -o ReadWrite ReadWrite.c -pthread
3+
gcc -std=c99 -o ReadWrite ReadWrite.c -pthread
44

55
Clean:
66
rm ReadWrite

p1/ReadWrite.c

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
#include <stdio.h>
2+
#include <stdlib.h>
23
#include <string.h>
34
#include <pthread.h>
45

6+
#define NUM_THREADS 10
7+
58
//
69
// Created by Emmanuel Massaquoi on 6/19/17.
710
//
811

912
int shared_var = 0;
10-
int resource_use = 0;
13+
int resource_use;
14+
int num_reader = 0;
15+
16+
int reading = 0;
17+
int writing = 1;
1118

1219
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
1320
pthread_cond_t read_phase = PTHREAD_COND_INITIALIZER;
@@ -19,7 +26,29 @@ void *writer (void *param);
1926

2027
int main(int argc, char *argv[]) {
2128

22-
printf("Hello World/m");
29+
pthread_t tid[NUM_THREADS];
30+
31+
for (int i = 0; i < 5; i++) {
32+
33+
printf("Create Reader\n");
34+
if(pthread_create(&tid[i], NULL, reader, NULL) != 0) {
35+
fprintf(stderr, "Unable to create reader thread");
36+
exit(1);
37+
}
38+
39+
printf("Create Writer\n");
40+
if(pthread_create(&tid[i+5], NULL, writer, NULL) != 0) {
41+
fprintf(stderr, "Unable to create writer thread");
42+
exit(1);
43+
}
44+
}
45+
46+
for (int j = 0; j < NUM_THREADS; ++j) {
47+
pthread_join(tid[j], NULL);
48+
}
49+
50+
printf("Parent quiting\n");
51+
2352
return 0;
2453
}
2554

@@ -29,10 +58,39 @@ void *reader(void *param) {
2958

3059
pthread_mutex_lock(&m);
3160

32-
while (resource_use == 1) {
61+
while (resource_use == writing) {
3362
pthread_cond_wait(&read_phase, &m);
3463
}
3564

65+
printf("Reader Value: %d\n", shared_var);
66+
printf("Number of Readers Present: %d\n", num_reader);
67+
68+
resource_use = reading;
69+
pthread_cond_broadcast(&write_phase);
70+
71+
pthread_mutex_unlock(&m);
72+
}
73+
74+
return 0;
75+
}
76+
77+
void *writer(void *param) {
78+
79+
for (int i = 1; i <= 20; ++i) {
80+
pthread_mutex_lock(&m);
81+
82+
while (resource_use == reading || num_reader != 0) {
83+
pthread_cond_wait(&write_phase, &m);
84+
}
85+
86+
shared_var = i;
87+
printf("Written Value: %d\n", shared_var);
88+
printf("Number of Readers Present: %d\n", num_reader);
89+
90+
resource_use = writing;
91+
pthread_cond_broadcast(&read_phase);
92+
3693
pthread_mutex_unlock(&m);
3794
}
95+
3896
}

p1Solutions/echo-client.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* ============================================================================
2+
* Introduction to Operating Systems
3+
* CS 8803, GT OMSCS
4+
*
5+
* Unauthorized copying of this file, via any medium is strictly prohibited.
6+
*
7+
* "echo-client.c"
8+
* Implements the client for "The Echo Protocol" in Problem Set 1.
9+
============================================================================ */
10+
#include <netdb.h>
11+
#include <netinet/in.h>
12+
#include <stdio.h>
13+
#include <strings.h>
14+
#include <stdlib.h>
15+
#include <unistd.h>
16+
17+
/* CONSTANTS =============================================================== */
18+
#define SERVER_ADDR "localhost"
19+
#define SERVER_PORT 8888
20+
#define BUFFER_SIZE 1024
21+
#define MESSAGE "ramblin wreck"
22+
23+
int main(int argc, char **argv) {
24+
25+
int socket_fd = 0;
26+
struct sockaddr_in server_socket_addr;
27+
char buffer[BUFFER_SIZE];
28+
29+
// Converts localhost into 0.0.0.0
30+
struct hostent *he = gethostbyname(SERVER_ADDR);
31+
unsigned long server_addr_nbo = *(unsigned long *)(he->h_addr_list[0]);
32+
33+
// Create socket (IPv4, stream-based, protocol likely set to TCP)
34+
if (0 > (socket_fd = socket(AF_INET, SOCK_STREAM, 0))) {
35+
fprintf(stderr, "client failed to create socket\n");
36+
exit(1);
37+
}
38+
39+
// Configure server socket address structure (init to zero, IPv4,
40+
// network byte order for port and address)
41+
bzero(&server_socket_addr, sizeof(server_socket_addr));
42+
server_socket_addr.sin_family = AF_INET;
43+
server_socket_addr.sin_port = htons(SERVER_PORT);
44+
server_socket_addr.sin_addr.s_addr = server_addr_nbo;
45+
46+
// Connect socket to server
47+
if (0 > connect(socket_fd, (struct sockaddr *)&server_socket_addr, sizeof(server_socket_addr))) {
48+
fprintf(stderr, "client failed to connect to %s:%d!\n", SERVER_ADDR, SERVER_PORT);
49+
close(socket_fd);
50+
exit(1);
51+
} else {
52+
fprintf(stdout, "client connected to to %s:%d!\n", SERVER_ADDR, SERVER_PORT);
53+
}
54+
55+
// Send echo message
56+
if (0 > send(socket_fd, MESSAGE, strlen(MESSAGE), 0)) {
57+
fprintf(stderr, "client failed to send echo message");
58+
close(socket_fd);
59+
exit(1);
60+
}
61+
62+
// Process response from server
63+
bzero(buffer, BUFFER_SIZE);
64+
if(0 > read(socket_fd, buffer, BUFFER_SIZE)) {
65+
fprintf(stderr, "client could not read response from server\n");
66+
close(socket_fd);
67+
exit(1);
68+
} else {
69+
fprintf(stdout, "echo from server: %s\n", buffer);
70+
}
71+
72+
// Close the socket and return the response length (in bytes)
73+
close(socket_fd);
74+
return 0;
75+
}

p1Solutions/echo-server.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* ============================================================================
2+
* Introduction to Operating Systems
3+
* CS 8803, GT OMSCS
4+
*
5+
* Unauthorized copying of this file, via any medium is strictly prohibited.
6+
*
7+
* "echo-server.c"
8+
* Implements the server for "The Echo Protocol" in Problem Set 1.
9+
============================================================================ */
10+
#include <arpa/inet.h>
11+
#include <ctype.h>
12+
#include <netdb.h>
13+
#include <netinet/in.h>
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
#include <string.h>
17+
#include <sys/socket.h>
18+
#include <unistd.h>
19+
20+
/* CONSTANTS =============================================================== */
21+
#define SERVER_PORT 8888
22+
#define BUFFER_SIZE 1024
23+
24+
void capitalizeBuffer(char *buffer) {
25+
do {
26+
*buffer = toupper((unsigned char) *buffer);
27+
} while (*buffer++);
28+
}
29+
30+
int main(int argc, char **argv) {
31+
32+
int socket_fd = 0;
33+
int client_socket_fd = 0;
34+
35+
char buffer[BUFFER_SIZE];
36+
int num_bytes = 0;
37+
38+
int set_reuse_addr = 1; // ON == 1
39+
int max_pending_connections = 1;
40+
41+
struct sockaddr_in server;
42+
struct sockaddr_in client;
43+
struct hostent *client_host_info;
44+
char *client_host_ip;
45+
socklen_t client_addr_len;
46+
47+
// Create socket (IPv4, stream-based, protocol likely set to TCP)
48+
if (0 > (socket_fd = socket(AF_INET, SOCK_STREAM, 0))) {
49+
fprintf(stderr, "server failed to create the listening socket\n");
50+
exit(1);
51+
}
52+
53+
// Set socket to use wildcards - i.e. 0.0.0.0:21 and 192.168.0.1:21
54+
// can be bound separately (helps to avoid conflicts)
55+
if (0 != setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &set_reuse_addr, sizeof(set_reuse_addr))) {
56+
fprintf(stderr, "server failed to set SO_REUSEADDR socket option (not fatal)\n");
57+
}
58+
59+
// Configure server socket address structure (init to zero, IPv4,
60+
// network byte order for port and address)
61+
// Address uses local wildcard 0.0.0.0.0 (will connect to any local addr)
62+
bzero(&server, sizeof(server));
63+
server.sin_family = AF_INET;
64+
server.sin_addr.s_addr = htonl(INADDR_ANY);
65+
server.sin_port = htons(SERVER_PORT);
66+
67+
// Bind the socket
68+
if (0 > bind(socket_fd, (struct sockaddr *)&server, sizeof(server))) {
69+
fprintf(stderr, "server failed to bind\n");
70+
exit(1);
71+
}
72+
73+
// Listen on the socket for up to some maximum pending connections
74+
if (0 > listen(socket_fd, max_pending_connections)) {
75+
fprintf(stderr, "server failed to listen\n");
76+
exit(1);
77+
} else {
78+
fprintf(stdout, "server listening for a connection on port %d\n", SERVER_PORT);
79+
}
80+
81+
// Get the size client's address structure
82+
client_addr_len = sizeof(client);
83+
84+
// Accept a new client
85+
if (0 > (client_socket_fd = accept(socket_fd, (struct sockaddr *)&client, &client_addr_len)))
86+
{
87+
fprintf(stderr, "server accept failed\n");
88+
} else {
89+
fprintf(stdout, "server accepted a client!\n");
90+
}
91+
92+
// Determine who sent the echo so that we can respond
93+
client_host_info = gethostbyaddr((const char *)&client.sin_addr.s_addr, sizeof(client.sin_addr.s_addr), AF_INET);
94+
if (client_host_info == NULL) {
95+
fprintf(stderr, "server could not determine client host address\n");
96+
}
97+
client_host_ip = inet_ntoa(client.sin_addr);
98+
if (client_host_ip == NULL) {
99+
fprintf(stderr, "server could not determine client host ip\n");
100+
}
101+
fprintf(stdout, "server established connection with %s (%s)\n", client_host_info->h_name, client_host_ip);
102+
103+
// Read echo from the client
104+
bzero(buffer, BUFFER_SIZE);
105+
num_bytes = read(client_socket_fd, buffer, BUFFER_SIZE);
106+
if (num_bytes == 0) {
107+
fprintf(stderr, "server could not read from socket\n");
108+
} else {
109+
fprintf(stdout, "server received %d bytes: %s\n", num_bytes, buffer);
110+
}
111+
112+
// Optional: Modifying client message before echo'ing (correctness test)
113+
capitalizeBuffer(buffer);
114+
115+
// Echo back to the client
116+
if (0 > write(client_socket_fd, buffer, strlen(buffer))) {
117+
fprintf(stderr, "server could not write back to socket\n");
118+
} else {
119+
fprintf(stdout, "server sending message back to client\n");
120+
}
121+
122+
// Close the socket and return
123+
close(socket_fd);
124+
return 0;
125+
}

0 commit comments

Comments
 (0)