Created
April 4, 2021 01:04
-
-
Save ChadRehmKineticData/3cd5a067753d00f394ad8b0b7e756d6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <errno.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/wait.h> | |
#include <signal.h> | |
#define BUFFER_SIZE 64 | |
void signalHandler(int signum); | |
void handleError(char* message, int checkNum); | |
int handlerCode = 0; | |
int main(int argc, char** argv) | |
{ | |
pid_t pid; | |
char buffer[BUFFER_SIZE]; | |
char message[] = "Hello World!"; | |
int fd[2], | |
pipePt, | |
elapsedSleep, | |
closeVal; | |
// Create pipe and error if failed. | |
pipePt = pipe(fd); | |
handleError("Could not create pipe", pipePt); | |
// The fork() system call creates a new process by making a copy of the existing one. | |
pid = fork(); | |
// Error if fork failed create. | |
handleError("Could not create process", pid); | |
if (pid == 0) // Code executed by child process | |
{ | |
// Register signal handler | |
if(signal(SIGUSR1, signalHandler) == SIG_ERR) { | |
printf("signal interupted"); | |
} | |
while(1) { | |
if (handlerCode == 100) { | |
printf("Received Signal from Parent!\n"); | |
// Child will read from pipe, so close the write end | |
closeVal = close(fd[1]); | |
handleError("Child errored trying to close write end of the pipe", closeVal); | |
// Read message from pipe into buffer | |
int readBytes = read(fd[0], buffer, BUFFER_SIZE); | |
// Error if read failed | |
handleError("Child unable to read from buffer", readBytes); | |
printf("Received message \"%s\" that was %d bytes.\n", buffer, readBytes); | |
closeVal = close(fd[0]); | |
handleError("Child errored trying to close read end of the pipe", closeVal); | |
kill(getpid(), SIGINT); | |
} | |
sleep(1); | |
} | |
} else { // Code executed by parent process | |
// Put parent process to sleep for 3 seconds. | |
elapsedSleep = sleep(3); | |
if(elapsedSleep > 0) { | |
printf("Warning: Parent sleep process interupted"); | |
} | |
// Parent will write to the pipe, so close the read end | |
closeVal = close(fd[0]); | |
handleError("Parent errored trying to close read end of the pipe", closeVal); | |
int writeBytes = strlen(message)+1; | |
// Write message to the pipe. Write one character longer | |
// than the length of the string to get the null terminator | |
write(fd[1], message, writeBytes); | |
printf("Wrote message \"%s\" that was %d bytes.\n", message, writeBytes); | |
closeVal = close(fd[1]); | |
handleError("Parent errored trying to close write end of the pipe", closeVal); | |
// Send signal to child | |
kill(pid, SIGUSR1); | |
printf("Sent signal to child process %d\n", pid); | |
wait(0); | |
} | |
return EXIT_SUCCESS; | |
} | |
void signalHandler(int signum) { | |
if (signum == SIGUSR1) { | |
handlerCode = 100; | |
} | |
} | |
void handleError(char* message, int checkNum) { | |
if (checkNum == -1) { | |
printf("Error: %s: %s\n", message, strerror( errno )); | |
exit(1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output:
Wrote message "Hello World!" that was 13 bytes.
Sent signal to child process 4207
Received Signal from Parent!
Received message "Hello World!" that was 13 bytes.