You are on page 1of 71

STUDY OF BASIC COMMANDS OF UNIX

COMMANDS:

CAL:
DESCRIPTION:
The cal command is used to display the calendar.

SYNTAX:
[$ CAL YEAR]

DATE:
DESCRIPTION:
The date command is used to display the day,month,year and time.

SYNTAX: [$DATE]
The command can also be used within suitable format specified as argument . Each format specified as argument .Each format is prescribed by a symbol followed by the % Operator describing the format, -m -> Display the month in number -h -> Display the month in words -d -> Display the day in words -y -> Display last two digits of the year

OPTIONS:

U print or sets coordinate universal time S set the current time to specified time

MAN:

DESCRIPTION:
Format and displays online manuals.

SYNTAX:
Man [option] name

CLEAR: DESCRIPTION:
Clear the terminal screen .

WHO:
DESCRIPTION:
Displays the information of logged in user.

OPTIONS: -a-> All


-b-> Time of the last symbol boot -d->Prints dead processor

WHO AM I:
DESCRIPTION:
Show the login of the current user .

PASSWD:
DESCRIPTION:
Update on users authentication token.

SYNTAX:
Passwd [*][-1][-v][-f][-d][-n mindays][-m maxdays][username]

OPTIONS:
-k-> Used to lock an account -n->Will set minimum password time

LOGOUT:

DESCRIPTION:
Exit off from current login.

FILE COMMANDS: pwd: DESCRIPTION:


Points the name of the current working directory

OPTIONS:
-help->displays help

mkdir: DESCRIPTION:
Used to make/create the new directory.

SYNTAX:
mkdir [option] directory

OPTIONS:
-m ->sets permission mode]

cd: DESCRIPTION:
Change the current directory to specified path.

SYNTAX:
cd [directory]

rmdir: DESCRIPTION:
Used to remove/delete the empty directories.

SYNTAX:
rmdir [option]directory]

OPTIONS:
-output->diagnostics from each step

ls: DESCRIPTION:
Used to list directory contents.

SYNTAX:
ls [option][file]

OPTIONS:
-a-> do not hide entries starting with author of each -c-> list entries by columns -d-> list directory entries -f-> do not sort -l-> use long history format -r->recursive list -s->sort by size -v->sort by version

cat: DESCRIPTION:
Concatenate files and prints on the standard output.

SYNTAX:
cat [option] [file]

OPTION:

-a->show all -n->number of all output lines -t->show tabs

cp: DESCRIPTION:
copies files and directories.

SYNTAX:
cp [option] source text

OPTIONS:
-r->copy recursively -u->copy only when the source is never

rm: DESCRIPTION:
Removes files (or) directories.

SYNTAX:
rm[option]..file..

OPTION:
-f->never prompt -r->recursive remove -i->interactive operation

mv: DESCRIPTION:
Move or remove files.

SYNTAX:

mv[options]..source text

OPTION:
-f->do not prompt -u->update -i->interactive operation

file: DESCRIPTION:
Determines the file type.

SYNTAX:
file[-f file name]

wc: DESCRIPTION:
Prints the number of bytes,words&lines.

SYNTAX:
wc[options][file]

OPTION:
-c-> prints the character count -w->prints word count -l-> print the line count

head: DESCRIPTION:
Gives the first part of the file.

SYNTAX:
head[options]..[file]

OPTIONS:
-c->print first N bytes -n->print first n lines

tail: DESCRIPTION:
Gives the last part of the file.

SYNTAX:
tail[option]..[file]

OPTIONS:
-c->print last N bytes -n->print last N lines

GREATEST OF THREE NUMBERS


PROGRAM :
echo enter a value: read a echo enter b value: read b echo enter c value: read c if[$a-gt $b-a $a-gt $c] then echo a=$a is greatest elif[$b-gt$c]

then echo b=$b is greatest else echo c=$c is greatest fi

OUTPUT :
Enter a value:3 Enter a value:2 Enter c value:5 C=5 is greatest

ARITHMETIC OPERATION USING SWITCH CASE


PROGRAM :
echo 1.add echo 2.sub echo 3.mul echo 4.div echo enter your choice: read n echo enter a value: read a echo enter b value: read b case $ n in

1)c=expr $a+$b;; 2) c=expr $a-$b;; 3) c=expr $a*$b;; 4) c=expr $a/$b;; esac

