OS Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 63

General commands

Command Function
Date Used to display the current system date and time.
date +%D Displays date only
date +%T Displays time only
date +% Y Displays the year part of date
date +% H Displays the hour part of time
Cal Calendar of the current month
cal year Displays calendar for all months of the specified year
cal month year Displays calendar for the specified month of the year
Who Login details of all users such as their IP, Terminal No, User name,
who am i Used to display the login details of the user
Uname Displays the Operating System
uname –r Shows version number of the OS (kernel).
uname –n Displays domain name of the server
echo $HOME Displays the user's home directory
Bc Basic calculator. Press Ctrl+d to quit
lp file Allows the user to spool a job along with others in a print queue.
man cmdname Manual for the given command. Press q to exit
history To display the commands used by the user since log on.
exit Exit from a process. If shell is the only process then logs out

Directory commands

Command Function
Pwd Path of the present working directory
mkdir dir A directory is created in the given name under the current directory
mkdir dir1 dir2 A number of sub-directories can be created under one stroke
cd subdir Change Directory. If the subdir starts with / then path starts from
root (absolute) otherwise from current working directory.
cd To switch to the home directory.
cd / To switch to the root directory.
cd .. To move back to the parent directory
rmdir subdir Removes an empty sub-directory.

File commands

Command Function
cat > filename To create a file with some contents. To end typing press Ctrl+d.
The > symbol means redirecting output to a file. (< for input)
cat filename Displays the file contents.
cat >> filename Used to append contents to a file
cp src des Copy files to given location. If already exists, it will be overwritten
cp –i src des Warns the user prior to overwriting the destination file
cp –r src des Copies the entire directory, all its sub-directories and files.
mv old new To rename an existing file or directory. –i option can also be used
mv f1 f2 f3 dir To move a group of files to a directory.
mv –v old new Display name of each file as it is moved.
rm file Used to delete a file or group of files. –i option can also be used
rm * To delete all the files in the directory.
rm –r * Deletes all files and sub-directories
rm –f * To forcibly remove even write-protected files
Ls Lists all files and subdirectories (blue colored) in sorted manner.
ls name To check whether a file or directory exists.
ls name* Short-hand notation to list out filenames of a specific pattern.
ls –a Lists all files including hidden files (files beginning with .)
ls –x dirname To have specific listing of a directory.
ls –R Recursive listing of all files in the subdirectories
ls –l Long listing showing file access rights (read/write/execute-rwx for
user/group/others-ugo).
cmp file1 file2 Used to compare two files. Displays nothing if files are identical.
wc file It produces a statistics of lines (l), words(w), and characters(c).
chmod perm file Changes permission for the specified file. (r=4, w=2, x=1)
chmod 740 file sets all rights for user, read only for groups
and no rights for others

The commands can be combined using the pipeline (|) operator. For example, number ofusers
logged in can be obtained as.

who | wc -l

Finally to terminate the unix session execute the command exit or logout.
PROGRAM

/* Process creation - fork.c */

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

