stuff
This commit is contained in:
1
Examples/ipc/MYFILE
Normal file
1
Examples/ipc/MYFILE
Normal file
@@ -0,0 +1 @@
|
|||||||
|
HELLO
|
||||||
BIN
Examples/ipc/a.out
Executable file
BIN
Examples/ipc/a.out
Executable file
Binary file not shown.
@@ -43,7 +43,7 @@ int main()
|
|||||||
wait(0);
|
wait(0);
|
||||||
|
|
||||||
/* Read data */
|
/* Read data */
|
||||||
char data[32];
|
char data[32] = {0};
|
||||||
read(myfile, data, sizeof(data));
|
read(myfile, data, sizeof(data));
|
||||||
printf("Parent received %s from child\n", data);
|
printf("Parent received %s from child\n", data);
|
||||||
|
|
||||||
|
|||||||
@@ -4,16 +4,17 @@
|
|||||||
* NOTE: message queues are persistent
|
* 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/types.h> // needed for pid_t
|
||||||
#include <sys/wait.h> // needed for wait system call
|
#include <sys/wait.h> // needed for wait system call
|
||||||
#include <unistd.h> // needed for fork 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 */
|
/* Struct for the queue message */
|
||||||
typedef struct message {
|
typedef struct message
|
||||||
|
{
|
||||||
int message_id;
|
int message_id;
|
||||||
char string[10];
|
char string[10];
|
||||||
} message;
|
} message;
|
||||||
@@ -22,30 +23,31 @@ int main()
|
|||||||
{
|
{
|
||||||
// Create attributes for new queue
|
// Create attributes for new queue
|
||||||
struct mq_attr queue_attr;
|
struct mq_attr queue_attr;
|
||||||
queue_attr.mq_flags = 0; // Ignored by kernel
|
queue_attr.mq_flags = 0; // Ignored by kernel
|
||||||
queue_attr.mq_maxmsg = 10; // Max messages the queue supports
|
queue_attr.mq_maxmsg = 10; // Max messages the queue supports
|
||||||
queue_attr.mq_msgsize = sizeof(message);
|
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
|
// Create and open a queue
|
||||||
mqd_t mqdes = mq_open("/CS3841QUEUE", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &queue_attr);
|
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");
|
printf("COULD NOT OPEN QUEUE\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
perror("fork");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
else if(pid == 0) // child
|
else if (pid == 0) // child
|
||||||
{
|
{
|
||||||
// Child receives message from parent
|
// Child receives message from parent
|
||||||
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);
|
printf("Child got %d: %s from parent\n", from_parent.message_id, from_parent.string);
|
||||||
|
|
||||||
// Close the queue
|
// Close the queue
|
||||||
@@ -59,7 +61,7 @@ int main()
|
|||||||
message to_child;
|
message to_child;
|
||||||
to_child.message_id = 10;
|
to_child.message_id = 10;
|
||||||
strcpy(to_child.string, "HELLO");
|
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 for child
|
||||||
wait(0);
|
wait(0);
|
||||||
|
|||||||
@@ -3,17 +3,18 @@
|
|||||||
* a child process to a parent process
|
* 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/types.h> // needed for pid_t
|
||||||
#include <sys/wait.h> // needed for wait system call
|
#include <sys/wait.h> // needed for wait system call
|
||||||
#include <unistd.h> // needed for fork, read, write, close system calls
|
#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()
|
int main()
|
||||||
{
|
{
|
||||||
/* Create a pipe */
|
/* Create a pipe */
|
||||||
int pipefd[2];
|
int pipefd[2];
|
||||||
if (pipe(pipefd) == -1) {
|
if (pipe(pipefd) == -1)
|
||||||
|
{
|
||||||
perror("pipe");
|
perror("pipe");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -46,7 +47,7 @@ int main()
|
|||||||
close(pipefd[0]);
|
close(pipefd[0]);
|
||||||
|
|
||||||
/* Write to child */
|
/* Write to child */
|
||||||
write(pipefd[1], "HELLO", 5);
|
write(pipefd[1], "HELLO WORLD!", 12);
|
||||||
|
|
||||||
/* Close pipe */
|
/* Close pipe */
|
||||||
close(pipefd[1]);
|
close(pipefd[1]);
|
||||||
|
|||||||
@@ -3,22 +3,24 @@
|
|||||||
* two pipes to send data in two directions
|
* 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/types.h> // needed for pid_t
|
||||||
#include <sys/wait.h> // needed for wait system call
|
#include <sys/wait.h> // needed for wait system call
|
||||||
#include <unistd.h> // needed for fork, read, write, close system calls
|
#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()
|
int main()
|
||||||
{
|
{
|
||||||
/* Create pipes */
|
/* Create pipes */
|
||||||
int pipe_to_child[2];
|
int pipe_to_child[2];
|
||||||
if (pipe(pipe_to_child) == -1) {
|
if (pipe(pipe_to_child) == -1)
|
||||||
|
{
|
||||||
printf("PIPE FAILURE\n");
|
printf("PIPE FAILURE\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
int pipe_from_child[2];
|
int pipe_from_child[2];
|
||||||
if (pipe(pipe_from_child) == -1) {
|
if (pipe(pipe_from_child) == -1)
|
||||||
|
{
|
||||||
printf("PIPE FAILURE\n");
|
printf("PIPE FAILURE\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,15 @@
|
|||||||
* NOTE: named shared memory segments are persistent
|
* 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 <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 <signal.h> // needed for signal system call
|
||||||
#include <stdio.h> // needed for printf, perror
|
#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 <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
|
#define MAPPED_SIZE 128
|
||||||
|
|
||||||
@@ -20,7 +20,8 @@ int main()
|
|||||||
{
|
{
|
||||||
// Create and open a shared memory segment
|
// Create and open a shared memory segment
|
||||||
int shmfd = shm_open("/CS3841MEMORY", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
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");
|
printf("COULD NOT OPEN SHARED MEMORY SEGMENT\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -30,20 +31,21 @@ int main()
|
|||||||
|
|
||||||
// Map the segment into the processes address space
|
// Map the segment into the processes address space
|
||||||
// NOTE: protection is set to allow reading and writing with a shared mapping
|
// 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);
|
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");
|
printf("COULD NOT MMAP\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
perror("fork");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
else if(pid == 0) // child
|
else if (pid == 0) // child
|
||||||
{
|
{
|
||||||
// Child writes to shared memory segment
|
// Child writes to shared memory segment
|
||||||
strcpy(mapped_space, "HELLO");
|
strcpy(mapped_space, "HELLO");
|
||||||
@@ -62,7 +64,7 @@ int main()
|
|||||||
wait(0);
|
wait(0);
|
||||||
|
|
||||||
// Parent reads from shared memory segment
|
// 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
|
// Unmap the shared memory
|
||||||
munmap(mapped_space, MAPPED_SIZE);
|
munmap(mapped_space, MAPPED_SIZE);
|
||||||
|
|||||||
@@ -5,15 +5,15 @@
|
|||||||
* NOTE: named shared memory segments are persistent
|
* 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 <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 <signal.h> // needed for signal system call
|
||||||
#include <stdio.h> // needed for printf, perror
|
#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 <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
|
#define MAPPED_SIZE 128
|
||||||
|
|
||||||
@@ -21,7 +21,8 @@ int main()
|
|||||||
{
|
{
|
||||||
// Create and open a shared memory segment
|
// Create and open a shared memory segment
|
||||||
int shmfd = shm_open("/CS3841MEMORY", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
|
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");
|
printf("COULD NOT OPEN SHARED MEMORY SEGMENT\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -31,20 +32,21 @@ int main()
|
|||||||
|
|
||||||
// Map the segment into the processes address space
|
// Map the segment into the processes address space
|
||||||
// NOTE: protection is set to allow reading and writing with a shared mapping
|
// 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);
|
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");
|
printf("COULD NOT MMAP\n");
|
||||||
exit(EXIT_FAILURE);
|
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");
|
perror("fork");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
else if(pid == 0) // child
|
else if (pid == 0) // child
|
||||||
{
|
{
|
||||||
// Child writes to shared memory segment
|
// Child writes to shared memory segment
|
||||||
strcpy(mapped_space, "HELLO");
|
strcpy(mapped_space, "HELLO");
|
||||||
@@ -63,7 +65,7 @@ int main()
|
|||||||
wait(0);
|
wait(0);
|
||||||
|
|
||||||
// Parent reads from shared memory segment
|
// 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
|
// Unmap the shared memory
|
||||||
munmap(mapped_space, MAPPED_SIZE);
|
munmap(mapped_space, MAPPED_SIZE);
|
||||||
@@ -76,4 +78,3 @@ int main()
|
|||||||
}
|
}
|
||||||
return 0; // Return success
|
return 0; // Return success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,10 @@
|
|||||||
* for the interrupt (CTRL+C) signal
|
* 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 <signal.h> // needed for signal system call
|
||||||
#include <stdio.h> // needed for printf
|
#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
|
// Signal handler
|
||||||
// Prints the PID of the process and the received signal
|
// Prints the PID of the process and the received signal
|
||||||
@@ -22,7 +22,8 @@ int main()
|
|||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
|
|
||||||
// Loop forever and sleep
|
// Loop forever and sleep
|
||||||
while(1) {
|
while (1)
|
||||||
|
{
|
||||||
printf("sleeping...\n");
|
printf("sleeping...\n");
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,10 +5,10 @@
|
|||||||
* fault and essentially loops forever
|
* 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 <signal.h> // needed for signal system call
|
||||||
#include <stdio.h> // needed for printf
|
#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
|
// Signal handler
|
||||||
// Prints the PID of the process and the received signal
|
// Prints the PID of the process and the received signal
|
||||||
@@ -24,6 +24,6 @@ int main()
|
|||||||
signal(SIGSEGV, signal_handler);
|
signal(SIGSEGV, signal_handler);
|
||||||
|
|
||||||
// Cause a segmentation fault
|
// Cause a segmentation fault
|
||||||
int* i = NULL;
|
int *i = NULL;
|
||||||
return *i + 10;
|
return *i + 10;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user