You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

44 lines
825 B

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <errno.h>
  8. int main(int argc, char **argv) {
  9. if (argc != 2) {
  10. printf("Usage: %s [subs]", argv[0]);
  11. exit(1);
  12. }
  13. printf("PUBLISHER\n");
  14. int subs = atoi(argv[1]);
  15. if (mkfifo("/tmp/ex1", 0777) == -1) {
  16. if (errno != EEXIST) {
  17. perror("mkfifo");
  18. exit(1);
  19. }
  20. }
  21. int fd = open("/tmp/ex1", O_WRONLY);
  22. if (fd == -1) {
  23. perror("open");
  24. exit(1);
  25. }
  26. while(1) {
  27. char mes[1024];
  28. fgets(mes, 1024, stdin);
  29. for (int i = 0; i < subs; ++i)
  30. write(fd, mes, 1024);
  31. sleep(1);
  32. }
  33. }
  34. // mkfifo reference
  35. // https://man7.org/linux/man-pages/man3/mkfifo.3.html