Network Lab
Network Lab
To familiarize and understand the use and functioning of System Calls used for
Operating system and network programming in Linux.
1. ps
This command tells which all processes are running on the system when ps runs.
ps -ef
This command gives processes running on the system, the owners of the processes and the names of
the processes. The above result is an abridged version of the output.
2. fork
This system call is used to create a new process. When a process makes a fork system call, a new
process is created which is identical to the process creating it. The process which calls fork is called
the parent process and the process that is created is called the child process. The child and parent
processes are identical, i.e, child gets a copy of the parent's data space, heap and stack, but have
different physical address spaces. Both processes start execution from the line next to fork. Fork
returns the process id of the child in the parent process and returns 0 in the child process.
#include<stdio.h>
void main()
{
int pid;
pid = fork();
if(pid > 0)
{
printf (“ Iam parent\n”);
}
else
{
printf(“Iam child\n”);
}
}
The parent process prints the first statement and the child prints the next statement.
3. exec
New programs can be run using exec system call. When a process calls exec, the process is
completely replaced by the new program. The new program starts executing from its main function.
A new process is not created, process id remains the same, and the current process's text, data, heap,
and stack segments are replaced by the new program. exec has many flavours one of which is execv.
execv takes two parameters. The first is the pathname of the program that is going to be executed. The
second is a pointer to an array of pointers that hold the addresses of arguments. These arguments are
the command line arguments for the new program.
4. wait
When a process terminates, its parent should receive some information reagarding the process like the
process id, the termination status, amount of CPU time taken etc. This is possible only if the parent
process waits for the termination of the child process. This waiting is done by calling the wait system
call. When the child process is running, the parent blocks when wait is called. If the child terminates
normally or abnormally, wait immedaitely returns with the termination status of the child. The wait
system call takes a parameter which is a pointer to a location in which the termination status is stored.
5. exit
When exit function is called, the process undergoes a normal termination.
6. open
This system call is used to open a file whose pathname is given as the first
parameter of the function. The second parameter gives the options that tell the
way in which the file can be used.
open(filepathname , O_RDWR);
This causes the file to be read or written. The function returns the file descriptor of the
file.
7. read
buffer, sizeof(buffer));
The above function reads sizeof(buffer) bytes into the array named buffer.
If the end of file is encountered, 0 is returned, else the number of bytes
read is returned.
8. write
buffer, sizeof(buffer));
1. Creating a socket
This sytem call creates a socket and returns a socket descriptor. The domain
parameter specifies a communication domain; this selects the protocol family
which will be used for communication. These families are defined in
<sys/socket.h>. In this program the AF_INET family is used. The type parameter
indicates the communication semantics. SOCK_STREAM is used for tcp
connection while SOCK_DGRAM is used for udp connection. The protocol
parameter specifies the protocol used and Protocol value for Internet
Protocol(IP) is always 0. The header files used are <sys/types.h> and
<sys/socket.h>.