Os Expt 1
Os Expt 1
1
Aim: To explore Linux commands
Explore usage of basic linux Commands and system calls for file,directory and process
management.
For example (mkdir,chdir,cat, ls chown,chmod ,chgrp,ps etc.)
System calls: open, read,write, close,getpid,setpid,getuid,getgid,getegid,geteuid,sort,grep,awk
etc.
Theory:
An operating system is software that manages all of the hardware resources associated with your
desktop or laptop. The operating system manages the communication between your software
and your hardware. Without the operating system (OS), the software wouldnt function.
The Linux operating system is everywhere From smartphones to cars, supercomputers and home
appliances, home desktops to enterprise servers,.
Since the mid-1990s Linux has been around and has since reached a user-base that spans the
globe. Linux is in your phones, your thermostats, in your cars, refrigerators, Roku devices, and
televisions. It also runs most of the Internet, all of the world’s top 500 supercomputers, and the
world’s stock exchanges.
Besides being the platform of choice to run desktops, servers, and embedded systems across the
globe, Linux is one of the most reliable, secure and worry-free operating systems available.
Just like Windows, iOS, and Mac OS, Linux is an operating system. In fact, one of the most
popular platforms on the planet, Android, is powered by the Linux operating system..
1. Bootloader – The software that manages the boot process of your computer. For
most users, this will simply be a splash screen that pops up and eventually goes away
to boot into the operating system.
2. Kernel – This is the one piece of the whole that is actually called ?Linux?. The kernel
is the core of the system and manages the CPU, memory, and peripheral devices. The
kernel is the lowest level of the OS.
3. Init system – This is a sub-system that bootstraps the user space and is charged with
controlling daemons. One of the most widely used init systems is systemd? which also
happens to be one of the most controversial. It is the init system that manages the boot
process, once the initial booting is handed over from the bootloader (i.e., GRUB or
GRand Unified Bootloader).
4. Daemons – These are background services (printing, sound, scheduling, etc.) that
either start up during boot or after you log into the desktop.
5. Graphical server – This is the sub-system that displays the graphics on your monitor.
It is commonly referred to as the X server or just X.
6. Desktop environment – This is the piece that the users actually interact with. There
are many desktop environments to choose from (GNOME, Cinnamon, Mate,
Pantheon, Enlightenment, KDE, Xfce, etc.). Each desktop environment includes built-
in applications (such as file managers, configuration tools, web browsers, and games).
7. Applications – Desktop environments do not offer the full array of apps. Just like
Windows and macOS, Linux offers thousands upon thousands of high-quality
software titles that can be easily found and installed. Most modern Linux distributions
(more on this below) include App Store-like tools that centralize and simplify
application installation. For example, Ubuntu Linux has the Ubuntu Software Center
(a rebrand of GNOME Software ) which allows you to quickly search among the
thousands of apps and install them from one centralized location.
ls
ls is a Linux shell command that lists directory contents of files and directories.
ls with no option list files and directories in bare format where we won’t be able to view
details like file types, size, modified date and time, permission and links etc.
mkdir
The mkdir command in Linux/Unix allows users to create or make new
directories. mkdir stands for “make directory.” With mkdir , you can also set
permissions, create multiple directories (folders) at once, and much more.
Chdir
The chdir command is a system function (system call) which is used to change the
current working directory. On some systems, this command is used as an alias for the
shell command cd. chdir changes the current working directory of the calling process to
the directory specified in path.
cat
The cat (short for “concatenate“) command is one of the most frequently
used command in Linux/Unix like operating systems. cat command allows us to create
single or multiple files, view contain of file, concatenate files and redirect output in
terminal or files.
Chown
Linux Chown Command Syntax. [OPTIONS] – the command can be used with or
without additional options. [USER] – the username or the numeric user ID of the new
owner of a file. [:] – use the colon when changing a group of a file. [GROUP] – changing
the group ownership of a file is optional.
chmod
In Linux, who can do what to a file or directory is controlled through sets
of permissions. There are three sets of permissions. One set for the owner of
the file, another set for the members of the file’s group, and a final set for
everyone else.
Chgrp
chgrp command in Linux is used to change the group ownership of a file or directory.
All files in Linux belong to an owner and a group. You can set the owner by using
“chown” command, and the group by the “chgrp” command.
Ps
ps command is used to list the currently running processes and their PIDs along with
some other information depends on different options. It reads the process information
from the virtual files in /proc file-system.
System calls
1)Open
if (fd ==-1)
{
// print which type of error have in a code
printf("Error Number % d\n", errno);
Output:
fd = 3
2)read: From the file indicated by the file descriptor fd, the read() function reads cnt bytes
of input into the memory area indicated by buf. A successful read() updates the access time
for the file.
fd = open("foo.txt", O_RDONLY);
if (fd < 0) { perror("r1"); exit(1); }
sz = read(fd, c, 10);
printf("called read(% d, c, 10). returned that"
" %d bytes were read.\n", fd, sz);
c[sz] = '\0';
printf("Those bytes are as follows: % s\n", c);
}
Output:
called read(3, c, 10). returned that 10 bytes were read.
Those bytes are as follows: 0 0 0 foo.
3) write: Writes cnt bytes from buf to the file or socket associated with fd. cnt should not be
greater than INT_MAX (defined in the limits.h header file). If cnt is zero, write() simply
returns 0 without attempting any other action.
close(fd);
}
Output:
called write(3, "hello geeks\n", 12). it returned 11
Here, when you see in the file foo.txt after running the code, you get a “hello geeks“. If foo.txt
file already have some content in it then write system call overwrite the content and all
previous content are deleted and only “hello geeks” content will have in the file.
4)close
Tells the operating system you are done with a file descriptor and Close the file which pointed
by fd.
Output:
opened the fd = 3
closed the fd.
5) getppid() : returns the process ID of the parent of the calling process. If the calling process
was created by the fork() function and the parent process still exists at the time of the getppid
function call, this function returns the process ID of the parent process. Otherwise, this
function returns a value of 1 which is the process id for init process.
Syntax:
pid_t getppid(void);
6)getpid() : returns the process ID of the calling process. This is often used by routines that
generate unique temporary filenames.
Syntax:
pid_t getpid(void);
7)setpid()
Sets the process group id of the process specified by pid to pgid.
8) The getuid() function returns the real user ID of the calling process. The real user ID
identifies the person who is logged in.
uid_t getuid(void);
9) The geteuid() function returns the effective user ID of the calling process. The effective user
ID gives the process various permissions during execution of “set-user-ID” mode processes
which use getuid() to determine the real user ID of the process that invoked them.
uid_t geteuid(void);
10) The getgid() function returns the real group ID of the calling process
11)The grep filter searches a file for a particular pattern of characters, and displays all lines
that contain that pattern. The pattern that is searched in the file is referred to as the regular
expression (grep stands for globally search for regular expression and print out).
Syntax:
12)sort
Procedure:
1)Install linux on the computer system
2)Implement all the basic commands given in the list with the proper syntax.
3)See the output of every command.
ls
Syntax
# ls
1) mkdir
# (kalimin㉿kalimin)-[~/Desktop]
└─$ mkdir OS
# (kalimin㉿kalimin)-[~/Desktop]
└─$ ls
attupatil.pcap coplat.pcap new_folder new.pcap OS os.txt
2) chdir
(kalimin㉿kalimin)-[~/Desktop]
└─$ chdir new_folder
┌──(kalimin㉿kalimin)-[~/Desktop/new_folder]
└─$
3) cat
(kalimin㉿kalimin)-[~/Desktop]
└─$ cat new.txt
this is new text
4) chown
(rootkalimin)-[/home/kalimin/Desktop/OS]
└─# chown kalimin files
┌──(rootkalimin)-[/home/kalimin/Desktop/OS]
└─# ls -l
total 4
drwxr-xr-x 2 kalimin root 4096 Apr 9 02:53 files
5) chmod
(rootkalimin)-[/home/kalimin/Desktop]
└─# chmod -x os.txt
┌──(rootkalimin)-[/home/kalimin/Desktop]
└─# ls -l
total 6428
-rw-r--r-- 1 kalimin kalimin 3965 Apr 9 02:23 os.txt
┌──(rootkalimin)-[/home/kalimin/Desktop]
└─# chmod -rwx os.txt
┌──(rootkalimin)-[/home/kalimin/Desktop]
└─# ls -l
total 6428
---------- 1 kalimin kalimin 3965 Apr 9 02:23 os.txt
6) chgrp
(rootkalimin)-[/home/kalimin/Desktop]
└─# ls -l
┌──(root�kalimin)-[/home/kalimin/Desktop]
└─# chgrp root os.txt
┌──(root�kalimin)-[/home/kalimin/Desktop]
└─# ls -l
total 6428
---------- 1 kalimin root 3965 Apr 9 02:23 os.txt
7) ps
(rootkalimin)-[/home/kalimin/Desktop]
└─# ps
PID TTY TIME CMD
1770 pts/0 00:00:00 sudo
1771 pts/0 00:00:00 su
1772 pts/0 00:00:02 zsh
2010 pts/0 00:00:00 ps
Outcome: At the end ,the learner will be able to write and execute basic commands and system
calls of OS in linux environment.
Conclusion; In this way we have learned basic commands and system calls in OS