PROGRAM 1
Familiarity and Usage of Linux System Calls
❖ Linux is an open source operating system for computers, servers
and embedded devices.
❖ Linux is a widely supported operating system.
❖ Linux is highly configurable and depends on modular design that
enables users to customize their own versions of linux.
❖ Linux O.S was developed by Linux Torvalds in 1991 to improve
UNIX OS.
❖ Linux is a fast growing operating system.
OBJECTIVES
❖ To explore the history of the UNIX operating system from which
Linux is derived and the principles upon which Linux’s design is
based.
❖ To examine the Linux process model and illustrate how Linux
schedules processes and provides interprocess communication.
❖ To look at memory management in Linux.
❖ To explore how Linux implements file systems and manages I/O
devices.
USES
❖ Server OS for web servers, database servers, file servers, email
servers and any other type of shared server.
❖ Embedded device or appliance OS for systems that require limited
computing function.
❖ Software development OS for enterprise software development.
FEATURES OF LINUX
❖ Multi User capability
❖ Portability
❖ Multi tasking
❖ Security
❖ Linux is a command line based operating system but can be
converted to GUI by package installations.
❖ Customisation of keyboards.
❖ Supports hierarchical file systems.
COMPONENTS OF LINUX SYSTEM
LINUX KERNEL
❖ Core part of O.S.
❖ Establishes communication between device and software.
❖ Responsibilities:
➢ Device management
➢ Memory management
➢ Process management and handling system calls
SYSTEM LIBRARIES
❖ Helps in kernel features.
❖ Well known system library for linux in Glibc(GNU C Library)
❖ They define a standard set of functions.
❖ System libraries implement much of operating system
functionality.
LINUX DISTRIBUTORS
❖ Standard, pre-compiled sets of packages.
❖ First distributors unpacked all files into appropriate places.
❖ Modern distributions include advanced package management.
❖ Early distributions are SLS and Slackware.
❖ Red Hat and Debian are popular distributions.
LINUX LICENSING
❖ Linux kernel distributions under GNU General Public License(GPL).
❖ Linux users may not make derived products proprietary.
❖ Software released under GPL may not be redistributed as binary
only product.
KERNEL MODULES
❖ Sections of kernel code that can be compiled, loaded and unloaded.
❖ Implements a device driver, file systems or networking protocol.
❖ Four components to support linux module:
➢ Module management system
➢ Module loader and unloader
➢ Driver registrations system
➢ Conflict resolution management
LINUX COMMANDS
S.No Command Description
1 ls Directory listing
2 ls -l long list (displays lots of info)
3 ls -t sort by modification time
4 ls -s sort by size
ls -h list file sizes in human readable format
5 ls -al Formatted listing with hidden files
6 ls -lt Sorting the Formatted listing by time
modification
7 clear
8 man need help with a command type
9 Echo Display a line of text
10 cd Change to home directory
11 cd dir Change directory to dir
12 Pwd Show current working directory
13 mkdir dir Creating a directory dir
14 cat >file Places the standard input into the file
15 touch file Create or update file
16 rm file Deleting the file
17 head file Output the first 10 lines of the file
18 tail file Output the last 10 lines of the file
19 date Show the current date and time
20 whoami Who you are logged in as
21 w Display who is on line
22 uptime Show current uptime
23 cal Show this month's calender
24 df Show the disk usage
25 du Show directory space usage
26 ps To display the currently working
processes
27 top Display all running process
28 kill pid Kill the process with given pid
29 bg List stopped or background jobs,resume
a stopped
job in the background
30 fg Brings the most recent job to
foreground
31 chmod octal file Change the permission of file to octal,which
can be found separately for user,group, world by adding,
Searching
• 4-read(r)
• 2-write(w)
• 1-execute(x)
OUTPUTS
❖ Command No.1: ls
Directory listing
❖ Command No. 2: ls-l
long list (displays lots of info)
❖ Command No. 3: ls-t
sort by modification time
❖ Command No. 4: ls-s
sort by size
❖ Command No. 5: ls-h
list file sizes in human readable format
❖ Command No. 6: ls-al
Formatted listing with hidden files
❖ Command No. 7: ls-lt
Sorting the Formatted listing by time modification
❖ Command No. 8: cat>file
Places the standard input into file
❖ Command No. 9: head file
Output first 10 lines of file
❖ Command No. 10: head -n 2 file
Output first 2 lines of file
❖ Command No. 11: tail file
Output last 10 lines of file
❖ Command No. 12: date
Show the current date and time
❖ Command No. 13: whoami
Who are you logged in as
❖ Command No. 14: w
Display who is online
❖ Command No. 15: uptime
Show current uptime
❖ Command No. 16: cal
Show the month’s calendar
❖ Command No. 17: df
Show this disk usage
❖ Command No. 18: du
Show the directory space usage
❖ Command No. 20: ps
To display the currently working process
❖ Command No. 21: top
Display all running processes
❖ Command No. 22: kill pid
Kill the process with given pid
❖ Command No. 23: bg
List stopped or background jobs, resume a stopped job in the
background
❖ Command No. 24: fg
Brings the most recent job to foreground
❖ Command No. 25: Chmod octal file
Change permission of file to Octal, which can be found separately
for user, group world by adding, searching
SYSTEM CALLS
a. Process management: fork(), exec(), wait(),
sleep() …
b. File management: open (), read (), write (), seek
(), close ()...
WHAT IS A SYSTEM CALL?
❖ A system call is a mechanism that provides the interface between a
process and the operating system.
❖ It is a programmatic method in which a computer program
requests a service from the kernel of the OS.
❖ System call offers the services of the operating system to the user
programs via API (Application Programming Interface).
DUAL MODE OPERATIONS IN OPERATING SYSTEM
❖ There are two modes of operation in the operating system.
❖ These are user mode and kernel mode
User Mode:
❖ The system is in user mode when the operating system is running a
user application such as handling a text editor.
❖ The transition from user mode to kernel mode occurs when the
application requests the help of the operating system or an
interrupt or a system call occurs.
❖ The mode bit is set to 1 in the user mode. It is changed from 1 to 0
when switching from user mode to kernel mode.
Kernel Mode:
❖ The system starts in kernel mode when it boots and after the
operating system is loaded, it executes applications in user mode.
❖ There are some privileged instructions that can only be executed in
kernel mode. These are interrupt instructions, input output
management etc
NEED FOR SYSTEM CALLS IN OPERATING SYSTEMS
❖ Reading and writing from files.
❖ To create or delete files.
❖ Creation and management of new processes.
❖ For sending and receiving packets.
❖ Access to hardware devices.
TYPES OF SYSTEM CALLS
❖ Process Control
❖ File Management
❖ Device Management
❖ Information Maintenance
❖ Communications
❖ Process Control Functions:
➢ End and Abort
➢ Load and Execute
➢ Create Process and Terminate Process
➢ Wait and Signed Event
➢ Allocate and free memory
❖ File Management Functions:
➢ Create a file
➢ Delete file
➢ Open and close file
➢ Read, write, and reposition
➢ Get and set file attributes
❖ Device Management Functions:
➢ Request and release device
➢ Logically attach/ detach devices
➢ Get and Set device attributes
❖ Information Maintenance Functions:
➢ Get or set time and date
➢ Get process and device attributes
❖ Communication Functions:
➢ Create, delete communications connections
➢ Send, receive message
➢ Help OS to transfer status information
➢ Attach or detach remote devices
PROCESS CREATION
BASIC PROCESS MANAGEMENT SYSTEM CALLS
❖ Fork: A parent process uses fork to create a new child process.
❖ The child process is a copy of the parent.
❖ After fork, both parent and child executes the same program but in
separate processes.
❖ Exec: Replaces the program executed by a process. The child may
use exec after a fork to replace the process’ memory space with a
new program executable making the child execute a different
program than the parent.
❖ Exit: Terminates the process with an exit status.
❖ Wait: The parent may use wait to suspend execution until a child
terminates. Using wait the parent can obtain the exit status of a
terminated child.
BASIC FILE MANAGEMENT SYSTEM CALLS
❖ Open: The open() system call allows you to access a file on a file
system. It allocates resources to the file. Many processes can open a
file at once or by a single process only.
❖ Read:It is used to obtain data from a file on the file system. It
accepts three arguments in general:
➢ A file descriptor.
➢ A buffer to store read data.
➢ The number of bytes to read from the file.
❖ Wait: In some systems, a process may have to wait for another
process to complete its execution before proceeding. When a
parent process makes a child process, the parent process execution
is suspended until the child process is finished. The wait() system
call is used to suspend the parent process. Once the child process
has completed its execution, control is returned to the parent
process.
❖ Write: It is used to write data from a user buffer to a device like a
file. It takes three arguments in general:
➢ A file descriptor.
➢ A pointer to the buffer in which data is saved.
➢ The number of bytes to be written from the buffer.
PROGRAMS
1. Display parent id and process id.
#include<stdio.h>
int main()
printf("\n parent process id %d",getppid());
printf("\n child process id %d\n",getpid());
OUTPUT:
2. Fork System calls.
#include <stdio.h>
#include <sys/types.h>
int main()
fork();
fork();
fork();
printf("hello\n");
return 0;
OUTPUT:
3. Process creation using fork
#include<stdio.h>
main()
printf("Before FORK \n");
printf("\n parent process id %d",getppid());
fork();
printf("After FORK \n\n");
printf("\n parent process id %d",getppid());
printf("\n child process id %d\n",getpid());
OUTPUT:
4. Making child as orphan
#include<stdio.h>
#include<signal.h>
main()
pid_t pid;
pid=fork();
if(pid==0)
printf("child has started%d\n",getpid());
printf("\nparent id%d\n",getppid());
sleep(30);
else
printf("\n parent has started%d\n",getppid());
kill(getppid(),SIGKILL);
}
printf("after fork chid finished\n\n");
OUTPUT:
5. Process with fork
#include<stdio.h>
main()
int pid;
pid=fork();
if(pid>0)
printf("from parent\n");
printf("parent process id%d\n",getppid());
else
printf("from children\n");
printf("child process id%d\n",getpid());
OUTPUT:
6. File management system calls
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
int main()
int n,fd;
char buff[50];
printf("Enter text to write in the file:\n");
n= read(0, buff, 50);
fd=open("bhavya",O_CREAT | O_RDWR, 0777);
write(fd, buff, n);
write(1, buff, n);
int close(int fd);
return 0;
OUTPUT: