Untitled

 avatar
unknown
plain_text
3 years ago
773 B
5
Indexable
#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

struct packet
{
  char msg[0xFF];
};

void child (int fd)
{
  struct packet data;
  strcpy(data.msg, "Hello World!");
  write(fd, &data, sizeof(data));
  exit(0);
}

void parent (int fd)
{
  wait(NULL);
  struct packet data;
  read(fd, &data, sizeof(data));
  printf("REceived: %s\n", data.msg);
}

int main (void)
{
  int fifo = mkfifo("./fifo", 0777);
  int fd = open("./fifo", O_RDWR);
  
  if (fd < 0)
    perror("open");
  
  if (fifo < 0)
    perror("mkfifo");
  
  int id = fork();
  
  if (id == 0)
    child(fd);
  else
    parent(fd);
    
  if (unlink("./fifo") < 0)
    perror("unlink");
    
}
Editor is loading...