• linux


    P_230_write

    //
    // Created by wybiacx on 2020/12/31.
    //
    
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #define FIFO_NAME "/tmp/my_info"
    
    int main(int argc,char *argv[]){
        int pipe_fd;
        int res;
        char buffer[] = "hello world!";
        if(access(FIFO_NAME, F_OK) == -1){
            res = mkfifo(FIFO_NAME, 0766);
            if(res != 0){
                fprintf(stderr, "Could not create fifo %s
    ", FIFO_NAME);
                exit(EXIT_FAILURE);
            }
        }
    
        printf("Process %d opening FIFO O_WRONLY
    ",getpid());
        pipe_fd = open(FIFO_NAME, O_WRONLY);
        printf("the file's descriptor is %d
    ",pipe_fd);
        if(pipe_fd != -1){
            res = write(pipe_fd, buffer,sizeof(buffer));
            if(res == -1){
                fprintf(stderr,"Write error on pipe
    ");
                exit(EXIT_FAILURE);
    
            }
            printf("write data is %s,%d bytes is write
    ",buffer,res);
            (void )close(pipe_fd);
        } else
            exit(EXIT_FAILURE);
    
        printf("Process %d finished
    ",getpid());
        exit(EXIT_SUCCESS);
    }

    P_231_read

    //
    // Created by wybiacx on 2020/12/31.
    //
    
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <limits.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #define FIFO_NAME "/tmp/my_info"
    
    int main(int argc,char *argv[]){
        int pipe_fd;
        int res;
        char buffer[4096];
        int bytes_read = 0;
        memset(buffer,'', sizeof(buffer));
        printf("Process %d opening FIFO O_RDONLY
    ",getpid());
        pipe_fd = open(FIFO_NAME, O_RDONLY);
    
        printf("the file's descriptor is %d
    ",pipe_fd);
        if(pipe_fd != -1){
            bytes_read = read(pipe_fd, buffer, sizeof(buffer));
            printf("the read data is %s
    ",buffer);
            close(pipe_fd);
        } else
            exit(EXIT_FAILURE);
    
        printf("Process %d finished, %d bytes read
    ",getpid(),bytes_read);
        exit(EXIT_SUCCESS);
    }

    不知名1

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main(int argc,char **argv)
    {
    //综合练习题最后一个
    //逆转字符串写入文件,将该文件复制
        char buf[128];
        FILE *fp1,*fp2;
        char copy[128];
        printf("请输入文件内容:");
        if((fp1=fopen(argv[1],"w+"))==NULL)
        {
            printf("初始文件打开失败!
    ");
            exit(1);
        }
        fgets(buf,128,stdin);
        printf("文件内容如下:%s
    ",buf);
        int len=strlen(buf);
        char buf1[len+1];
        int i;
        for(i=0;i<len;i++)
        {
            buf1[len-i]=buf[i];
        }
        buf1[len+1]='';
        printf("文件逆转换完成!
    ");
        if((fp2=fopen(argv[2],"w"))==NULL)
        {
            printf("目标文件打开失败!
    ");
            exit(1);
        }
        fputs(buf1,fp1);
        printf("文件内容输入完成!
    ");
    
        //从源文件读取内容
        fclose(fp1);//关闭原来的流
        if((fp1=fopen(argv[1],"r")))//以只读的方式打开源文件
        
        while((fgets(copy,sizeof(copy),fp1)))
        {
            fputs(copy,fp2);
        }
        printf("文件复制完成");
        
    
    }

    不知名2

    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<fcntl.h>
    int main(int argc,char **argv)
    {
    //进程通信服务端
        int fd;
        int re;
        
        re=mkfifo(argv[1],0750);
        if(re!=0)
        {
            printf("有名管道创建失败!
    ");
            exit(1);
        }
        char buf[128];
        printf("请输入要发送的消息:");
        fgets(buf,sizeof(buf),stdin);
        if((fd=open(argv[1],O_WRONLY))==-1)
        {
            printf("文件打开失败!");
            exit(1);
        }
        write(fd,buf,sizeof(buf));
        printf("信息以发送
    ");
        unlink(argv[1]);
        
    
    }

    不知名3

    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<fcntl.h>
    #include<unistd.h>
    int main(int argc,char **argv)
    {
    //进程通信客户端
        int fd;
        fd=open(argv[1],O_RDONLY);
        char buf[128];
        if(fd==-1)
        {
            printf("文件打开失败");
            exit(1);
        }
        read(fd,buf,sizeof(buf));
        printf("收到的消息是:%s",buf);
        
        
    }

    不知名4

    #include<stdio.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<string.h>
    int main(int argc,char **argv)
    {
    //父子进程
        int status;
        int fd;//文件描述符
        pid_t pid;
        if((fd=open(argv[1],O_RDWR|O_CREAT,0644))==-1)
        {
            printf("文件打开失败!
    ");
        }    
        char buf[128];
        printf("请输入文件内容:");
        fgets(buf,128,stdin);
        if((write(fd,buf,strlen(buf)))==-1)
        {
            printf("文件内容写入失败!
    ");
            exit(1);
        }
        pid=fork();
        if(pid==-1)
        {
            printf("子进程创建失败!
    ");
            exit(1);
        }
        else if(pid==0)
        {
            printf("子进程向文件写入内容:");
            fgets(buf,128,stdin);
            if((write(fd,buf,strlen(buf)))==-1)
            {
                printf("子进程写入文件内容失败!
    ");
                exit(1);
            }
        }
        else
        {
            sleep(5);//让子进程先执行
            printf("父进程向文件写入内容:");
            fgets(buf,128,stdin);
            if((write(fd,buf,strlen(buf)))==-1)
            {
                printf("父进程写入失败!
    ");
                exit(1);
            }
            wait(&status);//回收子进程
            return 0;
        }
    }
  • 相关阅读:
    kafka官方的kafka-server-start.sh不能关闭kafka进程解决办法
    Linux如何切换图形界面和命令行界面
    2019-9-28:渗透测试,基础学习,DNS投毒
    2019-9-28:渗透测试,基础学习,pgp常量,逻辑运算,DNS投毒,笔记
    2019-9-10:渗透测试,基础学习,nmap扫描命令,php基本语法学习,笔记
    2019-9-11:渗透测试,Kill远控软件,初接触
    2019-9-27:渗透测试,metasploit-framework初接触
    2019-9-26:渗透测试,基础学习,js正则以及什么是目录扫描,笔记
    2019-9-26:渗透测试,基础学习,nmap扫描kali虚拟机服务
    2019-9-25:渗透测试,基础学习,Hydra BP爆破,js基本知识,banner信息收集笔记
  • 原文地址:https://www.cnblogs.com/bingdada/p/14217266.html
Copyright © 2020-2023  润新知