main()
{
pid_t pid; int x
= 5; pid =
fork();x++;

if (pid < 0)
{
printf("Process creation error");
exit(-1);
}
else if (pid == 0)
{
printf("Child process:"); printf("\nProcess
id is %d", getpid());printf("\nValue of x
is %d", x);
printf("\nProcess id of parent is %d\n", getppid());
}
else
{
printf("\nParent process:");
printf("\nProcess id is %d", getpid());
printf("\nValue of x is %d", x);
printf("\nProcess id of shell is %d\n", getppid());
}
}
PROGRAM

/* Wait for child termination - wait.c */

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

main()
{
int i, status;pid_t
pid;

pid = fork();
if (pid < 0)
{
printf("\nProcess creation failure\n");
exit(-1);
}
else if(pid > 0)
{
wait(NULL);
printf ("\nParent starts\nEven Nos: ");for
(i=2;i<=10;i+=2)
printf ("%3d",i);
printf ("\nParent ends\n");
}
else if (pid == 0)
{
printf ("Child starts\nOdd Nos: ");for
(i=1;i<10;i+=2)
printf ("%3d",i); printf
("\nChild ends\n");
}
}
PROGRAM

/* Load a program in child process - exec.c */

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

main()
{
pid_t pid;

switch(pid = fork())
{
case -1:
perror("Fork failed");
exit(-1);

case 0:
printf("Child process\n");
execl("/bin/date", "date", 0);
exit(0);

default:
wait(NULL);
printf("Child Terminated\n");
exit(0);
}
}
PROGRAM
/* File status - stat.c */
#include <stdio.h> #include
<sys/stat.h> #include
<stdlib.h> #include <time.h>

int main(int argc, char*argv[])


{
struct stat file; int n;
if (argc != 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}
if ((n = stat(argv[1], &file)) == -1)
{
perror(argv[1]);
exit(-1);
}
printf("User id : %d\n", file.st_uid); printf("Group id
: %d\n", file.st_gid); printf("Block size : %d\n",
file.st_blksize); printf("Blocks allocated : %d\n",
file.st_blocks);printf("Inode no. : %d\n",
file.st_ino);
printf("Last accessed : %s", ctime(&(file.st_atime)));
printf("Last modified : %s", ctime(&(file.st_mtime)));
printf("File size : %d bytes\n", file.st_size); printf("No.
of links : %d\n", file.st_nlink);

printf("Permissions : ");
printf( (S_ISDIR(file.st_mode)) ? "d" : "-");
printf( (file.st_mode & S_IRUSR) ? "r" : "-");
printf( (file.st_mode & S_IWUSR) ? "w" : "-");
printf( (file.st_mode & S_IXUSR) ? "x" : "-");
printf( (file.st_mode & S_IRGRP) ? "r" : "-");
printf( (file.st_mode & S_IWGRP) ? "w" : "-");
printf( (file.st_mode & S_IXGRP) ? "x" : "-");
printf( (file.st_mode & S_IROTH) ? "r" : "-");
printf( (file.st_mode & S_IWOTH) ? "w" : "-");
printf( (file.st_mode & S_IXOTH) ? "x" : "-");printf("\n");

if(file.st_mode & S_IFREG) printf("File


type : Regular\n");
if(file.st_mode & S_IFDIR) printf("File
type : Directory\n");
}
PROGRAM
/* Directory content listing - dirlist.c */

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

main(int argc, char *argv[])


{
struct dirent *dptr;DIR
*dname;

if (argc != 2)
{
printf("Usage: ./a.out <dirname>\n");
exit(-1);
}

if((dname = opendir(argv[1])) == NULL)


{
perror(argv[1]);
exit(-1);
}

while(dptr=readdir(dname)) printf("%s\n",
dptr->d_name);

closedir(dname);
}
PROGRAM

/* File creation - fcreate.c */

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

main(int argc, char *argv[])


{
int fd, n, len;char
buf[100];
if (argc != 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}

fd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);


if(fd < 0)
{
printf("File creation problem\n");
exit(-1);
}

printf("Press Ctrl+D at end in a new line:\n");while((n


= read(0, buf, sizeof(buf))) > 0)
{
len = strlen(buf);
write(fd, buf, len);
}
close(fd);
}
PROGRAM

/* File Read - fread.c */

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

main(int argc, char *argv[])


{
int fd,i;
char buf[100];if
(argc < 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}

fd = open(argv[1], O_RDONLY);
if(fd == -1)
{
printf("%s file does not exist\n", argv[1]);exit(-1);
}

printf("Contents of the file %s is : \n", argv[1]);


while(read(fd, buf, sizeof(buf)) > 0)
printf("%s", buf);

close(fd);
}
PROGRAM

/* File append - fappend.c */

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

main(int argc, char *argv[])


{
int fd, n, len;char
buf[100];

if (argc != 2)
{
printf("Usage: ./a.out <filename>\n");
exit(-1);
}
fd = open(argv[1], O_APPEND|O_WRONLY|O_CREAT, 0644);if
(fd < 0)
{
perror(argv[1]);
exit(-1);
}

while((n = read(0, buf, sizeof(buf))) > 0)


{
len = strlen(buf);
write(fd, buf, len);
}

close(fd);
}
PROGRAM

/* ls command simulation - list.c */

#include <stdio.h>
#include <dirent.h>

main()
{
struct dirent **namelist;int
n,i;
char pathname[100];

getcwd(pathname);
n = scandir(pathname, &namelist, 0, alphasort);

if(n < 0)
printf("Error\n");else
for(i=0; i<n; i++)
if(namelist[i]->d_name[0] != '.') printf("%-
20s", namelist[i]->d_name);
}
PROGRAM

/* grep command simulation - mygrep.c */

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

main(int argc,char *argv[])


{
FILE *fd;
char str[100];char
c;
int i, flag, j, m, k;char
temp[30]; if(argc != 3)
{
printf("Usage: gcc mygrep.c –o mygrep\n"); printf("Usage:
./mygrep <search_text> <filename>\n");exit(-1);
}

fd = fopen(argv[2],"r");
if(fd == NULL)
{
printf("%s is not exist\n",argv[2]);
exit(-1);
}

while(!feof(fd))
{
i = 0;
while(1)
{
c = fgetc(fd);if(feof(fd))
{
str[i++] = '\0';break;
}
if(c == '\n')
{
str[i++] = '\0';break;
}
str[i++] = c;
}
if(strlen(str) >= strlen(argv[1]))
for(k=0; k<=strlen(str)-strlen(argv[1]); k++)
{
for(m=0; m<strlen(argv[1]); m++)
temp[m] = str[k+m];
temp[m] = '\0';
if(strcmp(temp,argv[1]) == 0)
{
printf("%s\n",str);
break;
}
}
}
}
PROGRAM

/* cp command simulation - copy.c */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>

#define SIZE 1024

main(int argc, char *argv[])


{
int src, dst, nread;char
buf[SIZE];

if (argc != 3)
{
printf("Usage: gcc copy.c -o copy\n"); printf("Usage:
./copy <filename> <newfile> \n");exit(-1);
}
if ((src = open(argv[1], O_RDONLY)) == -1)
{
perror(argv[1]);
exit(-1);
}

if ((dst = creat(argv[2], 0644)) == -1)


{
perror(argv[1]);
exit(-1);
}

while ((nread = read(src, buf, SIZE)) > 0)


{
if (write(dst, buf, nread) == -1)
{
printf("can't write\n");
exit(-1);
}
}

close(src);
close(dst);
}
PROGRAM

/* rm command simulation - del.c */

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

main(int argc, char* argv[])


{
int fd;

if (argc != 2)
{
printf("Usage: gcc del.c -o del\n");
printf("Usage: ./del <filename>\n");
exit(-1);
}

fd = open(argv[1], O_RDONLY);if
(fd != -1)
{
close(fd); unlink(argv[1]);
}
else
perror(argv[1]);
}
PROGRAM

A) Swapping values of two variables


# Swapping values – swap.sh echo -n "Enter value
for A : "read a

echo -n "Enter value for B : "read b

t=$a a=$b
b=$t

echo "Values after Swapping"

echo "A Value is $a and B Value is $b"

B) Farenheit to Centigrade Conversion


# Degree conversion – degconv.shecho -n "Enter
Fahrenheit : " read f

c=`expr \( $f - 32 \) \* 5 / 9`echo "Centigrade is : $c"

C) Biggest of 3 numbers
# Biggest – big3.sh

echo -n "Give value for A B and C: "read a b c

if [ $a -gt $b -a $a -gt $c ]then

echo "A is the Biggest number"elif [ $b -gt $c ]

then

echo "B is the Biggest number"else

echo "C is the Biggest number"

fi

D) Grade Determination
# Grade – grade.sh

echo -n "Enter the mark : "read mark

if [ $mark -gt 90 ]then

echo "S Grade" elif [ $mark -gt


80 ]then

echo "A Grade" elif [ $mark -gt


70 ]then
echo "B Grade" elif [ $mark -gt
60 ]then

echo "C Grade" elif [ $mark -gt


55 ]then

echo "D Grade" elif [ $mark -ge


50 ]then

echo "E Grade"else

echo "U Grade"

fi

E) Vowel or Consonant

# Vowel - vowel.sh

echo -n "Key in a lower case character : "read choice

case $choice in

a|e|i|o|u) echo "It's a Vowel";;

*) echo "It's a Consonant"

esac

F) Simple Calculator

# Arithmetic operations — calc.sh echo -n "Enter the


two numbers : "read a b

echo " 1. Addition" echo " 2.


Subtraction"

echo " 3. Multiplication"echo " 4. Division"

echo -n "Enter the option : "read option

case $option in

1) c=`expr $a + $b` echo


"$a + $b = $c";;
2) c=`expr $a - $b` echo
"$a - $b = $c";;
3) c=`expr $a \* $b` echo "$a * $b = $c";;

4) c=`expr $a / $b` echo "$a / $b =


$c";;
*) echo "Invalid Option"esac

G) Multiplication Table
# Multiplication table – multable.shclear

echo -n "Which multiplication table? : "read n

for x in 1 2 3 4 5 6 7 8 9 10do

p=`expr $x \* $n`

echo -n "$n X $x = $p"sleep 1

done

H) Number Reverse
# To reverse a number – reverse.shecho -n "Enter a
number : "

read nrd=0

while [ $n -gt 0 ]do

rem=`expr $n % 10` rd=`expr $rd \* 10 +


$rem`n=`expr $n / 10`

done

echo "Reversed number is $rd"

I) Prime Number
# Prime number – prime.sh echo -n "Enter the
number : "read n

i=2

m=`expr $n / 2` until [ $i -gt $m ]


do

q=`expr $n % $i`if [ $q -eq 0 ]


then

echo "Not a Prime number"exit

fi

i=`expr $i + 1`done

echo "Prime number"


PROGRAM

/* FCFS Scheduling - fcfs.c */

#include <stdio.h>

struct process
{
int pid; int
btime;int
wtime;int
ttime;
} p[10];

main()
{
int i,j,k,n,ttur,twat;
float awat,atur;

printf("Enter no. of process : ");


scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}

p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;twat
+= p[i].wtime;
}
awat = (float)twat / n;atur
= (float)ttur / n;

printf("\n FCFS Scheduling\n\n");


for(i=0; i<28; i++)
printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for(i=0; i<28; i++)
printf("-");

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


printf("\n P%d\t%4d\t%3d\t%2d",
p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n"); for(i=0;
i<28; i++)
printf("-");

printf("\n\nAverage waiting time : %5.2fms", awat);


printf("\nAverage turn around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n");
printf("|"); for(i=0;
i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n");
printf("0"); for(i=0;
i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
}
PROGRAM

/* SJF Scheduling – sjf.c */

#include <stdio.h>

struct process
{
int pid; int
btime;int
wtime;int
ttime;
} p[10], temp;

main()
{
int i,j,k,n,ttur,twat;
float awat,atur;

printf("Enter no. of process : ");


scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}

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


{
for(j=i+1; j<n; j++)
{
if((p[i].btime > p[j].btime) ||
(p[i].btime == p[j].btime && p[i].pid > p[j].pid))
{
temp = p[i]; p[i]
= p[j]; p[j] =
temp;
}

}
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;twat
+= p[i].wtime;
}
awat = (float)twat / n;atur
= (float)ttur / n;

printf("\n SJF Scheduling\n\n");


for(i=0; i<28; i++)
printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for(i=0; i<28; i++)
printf("-");
for(i=0; i<n; i++)
printf("\n P%-4d\t%4d\t%3d\t%2d",
p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n"); for(i=0;
i<28; i++)
printf("-");
printf("\n\nAverage waiting time : %5.2fms", awat);
printf("\nAverage turn around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n|"); for(i=0;
i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0"); for(i=0;
i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
}
PROGRAM

/* Priority Scheduling - pri.c */

#include <stdio.h>

struct process
{
int pid; int
btime;int pri;
int wtime;int
ttime;
} p[10], temp;

main()
{
int i,j,k,n,ttur,twat;
float awat,atur;

printf("Enter no. of process : ");


scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ", (i+1));
scanf("%d", &p[i].btime);
printf("Priority for process P%d : ", (i+1));scanf("%d",
&p[i].pri);
p[i].pid = i+1;
}

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


{
for(j=i+1; j<n; j++)
{
if((p[i].pri > p[j].pri) ||
(p[i].pri == p[j].pri && p[i].pid > p[j].pid) )
{
temp = p[i]; p[i]
= p[j]; p[j] =
temp;
}

}
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}

ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;twat
+= p[i].wtime;
}
awat = (float)twat / n;atur
= (float)ttur / n;

printf("\n\t Priority Scheduling\n\n");


for(i=0; i<38; i++)
printf("-");
printf("\nProcess B-Time Priority T-Time W-Time\n");
for(i=0; i<38; i++)
printf("-"); for
(i=0; i<n; i++)
printf("\n P%-4d\t%4d\t%3d\t%4d\t%4d",
p[i].pid,p[i].btime,p[i].pri,p[i].ttime,p[i].wtime);
printf("\n"); for(i=0;
i<38; i++)
printf("-");

printf("\n\nAverage waiting time : %5.2fms", awat);


printf("\nAverage turn around time : %5.2fms\n", atur);

printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n|"); for(i=0;
i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0"); for(i=0;
i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
}
PROGRAM

/* Round robin scheduling - rr.c */

#include <stdio.h>

main()
{
int i,x=-1,k[10],m=0,n,t,s=0;
int a[50],temp,b[50],p[10],bur[10],bur1[10];int
wat[10],tur[10],ttur=0,twat=0,j=0; float
awat,atur;

printf("Enter no. of process : ");


scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d : ", (i+1));scanf("%d",
&bur[i]);
bur1[i] = bur[i];
}
printf("Enter the time slice (in ms) : ");
scanf("%d", &t);

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


{
b[i] = bur[i] / t;
if((bur[i]%t) != 0)
b[i] += 1;
m += b[i];
}

printf("\n\t\tRound Robin Scheduling\n");

printf("\nGANTT Chart\n"); for(i=0;


i<m; i++)
printf(" -------------- ");
printf("\n");

a[0] = 0;
while(j < m)
{
if(x == n-1)x =
0;
else
x++;
if(bur[x] >= t)
{
bur[x] -= t;
a[j+1] = a[j] + t;
if(b[x] == 1)
{
p[s] = x;
k[s] = a[j+1];s++;
}
j++;
b[x] -= 1;
printf(" P%d |", x+1);
}
else if(bur[x] != 0)
{
a[j+1] = a[j] + bur[x];
bur[x] = 0;
if(b[x] == 1)
{
p[s] = x;
k[s] = a[j+1];s++;
}
j++;
b[x] -= 1;
printf(" P%d |",x+1);
}
}

printf("\n"); for(i=0;i<m;i++)
printf(" -------------- ");
printf("\n");

for(j=0; j<=m; j++)


printf("%d\t", a[j]);

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


{
for(j=i+1; j<n; j++)
{
if(p[i] > p[j])
{
temp = p[i]; p[i]
= p[j]; p[j] =
temp;

temp = k[i]; k[i]


= k[j]; k[j] =
temp;
}
}
}
for(i=0; i<n; i++)
{
wat[i] = k[i] - bur1[i];
tur[i] = k[i];
}
for(i=0; i<n; i++)
{
ttur += tur[i];twat
+= wat[i];
}

printf("\n\n"); for(i=0;
i<30; i++)
printf("-");
printf("\nProcess\tBurst\tTrnd\tWait\n");
for(i=0; i<30; i++)
printf("-"); for
(i=0; i<n; i++)
printf("\nP%-4d\t%4d\t%4d\t%4d", p[i]+1, bur1[i],
tur[i],wat[i]);
printf("\n"); for(i=0;
i<30; i++)
printf("-");

awat = (float)twat / n;atur


= (float)ttur / n;
printf("\n\nAverage waiting time : %.2f ms", awat);
printf("\nAverage turn around time : %.2f ms\n", atur);
}
PROGRAM

/* C program to demonstrate working of Semaphores */

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

sem_t mutex;

void* thread(void* arg)


{
//wait sem_wait(&mutex);
printf("\nEntered..\n");

//critical sectionsleep(4);

//signal
printf("\nJust Exiting...\n");
sem_post(&mutex);
}
int main()
{
sem_init(&mutex, 0, 1);
pthread_t t1,t2;
pthread_create(&t1,NULL,thread,NULL);
sleep(2);
pthread_create(&t2,NULL,thread,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL); sem_destroy(&mutex);
return 0;
}
PROGRAM

/* Fibonacci and Prime using pipe - fibprime.c */

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

main()
{
pid_t pid;
int pfd[2];
int i,j,flg,f1,f2,f3;
static unsigned int ar[25],br[25];

if(pipe(pfd) == -1)
{
printf("Error in pipe");
exit(-1);
}

pid=fork(); if
(pid == 0)
{
printf("Child process generates Fibonacci series\n" );f1 =
-1;
f2 = 1;
for(i = 0;i < 25; i++)
{
f3 = f1 + f2;
printf("%d\t",f3);f1 =
f2;
f2 = f3; ar[i] =
f3;
}
write(pfd[1],ar,25*sizeof(int));
}
else if (pid > 0)
{
wait(NULL);
read(pfd[0], br, 25*sizeof(int));
printf("\nParent prints Fibonacci that are Prime\n");

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


{
flg = 0;
if (br[i] <= 1)flg =
1;
for(j=2; j<=br[i]/2; j++)
{
if (br[i]%j == 0)
{
flg=1;
break;
}
}
if (flg == 0) printf("%d\t",
br[i]);
}
printf("\n");
}

else
{
printf("Process creation failed");
exit(-1);
}
}
PROGRAM

/* No. of users logged - cmdpipe.c */

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

int main()
{
int pfds[2];
pipe(pfds);

if (!fork())
{
close(1);
dup(pfds[1]);
close(pfds[0]); execlp("who",
"who", NULL);
}
else
{
close(0);
dup(pfds[0]);
close(pfds[1]);
execlp("wc", "wc", "-l", NULL);
}
}
PROGRAM

Server

/* Server chat process - srvmsg.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct mesgq
{
long type;
char text[200];
} mq;

main()
{
int msqid, len; key_t
key = 2013;

if((msqid = msgget(key, 0644|IPC_CREAT)) == -1)


{
perror("msgget");
exit(1);
}

printf("Enter text, ^D to quit:\n");


mq.type = 1;

while(fgets(mq.text, sizeof(mq.text), stdin) != NULL)


{
len = strlen(mq.text);
if (mq.text[len-1] == '\n')
mq.text[len-1] = '\0';
msgsnd(msqid, &mq, len+1, 0);

msgrcv(msqid, &mq, sizeof(mq.text), 0, 0);


printf("From Client: \"%s\"\n", mq.text);
}
msgctl(msqid, IPC_RMID, NULL);
}
Client

/* Client chat process - climsg.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct mesgq
{
long type;
char text[200];
} mq;

main()
{
int msqid, len; key_t
key = 2013;

if ((msqid = msgget(key, 0644)) == -1)


{
printf("Server not active\n");exit(1);
}

printf("Client ready :\n");


while (msgrcv(msqid, &mq, sizeof(mq.text), 0, 0) != -1)
{
printf("From Server: \"%s\"\n", mq.text);

fgets(mq.text, sizeof(mq.text), stdin);len


= strlen(mq.text);
if (mq.text[len-1] == '\n')
mq.text[len-1] = '\0';
msgsnd(msqid, &mq, len+1, 0);
}
printf("Server Disconnected\n");
}
PROGRAM

Server

/* Shared memory server - shms.c */

