This commit is contained in:
p-w-rs
2022-09-15 14:38:32 -05:00
parent 3d122bd7fb
commit 59e3d32e38
10 changed files with 64 additions and 54 deletions

1
Examples/ipc/MYFILE Normal file
View File

@@ -0,0 +1 @@
HELLO

BIN
Examples/ipc/a.out Executable file

Binary file not shown.

View File

@@ -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);

View File

@@ -4,16 +4,17 @@
* NOTE: message queues are persistent
*/
#include <mqueue.h> // needed for mq system calls
#include <stdio.h> // needed for printf, perror
#include <stdlib.h> // needed for exit
#include <string.h> // needed for strcpy
#include <sys/types.h> // needed for pid_t
#include <sys/wait.h> // needed for wait system call
#include <unistd.h> // needed for fork system call
#include <stdlib.h> // needed for exit
#include <stdio.h> // needed for printf, perror
#include <mqueue.h> // needed for mq system calls
#include <string.h> // needed for strcpy
/* Struct for the queue message */
typedef struct message {
typedef struct message
{
int message_id;
char string[10];
} message;
@@ -29,7 +30,8 @@ int main()
// 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);
}

View File

@@ -3,17 +3,18 @@
* a child process to a parent process
*/
#include <stdio.h> // needed for printf, perror
#include <stdlib.h> // needed for exit
#include <sys/types.h> // needed for pid_t
#include <sys/wait.h> // needed for wait system call
#include <unistd.h> // needed for fork, read, write, close system calls
#include <stdlib.h> // needed for exit
#include <stdio.h> // 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]);

View File

@@ -3,22 +3,24 @@
* two pipes to send data in two directions
*/
#include <stdio.h> // needed for printf, perror
#include <stdlib.h> // needed for exit
#include <sys/types.h> // needed for pid_t
#include <sys/wait.h> // needed for wait system call
#include <unistd.h> // needed for fork, read, write, close system calls
#include <stdlib.h> // needed for exit
#include <stdio.h> // 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);
}

View File

@@ -4,15 +4,15 @@
* NOTE: named shared memory segments are persistent
*/
#include <sys/types.h> // needed for pid_t
#include <sys/wait.h> // needed for wait system call
#include <fcntl.h> // needed for parameter values for shm_open
#include <unistd.h> // needed for fork, getpid, getppid, kill system calls
#include <stdlib.h> // needed for exit
#include <signal.h> // needed for signal system call
#include <stdio.h> // needed for printf, perror
#include <sys/mman.h> // needed for mmap, munmap, shm system calls
#include <stdlib.h> // needed for exit
#include <string.h> // needed for strcpy
#include <sys/mman.h> // needed for mmap, munmap, shm system calls
#include <sys/types.h> // needed for pid_t
#include <sys/wait.h> // needed for wait system call
#include <unistd.h> // 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);
}
@@ -31,7 +32,8 @@ 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) {
if (mapped_space == MAP_FAILED)
{
printf("COULD NOT MMAP\n");
exit(EXIT_FAILURE);
}

View File

@@ -5,15 +5,15 @@
* NOTE: named shared memory segments are persistent
*/
#include <sys/types.h> // needed for pid_t
#include <sys/wait.h> // needed for wait system call
#include <fcntl.h> // needed for parameter values for shm_open
#include <unistd.h> // needed for fork, getpid, getppid, kill system calls
#include <stdlib.h> // needed for exit
#include <signal.h> // needed for signal system call
#include <stdio.h> // needed for printf, perror
#include <sys/mman.h> // needed for mmap, munmap, shm system calls
#include <stdlib.h> // needed for exit
#include <string.h> // needed for strcpy
#include <sys/mman.h> // needed for mmap, munmap, shm system calls
#include <sys/types.h> // needed for pid_t
#include <sys/wait.h> // needed for wait system call
#include <unistd.h> // 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);
}
@@ -32,7 +33,8 @@ 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) {
if (mapped_space == MAP_FAILED)
{
printf("COULD NOT MMAP\n");
exit(EXIT_FAILURE);
}
@@ -76,4 +78,3 @@ int main()
}
return 0; // Return success
}

View File

@@ -3,10 +3,10 @@
* for the interrupt (CTRL+C) signal
*/
#include <sys/types.h> // needed for pid_t
#include <unistd.h> // needed for getpid, sleep system calls
#include <signal.h> // needed for signal system call
#include <stdio.h> // needed for printf
#include <sys/types.h> // needed for pid_t
#include <unistd.h> // 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);
}

View File

@@ -5,10 +5,10 @@
* fault and essentially loops forever
*/
#include <sys/types.h> // needed for pid_t
#include <unistd.h> // needed for getpid, sleep system calls
#include <signal.h> // needed for signal system call
#include <stdio.h> // needed for printf
#include <sys/types.h> // needed for pid_t
#include <unistd.h> // needed for getpid, sleep system calls
// Signal handler
// Prints the PID of the process and the received signal