System Calls Lab Manual
System Calls Lab Manual
System Calls Lab Manual
When CPU is in kernel mode, the code being executed can access any memory
address and any hardware resource.
Hence kernel mode is a very privileged and powerful mode.
If a program crashes in kernel mode, the entire system will be halted.
When CPU is in user mode, the programs don't have direct access to memory and
hardware resources.
In user mode, if any program crashes, only that particular program is halted.
That means the system will be in a safe state even if a program in user mode
crashes.
Hence, most programs in an OS run in user mode.
Command:
1) Ps: If you run ps command without any arguments, it displays
processes for the current shell. This command stands for 'Process
Status'. It is similar to the "Task Manager" that pop-ups in a Windows
Machine when we use Cntrl+Alt+Del. This command is similar to 'top'
command but the information displayed is different.
2) Top: The easiest way to find out what processes are running on your
server is to run the top command:
3) Kill: The easiest way to find out what processes are running on your
server is to run the top command:
4) Jobs: List background processes
5) & :Run the command in the background
6) Nice: Starts a process with a given priority
Often, you will want to adjust which processes are given priority in a server
environment.
Some processes might be considered mission critical for your situation, while
others may be executed whenever there might be leftover resources.
Linux controls priority through a value called niceness.
Nice values can range between "-19/-20" (highest priority) and "19/20" (lowest
priority) depending on the system.
7) Renice: Changes priority of an already running process
a.What is a Process?
An instance of a program is called a Process. In simple terms, any command
that you give to your Linux machine starts a new process.
Types of Processes:
Foreground Processes: They run on the screen and need input from the
user. For example Office Programs
Background Processes: They run in the background and usually do not
need user input. For example Antivirus.
Init is the parent of all Linux processes. It is the first process to start when a
computer boots up, and it runs until the system shuts down. It is the ancestor
of all other processes.
1. Process Creation:
The processes in most systems can execute concurrently, and they may be created and deleted dynamically.
Thus, these systems must provide a mechanism for process creation and termination.
I. fork()
Has a return value
Parent process => invokes fork() system call
Continue execution from the next line after fork()
Has its own copy of any data
Return value is > 0 //it’s the process id of the child process. This value is different from the Parents
own process id.
Child process => process created by fork() system call
Duplicate/Copy of the parent process //LINUX
Separate address space
Same code segments as parent process
Execute independently of parent process
Continue execution from the next line right after fork()
Has its own copy of any data
Return value is 0
II. wait ()
Used by the parent process
Parent’s execution is suspended
Child remains its execution
On termination of child, returns an exit status to the OS
Exit status is then returned to the waiting parent process //retrieved by wait ()
Parent process resumes execution
#include <sys/wait.h>
#include <sys/types.h>
III. exit()
Process terminates its execution by calling the exit() system call
It returns exit status, which is retrieved by the parent process using wait() command
EXIT_SUCCESS // integer value = 0
EXIT_FAILURE // integer value = 1
OS reclaims resources allocated by the terminated process (dead process) Typically performs clean-up
operations within the process space before returning control back to the OS
_exit()
Terminates the current process without any extra program clean-up
Usually used by the child process to prevent from erroneously release of resources belonging to the
parent process
i. sleep()
Process goes into an inactive state for a time period
Resume execution if
Time interval has expired
Signal/Interrupt is received
Takes a time value as parameter (in seconds on Unix-like OS and in milliseconds
on Windows OS)
sleep(2) // sleep for 2 seconds in Unix
Sleep(2*1000) // sleep for 2 seconds in Windows
./a.out 1 22
./a.out is the usual method of running an executable via the terminal. Here
1 and 22 are the numbers that we have passed as command line argument
to the program. These arguments are passed to the main function. In order
for the main function to be able to accept the arguments, we have to
change the signature of main function as follows:
argc in this case will not be equal to 2, but it will be equal to 3. This is
because the name ./a.out is also passed as command line argument. At
index 0 of arg, we have ./a.out; at index 1, we have 1; and at index 2, we
have 22. Here 1 and 22 are in the form of character string, we have to
convert them to integers by using a function atoi. Suppose we want to add
the passed numbers and print the sum on the screen:
cout<< atoi(arg[1]) + atoi(arg[2]);
Sample Code:
#include <stdio.h>
void main(int argc, char *argv[] ) {