OUTPUT :
1.add 2.sub 3.mul 4.div Enter your choice:3 Enter a value:3 Enter b value:5 The result is 15

STUDENT DETAILS USING WHILE LOOP


PROGRAM :
echo enter the no of students: read n count=1 while[$count-le$n] do echo enter the name: read name echo enter the no: read no echo enter the marks m1,m2,m3: read m1 read m2 read m3

tot=explr $m1+$m2+sm3 avg=expr $ tot/3 echo name is:$ name echo no is : $ no echo avg is: $ avg echo total is: $ tot count=expr $ count+1

OUTPUT :
enter the name: student enter the no:1 enter the marks m1,m2,m3:100 100 100 total is:300 avg is:100

SYSTEM CALLS USING FORK()


PROGRAM :
#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> main() { int pid; int a,b,t; printf("\nEnter a and b :"); scanf("%d%d",&a,&b); pid=fork(); if(pid<0) { printf("error in fork"); } if(pid==0) { printf("\nChild process to do swapping"); t=a;a=b;b=t; } else {

printf("\nParent process"); wait(2); } printf("pid value as %d\n",getpid()); printf("A as:%d,B as:%d\n",a,b); }

OUTPUT :
Enter a and b : 4 5 Child process to do swapping pid value as 6651 A as:5,B as:4 Parent process pid value as 6649 A as:4,B as:5

SYSTEM CALLS USING EXEC()


PROGRAM :
#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<fcntl.h> main(int age ,char *args[]) { int pid; pid=fork(); if(pid<0) { printf("error in fork"); } if(pid==0) { printf("\nChild Process\n"); execlp("/home/jec/uma/sem.c","sem.c",NULL); } else { wait(NULL); printf("\nChild Completed"); exit(0); } return 0;

OUTPUT :
Child Process Child Completed

FILE CREATION
PROGRAM :
#include<stdio.h> #include<unistd.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> main() { int fd; char buf1[100],buf2[]="hello everybody",fname[30]; printf("enter filename\n"); scanf("%s",fname); fd = creat(fname,S_IRWXU); if(fd < 0) { printf("error in file creation"); } else { write(fd,buf2,30); printf("contents written successfully\n"); fd=open(fname,O_RDONLY); read(fd,buf1,30); printf("contents is:\n%s\n",buf1); close(fd);

} }

OUTPUT :
jec@jec-desktop:~/uma$ cc systemcall3.c jec@jec-desktop:~/uma$ ./a.out enter filename devi contents written successfully contents is: hello everybody jec@jec-desktop:~/uma$ cat devi hello everybody

FILE ATTRIBUTES
PROGRAM:
#include<errno.h> #include<stdio.h> #include<sys/stat.h> #include<unistd.h> int main() { struct stat statv; if(stat("/home/staff/rajkumar/ww",&statv)==-1) perror("stat"); else { printf("\n the file user id is: %d",statv.st_uid); printf("\n the file group id is: %d",statv.st_gid); printf("\n the file size is :%d",statv.st_size); printf("\n the file inode number is :%d",statv.st_ino); } return (0); }

OUTPUT:
the file user id is: 920 the file group id is: 921 the file size is :33 the file inode number is :8230320

CHANGE THE CONTEXT OF A PROCESS PROGRAM:


#include <sys/wait.h> #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<unistd.h> #include<errno.h> int system (const char *cmd) { pid_t pid; int status; switch(pid=fork()) { case -1: return -1; case 0: execl ("/bin/sh","sh","-c",cmd,0); perror ("execl"); exit(errno); } if(waitpid(pid,&status,0)==pid && WIFEXITED(status)) return WEXITSTATUS(status); return -1;

} int main() { int rc=0; char buf[256]; do { printf("\n sh>"); if(!gets(buf)) break; rc=system(buf); } while(!rc); exit(rc); }

OUTPUT:
sh>ls a.out Desktop gg bb fact.sh ha hai big.sh ff m3.txt odd.sh p2.c prim.sh sal.sh menu.sh p1o.c p2c.c raj mm.c small.sh simcd.c user.sh simmv.c small1.sh

p1pc.c p3.c rajkumar simls.c ww

chat1.c ff.sh io4.c mul.sh p1w.c p4.c rrr chat2.c fib.sh m2.txt mylist p1z.c pn.sh rr.sh sh>cal January 2009 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

