From 59e3d32e381951d358e5a969cdb6886dfa4073c2 Mon Sep 17 00:00:00 2001 From: p-w-rs Date: Thu, 15 Sep 2022 14:38:32 -0500 Subject: [PATCH] stuff --- Examples/ipc/MYFILE | 1 + Examples/ipc/a.out | Bin 0 -> 49896 bytes Examples/ipc/ipcfs1.c | 2 +- Examples/ipc/ipcmsg1.c | 30 ++++++++++++++++-------------- Examples/ipc/ipcpipe1.c | 9 +++++---- Examples/ipc/ipcpipe2.c | 10 ++++++---- Examples/ipc/ipcshm1.c | 26 ++++++++++++++------------ Examples/ipc/ipcshm2.c | 27 ++++++++++++++------------- Examples/ipc/signal1.c | 7 ++++--- Examples/ipc/signal2.c | 6 +++--- 10 files changed, 64 insertions(+), 54 deletions(-) create mode 100644 Examples/ipc/MYFILE create mode 100755 Examples/ipc/a.out diff --git a/Examples/ipc/MYFILE b/Examples/ipc/MYFILE new file mode 100644 index 0000000..d9605cb --- /dev/null +++ b/Examples/ipc/MYFILE @@ -0,0 +1 @@ +HELLO \ No newline at end of file diff --git a/Examples/ipc/a.out b/Examples/ipc/a.out new file mode 100755 index 0000000000000000000000000000000000000000..d3ea8c2ed467a84bd84eee1c5bf7216f3cc9596c GIT binary patch literal 49896 zcmeI5U2IfE6vwBOLMb0D(GXP7RZ;~X_);TA&=y$Sh6*c`NC-IYc5m5Bx4Y}zEl^`) zP1BHOt>M9#MoFwMJ{TT&@QDYl9|^pf5Ticeqvn>>q(-8RCf5I%xwqZDfbor({3khc z&Y3xL=FIO-d7I^%KY#gqsZw?IN~uMV7Rb>CrAAZ?mQwdY5)iGE?JvZS#Sa~n(b`s5 z>)zIrJU<{%>#q3GuG$c@zO8ml1wKkqp-6<*W#?>JEZc6$w+n~C)Q4eF@E3={q8F>7 zYFF!0xj$W+AByIic+TeA8AODBxS%n}w7yZN4i@oT@wr68Ytb7|s7bJ!W%%Xb}z zXuY?+t6lFoc;K-8x^1?u@H46%tSdaT*141}4fLf8IbAG!cg&Zx%azrT5b>+?Ev&TG!ry0S$ASIvy&aamQj?K$!k6X;2NUk$pNNb+EDzuDCnza!1g;LE>i7%C!fL;zsLZpRzNUWTpnuE+&w;AMFQ#}ya ztDs{LS-YjbZFstiV z7~8O1@1G(LMJenFNc#s+8naXcX$o}FtRktcwOv0e8gY}=%N4#S_z>q6f_ zk4WFoMeX+CRT-T2zc+I;iLw64%Q2a_flfSZ@Frp|DgT-c@h?ILXPn9f zW8+3o`{#|mi7njuEhJvbkQcg@K_5vc>=x%RARe~ZZMTp|#R%~`ptntqUhz-Cn1X~G zR^k^%g;YtGU)=?F#*I)4yW+S52Qb zea7@Xrq7!Gl<9NmJIB`dLA4*AYh7Q5=c`$%V(RC)xw))-%c<*PwTg93tZial6-(ZL zPJJfUez86k>t(S%5z7&4Laf)sdQYrDvEGK2>`2CA``SA?j~t3Osh)z@tM(+~ot+2O zo~)b8#5||laZfv$SZgWP;}!a1#gymd%S|e2qmsszFxROL>z37c;EZ#qbr5OQTHdBA z)cqush`dvmCboThTZi|kvf8wHBOIV60TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq z5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH*AOR8}0TLhq5+DH* zAOR8}0TLhq5+DH*xO)Vq@jC(XV*tMgS_S|AdsCqA4fKP7-WcfZf!-PDeSyAvC*jT{ zKmsH{0wh2JBtQZrKmsH{0wh2JBtQZrKmsH{0wh2JBtQZrKmsH{0wh2JBtQZrKmsH{ z0wh2JBtQZrKmsH{0wh2JBtQZrKmsH{0wh2JBtQZrKmsH{0wnPNC9ryfIwsYxZqr(q z%Bk*N-JR{#C;L-gM!n>z^KF@dTn01!X`OcS83#iWUy82iNx3;pl^T($J69+<;*TU{8c)eoBU>6 // needed for mq system calls +#include // needed for printf, perror +#include // needed for exit +#include // needed for strcpy #include // needed for pid_t #include // needed for wait system call #include // needed for fork system call -#include // needed for exit -#include // needed for printf, perror -#include // needed for mq system calls -#include // needed for strcpy /* Struct for the queue message */ -typedef struct message { +typedef struct message +{ int message_id; char string[10]; } message; @@ -22,30 +23,31 @@ int main() { // Create attributes for new queue struct mq_attr queue_attr; - queue_attr.mq_flags = 0; // Ignored by kernel - queue_attr.mq_maxmsg = 10; // Max messages the queue supports + queue_attr.mq_flags = 0; // Ignored by kernel + queue_attr.mq_maxmsg = 10; // Max messages the queue supports queue_attr.mq_msgsize = sizeof(message); - queue_attr.mq_curmsgs = 0; // Not used + queue_attr.mq_curmsgs = 0; // Not used // Create and open a queue mqd_t mqdes = mq_open("/CS3841QUEUE", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &queue_attr); - if(mqdes == -1) { + if (mqdes == -1) + { printf("COULD NOT OPEN QUEUE\n"); exit(EXIT_FAILURE); } pid_t pid = fork(); // fork into 2 processes - if(pid < 0) // error + if (pid < 0) // error { perror("fork"); exit(EXIT_FAILURE); } - else if(pid == 0) // child + else if (pid == 0) // child { // Child receives message from parent message from_parent; - mq_receive(mqdes, (char*)&from_parent, sizeof(message), NULL); + mq_receive(mqdes, (char *)&from_parent, sizeof(message), NULL); printf("Child got %d: %s from parent\n", from_parent.message_id, from_parent.string); // Close the queue @@ -59,7 +61,7 @@ int main() message to_child; to_child.message_id = 10; strcpy(to_child.string, "HELLO"); - mq_send(mqdes, (char*)&to_child, sizeof(message), 0); + mq_send(mqdes, (char *)&to_child, sizeof(message), 0); // Wait for child wait(0); diff --git a/Examples/ipc/ipcpipe1.c b/Examples/ipc/ipcpipe1.c index ae3c814..da581d1 100644 --- a/Examples/ipc/ipcpipe1.c +++ b/Examples/ipc/ipcpipe1.c @@ -3,17 +3,18 @@ * a child process to a parent process */ +#include // needed for printf, perror +#include // needed for exit #include // needed for pid_t #include // needed for wait system call #include // needed for fork, read, write, close system calls -#include // needed for exit -#include // needed for printf, perror int main() { /* Create a pipe */ int pipefd[2]; - if (pipe(pipefd) == -1) { + if (pipe(pipefd) == -1) + { perror("pipe"); exit(EXIT_FAILURE); } @@ -46,7 +47,7 @@ int main() close(pipefd[0]); /* Write to child */ - write(pipefd[1], "HELLO", 5); + write(pipefd[1], "HELLO WORLD!", 12); /* Close pipe */ close(pipefd[1]); diff --git a/Examples/ipc/ipcpipe2.c b/Examples/ipc/ipcpipe2.c index a67c588..98313e1 100644 --- a/Examples/ipc/ipcpipe2.c +++ b/Examples/ipc/ipcpipe2.c @@ -3,22 +3,24 @@ * two pipes to send data in two directions */ +#include // needed for printf, perror +#include // needed for exit #include // needed for pid_t #include // needed for wait system call #include // needed for fork, read, write, close system calls -#include // needed for exit -#include // needed for printf, perror int main() { /* Create pipes */ int pipe_to_child[2]; - if (pipe(pipe_to_child) == -1) { + if (pipe(pipe_to_child) == -1) + { printf("PIPE FAILURE\n"); exit(EXIT_FAILURE); } int pipe_from_child[2]; - if (pipe(pipe_from_child) == -1) { + if (pipe(pipe_from_child) == -1) + { printf("PIPE FAILURE\n"); exit(EXIT_FAILURE); } diff --git a/Examples/ipc/ipcshm1.c b/Examples/ipc/ipcshm1.c index 53b1ddd..19862ee 100644 --- a/Examples/ipc/ipcshm1.c +++ b/Examples/ipc/ipcshm1.c @@ -4,15 +4,15 @@ * NOTE: named shared memory segments are persistent */ -#include // needed for pid_t -#include // needed for wait system call #include // needed for parameter values for shm_open -#include // needed for fork, getpid, getppid, kill system calls -#include // needed for exit #include // needed for signal system call #include // needed for printf, perror -#include // needed for mmap, munmap, shm system calls +#include // needed for exit #include // needed for strcpy +#include // needed for mmap, munmap, shm system calls +#include // needed for pid_t +#include // needed for wait system call +#include // needed for fork, getpid, getppid, kill system calls #define MAPPED_SIZE 128 @@ -20,7 +20,8 @@ int main() { // Create and open a shared memory segment int shmfd = shm_open("/CS3841MEMORY", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); - if(shmfd == -1) { + if (shmfd == -1) + { printf("COULD NOT OPEN SHARED MEMORY SEGMENT\n"); exit(EXIT_FAILURE); } @@ -30,20 +31,21 @@ int main() // Map the segment into the processes address space // NOTE: protection is set to allow reading and writing with a shared mapping - void* mapped_space = mmap(NULL, MAPPED_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); - if(mapped_space == MAP_FAILED) { + void *mapped_space = mmap(NULL, MAPPED_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); + if (mapped_space == MAP_FAILED) + { printf("COULD NOT MMAP\n"); exit(EXIT_FAILURE); } - pid_t pid = fork(); // fork into 2 processes + pid_t pid = fork(); // fork into 2 processes - if(pid < 0) // error + if (pid < 0) // error { perror("fork"); exit(EXIT_FAILURE); } - else if(pid == 0) // child + else if (pid == 0) // child { // Child writes to shared memory segment strcpy(mapped_space, "HELLO"); @@ -62,7 +64,7 @@ int main() wait(0); // Parent reads from shared memory segment - printf("Parent reads %s from shared mapped segment\n", (char*)mapped_space); + printf("Parent reads %s from shared mapped segment\n", (char *)mapped_space); // Unmap the shared memory munmap(mapped_space, MAPPED_SIZE); diff --git a/Examples/ipc/ipcshm2.c b/Examples/ipc/ipcshm2.c index 67ca5c7..1c15466 100644 --- a/Examples/ipc/ipcshm2.c +++ b/Examples/ipc/ipcshm2.c @@ -5,15 +5,15 @@ * NOTE: named shared memory segments are persistent */ -#include // needed for pid_t -#include // needed for wait system call #include // needed for parameter values for shm_open -#include // needed for fork, getpid, getppid, kill system calls -#include // needed for exit #include // needed for signal system call #include // needed for printf, perror -#include // needed for mmap, munmap, shm system calls +#include // needed for exit #include // needed for strcpy +#include // needed for mmap, munmap, shm system calls +#include // needed for pid_t +#include // needed for wait system call +#include // needed for fork, getpid, getppid, kill system calls #define MAPPED_SIZE 128 @@ -21,7 +21,8 @@ int main() { // Create and open a shared memory segment int shmfd = shm_open("/CS3841MEMORY", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); - if(shmfd == -1) { + if (shmfd == -1) + { printf("COULD NOT OPEN SHARED MEMORY SEGMENT\n"); exit(EXIT_FAILURE); } @@ -31,20 +32,21 @@ int main() // Map the segment into the processes address space // NOTE: protection is set to allow reading and writing with a shared mapping - void* mapped_space = mmap(NULL, MAPPED_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); - if(mapped_space == MAP_FAILED) { + void *mapped_space = mmap(NULL, MAPPED_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); + if (mapped_space == MAP_FAILED) + { printf("COULD NOT MMAP\n"); exit(EXIT_FAILURE); } - pid_t pid = fork(); // fork into 2 processes + pid_t pid = fork(); // fork into 2 processes - if(pid < 0) // error + if (pid < 0) // error { perror("fork"); exit(EXIT_FAILURE); } - else if(pid == 0) // child + else if (pid == 0) // child { // Child writes to shared memory segment strcpy(mapped_space, "HELLO"); @@ -63,7 +65,7 @@ int main() wait(0); // Parent reads from shared memory segment - printf("Parent reads %s from shared mapped segment\n", (char*)mapped_space); + printf("Parent reads %s from shared mapped segment\n", (char *)mapped_space); // Unmap the shared memory munmap(mapped_space, MAPPED_SIZE); @@ -76,4 +78,3 @@ int main() } return 0; // Return success } - diff --git a/Examples/ipc/signal1.c b/Examples/ipc/signal1.c index ed57a5c..bd8160c 100644 --- a/Examples/ipc/signal1.c +++ b/Examples/ipc/signal1.c @@ -3,10 +3,10 @@ * for the interrupt (CTRL+C) signal */ -#include // needed for pid_t -#include // needed for getpid, sleep system calls #include // needed for signal system call #include // needed for printf +#include // needed for pid_t +#include // needed for getpid, sleep system calls // Signal handler // Prints the PID of the process and the received signal @@ -22,7 +22,8 @@ int main() signal(SIGINT, signal_handler); // Loop forever and sleep - while(1) { + while (1) + { printf("sleeping...\n"); sleep(1); } diff --git a/Examples/ipc/signal2.c b/Examples/ipc/signal2.c index 709e7fc..3ec8ee1 100644 --- a/Examples/ipc/signal2.c +++ b/Examples/ipc/signal2.c @@ -5,10 +5,10 @@ * fault and essentially loops forever */ -#include // needed for pid_t -#include // needed for getpid, sleep system calls #include // needed for signal system call #include // needed for printf +#include // needed for pid_t +#include // needed for getpid, sleep system calls // Signal handler // Prints the PID of the process and the received signal @@ -24,6 +24,6 @@ int main() signal(SIGSEGV, signal_handler); // Cause a segmentation fault - int* i = NULL; + int *i = NULL; return *i + 10; }