#include <stdio.h>
#include <stdlib.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define shmsize 27

main()
{
char c; int
shmid;
key_t key = 2013;char
*shm, *s;

if ((shmid = shmget(key, shmsize, IPC_CREAT|0666)) < 0)


{
perror("shmget");
exit(1);
}
printf("Shared memory id : %d\n", shmid);

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)


{
perror("shmat");
exit(1);
}

memset(shm, 0, shmsize);s =
shm;
printf("Writing (a-z) onto shared memory\n");for
(c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = '\0';

while (*shm != '*');


printf("Client finished reading\n");

if(shmdt(shm) != 0)
fprintf(stderr, "Could not close memory segment.\n");

shmctl(shmid, IPC_RMID, 0);


}
Client

/* Shared memory client - shmc.c */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define shmsize 27

main()
{
int shmid;
key_t key = 2013;char
*shm, *s;

if ((shmid = shmget(key, shmsize, 0666)) < 0)


{
printf("Server not started\n");exit(1);
}
else
printf("Accessing shared memory id : %d\n",shmid);

if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)


{
perror("shmat");
exit(1);
}

printf("Shared memory contents:\n");for


(s = shm; *s != '\0'; s++)
putchar(*s);putchar('\n');

*shm = '*';
}
PROGRAM

/* Producer-Consumer problem using semaphore – pcsem.c */#include

<stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>

#define N 5
#define BUFSIZE 1
#define PERMS 0666

int *buffer;
int nextp = 0, nextc = 0;
int mutex, full, empty; /* semaphore variables */

void producer()
{
int data; if(nextp
== N)
nextp = 0;
printf("Enter data for producer to produce : ");
scanf("%d",(buffer + nextp));
nextp++;
}