SCANNING A DIRECTORY
PROGRAM:
#include<stdio.h> #include<sys/types.h> //#include<stdlib.h> #include<unistd.h> #include<string.h> #include<sys/stat.h> #if defined(BSD)&&I_POSIX_SOURCE #include<sys/dir.h> typedef struct dirent Dirent; #else #include<dirent.h> typedef struct dirent Dirent; #endif int main(int argc, char *argv[]) { Dirent* dp; int i; int cnt; DIR *dir_fdesc; while(--argc>0) { dir_fdesc=opendir(*++argv); for(i=0;i<2;i++) { for(cnt=0;dp=readdir(dir_fdesc);) {

if(i) printf("%s\n",dp->d_name); //if(strcmp(dp->d_name,".")&&strcmp(dp->d_name,"..")) cnt++; } if(!cnt) { rmdir(*argv); break; } rewinddir(dir_fdesc); } closedir(dir_fdesc); } }

OUTPUT:
/a.out /home/staff/rajkumar/gg pp.sh hh.txt rajkumar gg

IMPLEMENTATION OF I/O SYSTEM CALLS


PROGRAM:
#include<fcntl.h> #include<stdio.h> #include<unistd.h> int main(int argc, char *argv[]) { char buf[256]; int fdesc, len; while(--argc>0) { if(access(*++argv,F_OK)) { fdesc=open(*argv,O_WRONLY|O_CREAT,0744); write(fdesc, "HELLO WORLD\n",12); } else { fdesc=open(*argv,O_RDONLY); while(len=read(fdesc,buf,256)) write(1,buf,len); } close(fdesc); } }

OUTPUT:

./a.out ram.c [cse@localhost ~]$ vi ram.c HELLO WORLD

SIMULATION OF UNIX COMMANDS


PROGRAM :
#include<stdio.h>

int main() { char c; int n; printf("1.List of files in the directory\n2.List the lines\n3.Copy\n4.Remove\n"); scanf("%d",&n); switch(n) { case 1: system("ls"); break; case 2: system("grep argc john.c"); break; case 3: system("cp z c"); printf("the file is copied"); break; case 4: system("rm z c"); printf("the file is deleted"); break; } }

OUTPUT :
1.List of files in the directory 2.List the lines 3.Copy 4.Remove 1 Ric.c

Sab.c Sat.c 1.List of files in the directory 2.List the lines 3.Copy 4.Remove 2 int main(int argc,char * argv[]) 1.List of files in the directory 2.List the lines 3.Copy 4.Remove 3 The file is copied 1.List of files in the directory 2.List the lines 3.Copy 4.Remove 4 The file is deleted

FIRST COME FIRST SERVE SCHEDULING


PROGRAM :
#include<stdio.h> int main() { int n,i,b[10],w[10],t[10]; float ae,aw,at; printf("\n\tFIRST COME FIRST SERVE"); printf("\n\t-------------------------------------"); printf("\n\nENTER THE NUMBER OF JOBS : ");

scanf("%d",&n); for(i=0;i<n;i++) { printf("\nENTER THE BURST TIME FOR JOB %d :",i+1); scanf("%d",&b[i]); if(i==0) { w[0]=0; t[0]=b[0]; } else { w[i]=t[i-1]; t[i]=t[i-1]+b[i]; } } printf("\nJOB\tBURST TIME\tWAITING TIME\tTURN AROUND TIME\n"); printf("\n---------------------------------------------------------------------------------\n"); for(i=0;i<n;i++) { ae+=b[i]; printf("\n%d\t\t%d\t\t%d\t\t%d\n",i+1,b[i],w[i],t[i]); aw+=w[i]; at+=t[i]; } ae/=n; aw/=n; at/=n; printf("\nAVERAGE BURST TIME IS = %f",(float)ae); printf("\nAVERAGE WAITING TIME IS = %f",(float)aw); printf("\nAVERAGE TURN AROUND TIME IS = %f",(float)at); return 0; }

OUTPUT :
FIRST COME FIRST SERVE --------------------------------------

ENTER THE NUMBER OF JOBS : 4 ENTER THE BURST TIME FOR THE JOB1 : 10 ENTER THE BURST TIME FOR THE JOB2 : 20

ENTER THE BURST TIME FOR THE JOB3 : 30 ENTER THE BURST TIME FOR THE JOB4 : 40

