1
00:00:00,005 --> 00:00:01,007
- [Instructor] Device files

2
00:00:01,007 --> 00:00:03,009
are another important way

3
00:00:03,009 --> 00:00:07,007
to talk to the kernel via user space.

4
00:00:07,007 --> 00:00:11,007
In particular, to talk to device drivers

5
00:00:11,007 --> 00:00:15,009
for block devices and character devices.

6
00:00:15,009 --> 00:00:19,009
So things like your keyboard, say

7
00:00:19,009 --> 00:00:21,003
or your disc,

8
00:00:21,003 --> 00:00:23,009
but not things like a network driver,

9
00:00:23,009 --> 00:00:26,008
usually, not the usual network sort of stuff you do

10
00:00:26,008 --> 00:00:29,004
with a network driver.

11
00:00:29,004 --> 00:00:32,000
Device files are special kind of files

12
00:00:32,000 --> 00:00:34,000
and they have a type, they're either a C

13
00:00:34,000 --> 00:00:37,000
or a B, a character or a block.

14
00:00:37,000 --> 00:00:41,009
And the LS minus L command will show that CRB

15
00:00:41,009 --> 00:00:44,007
and then instead of a size

16
00:00:44,007 --> 00:00:48,004
LS will put what's known as the major number

17
00:00:48,004 --> 00:00:50,006
and the minor number.

18
00:00:50,006 --> 00:00:52,008
And the kernel maintains a relationship

19
00:00:52,008 --> 00:00:55,009
between the major number and the minor number

20
00:00:55,009 --> 00:00:57,004
and the type

21
00:00:57,004 --> 00:01:02,002
to know which device driver to call.

22
00:01:02,002 --> 00:01:04,003
And classically,

23
00:01:04,003 --> 00:01:07,002
each device driver had its own major number,

24
00:01:07,002 --> 00:01:10,006
but there's way too many device drivers these days.

25
00:01:10,006 --> 00:01:11,007
So it's really the combination

26
00:01:11,007 --> 00:01:13,001
of the major number and the minor number

27
00:01:13,001 --> 00:01:15,008
that identifies which driver.

28
00:01:15,008 --> 00:01:19,002
And a driver can have multiple minor numbers.

29
00:01:19,002 --> 00:01:23,008
Oftentimes differing minor numbers would be used to

30
00:01:23,008 --> 00:01:26,006
distinguish between multiple instances of a device,

31
00:01:26,006 --> 00:01:27,005
for example.

32
00:01:27,005 --> 00:01:31,008
For a disc, the minor number can identify which partition.

33
00:01:31,008 --> 00:01:35,001
So a device file for a disc

34
00:01:35,001 --> 00:01:37,005
might have the same major number

35
00:01:37,005 --> 00:01:41,003
for all the discs and all the partitions,

36
00:01:41,003 --> 00:01:44,004
but the minor number would say which disc

37
00:01:44,004 --> 00:01:46,005
and which partition.

38
00:01:46,005 --> 00:01:49,000
Device files are in /dev.

39
00:01:49,000 --> 00:01:51,003
And /dev usually is

40
00:01:51,003 --> 00:01:53,004
a directory that gets created at boot time

41
00:01:53,004 --> 00:01:56,000
and then populated.

42
00:01:56,000 --> 00:01:59,003
The startup procedures for Linux

43
00:01:59,003 --> 00:02:02,003
probably create some of the classic device files,

44
00:02:02,003 --> 00:02:06,005
but for drivers you might not have a device file

45
00:02:06,005 --> 00:02:09,002
until the system discovers a device

46
00:02:09,002 --> 00:02:13,004
and then those device files get created automatically.

47
00:02:13,004 --> 00:02:16,000
Now, let's take a look at one example.

48
00:02:16,000 --> 00:02:18,009
I did a long list on dev/sda1.

49
00:02:18,009 --> 00:02:20,006
We see the first character is a B.

50
00:02:20,006 --> 00:02:23,003
It's a block device file.

51
00:02:23,003 --> 00:02:24,009
There's only one link to it.

52
00:02:24,009 --> 00:02:25,009
It's owned by root.

53
00:02:25,009 --> 00:02:29,004
The group is disc, but then we see eight, one.

54
00:02:29,004 --> 00:02:31,000
That's the major and minor number.

55
00:02:31,000 --> 00:02:33,008
So eight is the SCSI device driver

56
00:02:33,008 --> 00:02:37,009
and one is actually saying first disc, first partition.

57
00:02:37,009 --> 00:02:39,005
Now, we've got date and time information.

58
00:02:39,005 --> 00:02:42,004
Then we have the path dev/sda1.

59
00:02:42,004 --> 00:02:45,006
So that eight, one is what's important.

60
00:02:45,006 --> 00:02:49,006
If we create a device file with a different name,

61
00:02:49,006 --> 00:02:53,005
we called it, "Hello there, I'm a disc,"

62
00:02:53,005 --> 00:02:56,002
and we made it a block device file eight, one,

63
00:02:56,002 --> 00:02:59,008
it would act the same as dev/sda1.

64
00:02:59,008 --> 00:03:01,009
Device files don't have to be in dev, by the way,

65
00:03:01,009 --> 00:03:03,007
you could put one in your home directory

66
00:03:03,007 --> 00:03:06,005
as long as that's allowed for your file system.

67
00:03:06,005 --> 00:03:08,009
But that would be kind of weird.

68
00:03:08,009 --> 00:03:11,007
So device drivers are associated

69
00:03:11,007 --> 00:03:13,001
with these device files, right?

70
00:03:13,001 --> 00:03:15,000
The device file is saying which driver to call.

71
00:03:15,000 --> 00:03:17,000
So when you open up a device file,

72
00:03:17,000 --> 00:03:22,001
the kernel figures out what open function to call,

73
00:03:22,001 --> 00:03:25,006
and a device driver registers its sets of functions

74
00:03:25,006 --> 00:03:29,005
with the kernel and says, "I'm driver number 57.

75
00:03:29,005 --> 00:03:33,007
If somebody opens device file with a major number, 57,

76
00:03:33,007 --> 00:03:36,002
call my open," sort of thing.

77
00:03:36,002 --> 00:03:38,005
So there's a whole set of functions

78
00:03:38,005 --> 00:03:40,009
that drivers can implement.

79
00:03:40,009 --> 00:03:43,006
Character drivers have one, sort of, set of functions.

80
00:03:43,006 --> 00:03:47,008
Block drivers have a different, sort of, set of functions.

81
00:03:47,008 --> 00:03:49,008
And when we use a device file,

82
00:03:49,008 --> 00:03:53,006
like if we say echo high greater than dev, null,

83
00:03:53,006 --> 00:03:57,006
the shell's going to open up dev/null.

84
00:03:57,006 --> 00:03:59,000
Dev/null is a device file.

85
00:03:59,000 --> 00:04:00,006
There's a device driver associated with it.

86
00:04:00,006 --> 00:04:02,003
It's called the memory driver.

87
00:04:02,003 --> 00:04:04,008
So the open for that would be called.

88
00:04:04,008 --> 00:04:06,006
And then the gridder then,

89
00:04:06,006 --> 00:04:07,008
says, "We're going to do a right,"

90
00:04:07,008 --> 00:04:12,002
and then the right function for that driver would be called.

91
00:04:12,002 --> 00:04:14,002
Let's look at a few challenges

92
00:04:14,002 --> 00:04:17,000
having to do with device files.