void consumer()
{
int g; if(nextc
== N)
nextc = 0;
g = *(buffer + nextc++); printf("\nConsumer
consumes data %d", g);
}

void sem_op(int id, int value)


{
struct sembuf op; int v;
op.sem_num = 0; op.sem_op
= value; op.sem_flg =
SEM_UNDO;
if((v = semop(id, &op, 1)) < 0)
printf("\nError executing semop instruction");
}
void sem_create(int semid, int initval)
{
int semval;union
semun
{
int val;
struct semid_ds *buf;
unsigned short *array;
} s;

s.val = initval;
if((semval = semctl(semid, 0, SETVAL, s)) < 0)printf("\nError
in executing semctl");
}

void sem_wait(int id)


{
int value = -1;
sem_op(id, value);
}

void sem_signal(int id)


{
int value = 1;
sem_op(id, value);
}

main()
{
int shmid, i;
pid_t pid;

if((shmid = shmget(1000, BUFSIZE, IPC_CREAT|PERMS)) < 0)


{
printf("\nUnable to create shared memory");return;
}
if((buffer = (int*)shmat(shmid, (char*)0, 0)) == (int*)-1)
{
printf("\nShared memory allocation error\n");exit(1);
}

if((mutex = semget(IPC_PRIVATE, 1, PERMS|IPC_CREAT)) == -1)


{
printf("\nCan't create mutex semaphore");
exit(1);
}
if((empty = semget(IPC_PRIVATE, 1, PERMS|IPC_CREAT)) == -1)
{
printf("\nCan't create empty semaphore");
exit(1);
}
if((full = semget(IPC_PRIVATE, 1, PERMS|IPC_CREAT)) == -1)
{
printf("\nCan't create full semaphore");
exit(1);
}

sem_create(mutex, 1);
sem_create(empty, N);
sem_create(full, 0);

if((pid = fork()) < 0)


{
printf("\nError in process creation");
exit(1);
}
else if(pid > 0)
{
for(i=0; i<N; i++)
{
sem_wait(empty);
sem_wait(mutex); producer();
sem_signal(mutex);
sem_signal(full);
}
}
else if(pid == 0)
{
for(i=0; i<N; i++)
{
sem_wait(full);
sem_wait(mutex); consumer();
sem_signal(mutex);
sem_signal(empty);
}
printf("\n");
}
}
PROGRAM

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

main()
{
int r[1][10], av[1][10];
int all[10][10], max[10][10], ne[10][10], w[10],safe[10];int
i=0, j=0, k=0, l=0, np=0, nr=0, count=0, cnt=0;

clrscr();
printf("enter the number of processes in a system");scanf("%d",
&np);
printf("enter the number of resources in a system");
scanf("%d",&nr);
for(i=1; i<=nr; i++)
{
printf("Enter no. of instances of resource R%d " ,i);
scanf("%d", &r[0][i]);
av[0][i] = r[0][i];
}

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


for(j=1; j<=nr; j++)
all[i][j] = ne[i][j] = max[i][j] = w[i]=0;
printf("Enter the allocation matrix");
for(i=1; i<=np; i++)
{
for(j=1; j<=nr; j++)
{
scanf("%d", &all[i][j]);
av[0][j] = av[0][j] - all[i][j];
}
}

printf("Enter the maximum matrix");


for(i=1; i<=np; i++)
{
for(j=1; j<=nr; j++)
{
scanf("%d",&max[i][j]);
}
}

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


{
for(j=1; j<=nr; j++)
{
ne[i][j] = max[i][j] - all[i][j];
}
}

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


{
printf("pocess P%d", i);
for(j=1; j<=nr; j++)
{
printf("\n allocated %d\t",all[i][j]);printf("maximum
%d\t",max[i][j]);
printf("need %d\t",ne[i][j]);
}
printf("\n \n");
}

printf("\nAvailability ");for(i=1;
i<=nr; i++)
printf("R%d %d\t", i, av[0][i]);
printf("\n "); printf("\n
safe sequence");
for(count=1; count<=np; count++)
{
for(i=1; i<=np; i++)
{
Cnt = 0;
for(j=1; j<=nr; j++)
{
if(ne[i][j] <= av[0][j] && w[i]==0)cnt++;
}
if(cnt == nr)
{
k++;
safe[k] = i; for(l=1;
l<=nr; l++)
av[0][l] = av[0][l] + all[i][l];
printf("\n P%d ",safe[k]); printf("\t
Availability "); for(l=1; l<=nr; l++)
printf("R%d %d\t", l, av[0][l]);
w[i]=1;
}
}
}
getch();
}
PROGRAM

#include<stdio.h>
#include<conio.h> int
max[100][100]; int
alloc[100][100];int
need[100][100]; int
avail[100];
int n, r; void
input();void
show(); void
cal();

main()
{
int i,j;
printf("Deadlock Detection Algo\n");
input();
show();
cal();
getch();
}

void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resource instances\t");
scanf("%d", &r);

printf("Enter the Max Matrix\n");


for(i=0; i<n; i++) for(j=0;
j<r; j++)
scanf("%d", &max[i][j]);

printf("Enter the Allocation Matrix\n");for(i=0;


i<n; i++)
for(j=0; j<r; j++) scanf("%d",
&alloc[i][j]);
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
scanf("%d",&avail[j]);
}

void show()
{
int i, j;
printf("Process\t Allocation\t Max\t Available\t");for(i=0;
i<n; i++)
{
printf("\nP%d\t ", i+1);
for(j=0; j<r; j++)
{
printf("%d ", alloc[i][j]);
}
printf("\t"); for(j=0;
j<r; j++)
{
printf("%d ", max[i][j]);
}
printf("\t");if(I ==
0)
{
for(j=0; j<r; j++) printf("%d
", avail[j]);
}
}
}

void cal()
{
int finish[100], temp, need[100][100], flag=1, k, c1=0;int
dead[100];
int safe[100];int i,
j;
for(i=0; i<n; i++)
{
finish[i] = 0;
}
/*find need matrix */for(i=0;
i<n; i++)
{
for(j=0; j<r; j++)
{
need[i][j]= max[i][j] - alloc[i][j];
}
}

while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0; for(j=0;j<r;j++)
{
if((finish[i]==0) && (need[i][j] <= avail[j]))
{
c++;
if(c == r)
{
for(k=0; k<r; k++)
{
avail[k] += alloc[i][j];
finish[i]=1;
flag=1;
}
if(finish[i] == 1)
{
i=n;
}
}
}
}
}
}
J = 0;
Flag = 0;
for(i=0; i<n; i++)
{
if(finish[i] == 0)
{
dead[j] = i;j++;
flag = 1;
}
}
if(flag == 1)
{
printf("\n\nSystem is in Deadlock and the Deadlock
process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t", dead[i]);
}
}
else
{
printf("\nNo Deadlock Occur");
}
}
PROGRAM

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

pthread_t tid[2];int
counter;
pthread_mutex_t lock;

void* trythis(void *arg)


{
pthread_mutex_lock(&lock);

unsigned long i = 0;counter


+= 1;
printf("\n Job %d has started\n", counter);

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

printf("\n Job %d has finished\n", counter);

pthread_mutex_unlock(&lock);

return NULL;
}

main()
{
int i = 0;int
error;

if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init has failed\n"); return
1;
}

while(i < 2)
{
err = pthread_create(&(tid[i]), NULL, &trythis, NULL);if
(error != 0)
printf("\nThread can't be created :[%s]",
strerror(error));
i++;
}
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
pthread_mutex_destroy(&lock);

return 0;
}
PROGRAM

/* First fit allocation - ffit.c */

#include <stdio.h>

struct process
{
int size; int
flag; int
holeid;
} p[10];
struct hole
{
int size;
int actual;
} h[10];

main()
{
int i, np, nh, j;

printf("Enter the number of Holes : ");


scanf("%d", &nh);
for(i=0; i<nh; i++)
{
printf("Enter size for hole H%d : ",i);
scanf("%d", &h[i].size);
h[i].actual = h[i].size;
}

printf("\nEnter number of process : " );


scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of process P%d : ",i);scanf("%d",
&p[i].size);
p[i].flag = 0;
}

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


{
for(j=0; j<nh; j++)
{
if(p[i].flag != 1)
{
if(p[i].size <= h[j].size)
{
p[i].flag = 1; p[i].holeid
= j; h[j].size -=
p[i].size;
}
}
}
}

printf("\n\tFirst fit\n");
printf("\nProcess\tPSize\tHole");for(i=0;
i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", i, h[i].actual, h[i].size);
printf("\n");
}
PROGRAM

#include <stdio.h>struct

process
{
int size; int
flag; int
holeid;
} p[10];

struct hole
{
int hid; int
size; int
actual;
} h[10];
main()
{
int i, np, nh, j;
void bsort(struct hole[], int);
printf("Enter the number of Holes : ");
scanf("%d", &nh);
for(i=0; i<nh; i++)
{
printf("Enter size for hole H%d : ",i);
scanf("%d", &h[i].size);
h[i].actual = h[i].size;
h[i].hid = i;
}
printf("\nEnter number of process : " );
scanf("%d",&np);
for(i=0;i<np;i++)
{
printf("enter the size of process P%d : ",i);scanf("%d",
&p[i].size);
p[i].flag = 0;
}
for(i=0; i<np; i++)
{
bsort(h, nh); for(j=0;
j<nh; j++)
{
if(p[i].flag != 1)
{
if(p[i].size <= h[j].size)
{
p[i].flag = 1; p[i].holeid
= h[j].hid;
h[j].size -= p[i].size;
}
}
}
}
printf("\n\tBest fit\n");
printf("\nProcess\tPSize\tHole");for(i=0;
i<np; i++)
{
if(p[i].flag != 1)
printf("\nP%d\t%d\tNot allocated", i, p[i].size);else
printf("\nP%d\t%d\tH%d", i, p[i].size, p[i].holeid);
}
printf("\n\nHole\tActual\tAvailable");
for(i=0; i<nh ;i++)
printf("\nH%d\t%d\t%d", h[i].hid, h[i].actual,h[i].size);
printf("\n");
}

void bsort(struct hole bh[], int n)


{
struct hole temp; int
i,j;
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if(bh[i].size > bh[j].size)
{
temp = bh[i];
bh[i] = bh[j];
bh[j] = temp;
}
}
}
}
PROGRAM