JOB

BURSTTIME

WAITING TIME

TURNAROUNDTIME

------------------------------------------------------------------------------------------------1 2 3 4 10 20 30 40 0 10 30 60 10 30 60 100

AVERAGE BURSTTIME IS = 25.0000000 AVERAGE WAITING TIME IS = 24.9999992 AVERAGE TURNAROUNDTIME IS = 50.00000

SHORTEST JOB FIRST SCHEDULING


PROGRAM :
#include<stdio.h> int main() { int n,b[12],b1[12],j[12],w[12],e[12]; int i,k,t,t1; float aw=0,ae=0; printf("\n\tSHORTEST JOB FIRST\n"); printf("\t-----------------------------------"); printf("\n\nENTER THE NUMBER OF JOBS : ");

scanf("%d",&n); for(i=0;i<n;i++) { printf("\nENTER THE BURST TIME OF JOB %d = ",i+1); scanf("%d",&b[i]); b1[i]=b[i]; j[i]=i; } for(i=0;i<n;i++) for(k=0;k<n;k++) if(b1[i]<b1[k]) { t=b1[i]; b1[i]=b1[k]; b1[k]=t; } printf("\nJOB\tBURST TIME\tWAITING TIME\tTURN AROUND TIME\t"); printf("\n----------------------------------------------------------------------------------"); for(i=0;i<n;i++) { if(i==0) { w[0]=0; e[0]=b1[0]; } else { w[i]=e[i-1]; e[i]=e[i-1]+b1[i]; } printf("\n%d\t\t%d\t\t%d\t\t%d\n",i+1,b1[i],w[i],e[i]);

aw+=w[i]; ae+=e[i]; } aw/=n; ae/=n; printf("\nAVERAGE WAITITNG TIME IS = %f",aw); printf("\nAVERAGE TURN AROUND TIME IS = %f",ae); return 0; }

OUTPUT :
SHORTEST JOB FIRST -------------------------------

ENTER THE NUMBER OF JOBS: 3 ENTER THE BURST TIME FOR THE JOB1 = 30 ENTER THE BURST TIME FOR THE JOB2 = 10 ENTER THE BURST TIME FOR THE JOB3 = 20

JOB

BURSTTIME

WAITING TIME

TURNAROUNDTIME

----------------------------------------------------------------------------------------------1 2 3 10 20 30 0 10 30 10 30 60

AVERAGE WAITING TIME IS

= 13.333333

AVERAGE TURNAROUNDTIME IS = 33.333332

PRIORITY SCHEDULING
PROGRAM :
#include<stdio.h> int main() { int i,j,n,temp,start[10],pri[10],finish[10],burst[10],sw=0,st=0; printf("\nPRIORITY SCHEDULING\n"); printf("\nENTER THE NUMBER OF JOBS : "); scanf("%d",&n); start[0]=0; for(i=0;i<n;i++) { printf("\nENTER THE BURST TIME & PRIORITY OF JOB %d : ",i+1); scanf("%d %d",&burst[i],&pri[i]); } for(i=0;i<n;i++)

for(j=i+1;j<n;j++) if(pri[j]<pri[i]) { temp=pri[i]; pri[i]=pri[j]; pri[j]=temp; temp=burst[i]; burst[i]=burst[j]; burst[j]=temp; } printf("\n BURST TIME\t PRIORITY\t START TIME\t FINISH TIME"); for(i=0;i<n;i++) { start[i+1]=finish[i]=start[i]+burst[i]; st+=finish[i]; sw+=start[i]; printf("\n%d\t\t%d\t\t%d\t\t%d",burst[i],pri[i],start[i],finish[i]); } printf("\n AVERAGE TURN AROUND TIME IS %f",(float)st/n); printf("\n AVERAGE WAITING TIME IS %f",(float)sw/n); return 0; }

OUTPUT :
PRIORITY SCHEDULING ENTER THE NUMBER OF JOBS : 3 ENTER THE BURST TIME & PRIORITY OF JOB 1 : 4 2 ENTER THE BURST TIME & PRIORITY OF JOB 2 : 6 1 ENTER THE BURST TIME & PRIORITY OF JOB 3 : 8 3 BURST TIME 6 PRIORITY 1 START TIME 0 FINISH TIME 6

4 8

2 3

6 10

10 18

