0% found this document useful (0 votes)
34 views7 pages

Experiment 2

This document describes an experiment on shell programming and system calls. It includes the aim, expected outcomes, books and websites referred. It provides details on shell scripts, including how to create one and add execute permission. It lists 5 programs to implement using shell scripts, including comparing file contents and finding factorial of a number. It also includes a C program to create a child process using fork(), have the child process read and print a file, and the parent process wait for the child to complete. Screenshots of sample shell scripts and program output are provided. The conclusion is that shell scripts and the fork system call were successfully implemented.

Uploaded by

Shruti Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

Experiment 2

This document describes an experiment on shell programming and system calls. It includes the aim, expected outcomes, books and websites referred. It provides details on shell scripts, including how to create one and add execute permission. It lists 5 programs to implement using shell scripts, including comparing file contents and finding factorial of a number. It also includes a C program to create a child process using fork(), have the child process read and print a file, and the parent process wait for the child to complete. Screenshots of sample shell scripts and program output are provided. The conclusion is that shell scripts and the fork system call were successfully implemented.

Uploaded by

Shruti Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

K. J.

Somaiya College of Engineering, Mumbai-77


(A Constituent College of Somaiya Vidyavihar University)
Department of Computer Engineering

Batch: B4 Roll No.: 1911119

Experiment No. 02

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

TITLE: Shell Programming and system calls


_____________________________________________________________________

AIM: To study the shell script and write the program using shell.
______________________________________________________________________
Expected Outcome of Experiment:

CO 1. To introduce basic concepts and functions of operating systems.

_____________________________________________________________________
Books/ Journals/ Websites referred:

1. Silberschatz A., Galvin P., Gagne G. “Operating Systems Principles”,


Willey Eight edition.
2. William Stallings “Operating Systems” Person, Seventh Edition
Edition.
3. Sumitabha Das “ UNIX Concepts & Applications”, McGraw Hill Second
Edition.
_____________________________________________________________________
Pre Lab/ Prior Concepts:

The shell provides you with an interface to the UNIX system. It gathers input from you
and executes programs based on that input. When a program finishes executing, it
displays that program's output.

Shell Scripts
The basic concept of a shell script is a list of commands, which are listed in the order of
execution. A good shell script will have comments, preceded by a pound sign, #,
describing the steps.

Department of Computer Engineering

Page No OSSS Sem V / July.- Nov 2021


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Computer Engineering

Steps to create a Shell Script:


create a file using any text editor say vi, gedit, nano etc
1.$ vi filename

2.Insert the script/ commands in file and save the file to execute the file we need to give
execute permission to the file
3.$ chmod 775 filename
4.Now execute the above file using any of following methods:
$ sh filename
OR
$ ./filename

NOTE: Before adding anything to your script, you need to alert the system that a shell
script is being started. This is done using the shebang construct. For example −
#!/bin/sh.
______________________________________________________________________
Description of the application to be implemented:
1. Write a shell Script that accepts two file names as command line arguments and
compare two file contents and check whether contents are same or not. If they are
same, then delete second file.

2. Write a shell script that accepts integer and find the factorial of number.

3. Write a shell script for adding users.

4. Write a shell script for counting no of logged in users.

5. Write a shell script for counting no of processes running on system

Program for System Call:

1. Write a Program for creating process using System call (E.g fork())
Create a child process. Display the details about that process using
getpid and getppid functions. In a child process, Open the file using file
system calls and read the contents and display.

Department of Computer Engineering

Page No OSSS Sem V / July.- Nov 2021


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Computer Engineering

Implementation details: (printout of code / screen shot)

1. compare.sh

if cmp -s "$1" "$2" ; then

rm -rf "$2"

echo "Both files have same contents."

else

echo "Both files have different contents."

fi

file1.txt

Hello world!

file2.txt

Hello world!

Output:

Department of Computer Engineering

Page No OSSS Sem V / July.- Nov 2021


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Computer Engineering

2. factorial.sh

echo "Enter a number :"


read num

fact=1

for((i=2;i<=num;i++))
{
fact=$((fact*i))
}

echo "Factorial of $num = $fact"

Output:

3. Adding users

Department of Computer Engineering

Page No OSSS Sem V / July.- Nov 2021


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Computer Engineering

4. Number of logged in users

5. Number of processes

echo "The number of processes running on the system :"


ps aux | wc -l

Output:

System Call:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void) {
pid_t process_id = fork();

if(process_id == 0)
{
printf("\nChild process:\n");

Department of Computer Engineering

Page No OSSS Sem V / July.- Nov 2021


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Computer Engineering

printf("PPID (Parent process id) : %d\n",getppid()); printf("PID (Process


id) : %d\n\n",getpid()); printf("Return value of fork:%d\n",process_id);
FILE *file_ptr;
file_ptr = fopen("File.txt", "r");
if (file_ptr == NULL)
{
printf("Cannot open given file \n");
exit(0);
}
printf("Contents of file.txt:\n");

char ch = fgetc(file_ptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(file_ptr);
}
fclose(file_ptr);
printf ("\n");
exit(1);
}

else if(process_id > 0)


{
printf("\nParent process:\n");
printf("PID (Process id) : %d\n",getpid()); printf("PPID (Parent process
id) : %d\n",getppid()); printf("Return value of fork:%d\n",process_id);
wait(NULL);

printf("Child process completed.\n");


}

else {
printf("Cannot create child process\n");
}

Department of Computer Engineering

Page No OSSS Sem V / July.- Nov 2021


K. J. Somaiya College of Engineering, Mumbai-77
(A Constituent College of Somaiya Vidyavihar University)
Department of Computer Engineering

Output:

Conclusion :

Learned and successfully implemented shell scripts and system call fork().

Post Lab Descriptive Questions

1) Explain in brief: Meta Characters, Positional Parameters and command


substitution with example.
Ans. Special characters, or metacharacters, have a special meaning to the shell.
They can be used as wildcards to specify the name of a file without having to type
out the file's full name. Some of the most commonly used metacharacters are "*",
"?", "[]", and "-". A positional parameter is a variable within a shell program; its
value is set from an argument specified on the command line that invokes the
program.
In computing, command substitution is a facility that allows a command to be run
and its output to be pasted back on the command line as arguments to another
command.

Date: 15/09/2021 Signature of faculty in-charge

Department of Computer Engineering

Page No OSSS Sem V / July.- Nov 2021

You might also like