IO those things

Digression

The experiment equipped centOSperformed on a remote server operating system, here to talk about the pit encountered here.

The written code folder chap10_codesubmitted to a remote server. In cmdthe inputscp -r 本地文件目录绝对路径 服务器用户名@服务器host:目标路径

Now let's look at it to the server

You can see the file on the server has been reached. Then you can gccrun the commands.

Of course, since the code already on the server, we can run the code to operate mobile phones, mobile end recommend artifactTermux

System level I / O

输入/输出(I/O)It is the process of copying data between main memory and an external device (e.g. a disk drive, and a network terminal).

run

ffiles1.c

#include "csapp.h"

int main(int argc, char *argv[])
{
    int fd1, fd2, fd3;
    char c1, c2, c3;
    char *fname = argv[1];
    fd1 = Open(fname, O_RDONLY, 0);
    fd2 = Open(fname, O_RDONLY, 0);
    fd3 = Open(fname, O_RDONLY, 0);
    dup2(fd2, fd3);

    Read(fd1, &c1, 1);
    Read(fd2, &c2, 1);
    Read(fd3, &c3, 1);
    printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3);

    Close(fd1);
    Close(fd2);
    Close(fd3);
    return 0;
}

abcde.txt

abcde

gcc -o ffiles1 ../ffiles1.c ../csapp.h ../csapp.c -lpthread( Note: I am in chap10_codethe directory and then create a bindirectory to store all executable files so. .cBefore the file you want to add ../represents the parent directory .cfile )

operation result:

ffiles2.c

#include "csapp.h"

int main(int argc, char *argv[])
{
    int fd1;
    int s = getpid() & 0x1;
    char c1, c2;
    char *fname = argv[1];
    fd1 = Open(fname, O_RDONLY, 0);
    Read(fd1, &c1, 1);
    if (fork()) {
    /* Parent */
    sleep(s);
    Read(fd1, &c2, 1);
    printf("Parent: c1 = %c, c2 = %c\n", c1, c2);
    } else {
    /* Child */
    sleep(1-s);
    Read(fd1, &c2, 1);
    printf("Child: c1 = %c, c2 = %c\n", c1, c2);
    }
    return 0;
}

Run gcc -o ffiles2 ffiles2.c csapp.h csapp.c -lpthreadcommand to get a file can be executedffiles2

Run ./ffiles2 ../abcde.txtcommand can be obtained:

We can see that the program does not return bashbecause the above code file is not closed. We should return 0add the wordsClose(fd1);

Just read read files, write files now let's look at it!

ffiles3.c

#include "csapp.h"

int main(int argc, char *argv[])
{
    int fd1, fd2, fd3;
    char *fname = argv[1];
    fd1 = Open(fname, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR);
    Write(fd1, "pqrs", 4);  

    fd3 = Open(fname, O_APPEND|O_WRONLY, 0);
    Write(fd3, "jklmn", 5);
    fd2 = dup(fd1);  /* Allocates new descriptor */
    Write(fd2, "wxyz", 4);
    Write(fd3, "ef", 2);

    Close(fd1);
    Close(fd2);
    Close(fd3);
    return 0;
}

Run gcc -o ffiles3 ffiles3.c csapp.h csapp.c -lpthreadget an executable fileffiles3

run./ffiles3 ../abcde.txt

And then cat ../abcde.txtview the abcde.txtcontents of the file

Can be seen from the figure, fd3the content is not written to the file because the fd1open file is not closed, the lock system on which to write, write prohibit other.

Guess you like

Origin www.cnblogs.com/ourEmpire/p/11870916.html