AVERAGE TURNAROUND TIME IS 11.333333 AVERAGE WAITING TIME IS 5.333333

ROUND ROBIN SCHEDULING


PROGRAM :
#include<stdio.h> main() { int i, wa=0,cum[30],b[30],count=0,f=0,wait[30],b1[30]; int k,x[30],xx[30],a,t=0,w,c,n,max=0,wt=0,s,j; printf("\nROUND ROBIN SCHEDULING"); printf("\nENTER THE NUMBER OF JOBS : "); scanf("%d",&n); for(i=0;i<20;i++) xx[i]=-1; for(i=0;i<n;i++) { printf("\nENTER THE BURST TIME OF JOB %d : ",i+1); scanf("%d",&b[i]); b1[i]=b[i]; max=max>b[i]?max:b[i]; }

printf("\nENTER THE TIME SLICE FOR JOBS : "); scanf("%d",&s); k=1; for(j=0;j<max;j+=s) { printf("\nJOB\t BURST TIME\t REMAINING TIME"); for(i=0;i<n;i++) { if(b[i]==0) continue; if(b[i]<s) { a=b[i]; x[k]=i; xx[k]=b[i]; k++; } else { a=b[i]; x[k]=i; xx[k]=s; k++; } b[i]=a-s; printf("\n\t%d\t\t%d\t\t%d",i+1,a,b[i]); } printf("\n\n"); scanf("%d",&c); } for(i=1;i<=k;i++) count+=1; cum[i]=0; for(i=2;i<=count;i++) { cum[i]=xx[i-1]+cum[i-1]; printf("%d\t",cum[i]); } for(j=0;j<n;j++) { for(i=count-1;i>=1;i++) { if(x[i]==j) { wait[j]=cum[i+1]-b1[j]; t=t+cum[i+1]; break;

} } } printf("\n WAITING TIME"); for(i=0;i<n;i++) { printf("\n%d",wait[i]); wa=wa+wait[i]; } printf("\n AVERAGE WAITING TIME is \t%f",(float)wa/n); printf("\n AVERAGE EXECUTION TIME is \t%f",(float)t/n); wt=wa+t; printf("\n AVERAGE TURN AROUND TIME is \t%f",(float)wt/n); }

OUTPUT :
ROUND ROBIN SCHEDULING ENTER THE NUMBER OF JOBS : 3 ENTER THE BURST TIME OF JOB 1 : 4 ENTER THE BURST TIME OF JOB 2 : 2 ENTER THE BURST TIME OF JOB 3 : 6 ENTER THE TIME SLICE FOR JOBS: 2 JOB 1 2 3 JOB 1 3 JOB 3 BURST TIME 4 2 6 BURST TIME 2 4 BURST TIME 2 REMAINING TIME 2 0 4 REMAINING TIME 0 2 REMAINING TIME 0

2 4 6 8 10 12 WAITING TIME 4 2 6

AVERAGE WAITING TIME IS 4.000000 AVERAGE EXECUTION TIME IS 8.00000 AVERAGE TURN AROUND TIME IS 12.000000

CHATTING PROGRAM
PROGRAM:
#include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> #include<unistd.h> #include<string.h> int main() { int mid; char mess[20]; printf("\n\n INTER PROCESS COMMUNICATION USING MESSAGE QUEUE..."); mid=msgget(27,IPC_CREAT|0777); if(mid==-1) { printf("\n\t invalid message id..");

exit(1); } printf("\n enter the message text:"); scanf("%s",mess); msgsnd(mid,mess,strlen(mess),0); printf("\n\n message has been sent..\n"); printf("\n the sent message is:%s\n",mess); }

OUTPUT:
INTER PROCESS COMMUNICATION USING MESSAGE QUEUE... enter the message text:hai message has been sent.. the sent message is:hai

PROGRAM:
#include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> #include<unistd.h> #include<string.h> int main() { int i,mid,len; char mess[20]; printf("\n\n INTER PROCESS COMMUNICATION USING MESSAGE QUEUE..."); mid==msgget(27,IPC_CREAT|0777); if(mid==-1) { printf("\n\t invalid message id.."); exit(1); }

msgrcv(mid,mess,100,0,0); printf("\n\n message has been recieved..\n"); printf("\n\n the recieved message is:%s\n",mess); }

OUTPUT:
INTER PROCESS COMMUNICATION USING MESSAGE QUEUE... message has been recieved.. the recieved message is:hai

