Skip to content
This repository was archived by the owner on Jun 20, 2022. It is now read-only.

Commit c6b0f21

Browse files
committed
Add CSE 381 notes (draft)
1 parent a51602d commit c6b0f21

File tree

8 files changed

+396
-0
lines changed

8 files changed

+396
-0
lines changed

CMakeLists.txt

+19
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,22 @@ add_executable(CSE_278_OVERLOADING_OPERATORS
4040

4141
add_executable(CSE_278_MISC
4242
"CSE 278 (Systems I - Introduction to Systems Programming)/Misc/main.cpp")
43+
44+
# CSE 381
45+
add_executable(CSE_381_DATA_STRUCTURES_VECTOR
46+
"CSE 381 (Systems II - OS, Currency, Virtualization, and Security)/C++ Data Structures/vector.cpp")
47+
48+
add_executable(CSE_381_DATA_STRUCTURES_UNORDERED_MAP
49+
"CSE 381 (Systems II - OS, Currency, Virtualization, and Security)/C++ Data Structures/unordered_map.cpp")
50+
51+
add_executable(CSE_381_CONSOLE_IO
52+
"CSE 381 (Systems II - OS, Currency, Virtualization, and Security)/Console IO/main.c")
53+
54+
add_executable(CSE_381_IPC_PIPES
55+
"CSE 381 (Systems II - OS, Currency, Virtualization, and Security)/IPC (Interprocess Communication)/pipes.c")
56+
57+
add_executable(CSE_381_IPC_SHARED_MEMORY
58+
"CSE 381 (Systems II - OS, Currency, Virtualization, and Security)/IPC (Interprocess Communication)/shared-memory.c")
59+
60+
add_executable(CSE_381_NETWORKING_SERVER
61+
"CSE 381 (Systems II - OS, Currency, Virtualization, and Security)/Networking/server.c")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <unordered_map>
3+
#include <string>
4+
5+
int main() {
6+
std::unordered_map<std::string, int> map;
7+
8+
map.insert({"Foo", 1});
9+
map.insert({"Bar", 2});
10+
map.insert({"Baz", 3});
11+
map["Baz"] *= 2;
12+
13+
for (std::pair<std::string, int> item : map) {
14+
std::cout << item.first << " :: " << item.second << std::endl;
15+
}
16+
17+
return EXIT_SUCCESS;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
int main() {
5+
std::vector<int> vector;
6+
7+
std::cout << "Empty: " << vector.empty() << std::endl;
8+
for (auto i = 0; i < 5; i++) {
9+
std::cout << "Adding " << i << " to vector" << std::endl;
10+
vector.push_back(i);
11+
}
12+
std::cout << std::endl;
13+
14+
std::cout << "Empty: " << vector.empty() << std::endl;
15+
std::cout << "Size: " << vector.size() << std::endl;
16+
std::cout << "Capacity: " << vector.capacity() << std::endl;
17+
std::cout << "Max size: " << vector.max_size() << std::endl;
18+
std::cout << std::endl;
19+
20+
std::cout << "Resizing to 4" << std::endl;
21+
std::cout << "New size: " << vector.size() << std::endl;
22+
std::cout << "New capacity: " << vector.capacity() << std::endl;
23+
std::cout << "New max size: " << vector.max_size() << std::endl;
24+
std::cout << std::endl;
25+
26+
std::cout << "Shrinking to fit" << std::endl;
27+
std::cout << "New size: " << vector.size() << std::endl;
28+
std::cout << "New capacity: " << vector.capacity() << std::endl;
29+
std::cout << "New max size: " << vector.max_size() << std::endl;
30+
std::cout << std::endl;
31+
32+
std::cout << "Output of begin and end: ";
33+
for (auto i = vector.begin(); i != vector.end(); ++i) {
34+
std::cout << *i << " ";
35+
}
36+
// for (auto &i : vector) {
37+
// std::cout << i << " ";
38+
// }
39+
std::cout << std::endl;
40+
41+
std::cout << "Output of cbegin and cend: ";
42+
for (auto i = vector.cbegin(); i != vector.cend(); ++i) {
43+
std::cout << *i << " ";
44+
}
45+
// for (auto i : vector) {
46+
// std::cout << i << " ";
47+
// }
48+
std::cout << std::endl;
49+
50+
std::cout << "Output of rbegin and rend: ";
51+
for (auto ir = vector.rbegin(); ir != vector.rend(); ir++) {
52+
std::cout << std::endl;
53+
std::cout << *ir << " ";
54+
}
55+
std::cout << std::endl;
56+
57+
std::cout << "\nOutput of crbegin and crend : ";
58+
for (auto ir = vector.crbegin(); ir != vector.crend(); ir++) {
59+
std::cout << *ir << " ";
60+
}
61+
std::cout << std::endl;
62+
63+
return EXIT_SUCCESS;
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main(int argc, char *argv[]) {
5+
int num;
6+
printf("Enter a number: ");
7+
scanf("%d", &num);
8+
printf("You entered %d\n", num);
9+
10+
return EXIT_SUCCESS;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <string.h>
5+
#include <unistd.h>
6+
7+
#define BUFFER_SIZE 25
8+
#define READ_END 0
9+
#define WRITE_END 1
10+
int main(int argc, char** argv) {
11+
int pipe_file_descriptors[2];
12+
if (pipe(pipe_file_descriptors) == -1) {
13+
fprintf(stderr, "Cannot create pipe\n"); // Perhaps the system is out of file descriptors?
14+
}
15+
16+
fflush(stdin);
17+
18+
// In this example, the parent will write to the child
19+
pid_t child_pid = fork();
20+
if (child_pid > 0) {
21+
// Parent
22+
// In this example, the parent doesn't need the read end
23+
close(pipe_file_descriptors[READ_END]);
24+
25+
// Send message to child
26+
char message[BUFFER_SIZE] = "Hello World\0";
27+
printf("Sending message: %s\n", message);
28+
write(pipe_file_descriptors[WRITE_END], message, strlen(message));
29+
30+
// Close pipe when finished
31+
close(pipe_file_descriptors[WRITE_END]);
32+
} else if (child_pid == 0) {
33+
// Child
34+
// In this example, the child doesn't need the write end
35+
close(pipe_file_descriptors[WRITE_END]);
36+
37+
// Read message from parent
38+
char message[BUFFER_SIZE];
39+
read(pipe_file_descriptors[READ_END], message, BUFFER_SIZE);
40+
printf("Got message: %s\n", message);
41+
42+
// Close pipe when finished
43+
close (pipe_file_descriptors[READ_END]);
44+
} else {
45+
fputs("Cannot fork to create child process\n", stderr);
46+
return EXIT_FAILURE;
47+
}
48+
return EXIT_SUCCESS;
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <unistd.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <string.h>
8+
#include <fcntl.h>
9+
#include <sys/shm.h>
10+
#include <sys/stat.h>
11+
#include <sys/wait.h>
12+
#include <unistd.h>
13+
#include <sys/mman.h>
14+
#include <errno.h>
15+
16+
#define SHARED_MEMORY_OBJ_POSIX_ID "CSE381"
17+
#define SHARED_MEMORY_OBJ_SIZE 4096
18+
19+
int main(int argc, char *argv[]) {
20+
pid_t child_pid = fork();
21+
if (child_pid > 0) {
22+
// You are the parent process - Your child's PID is child_pid
23+
const char *message_0 = "Hello";
24+
const char *message_1 = "World";
25+
26+
// See https://en.wikipedia.org/wiki/Memory-mapped_file
27+
int file_descriptor_to_shared_memory_object = shm_open(SHARED_MEMORY_OBJ_POSIX_ID, O_CREAT | O_RDWR, 0666);
28+
ftruncate(file_descriptor_to_shared_memory_object, SHARED_MEMORY_OBJ_SIZE);
29+
void *pointer_to_shared_memory_object = mmap(0, SHARED_MEMORY_OBJ_SIZE, PROT_WRITE, MAP_SHARED, file_descriptor_to_shared_memory_object, 0);
30+
31+
sprintf(pointer_to_shared_memory_object, "%s", message_0);
32+
pointer_to_shared_memory_object += strlen(message_0);
33+
sprintf(pointer_to_shared_memory_object, "%s", message_1);
34+
pointer_to_shared_memory_object += strlen(message_1);
35+
36+
wait(NULL);
37+
38+
shm_unlink(SHARED_MEMORY_OBJ_POSIX_ID);
39+
40+
return EXIT_SUCCESS;
41+
} else if (child_pid == 0) {
42+
// You don't have a child process - You are the child
43+
44+
// See https://en.wikipedia.org/wiki/Memory-mapped_file
45+
int file_descriptor_to_shared_memory_object = shm_open(SHARED_MEMORY_OBJ_POSIX_ID, O_RDONLY, 0666);
46+
ftruncate(file_descriptor_to_shared_memory_object, SHARED_MEMORY_OBJ_SIZE);
47+
void *pointer_to_shared_memory_object = mmap(0, SHARED_MEMORY_OBJ_SIZE, PROT_READ, MAP_SHARED, file_descriptor_to_shared_memory_object, 0);
48+
49+
printf("%s\n", (char *) pointer_to_shared_memory_object);
50+
51+
shm_unlink(SHARED_MEMORY_OBJ_POSIX_ID);
52+
return EXIT_SUCCESS;
53+
} else {
54+
fputs("Cannot fork to create child process", stderr);
55+
return EXIT_FAILURE;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <sys/socket.h>
5+
#include <netinet/in.h>
6+
#include <strings.h>
7+
#include <zconf.h>
8+
9+
#define BUFFER_SIZE 256
10+
#define INCOMING_CONNECTIONS_TO_QUEUE 5
11+
12+
void error(char *msg) {
13+
perror(msg);
14+
exit(1);
15+
}
16+
17+
int main(int argc, char *argv[]) {
18+
// Parse commandline parameters
19+
if (argc == 1) {
20+
fprintf(stdout, "Usage: %s PORT\n", argv[0]);
21+
fputs(stderr, "PORT is required");
22+
return EXIT_FAILURE;
23+
} else if (argc > 2) {
24+
fprintf(stdout, "Usage: %s PORT\n", argv[0]);
25+
fputs(stderr, "Too many arguments provided");
26+
return EXIT_FAILURE;
27+
}
28+
const int port_number = atoi(argv[1]);
29+
30+
int socket_file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
31+
if (socket_file_descriptor < 0) {
32+
fputs(stderr, "Cannot create IPv4 socket");
33+
return EXIT_FAILURE;
34+
}
35+
36+
struct sockaddr_in server_address;
37+
38+
bzero((char *) &server_address, sizeof(server_address));
39+
40+
server_address.sin_family = AF_INET;
41+
server_address.sin_port = htons(port_number);
42+
server_address.sin_addr.s_addr = INADDR_ANY;
43+
44+
char buffer[BUFFER_SIZE];
45+
46+
// Listen
47+
if (bind(socket_file_descriptor, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
48+
fputs(stderr, "Cannot bind socket");
49+
return EXIT_FAILURE;
50+
}
51+
listen(socket_file_descriptor, INCOMING_CONNECTIONS_TO_QUEUE);
52+
53+
// Accept connection
54+
struct sockaddr_in client_address;
55+
socklen_t client_address_length = sizeof(client_address);
56+
int clientsocket_file_descriptor = accept(socket_file_descriptor, (struct sockaddr *) &client_address, &client_address_length);
57+
if (clientsocket_file_descriptor < 0) {
58+
fputs(stderr, "Cannot accept TCP connection from socket");
59+
return EXIT_FAILURE;
60+
}
61+
bzero(buffer, 256);
62+
63+
// Read
64+
ssize_t bytesTransfered;
65+
bytesTransfered = read(clientsocket_file_descriptor, buffer, 255);
66+
if (bytesTransfered < 0) {
67+
fputs(stderr, "Cannot read from socket");
68+
return EXIT_FAILURE;
69+
}
70+
printf("Got message: %s\n", buffer);
71+
72+
// Write
73+
bytesTransfered = write(clientsocket_file_descriptor, "Received message: ", 18);
74+
if (bytesTransfered < 0) {
75+
fputs(stderr, "Cannot write to socket");
76+
return EXIT_FAILURE;
77+
}
78+
bytesTransfered = write(clientsocket_file_descriptor, buffer, BUFFER_SIZE);
79+
if (bytesTransfered < 0) {
80+
fputs(stderr, "Cannot write to socket");
81+
return EXIT_FAILURE;
82+
}
83+
84+
return EXIT_SUCCESS;
85+
}

0 commit comments

Comments
 (0)