From 89ba57eb2b9e024996da29ba5e429adf15b27a6d Mon Sep 17 00:00:00 2001 From: Matthew Strapp Date: Mon, 19 Oct 2020 07:55:41 -0500 Subject: add breakouts --- .vscode/tasks.json | 2 +- csci4061/101220_breakout/exercise.c | 22 ++++++++-- csci4061/101920_breakout/pipe_template.c | 72 ++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 csci4061/101920_breakout/pipe_template.c diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 9792107..6af30e3 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -6,7 +6,7 @@ { "label": "Build c++", "type": "shell", - "command": "g++", + "command": "gcc", "args": [ "-g", "-Wall", diff --git a/csci4061/101220_breakout/exercise.c b/csci4061/101220_breakout/exercise.c index 5021142..be8bb11 100644 --- a/csci4061/101220_breakout/exercise.c +++ b/csci4061/101220_breakout/exercise.c @@ -9,9 +9,10 @@ #include int numOfEntries(char* path) { - /* - Count the number of entries in path - */ + int entries = 0; + while (readdir(path) != NULL) { + entries++; + } return entries; } int main(int argc, char** argv){ @@ -27,7 +28,20 @@ int main(int argc, char** argv){ return -1; } struct dirent* entry; - + DIR* current = dir; + +while ((entry = readdir(dir)) != NULL) { + if (entry->d_type == "DT_DIR"){ + printf("Directory: %s\n", entry->d_name); + printf(" Entries: %d\n", numOfEntries(entry)); + } else if (entry->d_type == "DT_REG") { + printf("Regular File: %s\n", entry->d_name); + printf(" Owner: %d\n"); + printf(" Size: %f\n"); + } else { + printf("%s\n", entry -> d_name); + } + } /* Iterate through the elements in argv[1] Refer the ls example in slides if you have any doubts diff --git a/csci4061/101920_breakout/pipe_template.c b/csci4061/101920_breakout/pipe_template.c new file mode 100644 index 0000000..bb59d8d --- /dev/null +++ b/csci4061/101920_breakout/pipe_template.c @@ -0,0 +1,72 @@ +// ----------------------------------------------------------------------------- +// Pipes +// +// Write a program that creates a child process and pipes the string +// "hello child" to the child to be printed. The parent should wait for the +// child to terminate +// +// Template: Implement the TODOs +// +// Expected output: +// Parent Sending: hello child +// Child Received: hello child +// ----------------------------------------------------------------------------- + +#include +#include +#include +#include +#include + +int main(void) { + // Open pipe + // TODO + int ends[2]; + pipe(ends); + + char string_to_send[] = "hello child"; + int bytes_to_send_recv = strlen(string_to_send) + 1; + + // Create child process + pid_t pid = fork(); + if (pid < 0) { + // Error + fprintf(stderr, "ERROR: Failed to fork\n"); + } + else if (pid > 0) { + // Parent + printf("Parent Sending: %s\n", string_to_send); + + // Parent doesn't need read file descriptor + // TODO + close(ends[0]); + // Write the string to the pipe + // TODO + write(ends[1], string_to_send, bytes_to_send_recv); + // Done writing + // TODO + close(ends[1]); + // Wait for child to terminate + wait(NULL); + } + else { + // Child + + char *recv_buffer = malloc(bytes_to_send_recv); + + // Child doesn't need write file descriptor + // TODO + close(ends[1]); + // Read the string from the pipe + // TODO + read(ends[0], recv_buffer, bytes_to_send_recv); + // Done reading + // TODO + close(ends[0]); + // Print result + printf("Child Received: %s\n", recv_buffer); + free(recv_buffer); + } + + return 0; +} -- cgit v1.2.3