PRODUCER CONSUMER PROBLEM


PROGRAM :
#include<stdio.h> #include<sys/types.h> #include<unistd.h> #define bz 10 void producer(); void consumer(int y[bz]); int wait(int); int signal(int); int i,n,in=0,out=0,s,c,w,sig,mutex=1,np[bz],buffer[bz],ne[bz]; main() { do { printf("\n 1.produce the items\n2.consumer the items\n"); scanf("%d",&s); if(s==1) { producer(); }

if(s==2) { consumer(buffer); } printf("\nto continue ,press 1 or else press 0"); scanf("%d",&c); } while(c!=0); } void producer() { w=wait(mutex); printf("\nthe semaphore value of wait is =%d\n",w); printf("\nEnter the no of items:\n"); scanf("%d",&n); printf("\nEnter the items"); for(i=0;i<=n;i++) { scanf("%d",&np[i]); } printf("\n given items are:"); for(i=0;i<=n;i++) { printf("%d\n",np[i]); } if((in+1%bz==out)) { printf("\ndo anything"); } printf("\n the producer produce the items:\n"); for(i=0;i<=n;i++)

{ buffer[i]=np[i]; printf("%d\n",buffer[in]); in=(in+1)%bz; } sig=signal(mutex); printf("the semaphore value of signal is %d\n",sig); } void consumer(int y[bz]) { if(mutex) printf("\n the semsphore value of wait is=%d\n",w); if(in==out) { printf("\n do nothing\n"); } printf("\nthe consumer consumes the items:\n"); for(i=0;i<=n;i++) { ne[i]=y[out]; printf("%d\n",ne[i]); out=(out+1)%bz; } sig=signal(mutex); printf("the semaphore value of signal is %d\n",sig); } int signal(int m) { m++; return m; }

int wait(int n) { n++; return(n); }

OUTPUT :
1.produce the items 2.consumer the items 1

The semaphore value of wait is =2 Enter the no of items: 3 Enter the items2 3 4 1 given items are:2 3 4 1 the producer produce the items: 2 3 4 1 the semaphore value of signal is 2

to continue ,press 1 or else press 01

1.produce the items 2.consumer the items 2 the semsphore value of wait is=2 the consumer consumes the items: 2 3 4 1 the semaphore value of signal is 2 to continue ,press 1 or else press 0

DINING PHILOSOPHERS PROBLEM


PROGRAM :
#include<stdio.h> char state[10],self[10],spoon[10]; void test(int k) { if((state[(k+4)%5]!='e')&&(state[(k+1)]!='e')) { state[k]='e'; self[k]='s'; spoon[k]='n'; spoon[(k+4)%5]='n'; } } void pickup(int i) { state[i]='h'; test(i); if(state[i]=='h') self[i]='w'; } void putdown(int i) { state[i]='t'; spoon[i]='s'; spoon[i-1]='s'; test((i+4)%5); test((i+1)%5); } main() { int ch,a,n,i; printf("\nDINING PHILOSOPHERS PROBLEM :"); for(i=0;i<5;i++) { state[i]='t'; self[i]='s'; spoon[i]='s'; } printf("\nInitial state of each philosopher :"); printf("\nPHIL NO:\tTHINK\tSTATUS\t\tSPOON"); for(i=0;i<5;i++)

printf("\n%d\t\t%c\t\t%c\t\t%c",i+1,state[i],self[i],spoon[i]); printf("\n1.EXIT\n2.HUNGRY\n3.THINKING"); printf("\nEnter your choice :"); scanf("%d",&ch); while(ch!=1) { switch(ch) { case 1: return 0; case 2: printf("\nEnter which philosopher is hungry :"); scanf("%d",&n); pickup(n-1); break; case 3: printf("\nEnter which philosopher is thinking :"); scanf("%d",&n); putdown(n-1); break; } printf("State of each philosopher :"); printf("\nPHIL NO:\tTHINK/EAT\tSTATUS\t\tSPOON"); for(i=0;i<5;i++) printf("\n%d\t\t%c\t\t%c\t\t%c",i+1,state[i],self[i],spoon[i]); printf("\n1.EXIT\n2.HUNGRY\n3.THINKING"); printf("\nEnter your choice :"); scanf("%d",&ch); }

