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\n", FIFO_NAME);
            exit(EXIT_FAILURE);
        }
    }

    printf("Process %d opening FIFO O_WRONLY\n",getpid());
    pipe_fd = open(FIFO_NAME, O_WRONLY);
    printf("the file's descriptor is %d\n",pipe_fd);
    if(pipe_fd != -1){
        res = write(pipe_fd, buffer,sizeof(buffer));
        if(res == -1){
            fprintf(stderr,"Write error on pipe\n");
            exit(EXIT_FAILURE);

        }
        printf("write data is %s,%d bytes is write\n",buffer,res);
        (void )close(pipe_fd);
    } else
        exit(EXIT_FAILURE);

    printf("Process %d finished\n",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,'\0', sizeof(buffer));
    printf("Process %d opening FIFO O_RDONLY\n",getpid());
    pipe_fd = open(FIFO_NAME, O_RDONLY);

    printf("the file's descriptor is %d\n",pipe_fd);
    if(pipe_fd != -1){
        bytes_read = read(pipe_fd, buffer, sizeof(buffer));
        printf("the read data is %s\n",buffer);
        close(pipe_fd);
    } else
        exit(EXIT_FAILURE);

    printf("Process %d finished, %d bytes read\n",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("初始文件打开失败!\n");
        exit(1);
    }
    fgets(buf,128,stdin);
    printf("文件内容如下:%s\n",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]='\0';
    printf("文件逆转换完成!\n");
    if((fp2=fopen(argv[2],"w"))==NULL)
    {
        printf("目标文件打开失败!\n");
        exit(1);
    }
    fputs(buf1,fp1);
    printf("文件内容输入完成!\n");

    //从源文件读取内容
    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("有名管道创建失败!\n");
        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("信息以发送\n");
    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("文件打开失败!\n");
    }    
    char buf[128];
    printf("请输入文件内容:");
    fgets(buf,128,stdin);
    if((write(fd,buf,strlen(buf)))==-1)
    {
        printf("文件内容写入失败!\n");
        exit(1);
    }
    pid=fork();
    if(pid==-1)
    {
        printf("子进程创建失败!\n");
        exit(1);
    }
    else if(pid==0)
    {
        printf("子进程向文件写入内容:");
        fgets(buf,128,stdin);
        if((write(fd,buf,strlen(buf)))==-1)
        {
            printf("子进程写入文件内容失败!\n");
            exit(1);
        }
    }
    else
    {
        sleep(5);//让子进程先执行
        printf("父进程向文件写入内容:");
        fgets(buf,128,stdin);
        if((write(fd,buf,strlen(buf)))==-1)
        {
            printf("父进程写入失败!\n");
            exit(1);
        }
        wait(&status);//回收子进程
        return 0;
    }
}

 

posted @ 2020-12-31 15:56  君焰w  阅读(62)  评论(0编辑  收藏  举报