Digression
The experiment equipped centOS
performed on a remote server operating system, here to talk about the pit encountered here.
The written code folder chap10_code
submitted to a remote server. In cmd
the 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 gcc
run 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_code
the directory and then create a bin
directory to store all executable files so. .c
Before the file you want to add ../
represents the parent directory .c
file )
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 -lpthread
command to get a file can be executedffiles2
Run ./ffiles2 ../abcde.txt
command can be obtained:
We can see that the program does not return bash
because the above code file is not closed. We should return 0
add 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 -lpthread
get an executable fileffiles3
run./ffiles3 ../abcde.txt
And then cat ../abcde.txt
view the abcde.txt
contents of the file
Can be seen from the figure, fd3
the content is not written to the file because the fd1
open file is not closed, the lock system on which to write, write prohibit other.