OUTPUT :
DINING PHILOSOPHERS PROBLEM : Initial state of each philosopher : PHIL NO: 1 2 3 4 5 1.EXIT 2.HUNGRY 3.THINKING Enter your choice :2 Enter which philosopher is hungry :2 State of each philosopher : PHIL NO: THINK/EAT 1 t 2 e 3 t 4 t 5 t 1.EXIT 2.HUNGRY 3.THINKING Enter your choice :3 Enter which philosopher is thinking :4 State of each philosopher : PHIL NO: THINK/EAT 1 t 2 e 3 t 4 t 5 e 1.EXIT 2.HUNGRY 3.THINKING Enter your choice :1 THINK STATUS t s t s t s t s t s SPOON s s s s s

STATUS s s s s s

SPOON n n s s s

STATUS s s s s s

SPOON n n s n n

PAGING
PROGRAM :
#include<stdio.h> main() { int i,j,arr[100],pt[20],val,pgno,offset,phymem,fs,nf; printf("Memory management -Paging\n"); printf("\n*****************************"); printf("\nEnter size of physical memory"); scanf("%d",&phymem); for(i=20,j=0;i<phymem+20,j<phymem;i++,j++) arr[i]=j; printf("\nEnter size of frame or page "); scanf("%d",&fs); nf=phymem/fs; printf("\nNo of frames available are \t %d",nf); printf("\nEnter page table"); for(i=0;i<nf;i++) { scanf("%d",&pt[i]); } printf("\nEnter page no"); scanf("%d",&pgno); printf("\nEnter offset\n"); scanf("%d",&offset); val=(fs*pt[pgno])+offset; printf("The physical address is :%d\n",arr[val]); }

OUTPUT :
Memory management -Paging ***************************** Enter size of physical memory 64 Enter size of frame or page 8 No of frames available are Enter page table 1 2 3 4 5 6 7 8 Enter page no 5 Enter offset 3 The physical address is :31 8

BEST FIT
PROGRAM :
#include<stdio.h> main() { int i,n,m,ps[100],bs[150],j,pi[50],bi[50],t; printf("enter the no of blocks\n"); scanf("%d",&n); printf("enter the block size\n"); for(i=1;i<=n;i++) { printf("%d\n",i); scanf("%d",&bs[i]); bi[i]=i; } printf("enter no of process\n"); scanf("%d",&m); for(i=1;i<=m;i++) { printf("enter process %d size:\n",i); scanf("%d",&ps[i]); pi[i]=i; } for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(bs[i]<bs[j]) { t=bs[i]; bs[i]=bs[j]; bs[j]=t; t=bi[i]; bi[i]=bi[j]; bi[j]=t; } if(ps[i]<ps[j]) { t=ps[i]; ps[i]=ps[j]; ps[j]=t; t=pi[i]; pi[i]=pi[j]; pi[j]=t; } }

} for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(ps[j]<=bs[i]) { printf("process %d is allocated to block of size %d\n",pi[j],bs[i]); ps[j]=10000; break; } } } for(i=1;i<=m;i++) { if(ps[i]!=10000) { printf("\nprocess %d is not allowed",i); } } }

OUTPUT :
enter the no of blocks 4 enter the block size 1 200

2 250 3 300 4 400 enter no of process 3 enter process 1 size: 225 enter process 2 size: 275 enter process 3 size: 325 process 0 is allocated to block of size 200 process 1 is allocated to block of size 250 process 2 is allocated to block of size 300

FIRST FIT
PROGRAM :
#include<stdio.h> main() { int i,n,m,ps[100],bs[150],j;

printf("enter the no of blocks\n"); scanf("%d",&n); printf("enter the block size\n"); for(i=1;i<=n;i++) { printf("%d\n",i); scanf("%d",&bs[i]); } printf("enter no of process\n"); scanf("%d",&m); for(i=1;i<=m;i++) { printf("enter process %d size:\n",i); scanf("%d",&ps[i]); } //memory allocation for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(ps[j]<=bs[i]) { printf("process %d is allocated to block of size %d\n",j,bs[i]); ps[j]=10000; break; } } } for(i=1;i<=m;i++) { if(ps[i]!=10000) { printf("\nprocess %d is not allowed",i); } } }

OUTPUT :
enter the no of blocks 4 enter the block size 1