#include <stdio.h>#include
<math.h>

main()
{
int size, m, n, pgno, pagetable[3]={5,6,7}, i, j, frameno;double
m1;
int ra=0, ofs;

printf("Enter process size (in KB of max 12KB):");scanf("%d",


&size);
m1 = size / 4;n =
ceil(m1);
printf("Total No. of pages: %d", n); printf("\nEnter
relative address (in hexa) \n");scanf("%d", &ra);

pgno = ra / 1000;ofs =
ra % 1000;
printf("page no=%d\n", pgno);
printf("page table");
for(i=0;i<n;i++)
printf("\n %d [%d]", i, pagetable[i]);frameno
= pagetable[pgno];
printf("\nPhysical address: %d%d", frameno, ofs);

}
PROGRAM

#include <stdio.h>

main()
{
int i,j,l,rs[50],frame[10],nf,k,avail,count=0;

printf("Enter length of ref. string : ");


scanf("%d", &l);
printf("Enter reference string :\n");
for(i=1; i<=l; i++)
scanf("%d", &rs[i]); printf("Enter
number of frames : ");scanf("%d", &nf);
for(i=0; i<nf; i++)
frame[i] = -1;
j = 0;
printf("\nRef. str Page frames");
for(i=1; i<=l; i++)
{
printf("\n%4d\t", rs[i]);
avail = 0;
for(k=0; k<nf; k++)
if(frame[k] == rs[i])
avail = 1;
if(avail == 0)
{
frame[j] = rs[i];j =
(j+1) % nf; count++;
for(k=0; k<nf; k++)
printf("%4d", frame[k]);
}
}
printf("\n\nTotal no. of page faults : %d\n",count);
}
PROGRAM

/* LRU page replacement - lrupr.c */

#include <stdio.h>

int arrmin(int[], int);

main()
{
int i,j,len,rs[50],frame[10],nf,k,avail,count=0;int
access[10], freq=0, dm;

printf("Length of Reference string : ");


scanf("%d", &len);
printf("Enter reference string :\n");
for(i=1; i<=len; i++)
scanf("%d", &rs[i]); printf("Enter
no. of frames : ");scanf("%d", &nf);

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


frame[i] = -1;
j = 0;

printf("\nRef. str Page frames");


for(i=1; i<=len; i++)
{
printf("\n%4d\t", rs[i]);
avail = 0;
for(k=0; k<nf; k++)
{
if(frame[k] == rs[i])
{
avail = 1; access[k] =
++freq;break;
}
}
if(avail == 0)
{
dm = 0;
for(k=0; k<nf; k++)
{
if(frame[k] == -1)
dm = 1;
break;
}
if(dm == 1)
{
frame[k] = rs[i];
access[k] = ++freq;
count++;
}

else
{
j = arrmin(access, nf);
frame[j] = rs[i]; access[j]
= ++freq; count++;
}
for(k=0; k<nf; k++)
printf("%4d", frame[k]);
}
}
printf("\n\nTotal no. of page faults : %d\n", count);
}

int arrmin(int a[], int n)


{
int i, min = a[0];
for(i=1; i<n; i++)if
(min > a[i])
min = a[i];
for(i=0; i<n; i++)
if (min == a[i])
return i;
}
PROGRAM

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

struct
{
char dname[10]; char
fname[25][10];int fcnt;
}dir;

main()
{
int i, ch; char
f[30]; clrscr();
dir.fcnt = 0;
printf("\nEnter name of directory -- ");
scanf("%s", dir.dname);

while(1)
{
printf("\n\n 1. Create File\t2. Delete File\t3. Search File
\n4. Display Files\t5. Exit\nEnter your choice--");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the name of the file -- ");
scanf("%s", dir.fname[dir.fcnt]); dir.fcnt++;
break;

case 2:
printf("\n Enter the name of the file -- ");
scanf("%s", f);
for(i=0; i<dir.fcnt; i++)
{
if(strcmp(f, dir.fname[i]) == 0)
{
printf("File %s is deleted ",f);
strcpy(dir.fname[i], dir.fname[dir.fcnt-1]);break;
}
}
if(I == dir.fcnt)
printf("File %s not found", f);else
dir.fcnt--;break;

case 3:
printf("\n Enter the name of the file -- ");
scanf("%s", f);
for(i=0; i<dir.fcnt; i++)
{
if(strcmp(f, dir.fname[i]) == 0)
{
printf("File %s is found ", f);
break;
}
}
if(I == dir.fcnt)
printf("File %s not found", f);
break;

case 4:
if(dir.fcnt == 0)
printf("\n Directory Empty");else
{
printf("\n The Files are -- ");
for(i=0; i<dir.fcnt; i++)
printf("\t%s", dir.fname[i]);
}
break;

default:
exit(0);
}
}
getch();
}
PROGRAM

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

struct
{
char dname[10], fname[10][10];int
fcnt;
}dir[10];

main()
{
int i, ch, dcnt, k;char
f[30], d[30]; clrscr();
dcnt=0; while(1)
{
printf("\n\n 1. Create Directory\t 2. Create File\t 3.
Delete File");
printf("\n 4. Search File \t \t 5. Display \t 6. Exit \nEnter
your choice -- ");
scanf("%d", &ch);switch(ch)
{
case 1:
printf("\n Enter name of directory -- ");
scanf("%s", dir[dcnt].dname); dir[dcnt].fcnt =
0;
dcnt++;
printf("Directory created");
break;

case 2:
printf("\n Enter name of the directory -- ");scanf("%s",
d);
for(i=0; i<dcnt; i++)
if(strcmp(d,dir[i].dname) == 0)
{
printf("Enter name of the file -- "); scanf("%s",
dir[i].fname[dir[i].fcnt]);dir[i].fcnt++;
printf("File created");
break;
}
if(i == dcnt)
printf("Directory %s not found",d);break;

case 3:
printf("\nEnter name of the directory -- ");scanf("%s",
d);
for(i=0; i<dcnt; i++)
{
if(strcmp(d,dir[i].dname) == 0)
{
printf("Enter name of the file -- ");
scanf("%s", f);
for(k=0; k<dir[i].fcnt; k++)
{
if(strcmp(f, dir[i].fname[k]) == 0)
{
printf("File %s is deleted ", f);
dir[i].fcnt--;
strcpy(dir[i].fname[k],
dir[i].fname[dir[i].fcnt]);
goto jmp;
}
}
printf("File %s not found",f);goto
jmp;
}
}

printf("Directory %s not found",d);jmp


: break;

case 4:
printf("\nEnter name of the directory -- ");scanf("%s",
d);
for(i=0; i<dcnt; i++)
{
if(strcmp(d,dir[i].dname) == 0)
{
printf("Enter the name of the file -- ");
scanf("%s", f);
for(k=0; k<dir[i].fcnt; k++)
{
if(strcmp(f, dir[i].fname[k]) == 0)
{
printf("File %s is found ", f);goto
jmp1;
}
}
printf("File %s not found", f);goto
jmp1;
}
}
printf("Directory %s not found", d);
jmp1: break;

case 5:
if(dcnt == 0)
printf("\nNo Directory's "); else
{
printf("\nDirectory\tFiles");
for(i=0;i<dcnt;i++)
{
printf("\n%s\t\t",dir[i].dname);
for(k=0;k<dir[i].fcnt;k++)
printf("\t%s",dir[i].fname[k]);
}
}
break;

default:
exit(0);
}
}
getch();
}
PROGRAM

/* Contiguous Allocation - cntalloc.c */

#include <stdio.h>
#include <string.h>

int num=0, length[10], start[10];char


fid[20][4], a[20][4];

void directory()
{
int i;
printf("\nFile Start Length\n");
for(i=0; i<num; i++)
printf("%-4s %3d %6d\n",fid[i],start[i],length[i]);
}

void display()
{
int i;
for(i=0; i<20; i++)printf("%4d",i);
printf("\n"); for(i=0;
i<20; i++)
printf("%4s", a[i]);
}

main()
{
int i,n,k,temp,st,nb,ch,flag;char
id[4];

for(i=0; i<20; i++) strcpy(a[i],


"");
printf("Disk space before allocation:\n");
display();
do
{
printf("\nEnter File name (max 3 char) : ");
scanf("%s", id);
printf("Enter start block : ");
scanf("%d", &st);
printf("Enter no. of blocks : ");
scanf("%d", &nb); strcpy(fid[num],
id);
length[num] = nb; flag =
0;

if((st+nb) > 20)


{
printf("Requirement exceeds range\n");continue;
}

for(i=st; i<(st+nb); i++)


if(strcmp(a[i], "") != 0)
flag = 1;
if(flag == 1)
{
printf("Contiguous allocation not possible.\n");continue;
}
start[num] = st; for(i=st;
i<(st+nb); i++)
strcpy(a[i], id);;
printf("Allocation done\n");num++;

printf("\nAny more allocation (1. yes / 2. no)? : ");


scanf("%d", &ch);
} while (ch == 1); printf("\n\t\t\tContiguous
Allocation\n");printf("Directory:");
directory();
printf("\nDisk space after allocation:\n");
display();
}
PROGRAM

#include <stdio.h>
#include <conio.h>
#include <string.h>

main()
{
static int b[20], i, j, blocks[20][20];char
F[20][20], S[20], ch;
int sb[20], eb[20], x, n;
clrscr();
printf("\n Enter no. of Files ::"); scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter file %d name ::", i+1);
scanf("%s", &F[i]);
printf("\n Enter No. of blocks::", i+1);scanf("%d",&b[i]);
}

for(i=0;i<n;i++)
{
printf("\n Enter Starting block of file%d::",i+1);scanf("%d",
&sb[i]);
printf("\nEnter blocks for file%d::\n", i+1);for(j=0;
j<b[i]-1;)
{
printf("\n Enter the %dblock ::", j+2);
scanf("%d", &x);
if(b[i] != 0)
{
blocks[i][j] = x;j++;
}
else
printf("\n Invalid block::");
}
}

printf("\nEnter the Filename :");


scanf("%s", &S);
for(i=0; i<n; i++)
{
if(strcmp(F[i],S) == 0)
{
printf("\nFname\tBsize\tStart\tBlocks\n"); printf("\n
\n");
printf("\n%s\t%d\t%d\t", F[i], b[i], sb[i]); printf("%d-
>",sb[i]);
for(j=0; j<b[i]; j++)
{
if(b[i] != 0)
printf("%d->", blocks[i][j]);
}
}
}
printf("\n \n");
getch();
}

You might also like