KEMBAR78
Shell Script | PDF | Integer (Computer Science) | Computer Programming
0% found this document useful (0 votes)
6 views12 pages

Shell Script

The document provides a comprehensive guide on creating and executing shell scripts and C programs, including examples for UNIX commands, factorial calculations, CPU scheduling, file handling, semaphores, system calls, and threading. It includes step-by-step instructions for writing, saving, compiling, and running various scripts and programs. Additionally, it covers specific programming tasks such as checking for perfect squares, implementing the Banker's algorithm, and checking for prime numbers.

Uploaded by

s.rakesh2004
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)
6 views12 pages

Shell Script

The document provides a comprehensive guide on creating and executing shell scripts and C programs, including examples for UNIX commands, factorial calculations, CPU scheduling, file handling, semaphores, system calls, and threading. It includes step-by-step instructions for writing, saving, compiling, and running various scripts and programs. Additionally, it covers specific programming tasks such as checking for perfect squares, implementing the Banker's algorithm, and checking for prime numbers.

Uploaded by

s.rakesh2004
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/ 12

Shell Script

1. Create the script:


vi filename.sh
2. Press i → Insert your shell code
3. Press Esc, then type :wq → Save and quit
4. Run the script:
sh filename.sh

C Program
1. Create the file:
vi filename.c
2. Press i → Insert your C code
3. Press Esc, then type :wq → Save and quit
4. Compile:
cc filename.c
5. Run:
./a.out

1) UNIX Commands and Shell Programming


a. Display date in dd/mm/yyyy format (10 marks)
date +"%d/%m/%Y"
b. Shell script to generate factorial of 5 numbers (40 marks)
#!/bin/bash

factorial() {
num=$1
fact=1
for (( i=1; i<=num; i++ ))
do
fact=$((fact * i))
done
echo "Factorial of $num is $fact"
}

for (( i=1; i<=5; i++ ))


do
factorial $i
done
c. Calculate average waiting and turnaround time using Priority CPU Scheduling (Table
given with processes, arrival time, burst time, priority) (50 marks)
#include <stdio.h>

struct P {
int id, at, bt, pr, ct, tat, wt, done;
};

int main() {
struct P p[10];
int n, t = 0, c = 0;
float twt = 0, ttat = 0;
printf("Enter number of processes: ");
scanf("%d", &n);

for (int i = 0; i < n; i++) {


p[i].id = i;
printf("AT, BT, Priority for P%d: ", i);
scanf("%d%d%d", &p[i].at, &p[i].bt, &p[i].pr);
p[i].done = 0;
}

while (c < n) {
int idx = -1, pri = 9999;
for (int i = 0; i < n; i++)
if (!p[i].done && p[i].at <= t && p[i].pr < pri) {
pri = p[i].pr;
idx = i;
}

if (idx != -1) {
t = (t < p[idx].at) ? p[idx].at : t;
p[idx].ct = t + p[idx].bt;
p[idx].tat = p[idx].ct - p[idx].at;
p[idx].wt = p[idx].tat - p[idx].bt;
twt += p[idx].wt;
ttat += p[idx].tat;
t = p[idx].ct;
p[idx].done = 1;
c++;
} else t++;
}

printf("\nP\tAT\tBT\tPR\tCT\tTAT\tWT\n");
for (int i = 0; i < n; i++)
printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
p[i].id, p[i].at, p[i].bt, p[i].pr,
p[i].ct, p[i].tat, p[i].wt);
printf("\nAvg WT: %.2f\nAvg TAT: %.2f\n", twt / n, ttat / n);
return 0;
}

2) File Commands and Shell Programming


a. UNIX command to create and list file contents (10 marks)
Create a file named mydata.txt and add content:
echo "Welcome to UNIX file handling." > mydata.txt
List contents of the file:
cat mydata.txt
List file details:
ls -l mydata.txt

b. Shell program to find area of a square (40 marks)


Save this script as square_area_calculator.sh:
#!/bin/bash
# Script to calculate area of a square
echo "Enter the length of the side of the square:"
read side

area=$((side * side))

echo "Area of square with side $side = $area"


Run steps:
chmod +x square_area_calculator.sh
./square_area_calculator.sh

c. Implement Semaphore using C (50 marks)


Save this code in a file named simple_semaphore.c:
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

sem_t semaphore;

void* worker(void* arg) {


int thread_num = *(int*)arg;

printf("Thread %d is waiting to enter critical section...\n", thread_num);


sem_wait(&semaphore);
printf("Thread %d entered critical section.\n", thread_num);

sleep(2);

printf("Thread %d leaving critical section.\n", thread_num);


sem_post(&semaphore);

return NULL;
}

