0% found this document useful (0 votes)
7 views33 pages

Os Lab Internal Questions (Autorecovered)-1

The document contains a series of internal questions and code snippets related to operating systems and programming concepts. It covers topics such as inter-process communication, scheduling algorithms (FCFS, SJF, SRTF, Round Robin, and Priority), string manipulation, file operations, and shell scripting. Each section includes example code in C or shell script to illustrate the concepts discussed.

Uploaded by

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

Os Lab Internal Questions (Autorecovered)-1

The document contains a series of internal questions and code snippets related to operating systems and programming concepts. It covers topics such as inter-process communication, scheduling algorithms (FCFS, SJF, SRTF, Round Robin, and Priority), string manipulation, file operations, and shell scripting. Each section includes example code in C or shell script to illustrate the concepts discussed.

Uploaded by

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

OS LAB INTERNAL QUESTIONS

1. Fibonacci using parent child communication

#include <stdio.h>
#include<unistd.h>
int main()
{
int fd[2],n1,n2;
pipe(fd);
if (fork()){
printf("n: ");
scanf("%d",&n1);
write(fd[1],&n1,sizeof(int));
}else{
read(fd[0],&n2,sizeof(int));
int a = 0,b = 1;
printf("%d %d ",a,b);
for(int i = 2;i<n2;i++){
int c = a+b;
a = b;
b = c;
printf("%d ",c);
}
}
}
2. FCFS Scheduling
#include <stdio.h>
#include<stdlib.h>
typedef struct{
int id,at,bt;
}proc;
int cmp(const void *a,const void *b){
return ((proc *)a)->at - ((proc *)b)->at;
}
int main(){
int n,ct = 0,st = 0;
printf("n: "); scanf("%d",&n);
proc p[n];
printf("ID AT BT\n");
for(int i = 0;i<n;i++) scanf("%d %d %d",&p[i].id,&p[i].at,&p[i].bt);
qsort(p,n,sizeof(proc),cmp);
printf("ID AT BT WT CT\n");
for(int i = 0;i<n;i++){
st = p[i].at>ct ? p[i].at:ct;
ct = st+p[i].bt;
printf("%d %d %d %d %d\n",p[i].id,p[i].at,p[i].bt,st-p[i].at,ct);
}
}
3. Reverse of a String

echo -n "Enter a string: "


read str
rev_str=$(echo "$str" | rev)
echo "Reversed string: $rev_str"
4. Implement a C program in which parent process accepts an integer and send it to
child process. Child process should generate prime number series considering the
integer received from parent as range and display it.

#include <stdio.h>
#include<unistd.h>
int main()
{
int fd[2],n1,n2;
pipe(fd);
if (fork()){
printf("n: ");
scanf("%d",&n1);
write(fd[1],&n1,sizeof(int));
}else{
read(fd[0],&n2,sizeof(int));
for(int i = 2;i<=n2;i++){
//checking prime
int flag = 1;
for(int j = 2;j<i;j++){
if(i%j == 0){
flag = 0;
break;
}
}
if(flag)printf("%d ",i);
}
}
}
5. SJF Scheduling

#include <stdio.h>
#include<stdlib.h>
typedef struct{
int id,at,bt;
}proc;
int cmpat(const void *a,const void *b){ return ((proc *)a)->at - ((proc *)b)->at; }
int cmpbt(const void *a,const void *b){ return ((proc *)a)->bt - ((proc *)b)->bt; }
void schedule(proc p[],int n){
int start = 0,end = 0,time = 0;
proc q;
printf("ID AT BT CT WT\n");
while(start<n){
qsort(p+start,n-start,sizeof(proc),cmpat);
if(time <p[start].at) time = p[start].at;
for(int s = start;s<n;s++){
if(p[s].at>time) break;
end = s;
}
qsort(p+start,end-start+1,sizeof(proc),cmpbt);
q = p[start++];
time += q.bt;
printf("%d %d %d %d %d\n",q.id,q.at,q.bt,time,time - q.at);
}
}
int main(){
int n;
printf("n: "); scanf("%d",&n);
proc p[n];
printf("ID AT BT\n");
for(int i = 0;i<n;i++) scanf("%d %d %d",&p[i].id,&p[i].at,&p[i].bt);
schedule(p,n);
}
6. shell program whether the given year is a leap year or not

echo -n "Year: "


