aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--csci4061/092120_breakout/main.c6
-rw-r--r--csci4061/101220_breakout/exercise.c88
-rw-r--r--csci4061/102620_breakout/msg_queue_template.c107
3 files changed, 170 insertions, 31 deletions
diff --git a/csci4061/092120_breakout/main.c b/csci4061/092120_breakout/main.c
index e5577dd..8d8e08d 100644
--- a/csci4061/092120_breakout/main.c
+++ b/csci4061/092120_breakout/main.c
@@ -26,8 +26,10 @@ int main(int argc, char **argv) {
pid_t pid;
for (int i=0; i<n; i++) {
pid = fork();
- if (pid != 0) {
- printf("%d\n", getpid());
+ if (pid == 0) {
+
+ } else {
+printf("%d\n", getpid());
execl("/bin/echo", "/bin/echo", "hello", "there", NULL);
}
}
diff --git a/csci4061/101220_breakout/exercise.c b/csci4061/101220_breakout/exercise.c
index be8bb11..b6e863e 100644
--- a/csci4061/101220_breakout/exercise.c
+++ b/csci4061/101220_breakout/exercise.c
@@ -7,46 +7,76 @@
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
+#include <time.h>
-int numOfEntries(char* path) {
- int entries = 0;
- while (readdir(path) != NULL) {
- entries++;
- } return entries;
+int numOfEntry(char *path)
+{
+ int count = 0;
+ DIR *dir = opendir(path);
+ struct dirent *entry;
+
+ while ((entry = readdir(dir)) != NULL)
+ {
+
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+ continue;
+
+ count++;
+ }
+
+ closedir(dir);
+
+ return count;
}
-int main(int argc, char** argv){
- if (argc < 2) {
+int main(int argc, char **argv)
+{
+
+ if (argc < 2)
+ {
printf("Pass the path as an argument to the program");
exit(1);
}
- char* path = argv[1];
- DIR* dir = opendir(path);
- if(dir==NULL){
+ char *path = argv[1];
+
+ DIR *dir = opendir(path);
+ if (dir == NULL)
+ {
printf("The path passed is invalid");
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);
+ struct dirent *entry;
+
+ while ((entry = readdir(dir)) != NULL)
+ {
+
+ if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
+ continue;
+
+ if (entry->d_type == DT_DIR)
+ {
+ char next[strlen(path) + strlen(entry->d_name) + 2];
+ next[0] = '\0';
+ strcat(next, path);
+ strcat(next, "/");
+ strcat(next, entry->d_name);
+ printf("Directory: %s\n\tEntries: %d\n", entry->d_name, numOfEntry(next));
}
- }
- /*
- Iterate through the elements in argv[1]
- Refer the ls example in slides if you have any doubts
- */
-
+ else if (entry->d_type == DT_REG)
+ {
+ struct stat *buf = (struct stat *)malloc(sizeof(struct stat));
+ stat(entry->d_name, buf);
+ printf("Regular File: %s\n\tOwner: %d\n\tSize: %f\n\tInode: %llu\n",
+ entry->d_name, buf->st_uid, (double)buf->st_size, buf->st_ino);
+ free(buf);
+ }
+ else
+ {
+ printf("File: %s\n\tType:%hhu\n", entry->d_name, entry->d_type);
+ }
+ }
+
closedir(dir);
return 0;
} \ No newline at end of file
diff --git a/csci4061/102620_breakout/msg_queue_template.c b/csci4061/102620_breakout/msg_queue_template.c
new file mode 100644
index 0000000..676291e
--- /dev/null
+++ b/csci4061/102620_breakout/msg_queue_template.c
@@ -0,0 +1,107 @@
+/*
+* Recitation Section Number:
+* Breakout Number:
+* Member Name (X500)
+* Member Name (X500)
+* Member Name (X500)
+* Member Name (X500)
+*/
+
+#include <stdio.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include <zconf.h>
+#include <string.h>
+#include <stdlib.h>
+
+#define PERM 0666
+#define MSGSIZE 100
+#define NCHILD 3
+
+// structure for message queue
+typedef struct msg_buffer {
+ long mtype;
+ char mtext[MSGSIZE];
+} message;
+
+int main(void) {
+ key_t key;
+ int msgid;
+ pid_t pid1, pid2;
+ message msg;
+
+ // generate unique key
+ key = ftok("recitation", 4061);
+
+ // creates a message queue
+ msgid = msgget(key, PERM | IPC_CREAT);
+
+
+ // sender child process
+ pid1 = fork();
+ if (pid1 == 0) {
+ for (int i = 0; i < NCHILD; i++) {
+ msg.mtype = 111;
+ memset(msg.mtext, '\0', MSGSIZE);
+ sprintf(msg.mtext, "Hello child %d", i);
+ // send message to other child processes
+ msgsnd(msgid, &msg, MSGSIZE, 0);
+
+
+ // display the message
+ printf("[%d] Data sent is : %s \n", i, msg.mtext);
+ }
+
+ for (int i = 0; i < NCHILD; i++) {
+ msg.mtype = 222;
+ memset(msg.mtext, '\0', MSGSIZE);
+ sprintf(msg.mtext, "Message %d", i);
+ // send message to other child processes
+ msgsnd(msgid, &msg, MSGSIZE, 0);
+
+ // display the message
+ printf("[%d] Data sent is : %s \n", i, msg.mtext);
+ }
+
+ for (int i = 0; i < NCHILD; i++) {
+ msg.mtype = 333;
+ memset(msg.mtext, '\0', MSGSIZE);
+ sprintf(msg.mtext, "Bye %d", i);
+ // send message to other child processes
+ msgsnd(msgid, &msg, MSGSIZE, 0);
+
+ // display the message
+ printf("[%d] Data sent is : %s \n", i, msg.mtext);
+ }
+
+ exit(0);
+ } else if (pid1 < 0) {
+ printf("fork1 error\n");
+ return -1;
+ }
+
+ // receiver child processes
+ for (int j = 0; j < NCHILD; j++) {
+ if ((pid2 = fork()) == 0) {
+ // receive message
+ msgrcv(msgid, &msg, MSGSIZE, +222, 0);
+
+
+ // display the message
+ printf("[%d] Data received is : %s \n", j, msg.mtext);
+
+ exit(0);
+ } else if (pid2 < 0) {
+ printf("fork2 error\n");
+ return -1;
+ }
+ }
+
+ while (wait(NULL) > 0);
+
+ // to destroy the message queue
+ msgctl(msgid, IPC_RMID, NULL);
+
+
+ return 0;
+} \ No newline at end of file