int main() {
pthread_t threads[3];
int thread_args[3];

sem_init(&semaphore, 0, 1);
for(int i = 0; i < 3; i++) {
thread_args[i] = i + 1;
pthread_create(&threads[i], NULL, worker, &thread_args[i]);
}

for(int i = 0; i < 3; i++) {


pthread_join(threads[i], NULL);
}

sem_destroy(&semaphore);
return 0;
}
Compile and run:
gcc simple_semaphore.c -pthread -o semaphore_demo
./semaphore_demo

3)System Calls and Advanced Shell Programming


a. Use getpid() system call (10 marks)
#include <stdio.h>
#include <unistd.h>

int main() {
pid_t pid;

pid = getpid(); // Get current process ID

printf("Current process ID: %d\n", pid);

return 0;
}
How to run:
Save as getpid_example.c
Compile: cc getpid_example.c -o getpid_example
Run: ./getpid_example

d. Shell program to check if a number is perfect square (40 marks)

#!/bin/bash

# Script to check if a number is a perfect square

echo "Enter a number:"

read num

if [ $num -lt 0 ]; then

echo "Negative numbers cannot be perfect squares."

exit 1

fi

# Calculate square root using bc (scale=0 to get integer part)

sqrt=$(echo "scale=0; sqrt($num)" | bc)

# Square the integer root

square=$((sqrt * sqrt))

if [ $square -eq $num ]; then

echo "$num is a perfect square."

else

echo "$num is NOT a perfect square."

fi
How to run:

Save as perfect_square.sh

Make executable: chmod +x perfect_square.sh

Run: ./perfect_square.sh
e. Implement Banker’s Algorithm using C (50 marks)
#include <stdio.h>
#include <stdbool.h>

#define P 5
#define R 3

int main() {
int i, j, k;
int available[R] = {3, 3, 2};
int max[P][R] = {
{7, 5, 3},
{3, 2, 2},
{9, 0, 2},
{2, 2, 2},
{4, 3, 3}
};
int allocation[P][R] = {
{0, 1, 0},
{2, 0, 0},
{3, 0, 2},
{2, 1, 1},
{0, 0, 2}
};
int need[P][R];
for(i=0; i<P; i++) {
for(j=0; j<R; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}

bool finish[P] = {false};


int safeSeq[P];
int count = 0;
int work[R];
for(i=0; i<R; i++) {
work[i] = available[i];
}

while(count < P) {
bool found = false;
for(i=0; i<P; i++) {
if(!finish[i]) {
bool canAllocate = true;
for(j=0; j<R; j++) {
if(need[i][j] > work[j]) {
canAllocate = false;
break;
}
}
if(canAllocate) {
for(k=0; k<R; k++) {
work[k] += allocation[i][k];
}
safeSeq[count++] = i;
finish[i] = true;
found = true;
}
}
}
if(!found) {
printf("System is NOT in safe state.\n");
return 0;
}
}

printf("System is in safe state.\nSafe sequence is: ");


for(i=0; i<P; i++) {
printf("%d ", safeSeq[i]);
}
printf("\n");

return 0;
}
How to run:
Save as bankers_algorithm.c
Compile: cc bankers_algorithm.c -o bankers_algorithm
Run: ./bankers_algorithm

4)Calendar, Prime Check, and Threading


a. Display calendar of a year using UNIX command (10 marks)
cal 2025
f. Shell script to check if a number is prime (40 marks)
#!/bin/bash
# Script to check if a number is prime
echo "Enter a positive integer:"
read num

if [ "$num" -le 1 ]; then


echo "$num is NOT a prime number."
exit 0
fi

for (( i=2; i*i<=num; i++ ))


do
if [ $((num % i)) -eq 0 ]; then
echo "$num is NOT a prime number."
exit 0
fi
done

echo "$num is a prime number."


To run:
chmod +x prime_check.sh
./prime_check.sh
g. Implement threading using C (50 marks)
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void* thread_function(void* arg) {


char* message = (char*)arg;
for(int i = 0; i < 5; i++) {
printf("%s: iteration %d\n", message, i+1);
sleep(1);
}
return NULL;
}

int main() {
pthread_t thread1, thread2;
const char* msg1 = "Thread 1";
const char* msg2 = "Thread 2";

// Create threads
pthread_create(&thread1, NULL, thread_function, (void*)msg1);
pthread_create(&thread2, NULL, thread_function, (void*)msg2);

// Wait for threads to finish


pthread_join(thread1, NULL);
pthread_join(thread2, NULL);

printf("Both threads completed.\n");

return 0;
}
To compile and run:
cc threading_example.c -pthread -o threading_example
./threading_example

You might also like