多进程基础练习

 

 

C语言read()函数:用于读取打开文件的内容

函数名:read

头文件:<io.h>

函数原型: int read(int handle,void *buf,int len);

功能:用于读取打开文件的内容

参数:int handle 为要读取的文件

void *buf 为要将读取的内容保存的缓冲区

int len 读取文件的长度

返回值:返回实际读取的字节数

C语⾔open()函数:打开⽂件函数


头⽂件:#include <sys/types.h>    #include <sys/stat.h>    #include <fcntl.h>
定义函数:
    int open(const char * pathname, int flags);
    int open(const char * pathname, int flags, mode_t mode

函数说明:
参数 pathname 指向欲打开的⽂件路径字符串. 下列是参数flags 所能使⽤的旗标:
O_RDONLY 以只读⽅式打开⽂件
O_WRONLY 以只写⽅式打开⽂件
O_RDWR 以可读写⽅式打开⽂件.
等等。

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

int main(int argc, char* argv[]){
    int n = atoi(argv[2]) - 1;
    pid_t pid;
    int fd1_td1 = 0;
    int fd2_td1 = 0;
    int fd1_td2 = 0;
    int len = 0;
    char buf[5] = {"\0"};
    printf("[pid](%d) [ppid](%d): start\n", getpid(),getppid());
    //fd1_td1 = fileno(fp1);
    fd1_td1 = open("td1.txt", O_RDONLY);
    int i = 0;
    for(i = 0; i < n; i++){
        pid = fork();
           if (pid==-1)
        {
            perror("fork error!");
            exit(1);
        }

        //循环中,fork函数调用n次,子进程返回0,父进程返回子进程的pid,
        //为了避免子进程也fork,需要判断并break
        if (pid==0)
        {
            break;
        }
    }
    if(pid>0)  //父进程
    {
        printf("[pid](%d) [ppid](%d): [fd1_td1](%d) is got\n", getpid(),getppid(),fd1_td1);
        len = read(fd1_td1, buf, 5);
        printf("[pid](%d) [ppid](%d): [read](%s) by [fd1_td1](%d)\n", getpid(), getppid(), buf, fd1_td1);
        close(fd1_td1);
        //fd1_td2 = fileno(fp2);
        fd1_td2 = open("td2.txt", O_RDONLY);
        printf("[pid](%d) [ppid](%d): [fd1_td2](%d) is got\n", getpid(),getppid(),fd1_td2);
        len = read(fd1_td2, buf, 5);
        printf("[pid](%d) [ppid](%d): [read](%s) by [fd1_td2](%d)\n", getpid(), getppid(), buf, fd1_td2);
        close(fd1_td2);
        sleep(2); //这里延迟父进程程序,等子进程先执行完。
    }
    else if(pid==0)  //子进程
    { 
        printf("[pid](%d) [ppid](%d): start\n",getpid(),getppid());
        len = read(fd1_td1, buf, 5);
        printf("[pid](%d) [ppid](%d): [read](%s) by [fd1_td1](%d)\n", getpid(), getppid(), buf, fd1_td1);
        //fd2_td1 = fileno(fp1);
        fd2_td1 = open("td1.txt", O_RDONLY);        
        printf("[pid](%d) [ppid](%d): [fd2_td1](%d) is got\n", getpid(),getppid(),fd2_td1);
        len = read(fd2_td1, buf, 5);
        printf("[pid](%d) [ppid](%d): [read](%s) by [fd2_td1](%d)\n", getpid(), getppid(), buf, fd2_td1);
        close(fd2_td1);
        close(fd1_td1);
        printf("[pid](%d) [ppid](%d): exit\n",getpid(),getppid());
        exit(0);
    }

    return 0;
}

父进程创建的会按序读下去,子进程创建的永远是读前五个

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

理工男头发不值钱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值