Do you really have a thorough understanding of computer systems? Chapter two------the interaction between programs and programs

Source: Internet
Author: User

In the previous chapter, we mainly learned how an independent program works on the system, but when we actually write the program, we find that the program sometimes interacts with the IO device or other programs. This chapter focuses on the knowledge of how the program interacts with IO devices.

The interaction between the program and the IO device is often seen, such as with disks, keyboards, and networks. The UNIX system is designed with all the files in mind, so all IO devices are modeled as files. This idea makes it possible to ignore the underlying mechanism of a file while reading and writing files, and to read all the different types of files with the read and write functions. In the UNIX system level to read and write the contents of the file, then open, close, read, write the four functions of the people must know.

First, let me introduce the EOF that we often encounter when reading files, and the kernel maintains a file location for each open file. For the read function, the Read file copies n bytes from the file to the memory, if the current file location to the end of the file is less than n bytes, returns the actual number of bytes read, if read again, will trigger a condition called end-of-file, the application can detect this condition. There is no EOF symbol at the end of the file.

Next, let's introduce the shared file. Do you have any idea of the following procedure? Suppose the value in Hh.txt is ABC.
#include <stdio.h>
#include <sys/type.h>
#include <sys/stat.h>
#include <fcntl.h>
int main ()
{
int fd1, int fd2;
char c;
FD1 = open ("Hh.txt", o_rdonly, 0);
FD2 = open ("Hh.txt", o_rdonly, 0);
Read (FD1, &c, 1);
Read (FD2, &c, 1);
printf ("%c", c);
Close (FD1);
Close (FD2);
return 0;
}

Let me tell you the result, the printed value is a. Three data structures are used in the kernel to represent open files. The first is the int descriptor, which is the FD1, FD2 in the above program. Each process has a single descriptor table. The second is to open the file table, all processes are shared, it mainly holds the location of the file, as well as the file location to open the file when reading a file, if you read a character, then the file location is no longer 0 but 1. The third type is the V-node table, which is shared by all processes. The properties of the file are mostly saved.

For the above program, there are two open file tables, each file table holds the location of open files, they are independent of each other, so in the last read, read a.

Of course we don't use these system-level functions most of the time when we write programs, and we've already developed a set of standard IO functions for us to use, such as Printf,fopen,fread.

The communication and interaction between our program and other programs (typically the client and server) is done using sockets, which is a type of file for the network abstraction, which is also referenced with a file descriptor, so that what we have explained above is fully applicable to the socket.

In the case of multithreaded programming, the interaction and communication between programs and programs in different threads. Here to consider the problem is more, and the model is not the same as the previous model, in order not to affect the theme of this article, here is no longer cumbersome, next time for everyone to explain.

Do you really have a thorough understanding of computer systems? Chapter two------the interaction between programs and programs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.