read year
if [ $((year % 400)) -eq 0 ] || ( [ $((year % 4)) -eq 0 ] && [ $((year % 100)) -ne 0 ] )
then
echo "$year is a leap year."
else
echo "$year is not a leap year."
fi
7. implement Fibonacci series using child process

same as 1

8. SRTF SCHEDULING

#include <stdio.h>
#include<stdlib.h>
typedef struct{
int id,at,bt;
}proc;
int cmpat(const void *a,const void *b){ return ((proc *)a)->at - ((proc *)b)->at; }
int cmpbt(const void *a,const void *b){ return ((proc *)a)->bt - ((proc *)b)->bt; }
void schedule(proc p[],int n){
int start = 0,end = 0,time = 0;
printf("ID AT BT CT WT\n");
while(start<n){
qsort(p+start,n-start,sizeof(proc),cmpat);
if(time <p[start].at) time = p[start].at;
for(int s = start;s<n;s++){
if(p[s].at>time) break;
end = s;
}
qsort(p+start,end-start+1,sizeof(proc),cmpbt);
p[start].bt-=1;
time+=1;
printf("%d %d %d %d %d\n",p[start].id,p[start].at,p[start].bt,time,time -
p[start].at);
if(p[start].bt<1)start++;
}
}
int main(){
int n;
printf("n: "); scanf("%d",&n);
proc p[n];
printf("ID AT BT\n");
for(int i = 0;i<n;i++) scanf("%d %d %d",&p[i].id,&p[i].at,&p[i].bt);
schedule(p,n);
}

9. vowel and consonant count in string using script

echo -n "Enter a string: "


read str
vowels=0
consonants=0

