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 0000000..d3ea8c2 Binary files /dev/null and b/Examples/ipc/a.out differ diff --git a/Examples/ipc/ipcfs1.c b/Examples/ipc/ipcfs1.c index bc53321..ef3fad4 100644 --- a/Examples/ipc/ipcfs1.c +++ b/Examples/ipc/ipcfs1.c @@ -43,7 +43,7 @@ int main() wait(0); /* Read data */ - char data[32]; + char data[32] = {0}; read(myfile, data, sizeof(data)); printf("Parent received %s from child\n", data); diff --git a/Examples/ipc/ipcmsg1.c b/Examples/ipc/ipcmsg1.c index b3c918d..98836ff 100644 --- a/Examples/ipc/ipcmsg1.c +++ b/Examples/ipc/ipcmsg1.c @@ -1,19 +1,20 @@ /* - * ipcmsg1.c - Using a message queue to send data from + * ipcmsg1.c - Using a message queue to send data from * a parent process to a child process * NOTE: message queues are persistent */ +#include // 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; }