400 2 500 3 300 4 600 enter no of process 4 enter process 1 size: 200 enter process 2 size: 250 enter process 3 size: 350 enter process 4 size: 550 process 1 is allocated to block of size 400 process 2 is allocated to block of size 500 process 3 is allocated to block of size 600 process 4 is not allowed

ORPHAN PROCESS
PROGRAM:
main() { if(fork()==0) {

system("clear"); printf("CHILD:i am child & my number is :%d \n",getpid()); printf("CHILD: my parent is :%d \n",getppid()); } else { printf("PARENT:i am parent:%d \n",getpid()); printf("PARENT: my parent is :%d \n",getppid()); } }

OUTPUT:
CHILD:i am child & my number is :2663 CHILD: my parent is :1

PARENT-CHILD PROCESS
PROGRAM:
main() { if(fork()==0) { system("clear"); printf("CHILD:i am child & my number is :%d \n",getpid()); printf("CHILD: my parent is :%d \n",getppid()); } else

{ printf("PARENT:i am parent:%d \n",getpid()); sleep(2); printf("PARENT: my parent is :%d \n",getppid()); } }

OUTPUT:
PARENT:i am parent:2682 CHILD:i am child & my number is :2683 CHILD: my parent is :2682 PARENT: my parent is :2434

WAITING PROCESS
PROGRAM:
main() { int pid,dip,cpid; pid=fork(); if(pid==0) { printf("1st Child Process Id is %d\n",getpid()); printf("1st Child Process TERMINATING FROM THE MEMORY\n"); } else

{ dip=fork(); if(dip==0) { printf("2st Child Process Id is %d\n",getpid()); printf("2st Child Process TERMINATING FROM THE MEMORY\n"); } else { cpid=wait(0); printf("Child with pid:%d is died\n",cpid); cpid=wait(0); printf("Child with pid:%d is died\n",cpid); } } }

OUTPUT:
1st Child Process Id is 2699 1st Child Process TERMINATING FROM THE MEMORY 2st Child Process Id is 2700 2st Child Process TERMINATING FROM THE MEMORY Child with pid:2699 is died Child with pid:2700 is died

ZOMBIE PROCESS
PROGRAM:
main() { int pid=fork(); system("clear"); printf("my number is %d\n",getpid()); if(pid>0) { printf("My parent number is %d\n",getpid()); sleep(05); }

OUTPUT:
my number is 2716 My parent number is 2716 my number is 2717

SINGLY LINKED LIST


PROGRAM:
#include<stdio.h> #include<malloc.h> struct queue { int no; struct queue *next; } *p,*q,*temp,*head=NULL; int main() { int ch;

void enqueue(); void dequeue(); void list(); while(1) { printf("\n 1...Insert"); printf("\n 2...Delete"); printf("\n 3...Display"); printf("\n 4...Quit"); printf("Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: list(); break; default: exit(0); } } return 0; } void enqueue()

{ temp=(struct queue*) malloc(sizeof(struct queue)); printf("\nEnter number to be inserted:"); scanf("%d",&temp->no); if(head==NULL) { head=temp; temp->next=NULL; } else { q=head; while(q->next!=NULL) { q=q->next; } q->next=temp; temp->next=NULL; } printf("\n\nElement %d is pushed into queue",temp->no); } void dequeue() { p=head; if(head==NULL) { printf("\n\nQueue is empty"); } else

{ head=head->next; printf("\n\nElement %d is dequeued from queue",p->no); free(p); } } void list() { if(head==NULL) { printf("\n\nQueue is empty"); } else { p=head; printf("\n\nElement present in queue\n"); while(p!=NULL) { printf("%d\t",p->no); p=p->next; } } }

OUTPUT:
1...Insert 2...Delete 3...Display 4...Quit Enter your choice:1 Enter number to be inserted:12 Element 12 is pushed into queue 1...Insert 2...Delete 3...Display 4...Quit Enter your choice:1 Enter number to be inserted:13 Element 13 is pushed into queue 1...Insert 2...Delete 3...Display

4...Quit Enter your choice:1 Enter number to be inserted:14 Element 14 is pushed into queue 1...Insert 2...Delete 3...Display 4...Quit

Enter your choice:2 Element 12 is dequeued from queue 1...Insert 2...Delete 3...Display 4...Quit Enter your choice:3 Element present in queue 13 14

1...Insert 2...Delete 3...Display 4... Quit Enter your choice:4

You might also like