for (( i=0; i<${#str}; i++ )); do


char="${str:i:1}"
if [[ "$char" =~ [aeiouAEIOU] ]]; then
((vowels++))
elif [[ "$char" =~ [a-zA-Z] ]]; then
((consonants++))
fi
done

echo "Vowels: $vowels"


echo "Consonants: $consonants"

10. inter process communication using message passing senario (client send lower case
or uppercase str to server, server change it case from lower case to upper and vice
versa client receives the message and print it

#include <stdio.h>
#include <ctype.h>// for isupper,tolower,toupper
#include <string.h>//for strlen
#include <unistd.h>
int main(){
int fd[2];
char s1[100],s2[100];
pipe(fd);
if(fork()){
printf("String: ");
scanf("%s",s1);
write(fd[1],s1,sizeof(char)*100);
}else{
read(fd[0],s2,sizeof(char)*100);
for(int i = 0;i<strlen(s2);i++){
if(isupper(s2[i])) printf("%c",tolower(s2[i]));
else printf("%c",toupper(s2[i]));
}
}
}

11. copy the contents of the file to another file

#include <stdio.h>
void main() {
FILE *src = fopen("source.txt", "r");
FILE *dest = fopen("destination.txt", "w");
if (!src || !dest) {
printf("Error opening files.\n");
return;
}
char ch;
while ((ch = fgetc(src)) != EOF) fputc(ch, dest);
fclose(src); fclose(dest);
printf("File copied successfully.\n");
}

12. inter process communication using message passing senario (client send lower case
or uppercase str to server, server change it case from lower case to upper and vice
versa client receives the message and print it

same as 10
13. execv system call

#include <stdio.h>
#include <unistd.h>
int main(int argc,char * argv[]){
execv("/bin/ls",argv);
}

14. sum and average with shell

echo -n "Enter numbers separated by spaces: "


read -a nums

sum=0
for num in "${nums[@]}"; do sum=$((sum + num)); done

avg=$((sum / ${#nums[@]}))

echo "Sum: $sum"


echo "Average: $avg"

15. Round Robin

#include <stdio.h>

typedef struct {
int id, at, bt, rt, ct, tat, wt;
} proc;
void schedule(proc p[], int n, int tq) {
int time = 0, done = 0;
printf("ID AT BT CT WT TAT\n");
while (done < n) {
int idle = 1;
for (int i = 0; i < n; i++) {
if (p[i].at <= time && p[i].rt > 0) {
idle = 0;
int run_time = (p[i].rt > tq) ? tq : p[i].rt;
p[i].rt -= run_time;
time += run_time;
if (p[i].rt == 0) {
p[i].ct = time;
p[i].tat = p[i].ct - p[i].at;
p[i].wt = p[i].tat - p[i].bt;
done++;
printf("%d %d %d %d %d %d\n", p[i].id, p[i].at, p[i].bt, p[i].ct, p[i].wt,
p[i].tat);
}
}
}
if (idle) time++;
}
}
int main() {
int n, tq;
printf("Enter N and Time Quantum:\n");
scanf("%d %d", &n, &tq);
proc p[n];
printf("Enter ID AT BT:\n");
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &p[i].id, &p[i].at, &p[i].bt);
p[i].rt = p[i].bt;
}
schedule(p, n, tq);
}
16. w processes parent and child parent shd create another child and child shd create
another child such that there are 4 active processes
#include <stdio.h>
#include <unistd.h>
void main(){
if(fork()){
printf("Process 1\n");
if(fork()){
printf("Process 2\n");
}
}else{
printf("Process 3\n");
if(fork()){
printf("Process 4\n");
}
}
}

17. priority scheduling

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int id, at, bt, pr;
} proc;
int cmpat(const void *a, const void *b) { return ((proc *)a)->at - ((proc *)b)->at; }
int cmppriority(const void *a, const void *b){return ((proc *)a)->pr -((proc *)b)->pr;}
void schedule(proc p[], int n) {
int time = 0, start = 0, end = 0;
printf("ID AT BT Priority CT WT\n");
while (start < n) {
qsort(p + start, n - start, sizeof(proc), cmpat);
if (time < p[start].at) {
time = p[start].at;
}
for (int s = start; s < n; s++) {
if (p[s].at > time) break;
end = s;
}
qsort(p + start, end - start + 1, sizeof(proc), cmppriority);
p[start].bt -= 1;
time += 1;
printf("%d %d %d %d %d %d\n", p[start].id, p[start].at, p[start].bt, p[start].pr,
time, time - p[start].at);
if (p[start].bt <= 0) start++;
}
}

int main() {
int n;
printf("n: ");
scanf("%d", &n);
proc p[n];
printf("ID AT BT PRIORITY\n");
for (int i = 0; i < n; i++) scanf("%d %d %d %d", &p[i].id, &p[i].at, &p[i].bt,
&p[i].pr);
schedule(p, n);
}

18. find no of vowels in a string shell prog

echo -n "Enter a string: "


read str
vowels=0

for (( i=0; i<${#str}; i++ )); do


char="${str:i:1}"
if [[ "$char" =~ [aeiouAEIOU] ]]; then
((vowels++))
fi
done

echo "Vowels: $vowels"

19. Display file flags

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc,char * argv[]){
if (argc!=2) printf("Wrong no of arguments");
else{
struct stat str;
stat(argv[1],&str);
printf("mode is %lu\n",str.st_mode);
printf("device type is %lu\n",str.st_dev);
printf("mode no is %lu\n",str.st_ino);
printf("no of links is %lu\n",str.st_nlink);
printf("user id is %lu\n",str.st_uid);
printf("group id is %lu\n",str.st_gid);
printf("device name is %lu\n",str.st_rdev);
printf("block size if %lu\n",str.st_blksize);
printf("no of blocks is %lu\n",str.st_blocks);
}
}

20. implement a c program read integers in parent process to sort and give sorted order
in child process

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int cmp(const void *a,const void *b){ return (*(int *)a)-(*(int *)b); }
int main(int argc,char * argv[]){
int n1,fd[2];
pipe(fd);
if(fork()){
printf("n: ");scanf("%d",&n1);
int a[n1];
printf("Elements: ");
for(int i = 0;i<n1;i++) scanf("%d",&a[i]);
write(fd[1],&n1,sizeof(int));
write(fd[1],a,sizeof(int)*n1);
}else{
int n2;
read(fd[0],&n2,sizeof(int));
int b[n2];
read(fd[0],b,sizeof(int)*n2);
qsort(b,n2,sizeof(int),cmp);
for(int i = 0;i<n2;i++) printf("%d ",b[i]);
}
}

21. Display types of files

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc,char * argv[]){
int i;
struct stat buf;
char * ptr;
for(i = 0;i<argc;i++){
printf("%s ",argv[i]);
if(stat(argv[i],&buf)<0){
printf("Error\n");
continue;
}
if (S_ISREG(buf.st_mode)) ptr = "regular\n";
else if(S_ISDIR(buf.st_mode)) ptr = "directory\n";
else if(S_ISCHR(buf.st_mode)) ptr = "character special\n";
else if(S_ISBLK(buf.st_mode)) ptr = "block special\n";
else if(S_ISFIFO(buf.st_mode)) ptr = "FIFO\n";
else ptr = "Unknown";
printf("%s\n",ptr);
}
}

22. sort the array in parent process and display the sorted array in childs process

same as 20
23. square and cube of a number in shell

echo -n "Enter a number:"


read a
as=$(($a*$a))
echo "Square = $as, cube = $(($as*$a))"

24. priority scheduling algorithm

same as 17

25. parent takes input a positive value M and wait till child process completes and
executes where child should return fibonacci of that Mth integer (which parent takes
input) .

#include <stdio.h>
#include<unistd.h>
int main()
{
int fd[2],n1,n2;
pipe(fd);
if (fork()){
printf("n: ");
scanf("%d",&n1);
write(fd[1],&n1,sizeof(int));
}else{
read(fd[0],&n2,sizeof(int));
int a = 0,b = 1,c;
for(int i = 2;i<n2;i++){
c = a+b;
a = b;
b = c;
}
printf("%d ",c);
}
}
26. Priority scheduling algorithm with preemption

Same as 17

27. Find whether the num is armstrong or not

echo -n "Enter a number:"


read a
len=$(echo $a|wc -c)
sum=0
og=$a
len=$(($len-1))
while [ $a -gt 0 ]
do
d=$(($a % 10))
pow=1
for ((i=0; i<len; i++))
do
pow=$(($pow * $d))
done
sum=$(($sum + $pow))
a=$(($a / 10))
done
if [ $og -eq $sum ]
then
echo "Armstrong"
else
echo "Not Armstong"
fi

28. Round robin scheduling

Same as 15

29. shell program on reverse the string

same as 3
30. on command line arguments perform sorting

#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a,const void *b){ return (*(int*)a) - (*(int*)b); }
int main(int argc,char* argv[]){
int n = argc-1;
int a[n];
for (int i = 0; i < n; i++) a[i] = atoi(argv[i + 1]);
qsort(a, n, sizeof(int), cmp);
for (int i = 0; i < n; i++) printf("%d ", a[i]);
}

31. Finding the prime numbers in the given range taking input in parent process and
generating prime numbers in child process.(Bidirectional pipes concept)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
int isprime(int n){
if (n<2) return 0;
for (int i=2;i<=sqrt(n);i++) if (n%i==0) return 0;
return 1;
}
int main(){
int p1[2],p2[2];
pipe(p1);
pipe(p2);
if(fork()){
int r;
printf("r: "); scanf("%d",&r);
write(p1[1],&r,sizeof(int));
int primes[r],n;
read(p2[0],&n,sizeof(int));
read(p2[0],primes,n*sizeof(int));
for(int i=0;i<n;i++) printf("%d ",primes[i]);
}else{
int r;
read(p1[0],&r,sizeof(int));
int primes[r],n=0;
for(int i=1;i<=r;i++) if(isprime(i)) primes[n++]=i;
write(p2[1],&n,sizeof(int));
write(p2[1],primes,n*sizeof(int));
}
}

32. Shell program to swap two numbers without using a temporary variable

read a
read b
a=$((a + b))
b=$((a - b))
a=$((a - b))
echo "After swap: a=$a b=$b"

33. Find the sum of array elements using bidirectional pipes concept

#include <stdio.h>
#include <unistd.h>
int main(){
int p1[2],p2[2];
pipe(p1);
pipe(p2);
if(fork()){
int n;
printf("n: "); scanf("%d",&n);
int a[n];
printf("Elements: ");
for(int i=0;i<n;i++) scanf("%d",&a[i]);
write(p1[1],&n,sizeof(int));
write(p1[1],a,n*sizeof(int));
int sum;
read(p2[0],&sum,sizeof(int));
printf("Sum = %d\n",sum);
}else{
int n;
read(p1[0],&n,sizeof(int));
int a[n],sum=0;
read(p1[0],a,n*sizeof(int));
for(int i=0;i<n;i++) sum+=a[i];
write(p2[1],&sum,sizeof(int));
}
}

34. Shell program to swap 2 numbers using temporary variable

read a
read b
c=$a
a=$b
b=$c
echo "After swap: a=$a b=$b"

35. sorting an array by taking input in parent and sorting in child process.(bidirectional
pipes concept)

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int cmp(const void *a,const void *b){ return (*(int*)a)-(*(int*)b); }

int main(){
int p1[2],p2[2];
pipe(p1);
pipe(p2);
if(fork()){
int n;
printf("n: "); scanf("%d",&n);
int a[n];
printf("Elements: ");
for(int i=0;i<n;i++) scanf("%d",&a[i]);
write(p1[1],&n,sizeof(int));
write(p1[1],a,n*sizeof(int));
read(p2[0],a,n*sizeof(int));
for(int i=0;i<n;i++) printf("%d ",a[i]);
}else{
int n;
read(p1[0],&n,sizeof(int));
int a[n];
read(p1[0],a,n*sizeof(int));
qsort(a,n,sizeof(int),cmp);
write(p2[1],a,n*sizeof(int));
}
}

36. shell program to check a number is even or odd

read a
if (( a % 2 == 0 ))
then
echo "even"
else
echo "odd"
fi

Internal 2:

37. Implement a c program in which main program accepts an integer array parent
process sorts an integer array and passes the sorted array to child process

#include <stdio.h>

#include<stdlib.h>

#include<unistd.h>
int cmp(const void *a,const void *b){ return (*(int*)a) - (*(int*)b); }

int main(){

int fd[2],n1,n2;

pipe(fd);

if (fork()){

printf("n: "); scanf("%d",&n1);

int a[n1];

printf("Elements: ");

for(int i = 0;i<n1;i++) scanf("%d",&a[i]);

qsort(a,n1,sizeof(int),cmp);

write(fd[1],&n1,sizeof(int));

write(fd[1],a,sizeof(int)*n1);

}else{

read(fd[0],&n2,sizeof(int));

int b[n2];

read(fd[0],b,sizeof(int)*n2);

for(int i = 0;i<n2;i++) printf("%d ",b[i]);

38. Implement a C program in which parent(main) program accepts an integer m as


input and passes this to child process ,where child process prints the mth Fibonacci
number using RECURSIVE function. Parent process should wait until child process
complete and exit.

#include <stdio.h>
#include<unistd.h>
int fib(int n){
if (n<=1) return n;
else return fib(n-1)+fib(n-2);
}
int main(){
int fd1[2],fd2[2],n1,n2,n3;
pipe(fd1);
pipe(fd2);
if (fork()){
printf("n: "); scanf("%d",&n1);
write(fd1[1],&n1,sizeof(int));
read(fd2[0],&n3,sizeof(int));
printf("%d",n3);
}else{
read(fd1[0],&n2,sizeof(int));
int a = fib(n2-1);
write(fd2[1],&a,sizeof(int));
}
}

39. Implement a C program to establish inter-process communication between client


and server using message queues. The server is specialized in converting characters
from lower case to upper case and vice versa. When the client receives the message
from the server, it prints it out.

Msg.h

#include <stdio.h>
#include<stdlib.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define MSG_SIZE 100
#define QUEUE_KEY 1234
typedef struct{
long mtype;
char mtxt[MSG_SIZE];
}msg_buffer;

Server.c

#include "msg.h"
#include <ctype.h>
#include <string.h>
int main() {
int msgid;
msg_buffer msg;
if ((msgid = msgget(QUEUE_KEY, 0666 | IPC_CREAT)) == -1) {
printf("Error\n");
exit(0);
}
printf("Message: ");
scanf("%s", msg.mtxt);
msg.mtype = 1;
for(int i = 0;i<strlen(msg.mtxt);i++){
if(isupper(msg.mtxt[i])) msg.mtxt[i] = tolower(msg.mtxt[i]);
else msg.mtxt[i] = toupper(msg.mtxt[i]);
}
msgsnd(msgid, &msg, sizeof(msg.mtxt), 0);
}

Client.c

#include "msg.h"
#include <string.h>
#include <ctype.h>

int main() {
int msgid;
msg_buffer msg;
if ((msgid = msgget(QUEUE_KEY, 0666 | IPC_CREAT)) == -1) {
printf("Error\n");
exit(0);
}
msgrcv(msgid, &msg, sizeof(msg.mtxt), 1, 0)
printf("%s",msg.mtxt);
}

40. write 2 programs,producer.c implementing producer and consumer.c implementing


consumer using shared memory buffer size 5.Your program should be with multiple
consumers and should be between 0 and 5, multiple consumers and one producer

producer.c
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<unistd.h>

#define SHMKEY 1234


#define SIZE 5

int main(){
int shmid = shmget(SHMKEY,SIZE*sizeof(int),IPC_CREAT|0666);
int * shm = (int*)shmat(shmid,NULL,0);
for(int i = 0;i<SIZE;i++) shm[i] = -1;
int item = 0;
while(1){
for(int i = 0;i<SIZE;i++){
if(shm[i] == -1){
shm[i] = item++;
printf("Produced %d at %d\n",shm[i],i);
break;
}
}
sleep(1);
}
}
Consumer.c
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<unistd.h>

#define SHMKEY 1234


#define SIZE 5

int main(){
int shmid = shmget(SHMKEY,SIZE*sizeof(int),0666);
int * shm = (int*)shmat(shmid,NULL,0);
while(1){
for(int i = 0;i<SIZE;i++){
if(shm[i] !=-1){
printf("Consumed: %d from %d\n",shm[i],i);
shm[i] = -1;
break;
}
}
usleep(500000); //0.5 seconds
}
}

For running

cc producer.c -o producer
cc consumer.c -o consumer
./consumer & ./consumer & ./consumer & ./consumer & ./consumer & ./producer
(5 consumers and 1 producer)

41. Implement producer consumer problem using semaphores and shared memory
(using shared memory – same as 40)

Smop.h
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define SEMKEY 99990L
#define PERMS 0666

static struct sembuf lock0[2] = { 0,0,0,0,1,0 };


static struct sembuf unlock0[1] = { 0,-1,IPC_NOWAIT };
static struct sembuf lock1[2] = { 1,0,0,1,1,0 };
static struct sembuf unlock1[1] = { 1,-1,IPC_NOWAIT };
static struct sembuf lock2[2] = { 2,0,0,2,1,0 };
static struct sembuf unlock2[1] = { 2,-1,IPC_NOWAIT };

int semlock0(int semid){ semop(semid,&lock0[0],2); }


int semunlock0(int semid){ semop(semid,&unlock0[0],1); }
int semlock1(int semid){ semop(semid,&lock1[0],2); }
int semunlock1(int semid){ semop(semid,&unlock1[0],1); }
int semlock2(int semid){ semop(semid,&lock2[0],2); }
int semunlock2(int semid){ semop(semid,&unlock2[0],1); }

producer.c

#include "smop.h"
int main(){
int semid = semget(SEMKEY,3,PERMS|IPC_CREAT);
int temp,flag = 1;
//have to initialize semaphore values (lekapothe raatle)
int first = 1;
if (first) {
for (int i = 0; i < 3; i++)semctl(semid, i, SETVAL, 0);
first = 0;
}
while (flag!=2){
printf("Enter 0 to start producing: "); scanf("%d",&temp);
semlock0(semid);
semlock1(semid);
printf("Producer Producing, Enter 1 to stop : "); scanf("%d",&temp);
semunlock2(semid);
semunlock0(semid);
printf("Producer Stopped Producing, Enter 2 to exit: ");
scanf("%d",&flag);
}
}

Consumer.c

#include "smop.h"
int main(){
int semid = semget(SEMKEY,3,PERMS|IPC_CREAT);
int temp,flag = 1;
while(flag!=2){
printf("Enter 0 to start Consuming: "); scanf("%d",&temp);
semlock0(semid);
semlock2(semid);
printf("Consumer Consuming, Enter 0 to stop: "); scanf("%d",&temp);
semunlock1(semid);
semunlock0(semid);
printf("Consumer stopped Consuming, Enter 2 to stop: ");
scanf("%d",&flag);
}
}

42. Write 2 programs that will both send and receive messages and construct the
following dialog between them:(Process 1) Sends the message “Are you hearing
me?”(Process 2) Receives the message and replies “Loud and Clear”(Process 1)
Receives the reply and then says, “I can hear you too.”

Msg.h

#include<stdio.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#define QUEUE_KEY 1234
#define MSG_SIZE 100
typedef struct{
long mtype;
char mtxt[MSG_SIZE];
}msg_buffer;

Process1.c
#include"msg.h"
int main(){
int msgid = msgget(QUEUE_KEY,0666|IPC_CREAT);
msg_buffer msg;

msg.mtype = 1;
strcpy(msg.mtxt, "ARE YOU HEARING ME");
msgsnd(msgid, &msg, sizeof(msg.mtxt), 0);
msgrcv(msgid, &msg, sizeof(msg.mtxt), 1, 0);
printf("%s\n", msg.mtxt);

msg.mtype = 1;
strcpy(msg.mtxt, "I CAN HEAR YOU TOO");
msgsnd(msgid, &msg, sizeof(msg.mtxt), 0);
msgrcv(msgid, &msg, sizeof(msg.mtxt), 1, 0);
printf("%s\n", msg.mtxt);
}
Process2.c

#include"msg.h"
int main(){
int msgid = msgget(QUEUE_KEY,0666|IPC_CREAT);
msg_buffer msg;
msgrcv(msgid,&msg,sizeof(msg.mtxt),1,0);
printf("%s\n",msg.mtxt);
strcpy(msg.mtxt, "LOUD AND CLEAR");
msgsnd(msgid,&msg,sizeof(msg.mtxt),0);
msgrcv(msgid,&msg,sizeof(msg.mtxt),1,0);
printf("%s",msg.mtxt);
}

43. Implement Message queue between Server and Client, client sends a filename to
server, server reads the filename and sends back the contents of the file to Client

Msg.h

#include<stdio.h>

#include<string.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#define QUEUE_KEY 1234

#define MSG_SIZE 100

typedef struct{

long mtype;

char mtxt[MSG_SIZE];

}msg_buffer;

server.c

#include"msg.h"

int main(){

int msgid = msgget(MSGKEY,0666|IPC_CREAT);

msg_buffer msg;

msgrcv(msgid,&msg,sizeof(msg.mtxt),1,0);

FILE *ptr = fopen(msg.mtxt,"r");

char text[MSGSIZE],ch;

int i = 0;
while((ch = fgetc(ptr))!=EOF) text[i++] = ch;

text[i] = '\0';

fclose(ptr);

strcpy(msg.mtxt,text);

msgsnd(msgid,&msg,sizeof(msg.mtxt),0);

Client.c

#include "msg.h"

int main(){

int msgid = msgget(MSGKEY,0666|IPC_CREAT);

msg_buffer msg;

msg.mtype= 1;

strcpy(msg.mtxt,"temp.txt");

msgsnd(msgid,&msg,sizeof(msg.mtxt),0);

msgrcv(msgid,&msg,sizeof(msg.mtxt),1,0);

printf("%s",msg.mtxt);

44. write a C program to implement reader writer problem using semaphores

semop.h

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define SEMKEY 99990L
#define PERMS 0666
static struct sembuf lock0[2] = { 0,0,0,0,1,0 };
static struct sembuf unlock0[1] = { 0,-1,IPC_NOWAIT };
static struct sembuf lock1[2] = { 1,0,0,1,1,0 };
static struct sembuf unlock1[1] = { 1,-1,IPC_NOWAIT };
static struct sembuf lock2[2] = { 2,0,0,2,1,0 };
static struct sembuf unlock2[1] = { 2,-1,IPC_NOWAIT };

int semlock0(int semid){ semop(semid,&lock0[0],2); }


int semunlock0(int semid){ semop(semid,&unlock0[0],1); }
int semlock1(int semid){ semop(semid,&lock1[0],2); }
int semunlock1(int semid){ semop(semid,&unlock1[0],1); }
int semlock2(int semid){ semop(semid,&lock2[0],2); }
int semunlock2(int semid){ semop(semid,&unlock2[0],1); }

reader.c

#include "semop.h"
int main() {
int semid, temp, flag = 1;
semid = semget(SEMKEY, 3, PERMS | IPC_CREAT);
//have to initialize semaphore values (lekapothe raatle)
int first = 1;
if (first) {
for (int i = 0; i < 3; i++)semctl(semid, i, SETVAL, 0);
first = 0;
}
while (flag != 2) {
printf("Reader waiting, Enter 0 to start reading: ");
scanf("%d", &temp);

semlock0(semid);
semlock1(semid);

printf("Reader is reading, Enter 1 to stop reading: ");


scanf("%d", &temp);

semunlock1(semid);
semunlock0(semid);
printf("Reader finished reading, Enter 2 to exit: ");
scanf("%d", &flag);
}
}

writer.c

#include "semop.h"
int main() {
int semid, temp, flag = 1;
semid = semget(SEMKEY, 3, PERMS | IPC_CREAT);

while (flag != 2) {
printf("Writer waiting, Enter 0 to start writing: ");
scanf("%d", &temp);
semlock0(semid);
semlock2(semid);
printf("Writer is writing, Enter 1 to stop writing: ");
scanf("%d", &temp);
semunlock0(semid);
semunlock2(semid);
printf("Writer finished writing, Enter 2 to exit: ");
scanf("%d", &flag);
}
}

45. Implement dining philosopher problem using semaphores

semop.h

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define SEMKEY 99990L
#define PERMS 0666

static struct sembuf lock0[2] = { 0,0,0,0,1,0 };


static struct sembuf unlock0[1] = { 0,-1,IPC_NOWAIT };
static struct sembuf lock1[2] = { 1,0,0,1,1,0 };
static struct sembuf unlock1[1] = { 1,-1,IPC_NOWAIT };
static struct sembuf lock2[2] = { 2,0,0,2,1,0 };
static struct sembuf unlock2[1] = { 2,-1,IPC_NOWAIT };
int semlock0(int semid){ semop(semid,&lock0[0],2); }
int semunlock0(int semid){ semop(semid,&unlock0[0],1); }
int semlock1(int semid){ semop(semid,&lock1[0],2); }
int semunlock1(int semid){ semop(semid,&unlock1[0],1); }
int semlock2(int semid){ semop(semid,&lock2[0],2); }
int semunlock2(int semid){ semop(semid,&unlock2[0],1); }

philosopher.c

#include "semop.h"
int main(){
int pno,semid,temp;
semid = semget(SEMKEY,3,PERMS|IPC_CREAT);
printf("Enter Philospher Number: "); scanf("%d",&pno);
switch(pno){
case 0:
printf("Philospher %d thinking, Enter 0 to start Eating ",pno);
scanf("%d",&temp);
semlock0(semid);
semlock1(semid);
printf("Philosopher %d is eating,Enter 1 to stop",pno);
scanf("%d",&temp);
semunlock1(semid);
semunlock0(semid);
printf("Philosopher %d thinking",pno);
break;
case 1:
printf("Philospher %d thinking, Enter 0 to start Eating ",pno);
scanf("%d",&temp);
semlock1(semid);
semlock2(semid);
printf("Philosopher %d is eating,Enter 1 to stop",pno);
scanf("%d",&temp);
semunlock2(semid);
semunlock1(semid);
printf("Philosopher %d thinking",pno);
break;
case 2:
printf("Philospher %d thinking, Enter 0 to start Eating ",pno);
scanf("%d",&temp);
semlock2(semid);
semlock0(semid);
printf("Philosopher %d is eating,Enter 1 to stop",pno);
scanf("%d",&temp);
semunlock0(semid);
semunlock2(semid);
printf("Philosopher %d thinking",pno);
break;
}
}

46. Implement Fifo page replacement algorithm

#include <stdio.h>
int main() {
int n, f, c = 0, faults = 0;
scanf("%d%d", &n, &f);
int pages[n], frames[f];
for (int i = 0; i < f; i++) frames[i] = -1;
for (int i = 0; i < n; i++) scanf("%d", &pages[i]);
for (int i = 0; i < n; i++) {
int found = 0;
for(int k = 0; k < f; k++)if(frames[k] == pages[i]) found = 1;
if (!found) {
frames[c++ % f] = pages[i];
faults++;
}
for (int k = 0; k < f; k++) printf("%d ", frames[k]);
printf("\n");
}
printf("Page Faults = %d\n", faults);
}

47. implement LRU page replacement algorithm.


#include <stdio.h>
int main() {
int n, f, pages[100], frames[10], recent[10], i, j, k, time = 0;
scanf("%d%d", &n, &f);
for (i = 0; i < f; i++) frames[i] = -1, recent[i] = -1;
for (i = 0; i < n; i++) scanf("%d", &pages[i]);
for (i = 0; i < n; i++, time++) {
int found = 0;
for (j = 0; j < f; j++) if (frames[j] == pages[i]) found = recent[j] = time;
if (!found) {
int lru = 0;
for (j=1;j<f;j++)if(recent[j]<recent[lru]) lru = j;
frames[lru] = pages[i];
recent[lru] = time;
}
for (k = 0; k < f; k++) printf("%d ", frames[k]);
printf("\n");
}
}

You might also like