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

Ios Practical Answers - Durga

The document provides practical answers for various programming tasks in UNIX and C, including commands for counting lines, words, and characters in a file, creating processes, renaming files, calculating the area and circumference of a circle, and implementing disk scheduling strategies. It also includes shell scripts for converting Celsius to Fahrenheit and displaying student grades, along with sample viva questions related to the topics covered. Additionally, it highlights the use of system calls, threading in C, and the importance of handling input validation in scripts.

Uploaded by

shreedurga034
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)
12 views7 pages

Ios Practical Answers - Durga

The document provides practical answers for various programming tasks in UNIX and C, including commands for counting lines, words, and characters in a file, creating processes, renaming files, calculating the area and circumference of a circle, and implementing disk scheduling strategies. It also includes shell scripts for converting Celsius to Fahrenheit and displaying student grades, along with sample viva questions related to the topics covered. Additionally, it highlights the use of system calls, threading in C, and the importance of handling input validation in scripts.

Uploaded by

shreedurga034
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

Ios practical answers

1)a)List out the number of lines, characters and words present in a file using the necessary UNIX

command. (15)

You can use the wc (word count) command in UNIX:

wc filename

output:

<lines> <words> <characters> filename

Example:

wc myfile.txt

If you want individual counts:

wc -l filename # lines

wc -w filename # words

wc -m filename # characters

1)b) Create a process and print the process id of current process and its parent process. (15)

#include <stdio.h>

#include <unistd.h>

int main() {

pid_t pid = getpid();

pid_t ppid = getppid();

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

printf("Parent Process ID: %d\n", ppid);

return 0;

}
3)a) Using the desired UNIX command rename a file of the user to a new name. (15)

Use the mv (move) command:

mv old_filename new_filename

Example:

mv report.txt summary.txt

This renames report.txt to summary.txt.

3)b) Write a Shell program to find the area and circumference of a circle. (15)

#!/bin/bash

echo -n "Enter radius: "

read r

area=$(echo "3.14 * $r * $r" | bc)

circ=$(echo "2 * 3.14 * $r" | bc)

echo "Area: $area"

echo "Circumference: $circ"

4)a) Write a Shell program to display student grades. (30)

#!/bin/bash

echo -n "Enter student marks (0–100): "

read marks

if [ $marks -ge 90 ]; then

grade="A+"

elif [ $marks -ge 80 ]; then

grade="A"

elif [ $marks -ge 70 ]; then

grade="B"

elif [ $marks -ge 60 ]; then

grade="C"
elif [ $marks -ge 50 ]; then

grade="D"

else

grade="F"

fi

echo "Grade: $grade"

7) Write a C program for implementing FCFS disk scheduling strategy. (100)

#include <stdio.h>

#include <stdlib.h>

int main() {

int n, i, head, seek=0;

printf("Enter number of requests: ");

scanf("%d", &n);

int requests[n];

printf("Enter requests sequence:\n");

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

scanf("%d", &requests[i]);

printf("Enter initial head position: ");

scanf("%d", &head);

printf("Seek sequence: %d ", head);

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

seek += abs(requests[i] - head);

head = requests[i];

printf("%d ", head);

printf("\nTotal seek time = %d\n", seek);

return 0;

}
8) Write a C program to implement SSTF disk scheduling strategy.

#include <stdio.h>

#include <stdlib.h>

int main() {

int n, head, seek=0, done=0;

scanf("%d", &n);

int r[n], v[n];

for(int i=0;i<n;i++) { scanf("%d",&r[i]); v[i]=0; }

scanf("%d",&head);

while(done<n) {

int md=1e9, idx=-1;

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

if(!v[i] && abs(head-r[i])<md) { md=abs(head-r[i]); idx=i; }

seek+=md; head=r[idx]; v[idx]=1; done++;

printf("%d\n",seek);

10)a) Illustrate the following System Calls: Getpid() (10)

#include <stdio.h>

#include <unistd.h>

int main() {

pid_t pid = getpid();

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

return 0;

10)b) Develop a Shell program to convert Celsius to Fahrenheit (40)


#!/bin/bash

read -p "Enter temperature in Celsius: " c

f=$(( (c * 9 / 5) + 32 ))

echo "$c Celsius = $f Fahrenheit"

10)c) Implement Threading using C. (50)

#include <stdio.h>

#include <pthread.h>

void* f(void* a){ printf("Hello from thread\n"); return NULL; }

int main() {

pthread_t t;

pthread_create(&t, NULL, f, NULL);

pthread_join(t, NULL);

Additional viva questions(which maybe asked based on the


program given )
1. Q: What does the wc command do in UNIX?
A: The wc (word count) command is used to count the number of lines, words, and
characters in a file.

2. Q: How do you count only the number of words in a file using wc?
A: Use the command wc -w filename.

3. Q: What is the difference between getpid() and getppid() in C?


A: getpid() returns the ID of the current process, while getppid() returns the ID of the
parent process.

4. Q: What does the mv command do in UNIX?


A: The mv command is used to move or rename files and directories.
5. Q: What is the purpose of bc in shell scripting?
A: bc is used for performing floating-point arithmetic in shell scripts.

6. Q: How do you calculate the area of a circle in a shell script?


A: By using the formula π * r * r, implemented with bc as echo "3.14 * $r * $r" | bc.

7. Q: What does the FCFS disk scheduling algorithm do?


A: FCFS (First-Come-First-Serve) processes disk requests in the order they arrive.

8. Q: Why is FCFS not always the best disk scheduling strategy?


A: Because it can result in high total seek time and inefficient disk movement.

9. Q: What does SSTF stand for and how does it work?


A: SSTF stands for Shortest Seek Time First. It selects the disk request closest to the current
head position.

10. Q: What is a major drawback of SSTF?


A: It can lead to starvation for distant requests.

11. Q: How is the shortest seek time selected in SSTF?


A: By calculating the absolute distance between current head position and all pending
requests, and choosing the smallest.

12. Q: What is the output of getpid() in C?


A: It returns the process ID of the program currently being executed.

13. Q: What is the formula for converting Celsius to Fahrenheit?


A: The formula is (C * 9 / 5) + 32.

14. Q: How would you make the temperature conversion script handle decimal inputs?
A: Use bc with scale, like echo "scale=2; ($c * 9 / 5) + 32" | bc.
15. Q: What is a thread in C?
A: A thread is a lightweight process used to perform multiple tasks concurrently within a
program.

16. Q: What does pthread_create do?


A: It creates a new thread and starts executing the specified function in parallel with the
main thread.

17. Q: What happens if you don’t use pthread_join?


A: The main thread may terminate before the created thread finishes its execution.

18. Q: Can multiple threads share the same global variable?


A: Yes, threads share the same memory space and can access global variables.

19. Q: What is the use of elif in a shell script for grading?


A: elif allows checking multiple conditions sequentially if the previous if condition fails.

20. Q: How can you prevent invalid marks like over 100 in a grading script?
A: Add a condition like if [ $marks -le 100 ] to ensure input is within the valid range.

ALL THE ANSWERS AND THE SAMPLE VIVA QUESTIONS WERE GENERATED BY CHAT GPT SO
EDHAVADHU THAVARUGAL IRUNDHAL NAN PORUPU ALLA .

You might also like