9-8 B线程杀死A线程

发布于:2022-12-24 ⋅ 阅读:(389) ⋅ 点赞:(0)

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <strings.h>
#include <unistd.h>
#include <signal.h>

typedef void (*sighandler_t)(int);
int main()
{
  if (mkfifo("./mkfifokill", 0664) < 0)
  {
    if (17 != errno)
    {
      perror("mkfifo");
      return -1;
    }
  }
  printf("创建管道文件成功\n");
  int fd_r = open("./mkfifokill", O_RDONLY);
  if (fd_r < 0)
  {
    perror("open");
  }
  printf("打开文件大小成功\n");

  int b;
  ssize_t size = 0;
  size = read(fd_r, &b, sizeof(b));
  if (size < 0)
  {
    perror("read");
    return -1;
  }
  if (size == 0)
  {
    printf("写端已经关闭\n");
  }
  printf("%d\n", b);
  if (9 == b)
  {
    kill(getpid(), 9);
  }
  else if (19 == b)
  {
     kill(getpid(), 19);
  }

  while (1)
    ;
  close(fd_r);
  return 0;
}
//************************************************************************************//

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <strings.h>

int main()
{
  if (mkfifo("./mkfifokill", 0664) < 0)
  {
    if (17 != errno)
    {
      perror("mkfifo");
      return -1;
    }
  }
  printf("创建管道文件成功\n");
  int fd_w = open("./mkfifokill", O_WRONLY);
  if (fd_w < 0)
  {
    perror("open");
  }
  printf("打开文件大小成功\n");

  int a = 9;
  ssize_t size = 0;
  size = write(fd_w, &a, sizeof(a));
  if (size < 0)
  {
    perror("write");
    return -1;
  }
  close(fd_w);
  return 0;
}
 


网站公告

今日签到

点亮在社区的每一天
去签到