⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writepipe2.c

📁 这是linux下的基础C编程代码。都是已经调试成功的
💻 C
字号:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include <time.h>
#include <string.h>

#define start 1
#define stop 2

int main(void)
{

   int fd;
   int len;
   char buf[PIPE_BUF];
   char action[20];
   int request;

   mode_t mode=0666;

   if(access("pipe2", 0)!=0)
   {
      if((mkfifo("pipe2",mode))<0)
	  {
	     printf("mkfifo error");
	     exit(1);
	  }
   }


   printf("fifo running....\n");

   fd=open("pipe2",O_WRONLY);

   if(fd<0)
   {
       printf("open error");
	   exit(1);
   }
 
 while(1)
 {
	   scanf("%d",&request);

	   switch(request)
	   {
	   case start:        strcpy(action,"start"); break;
	   case stop:         strcpy(action,"stop"); break;
	   }

	   len=strlen(action);
       strcpy(buf,action);
	  
	   printf("\nlen=%d\n,buf=%s\n",len,buf);
	   
	   if((write(fd,buf,len+1))<0)
	   {
		   perror("write error");
		   close(fd);
		   exit(1);
	   }
 }

   close(fd);
   exit(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -