0% found this document useful (0 votes)
44 views

OSLAB1

The document provides information about various Linux commands through their name, description, syntax, and examples. It summarizes 27 commands, including mkdir, ls, cd, man, rm, rmdir, echo, touch, cat, cp, mv, date, cal, pwd, history, chmod, ps, logout, shutdown, finger, kill, time, more, chown, who, and open, create, close, write, read, lseek. The document aims to study and practice these general purpose utility commands in Linux/UNIX.

Uploaded by

Mariya Babu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

OSLAB1

The document provides information about various Linux commands through their name, description, syntax, and examples. It summarizes 27 commands, including mkdir, ls, cd, man, rm, rmdir, echo, touch, cat, cp, mv, date, cal, pwd, history, chmod, ps, logout, shutdown, finger, kill, time, more, chown, who, and open, create, close, write, read, lseek. The document aims to study and practice these general purpose utility commands in Linux/UNIX.

Uploaded by

Mariya Babu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

LAB-01

Aim:Study and practice of linux/unix general purpose utility command list


Man,who,cat,cd,cp,ps,ls,mv,rm,mkdir,rmdir,echo,more,date,time,kill,history,chown,
chmod,Finger,pwd,cal,logout,shutdown

Name of the command:mkdir


Description: mkdir command in Linux allows the user to create directories (also referred
to as folders in some operating systems). This command can create multiple directories at
once as well as set the permissions for the directories.

Syntax: mkdir [options...] [directories ...]


Example with output:

Name of the command:ls


Description: ls is a Linux shell command that lists directory contents of files and
directories. It provides valuable information about files, directories, and their attributes

Syntax: ls [option] [file/directory]


Option:ls -l(use a long listing format)
Example with output:

Name of the command:ls -l


Description:. ls is a Linux shell command that lists directory contents of files and
directories. It is one of the most often used commands in regular Linux/UNIX operations is
the ls command. One of the few commands new users learn right away, the command is
used to list the contents of a directory

Syntax:ls -l
Options:
Example with output:
Name of the command:cd
Description: cd command in Linux known as the change directory command. It is used
to move efficiently from the current working directory to different directories in our System

Syntax: cd [directory]
Example with output:

Name of the command: man ls


Description: The man command is a built-in manual for using unix commands. It allows
users to view the reference manuals of a command or utility run in the terminal. The man
page (short for manual page) includes a command description, applicable options, flags,
examples, and other informative sections

Syntax:man ls
Examples with outputs:
No output
Name of the command: rm
Description: rm (short for remove) is a basic command on Unix and Unix-like operating
systems used to remove objects such as computer files, directories and symbolic links from
file systems and also special files such as device nodes, pipes and sockets, similar to the del
command in MS-DOS, OS/2, and Microsoft Windows

Syntax: rm <file name>


Examples with outputs:

Name of the command: rmdir


Description: The rmdir command removes the directory, specified by the Directory
parameter, from the system. The directory must be empty before you can remove it, and you
must have write permission in its parent directory. Use the ls -al command to check whether
the directory is empty.

Syntax: rmdir <dir name>


Examples with outputs:

Name of the command: echo

Description: The echo command is used to display text or messages on the terminal.
Syntax:echo option
Examples with outputs:

Name of the command:touch


Description: The touch command is a standard command used in UNIX/Linux operating
system which is used to create, change and modify timestamps of a file

Syntax: touch File1_name File2_name File3_name


Example with output:

Name of the command:cat


Description: Cat(concatenate) command is very frequently used in Linux. It reads data
from the file and gives its content as output. It helps us to create, view, and concatenate
files

Syntax: cat file_name


Example with output:

Name of the command:cp


Description: cp stands for a copy. This command is used to copy files or groups of files
or directories. It creates an exact image of a file on a disk with a different file
name. cp command requires at least two filenames in its arguments.
Syntax: cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
Example with output:

Name of the command:mv


Description:. In UNIX-based operating systems like Linux and macOS, `mv` stands for
“move”. But in this article, we will be talking about the “mv command in Linux”. As its
name suggests this command is used to rename file directories and move files from one
location to another within a file system.

Syntax: Syntax: mv [options(s)] [source_file_name(s)] [Destination_file_name]


Example with output:

Name of the command:date


Description: date command is used to display the system date and time. date command is
also used to set date and time of the system. By default the date command displays the date
in the time zone on which unix/linux operating system is configured.You must be the super-
user (root) to change the date and time.

Syntax: date [OPTION]... [+FORMAT]


date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Example with output:

Name of the command:cal


Description: cal command is a calendar command in Linux which is used to see the
calendar of a specific month or a whole year .

Syntax: cal [ [ month ] year]


Options:-s,-m,-h,-3
Example with output:
Name of the command:pwd
Description: pwd stands for Print Working Directory. It prints the path of the working
directory, starting from the root. pwd is shell built-in command(pwd) or an actual
binary(/bin/pwd). $PWD is an environment variable that stores the path of the current
directory. This command has two flags.

Syntax: pwd [OPTIONS]


Example with output:

Name of the command:history


Description: history command is used to view the previously executed command
Syntax: $ history
Example with output:

Name of the command:chmod


Description: In Unix operating systems, the chmod command is used to change the
access mode of a file. The name is an abbreviation of change mode

Syntax: chmod [options] [mode] [File_name]


Example with output:
Name of the command:ps
Description: Linux provides us a utility called ps for viewing information related with
the processes on a system which stands as abbreviation for “Process Status”. ps command
is used to list the currently running processes and their PIDs along with some other
information depends on different options

Syntax: ps [options]
Example with output:

Name of the command:logout


Description: The logout command is a Linux/UNIX shell command that performs the
task of logging out the logged-in user from the system in that session, programmatically.
Logout only works in logon shells, not in the non-logon shells
Syntax: $ logout
Example with output:

Name of the command:shutdown


Description: The shutdown command in Linux is used to shutdown the system in a safe
way

Syntax: shutdown [OPTIONS] [TIME] [MESSAGE]


Example with output:
No output

Name of the command:finger


Description: Finger command is a user information lookup command which gives details
of all the users logged

Syntax:finger <options><user>
Example with output:
No output

Name of the command:kill


Description: kill command in Linux (located in /bin/kill), is a built-in command which is
used to terminate processes manually

Syntax: kill [signal] PID


Example with output:
No output
Name of the command:time
Description: Linux time command displays how long it takes to execute a command. It
helps in checking the performance of the scripts and commands.

Syntax: time [option] [command]


Example with output:

Name of the command:more


Description: more command is used to view the text files in the command prompt,
displaying one screen at a time

Syntax: more [-options] [-num] [+/pattern] [+linenum] [file_name]


Example with output:

Name of the command:chown


Description: chown command is used to change the file Owner or group.
Whenever you want to change ownership, you can use chown commandes details of all the
users logged in

Syntax: chown [OPTION]… [OWNER][:GROUP] FILE…


chown [OPTION]… –reference=RFILE FILE…
Example with output:
No output

Name of the command:who


Description: The who command is used to get information about currently logged in
user on to system.

Syntax: $who [options] [filename]


Example with output:
No output
LAB-02
Aim:implementation of open(),create(),close(),write(),read(),lseek()
1.Name of the system call:create()
Description: The create() function is used to create a new empty file in C. We can
specify the permission and the name of the file which we want to create using the create()
function. It is defined inside <unistd.h> header file and the flags that are passed as
arguments are defined inside <fcntl.h> header file.
Syntax: int create(char *filename, mode_t mode)
Example with program:
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
int create(const char *pathname, mode_t mode) {
return open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
}
int main() {
int fd = create("oslab1file.txt", 0644);
if (fd == -1) {
perror("Error creating the file");
return 1;
}
else
{
printf("file created successfully");
}
close(fd);
return 0;
}
Output:

2.Name of the system call:open()


Description: The open() function in C is used to open the file for reading, writing, or
both. It is also capable of creating the file if it does not exist. It is defined
inside <unistd.h> header file and the flags that are passed as arguments are defined
inside <fcntl.h> header file
Syntax: int open (const char* Path, int flags);
Example with program:
#include<stdio.h>
#include<stdlib.h>
#include <fcntl.h>
int main()
{
int sz;
int fd = open("mani2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
{
perror("r1");
exit(1);
}

sz = write(fd, "hello manikanta\n", strlen("hello manikanta\n"));


printf("called write(% d, \"hello manikanta\\n\", %d)."
" It returned %d\n", fd, strlen("hello manikanta\n"), sz);
close(fd);
}
Output:

3.Name of the system call:read()


Description: From the file indicated by the file descriptor fd, the read() function reads
the specified amount of bytes cnt of input into the memory area indicated by buf. A
successful read() updates the access time for the file. The read() function is also defined
inside the <unistd.h> header file

Syntax: size_t read (int fd, void* buf, size_t cnt);


Example with program:
#include <fcntl.h>
#include<stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
int fd, sz;
char* c = (char*)calloc(100, sizeof(char));
fd = open("mani2.txt", O_RDONLY);
if (fd < 0) {
perror("r1");
exit(1);
}
sz = read(fd, c, 15);
printf("called read(% d, c, 15). returned that"
" %d bytes were read.\n",
fd, sz);
c[sz] = '\0';
printf("Those bytes are as follows: % s\n", c);
return 0;}

Output:

4.Name of the system call:write()


Description: 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.

Syntax: size_t write (int fd, void* buf, size_t cnt);


Example with program:
#include<stdio.h>
#include<stdlib.h>
#include <fcntl.h>
int main()
{
int sz;
int fd = open("mani2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
{
perror("r1");
exit(1);
}
sz = write(fd, "hello manikanta\n", strlen("hello manikanta\n"));
printf("called write(% d, \"hello manikanta\\n\", %d)."
" It returned %d\n", fd, strlen("hello manikanta\n"), sz);
close(fd);
}
Output:

5.Name of the system call:close()


Description: The close() function in C tells the operating system that you are done with a
file descriptor and closes the file pointed by the file descriptor. It is defined
inside <unistd.h> header file

Syntax: int close(int fd);


Example with program:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include<stdlib.h>
int main()
{
int fd1 = open("mani2.txt", O_RDONLY);
if (fd1 < 0) {
perror("c1");
exit(1);
}
printf("opened the fd = % d\n", fd1);
if (close(fd1) < 0) {
perror("c1");
exit(1);
}
printf("closed the fd.\n");
}
Output:

6.Name of the system call:lseek()


Description: lseek() system call repositions the read/write file offset i.e., it changes the
positions of the read/write pointer within the file. In every file any read or write operations
happen at the position pointed to by the pointer. lseek() system call helps us to manage the
position of this pointer within a file.

Syntax: off_t lseek(int fd, off_t offset, int whence);


Example with program:
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
int n,f;
char buff[10];
f=open("mani2.txt",O_RDWR);
lseek(f,4,SEEK_CUR);
read(f,buff,10);
write(1,buff,10);
}

Output:
7.Example Program:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
int fd; // File descriptor
// Create a new file named "sample.txt" with write permissions
fd = open("sample.txt", O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
perror("open");
exit(1);
}
// Write data to the file
write(fd, "Hello, World!\n", 14);
// Close the file
close(fd);
// Reopen the file in read-only mode
fd = open("sample.txt", O_RDONLY);
if (fd < 0) {
perror("open");
exit(1);
}
char buffer[20];
// Read data from the file
read(fd, buffer, 20);
printf("Read from file: %s", buffer);
// Move the file pointer to the beginning
lseek(fd, 0, SEEK_SET);
// Close the file
close(fd);
// Remove the file
if (unlink("sample.txt") != 0) {
perror("unlink");
exit(1);
}

return 0;
}

Output:
LAB-3
Aim: Implementation of fork(), wait(), exec() and exit() process system calls.
1.Name of the system call: fork()
Description:The use of the fork() system call is to create a new process by duplicating the
calling process. The fork() system call is made by the parent process, and if it is successful, a
child process is created. The fork() system call does not accept any parameters.

Syntax: pid_t fork();


Example Program:
#include <stdio.h>
#include <sys/types.h>;
#include <unistd.h>;
int main()
{
// make two process which run same
// program after this instruction
fork();
fork();
printf("Hello mani!\n");
return 0;
}

Output:

2.Name of the System call:Wait ( ):


Description :The parent process may then issue a wait system call, whic suspends the
execution of the parent process while the child executes. When the child process terminates,
it returns an exit status to the operating system, which is then returned to the waiting parent
process. The parent process then resumes execution.

Syntax: pid_t wait(int *stat_loc);


Example Program:
#include<stdio.h>
#include<stdlib.h>
#include<sys/wait.h>
#include<unistd.h>
int main()
{
pid_t cpid;
if (fork()== 0)
exit(0); /* terminate child */
else
cpid = wait(NULL); /* reaping parent */
printf("Parent pid = %d\n", getpid());
printf("Child pid = %d\n", cpid);
return 0;
}

Output:

3.Name of the System call:exec ( ):


Description :The exec family of functions replaces the current running process with a
new process. It can be used to run a C program by using another C program. It comes under
the header file unistd.h.
Syntax: int execvp (const char *file, char *const argv[]);
Example program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
(void) argc
printf("Here I am: %s's PID is %d\n", argv[0], getpid());
if (execl("print-pid", "print-pid", (char *)NULL) == -1)
exit(EXIT_FAILURE); /* execl failed */
printf("Here I go: %s's PID is %d\n", argv[0], getpid());
return EXIT_SUCCESS;
}

Output:

4.Name of the system call:Exit ( )


Description: The exit() is a system call that is used to end program execution. This call
indicates that the thread execution is complete, which is especially useful in multi-threaded
environments. The operating system reclaims resources spent by the process following the
use of the exit() system function.
Syntax:
Example Program:
#include <stdio.h>
#include <stdlib.h>
// Driver Code
int main(void)
{
printf("START");
exit(0);
// The program is terminated here
// This line is not printed
printf("End of program");
}

Output:
Lab-04
Aim:Implementation of FCFS,SJF scheduling algorithms.
1.Name of the algorithm:First Come First Serve(FCFS) without arrival time
Description: (FCFS), is the simplest scheduling algorithm. FIFO simply queues
processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only after
the previous gets fully executed.

Program:
#include <stdio.h>
int main() {
int bt[100], ex[100], seq[100], re[100] = {0}, wt[100], tat[100];
int n, i, k = 0, idle = 0;
float av1 = 0, av2 = 0;
printf("Enter number of processes\n");
scanf("%d", &n);
printf("Enter burst time for processes\n");
for (i = 0; i < n; i++) {
scanf("%d", &bt[i]);
}
int start = 0;
printf("Sequence of execution is\n");
for (i = 0; i < n; i++, k++) {
int pos = i + 1;
printf("[P%d] ", pos);
seq[k] = pos;
if (start < re[pos - 1]) {
idle += re[pos - 1] - start;
start = re[pos - 1];
}
re[pos - 1] = start;
start += bt[pos - 1];
ex[pos - 1] = start;
}
printf("\n");
for (i = 0; i < n; i++) {
tat[i] = ex[i];
wt[i] = re[i];
}
printf("Process Burst-time(ms) Waiting-time(ms) Turnaround-time(ms)\n");
for (i = 0; i < n; i++) {
printf("P%d %d %d %d\n", i + 1, bt[i], wt[i], tat[i]);
}
for (i = 0; i < n; i++) {
av1 += tat[i];
av2 += wt[i];
}
printf("Average waiting time(ms) %.2f\nAverage turnaround time(ms) %.2f\nCPU idle
time(ms) %d\n", av2 / n, av1 / n, idle);
return 0;
}

Output:
2.Name of the algorithm:First Come First Serve(FCFS) with arrival time
Description: (FCFS), is the simplest scheduling algorithm. FIFO simply queues
processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only after
the previous gets fully executed.

Program:
#include<stdio.h>
int main()
{
int at[10],at2[10],bt[100],ex[100],seq[100],re[100],wt[100],tat[100];
int n,i,j,start,pos,max=0,min,idle=0,k=0;
float av1=0,av2=0;
printf("Enter number of process\n");
scanf("%d",&n);
printf("Enter arrival time for processess\n");
for(i=0;i<n;i++)
{
scanf("%d",&at[i]);
at2[i]=at[i];
}
printf("Enter burst time for processess\n");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
start=at[0];
for(i=1;i<n;i++)
{
if(start>at[i])
{
start=at[i];
}
}
printf("Sequence of execution is\n");
for(i=0;i<n;i++)
{
if(max<at[i])
{
max=at[i];
}
}
max=max+1;
for(i=0;i<n;i++,k++)
{ min=max;
for(j=0;j<n;j++){
if(at[j]!=-1)
{
if(at[j]<min)
{
min=at[j];
pos=j;
}
}
}
printf("[P%d] ",pos);
seq[k]=pos;
if(start<at[pos]){
re[pos]=start;
idle+=at[pos]-start;
start=at[pos];
start+=bt[pos];
at[pos]=-1;
ex[pos]=start;
}
else{
re[pos]=start;
start+=bt[pos];
at[pos]=-1;
ex[pos]=start;
}
}
printf("\n");
for(i=0;i<n;i++)
{
tat[i]=ex[i]-at2[i];
wt[i]=tat[i]-bt[i];
}
printf("Process Arrival-time(ms) Burst-time(ms) Waiting-time(ms) Turnaround-
time(ms)\n");
for(i=0;i<n;i++)
{
printf("P%d %d %d %d %d\n",i,at2[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
av1+=tat[i];
av2+=wt[i];
}
printf("Average waiting time(ms) %f\nAverage turnaroundtime(ms) %f\nCPU idle
time(ms)%d\n",av2/n,av1/n,idle);
}
Output:

3.Name of the algorithm: Shortest Job First Scheduling with preemptive


Description: The shortest job first (SJF) or shortest job next, is a scheduling policy that
selects the waiting process with the smallest execution time to execute next

Program:
#include <stdio.h>
#include <limits.h>
// Structure to represent a process
struct Process {
int id;
int arrival_time;
int burst_time;
int remaining_time; // Remaining burst time
int completion_time; // Completion time
};
// Function to find the process with the shortest remaining time
int findShortestJob(struct Process processes[], int n, int currentTime) {
int shortestJob = -1;
int shortestTime = INT_MAX;
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= currentTime && processes[i].remaining_time <
shortestTime && processes[i].remaining_time > 0) {
shortestJob = i;
shortestTime = processes[i].remaining_time;
}
}
return shortestJob;
}
// Function to perform preemptive SJF scheduling
void sjfPreemptive(struct Process processes[], int n) {
int currentTime = 0; // Current time
int completed = 0; // Number of completed processes
int totalWaitingTime = 0; // Accumulator for waiting times
int totalTurnaroundTime = 0; // Accumulator for turnaround times
printf("Gantt Chart:\n");
while (completed < n) {
int shortestJob = findShortestJob(processes, n, currentTime);
if (shortestJob == -1) {
printf("IDLE (%d-%d) | ", currentTime, currentTime + 1);
currentTime++; // No process is ready, so wait
} else {
processes[shortestJob].remaining_time--;
printf("P%d (%d-%d) | ", processes[shortestJob].id, currentTime, currentTime + 1);
currentTime++;
if (processes[shortestJob].remaining_time == 0) {
completed++;
processes[shortestJob].completion_time = currentTime;
int turnaround_time = processes[shortestJob].completion_time -
processes[shortestJob].arrival_time;
int waiting_time = turnaround_time - processes[shortestJob].burst_time;
totalTurnaroundTime += turnaround_time;
totalWaitingTime += waiting_time;
printf("P%d completed (%d-%d). Turnaround Time: %d, Waiting Time: %d\n",
processes[shortestJob].id, processes[shortestJob].completion_time - 1,
processes[shortestJob].completion_time,
turnaround_time, waiting_time);
}
}
}
printf("\n");
// Calculate and print averages
printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);
printf("Average Waiting Time: %.2f\n", (float)totalWaitingTime / n);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
printf("Enter arrival time for process P%d: ", i + 1);
scanf("%d", &processes[i].arrival_time);
printf("Enter burst time for process P%d: ", i + 1);
scanf("%d", &processes[i].burst_time);
processes[i].remaining_time = processes[i].burst_time;
}
printf("\n");
sjfPreemptive(processes, n);
return 0;
}

Output:

4. Name of the algorithm: Shortest Job First Scheduling with non preemptive

Description: The shortest job first (SJF) or shortest job next, is a scheduling policy that
selects the waiting process with the smallest execution time to execute next

Progam:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("Enter Burst Time:");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("Process Burst Time Waiting Time Turnaround Time\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("p%d\t%d\t\t%d\t\t%d\n",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f",avg_tat);
}

Output:

You might also like