The first lesson will be for introduction to Unix and system calls.
Although this may seem very trivial this lesson, it is important to show you the workflow and working
with system calls, and I will give you many tips and useful commands to help with your development.
The first topic in this lesson that I would like to cover is system calls.
System calls are a way to request service from the kernel.
We will go over what services can be requested from the kernel later on in this course.
But before that, I just want to do a quick review on what the kernel is.
The kernel is the center of the Unix operating system is essentially a process that interfaces between
the user and the hardware to allow its user level processes to request services of the operating system.
And that will be shown in a lot of our coding examples.
There are many different categories of system calls.
Since there are so many, I will only be able to cover a portion of two of them.
For now, I would just give you a quick overview of all the categories.
The first category is file manipulation.
These system calls are used to create a file, delete a file, open close and also get file attributes.
The next category of system calls is process control.
These system calls are used to create a process and terminate a process.
The next category is device management.
Here you are able to request a device or release a device with the system calls.
Next, we have information maintenance.
Here you were able to get the time or date and get other system data that you might need.
The last category is communications.
Here you can create or delete a communication connection and also send or receive messages using their
types of system calls.
Now, before we move on, I just wanted to give a quick note about system calls.
The rule of thumb when using system calls is that you must always check the return value to see if an
error has occurred.
For example, you might be using a system call to create a process, but there are a lot of things that
could go wrong when trying to do that.
If your program continues without checking the return value of the system, call your program will crash
and could lead to vulnerabilities.
Although this is very important, I may skip the check here and there to simplify the code that I'm
showing in my examples.
The next topic in this lesson that I feel is very important to visualize is the Unix operating system
structure.
This is important to know so you can see exactly how your program is interacting with the Linux kernel.
Now, just very quickly, there are two types of spaces in Unix.
There is a user space and then there's the kernel space.
And in your user program system, calls are the only entry point into the kernel system.
The user space is where applications and utilities are.
Applications are programs written by a program, and these are generally the source code that you think
of that are written C or C++.
And in the application layer, you are able to use function calls either predefined in a library or
one that you'd find in your code.
The user space also includes utilities, or some people call them libraries, and these are your standard
header files such as STD i o header file, which allows you to use pre compiled like functions for input
and output.
Moving onto the kernel space, the kernel space also consists of two layers the portable operating system
layer and the machine dependent layer.
The portable operating system layer is where the system calls are used.
Calls such as Read, Open and write are using this layer to manipulate files, and the next layer within
the kernel space is the machine dependent layer.
This layer includes other essential services, such as input and output, device drivers, memory management
and process management.
The first coding example that I would like to show you is a simple Hello World program, and this program
uses the right system call to accomplish Hello World.
And this will give you a good feeling of the workflow and user system calls where to get information
about a certain system, calls how to write them, and much more.
So I'm going to switch over to my virtual box to get started.
OK, so the first program that I wanted to run was a simple Hello world program, although this looks
very simple.
And you know what's going to happen, but it's actually behind the scenes what I want to show you.
Just for starters, we will compile this with GTC and then run data down and as expected.
Hello World is presented to the council.
Now, that might seem very easy to understand, but it's actually a little more complicated than that.
So the colonel actually doesn't know what print means.
This program is in the user space, so in order to get to the kernel space, it needs a new system calls.
In order to see what system called this print statement is using.
We could run a handy command called Trace.
And this essentially does a system called Trace on your program.
So we do trace and then we run a program agent out, and a lot is, I'll put it to the council, but
we'll sift through this to see exactly what we need.
So the first line that was executed was in the IEEE, and you see it running data out.
So this is basically a system call to run a program, and I will be covering that later.
Here it's memory mapping some things and protecting some areas.
But what's important is this line at the bottom which says, write one Hello world 13 Hello World,
because 13 certainly is very complicated, but we're going to look into exactly how that system call
was created and we're going to create our own.
Our statement is not known by the kernel.
It is translated to a state system call, which the kernel knows, and it actually is using this right
command to show it on the council.
I'm going to show you quickly how we can arrive at that right solution instead of using print F..
So the first thing that you want to do is now that we know that our system call that we want to use
is called right.
We're just going to tape man for manual, right?
And when this comes up, the General Command manual comes up for the right, and I just want you to
take a quick note that this is the right one.
This version of right, it actually sends a message to another user.
So there's actually a different page for the right that we want in this man to write.
So here's a second version of right.
And this version of right allows you to write to file a descriptor.
I just want you take a quick note that this if you want to use the right command, you need to include
the unit state edge.
And as you can see here, I already have that included right here.
So now that we know that the right system call takes in these arguments, we have to figure out exactly
what these arguments are in order to accomplish what we want.
So we see it takes a integer of a file descriptor and down here you can see more about that.
But I want to show you how you can arrive at this if you had no idea how you would get it to print to
the council.
So just jumping back to the slides real quick.
There are essentially three types of foul streams that every program almost has, and that is steady
in a steady out and steady air.
And these are the values that are associated with them.
So if you remember and recall our right command that we saw in the streets, we saw that it had a value
for one and that's for city out, which that is exactly what we saw in the council.
But even if this is not a good enough answer for you, I want to show you how you can find these values
even if you do not know this.
So we'll be heading back to the virtual environment.
So let's say that you did not know what those foul streams were and their associated values.
So what you would have to do is first look at the manual page for what you're using.
And as you can see here, we need to include unique study that each.
So what that means is every single variable that is defined in this header file could be used in here.
Let's quit and then go look at a useful command that could be used to find the location of the header
files that are used in your program.
What are you going to want to do?
Is compile your program with a Dot Dash H command, so you do see Dash H and then type in your program
and copilot.
And this will essentially print out all the header information regarding your program.
Now, the line that is important to us is this slash user slash includes slash unit SDH.
This is the exact location of where the head of fire is located and will be able to go in there to find
additional information about those file streams.
So we're going to do change directory to slash user slash include.
And then in here, this is where all our head files are.
But the ones that we want to look at is the unique STD stage.
First, I want to show you that there is an easy way to do this because when you are working with variables
that are used and defined because like I showed before, instead of using one, you could just use stdout
out number.
In order to find those, what you're going to want to do is run a grep which the global regular expression
print here, which is going to grip the power and define because this is going to be defining certain
variables and then the file that we want to look in.
And that's unit stage in here, we can see a bunch of variables are defined.
However, here are the variables that are defined that we need.
Here is the exact same thing I showed on the slide deck, which is stored in file number and that pertains
to zero and that's for standard input, said Alt File No.
One.
And this relates to standard output, and that is exactly what our right command used.
The reason why I show you this is because this is kind of the workflow.
If you have no idea what a certain concern is or a certain defined variable is that you see in some
code you can go look in the header file grep it to actually find it.
Let's go back to our actual program and make some changes.
Now that we know that let's just bring up the man and page real quick because we're going to be using
that.
So we're going to be following the same output.
So we're going to keep our print f command here.
But now beneath it, we're going to write our own.
We're going to write print except using system calls.
Here's one for using system calls.
Here we're going to do a right because this is what we found right here, right?
A file descriptor.
And now that we know the file is descriptor for standard output is one we could put one.
However, if you want to use the defined variables, you can also type in study out, underscore file
note.
And that's the same thing that we saw in the slide deck and also in the header file, sort of while
I went through that process to show you that.
So that's a file descriptor.
Next, we want to look at the buffer.
And as you can see, it's a pointer.
But since we're using just hard coding right now, we can just type it in the buffer that we want to
print is Hello World exclamation mark and then a new line at the end of that.
And then lastly, it says you want account.
So this essentially expresses the count in your buffer that you want to print out to the standard.
In this case, the standard output, which is the file descriptor.
Here, we want to type in the number of bytes, the number of bytes of Helloworld.
Such and instead of counting, we're going to simply type, size of and then type in the actual command
or text.
Now there are many different ways to do this, but this is essentially just the easiest way that I wanted
to show you using a system call and using one line.
Although this seems unpractical how we're doing this.
This workflow and procedure can be used to accomplish any program with any system calls, so I just
want to show you that.
So now we're going to save that, put this out, and then we're going to compile it again.
Oops.
Oh yeah, I'm in the wrong folder right now.
So change desktop system program.
Add one in here.
We are going to run the C Helloworld I C, and here we have an error.
So it looks like I forgot to put a
semicolon and we're going to run again.
So let's run it now that we have it and you see that Helloworld is printed twice and that is exactly
what we would have expected, because here we have first printing it using a print statement.
And here we are using system calls.
So.
Now, just for safe measures, we could run a test trace, and here you will be able to look at the
bottom and here is a system call for enough statement.
And here's our exact system call for the one that we wrote.
And as you can see, it's a little bit different, and that's because it looks like I added a while.
So when it's a little, it's kind of complicated behind it.
But if we're used string length instead, it will work fine if we run that and then run as a trace.
And then here we can see that it is the exact same system call.
We wrote a separate, different ways here and here, but this is kind of will be helpful for further
lessons where you might need to look at header files or figure out what a certain defined variable is.
And also more information about the system.
Call for the sake of this course and for further explanation.
Exploration on implement commands you should look into and try to implement the unit commands and see.
So if you wanted to implement a command such as change directory or make directory or move, you can
all implement these using system cause I would advise you to just first write it in C and then run it
s trace.
See what system calls they're using.
Then go look up the manual for a certain system calls and then try to implement it yourself.
Using the actual system calls the exact same workflow that we used all for this simple print statement
that can be used for much harder problems.
That is the end of the first lesson of this course.
I hope it is very helpful.
I will see you in the next one.