LAB MANUAL OF. UNIX & OS LAB (IT) ... communication using shared memory
system calls. 17-18. 13. Write a C ... O.S. PROGRAMMES. S.NO. PROGRAM ...
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE NUSTULAPUR, KARIMNAGAR
1
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
LAB MANUAL OF UNIX & OS LAB (IT) (III B.Tech. II SEM)
2
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
INDEX SNO
PROGRAM NAME
1
Write a shell script to print the multiplication table for the given number. Write a shell script that copies multiple files into a directory. Write a shell script to find no. of words and characters in a given file. Write a shell script to display all files in a given directory. Write a shell script to perform simple calculator. Write a C program to count no.of blanks,characters a) Using standard i/o function. b) Using system calls. Write a C program to illustrate the following Command using system Calls a) cat b) ls c) mv Write a C program to illustrate the stat system call. Write a C program that illustrates the creation of child process using fork() system call. Write a C program that illustrates file locking. Write a C program that implements producer consumer problem using semaphore system calls. Write a C program to illustrate inter process communication using shared memory system calls. Write a C program to (a) create message queue (b) write to message queue (c) read from message queue
2 3 4 5 6
7
8 9 10 11 12 13
3
PAGE NO 1 2 3 4 5 6-7
8-10
11 12 13-14 15-16 17-18 19-20
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
O.S. PROGRAMMES S.NO 1 2 3 4 5 6 7 8 9 10 11 12 13
PROGRAM NAME Write a C program for ROUND ROBIN CPU scheduling algorithm Write a C program for SJF CPU scheduling algorithm Write a C program for FCFS CPU scheduling algorithm Write a C Program for priority CPU scheduling algorithm. Write a C Program for sequential file allocation. Program for indexed file allocation strategy. Program for linked file allocation strategy. Write a C Program for MVT first fit. Write a C Program for MFT . Write a C Program for MVT best fit. Program for bankers algorithm Write a C program for FIFO page allocation algorithm. Write a C program for LRU page replacement algorithm.
4
PAGE NO 20-21 22-23 24 25-26 27-28 29-30 31-32 33 34 35-36 37-39 40-41 42-44
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
Program 1: AIM: Write a shell script to print the multiplication table for the given number. Algorithm: 1.Start. 2.Read n. 3.i=1. 4.f = n * i. 5.i = i + 1. 6.Display f. 7.Repeat 4,5,6 steps until i = 10. 8.End.
Source Code: #! /bin/sh echo “enter the number” read n for i in 1 2 3 4 5 6 7 8 9 10 do x= `expr $n \* $i` done
Input: enter the number 6
Output: 6*1=6 6 * 2 = 12
5
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60
Program 2: AIM: Write a shell script that copies multiple files into a directory. Algorithm: 1.Start. 2.Read *.c files into i. 3.Copy $i to the root directory. 4.Repeat 2,3 steps until all values of $i.. 5.End.
Source Code: #! /bin/sh for i in `ls *.c` do cp $i /root done echo “file are copied into root”
Output: file are copied into root”
Program 3 : AIM: Write a shell script to find no. of words and characters in a given file. Algorithm: 1. Start. 6
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 2. 3. 4. 5. 6. 7. 8. 9.
cc = 0,w=0. give file name as commandline argument (i.e $1). for i in `cat $1`. w = w + 1. c = expr “$i” : “*”. cc = cc + c. Repeat 4,5,6,7 steps until eof. End.
Source Code: # /bin/sh w=0 cc=0 for i in ‘cat $1’ do j= $i echo $j w=`expr $w + 1` c=`expr “$j”:”.*”` cc = `expr $cc + $c` done echo “no.of characters “ $cc echo “no.of words” $w
Input:. ./a.out sun
Output: no. of characters 56 no. of words 11
7
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
Program 4: AIM: Write a shell script to display all files in a given directory. Algorithm: 1.Start. 2.Read i value from ls sss. 3.Display i value . 4.Repeat above 2 steps until end of directory. 5.End.
Source Code: #! /bin/sh for i in `ls sss` do echo $i done
Output: abc.txt ss.c
8
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
Program 5: AIM: Write a shell script to perform simple calculator. Algorithm: 1.Start 2.Read a and b 3.Read op 4.Depending on value of operator perform Operation. 5.End
Source Code: #! /bin/sh echo ‘enter the value for a’ read a echo ‘enter the value for b’ read b
9
case
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE echo ‘enter operator’ read op case $op in +) c=`expr $a + $b`;; -) c = `expr $a -$b`;; \*) c = `expr $a \* $b`;; /) c = `expr $a / $b ;; esac echo $c
Input: enter the value for a 3 enter the value for b 6 enter the operator *
Output: 18
Program 6a: AIM: Write a C program to count no. of blanks, characters, lines using standard i/o function. Algorithm: 1. 2. 3. 4. 5. 6. 7. 8.
Start. open the file using fopen( ) function in “r” mode ch=fgetc(fp) (to read character by character) if ch = ‘ ‘ or ‘\n’ b=b+1. if ch = ‘\n’ l=l+1. c=c+1. Repeat 3,4,5&6 steps until ch = eof End
Source Code: #include int main( ) { 10
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE file *fp: int b=0,nl=0,c=0; char ch; fp=fopen(“text.txt”,”r”); while(ch!=eof) { ch=fgetc(fp); if(ch==’ ‘) b++; if(ch==’\n’) nl++; c++; } printf(“no.of blanks %d”,b); printf(“no.of lines %d”,nl); printf(“no.of characters %d”,c); }
Input: ./a.out sss.txt
Output: no.of blanks 5 n.of lines 2 no.of characters 36
Program 6b: AIM: Write a C program to count no. of blanks, characters, lines using system calls. Algorithm: 1. 2. 3. 4. 5. 6. 7. 8.
Start. open the file using open( ) system call in O_RDONLY rc=read(fd,buf,5) (to read characters ) if ch = ‘ ‘ or ‘\n’ w=w+1. if ch = ‘\n’ l=l+1. c=c+1. Repeat 3,4,5&6 steps until ch = eof End
11
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Source Code: #include int main( ) { int fd: int b=0,nl=0,c=0; char ch[1]; fd=open(“text.txt”,O_RDONLY); while((rc=read(fd,ch,1))>0) { if(ch==’ ‘) b++; if(ch==’\n’) nl++; c++; } printf(“no.of blanks %d”,b); printf(“no.of lines %d”,nl); printf(“no.of characters %d”,c); }
Input: ./a.out sss.txt
Output: no. of blanks 5 no. of lines 2 no. of characters 36
Program 7a: AIM: Write a C program to illustrate the mv command using system Calls Algorithm: 1. open one existed file and one new open file using open( ) system call 2. read the contents from keyboard using read( ) 3. write these contents into file using write() 4. repeat 2,3 steps until eof 5. close 2 file using fclose( ) system call 6. delete existed file using using unlink( ) system
12
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Source Code: #include #include #include int main() { int fd,fd1,rc; char ch[5]; fd1=open(“sss”,O_WRONLY | O_CREATE); while((rc=read(stdin,ch,5))>0) write(fd1,ch,rc); close fd1; fd1=open(“sss”,O_RDONLY); while((rc=read(fd1,ch,5))>0) write(stdout,ch,rc); close fd1; }
Input: hai hello A friend in need is a friend in deed ^z
Output: hai hello A friend in need is a friend in dee
Program 7b : AIM: Write a program to illustrate “ls” command using system calls Algorithm: 1. 2. 3. 4. 5. 6.
Start. open directory using opendir( ) system call. read the directory using readdir( ) system call. print dp.name and dp.inode . repeat above step until end of directory. End 13
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE Source Code: #include #include int main(int argc,char *argv[]) { dir *dp; struct dirent *d; if(((dp=opendir(“sss”)))==null) printf(“cannodt open\n”); while((d=readdir(dp)!=null)) { printf(“%s\t”,d->d_name); printf(“%d\n”,d->d_ino); } closedir(dirp); exit(0); }
output: mul.sh division.c process.c
12344 12345 12346
Program 7c: AIM: Write a C program to illustrate the mv command using system calls. Algorithm: 1. Start 2. open an existed file and one new open file using open() 14
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 3. 4. 5. 6. 7. 8.
system call read the contents from existed file using read( ) system call write these contents into new file using write system call using write( ) system call repeat above 2 steps until eof close 2 file using fclose( ) system call delete existed file using using unlink( ) system End.
Source Code: #include #include #include int main() { int fd,fd1,rc; char ch[5]; fd=open(“sss”,O_RDONLY ); fd1= open(“rrr”,O_WRONLY | O_CREATE); while((rc=read(fd,ch,5))>0) write(fd1,ch,rc); close fd1; close fd; unlink(fd); printf(“ file is copied “); }
Output: file is copied
Program 8: AIM: Write a C program to illustrate the stat system calls Algorithm: 1. Declare s and st of type struct stat. 2. Use stat system call by specifying sss.c file 15
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE 3. stat returns s (s may be 0 or -1) 4. if smtext,helloworld”); free(msg); printf(“message placed on queue successfully\n”); recv_msg=(struct msgbuf*)malloc(sizeof(struct msgbuf)+ strlen(“hello world”)); if(rc==-1) { perror(“main:msgrcv”); exit(1); } printf(“msgrcv:received message:\nmtype’%d’\n; mtext ’%s’\n”,recv_msg->mtype,recv_msg->mtext); return 0; }
Output: message queue created 0 message placed on the queue successfully msgrcv:received message: mtype: 0 mtext: hello world
25
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
LAB MANUAL OF
OPERATING SYSTEM LAB
26
JYOTHISHMATHI INSTITUTE OF TECHNOLOGY & SCIENCE
Program 1: AIM: Write a C program for ROUND ROBIN CPU scheduling algorithm. Source Code: #include main() { int s[10],p[10],n,i,j,wi=0,w[10],t[10], st[10],tq,tst=0; int tt=0,tw=0; float aw at; printf("enter no.of process"); scanf("%d",&n); printf("\n enter time quanum"); scanf("%d",&tq); printf("\n enter process&service time"); for(i=0;i