Вы находитесь на странице: 1из 47

ZOMBIE PROCESS

#include<stdio.h> main() { int pid=fork(); printf("my numbers is %d",getpid()); printf("\n"); if(pid>0) { printf("my parent id is %d",getpid()); sleep(2); } } OUTPUT: my numbers is 5571 my numbers is 5570 my parent id is 5570

WAIT PROGRAM
#include<stdio.h> main() { int pid,dip,cpid; pid=fork(); if(pid==0) { printf("\n first child process id is %d",getpid()); printf("\n first child process terminating from memory"); } else { dip=fork(); if(dip==0) { printf("\n second child process id is %d died",getpid()); printf("\n second child process terminating from memory"); } else { cpid=wait(0); printf("\n child with pid %d died",cpid); cpid=wait(0); printf("\n child with pid %d died",cpid); printf("\n parent is ending"); } } }

OUTPUT: first child process id is 6476 first child process terminating from memory second child process id is 6477 died second child process terminating from memory child with pid 6476 died child with pid 6477 died

FORK PROGRAM
#include<stdio.h> main(int argc,char *argv[]) { int pid; pid=fork(); if(pid<0) { printf(stdin,"fork failed"); exit(-1); } else if(pid==0) execlp("/bin/ls","ls",NULL); else { wait(NULL); printf("child completed"); exit(0); } } OUTPUT: akky fork meeenu.c sakthi6.c alagra.h fork.1.c meenaaa.c sakthi8.c alagra.h.gch fork1.c meenaa.c sakthi.c albin.c forkbright.c meenas.c samby.c alclr fork.c meenbest.c sam.c forkk.c meens.c sanal ammu.c forksc.c meenss.c sanda.c an2ny fpage.c meenu.c sandy.c an2ny.cc fx.c melba santa.c anjel.c ga.c melbaablem.c sarma.c annette.c gan14.c merlin.c sase.c

FIRST COME FIRST SERVE


#include<stdio.h> main() { struct { int id,btime,wtime,totime; } jb[20]; int i,j,n,wsum=0,fsum=0; float aw=0.0,at=0.0; printf("enter the number of jobs\n"); scanf("%d",&n); printf("\n\t enter the btime for each process"); printf("\n\n\tpid\t\tbtime"); printf("\n*********************************\n"); for(i=0;i<n;i++) { jb[i].id=i+1; printf("\t%3d\t\t",jb[i].id); scanf("\t%3d",&jb[i].btime); } jb[0].wtime=0; jb[0].totime=jb[0].btime; printf("\n********* first come first serve *************"); printf("\n\tpid\tbtime\twtime\ttotime"); for(i=0;i<n;i++) { printf("\n\t%3d\t%3d\t%3d\t%3d\n",jb[i].id,jb[i].btime,jb[i].wtime,jb[i].totime) ; wsum=wsum+jb[i].wtime; fsum=fsum+jb[i].totime; jb[i+1].wtime=jb[i].wtime+jb[i].btime; jb[i+1].totime=jb[i+1].wtime+jb[i+1].btime; } for(i=0;i<75;i++) printf("-"); aw=wsum*1.0/n; at=fsum*1.0/n; printf("\n\naverage waiting time \n%6.2f\n",aw); printf("\n\naverage turn around time \n%6.2f\n",at); }

OUTPUT: enter the number of jobs 3 enter the btime for each process pid btime ********************************* 1 24 2 3 3 3 ********* first come first serve ************* pid btime wtime totime 1 24 0 24 2 3 24 27

3 3 27 30 -------------------------------------------------------------------------average waiting time 17.00 average turn around time 27.00

PRIORITY SCHEDULING
#include<stdio.h> main() { struct { int id,btime,wtime,totime,pty; } p[20]; int t,i,j,n,wsum=0,fsum=0; float aw=0.0,at=0.0; printf("enter the number of job\n"); scanf("%d",&n); printf("\n\tenter the btime & priority for each process"); printf("\n\n\tpid\t\tbtime\t\tpriority"); printf("\n***************\n"); for(i=0;i<n;i++) { p[i].id=i+1; printf("\t%3d\t\t",p[i].id); scanf("\t%3d",&p[i].btime); scanf("%3d",&p[i].pty); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(p[i].pty>p[j].pty) { t=p[i].btime; p[j].btime=p[j].btime; p[j].btime=t; t=p[i].id; p[i].id=p[j].id; p[j].id=t; t=p[i].pty; p[i].pty=p[j].pty; p[j].pty=t; } } } p[0].wtime=0; p[0].totime=p[0].btime; printf("\n************priority scheduling***********"); 6

printf("\n\tpid\tbtime\tpty\twtime\ttotime"); for(i=0;i<n;i++) { printf("\n\t%3d\t%3d\t%3d\t%3d\t%3d\n",p[i].id,p[i].btime,p[i].pty,p[i].wtime,p[ i].totime); wsum=wsum+p[i].wtime; fsum=fsum+p[i].totime; p[i+1].wtime=p[i].wtime+p[i].btime; p[i+1].totime=p[i+1].wtime+p[i+1].btime; } for(i=0;i<75;i++) printf("-"); aw=wsum*1.0/n; at=fsum*1.0/n; printf("\n\n average waiting time\n%6.2f\n",aw); printf("\n\n average turnaround time\n%6.2f\n",at); } OUTPUT: enter the number of job 3 enter the btime & priority for each process pid 1 2 3 btime priority *************** 24 2 3 1 3 3

************priority scheduling*********** pid btime pty wtime totime 2 24 1 0 24 1 24 2 24 48

3 3 3 48 51 --------------------------------------------------------------------------average waiting time 24.00 average turnaround time 41.00 7

SHORTEST JOB FIRST


#include<stdio.h> main() { struct { int id,btime,wtime,totime; }s[20]; int i,j,n,wsum=0,fsum=0,t; float aw=0.0,at=0.0; printf("enter the number of job\n"); scanf("%d",&n); printf("\n\t enter the btime & priority for each process"); printf("\n\n\tpid\t\tbtime"); printf("\n*********************************************************\n"); for(i=0;i<n;i++) { s[i].id=i+1; printf("\t%3d\t\t",s[i].id); scanf("\t%3d",&s[i].btime); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(s[i].btime>s[j].btime) { t=s[i].btime; s[i].btime=s[j].btime; s[j].btime=t; t=s[i].id; s[i].id=s[j].id; s[j].id=t; } } } s[0].wtime=0; s[0].totime=s[0].btime; printf("\n************* shortest job first ****************"); printf("\n\tpid\tbtime\twtime\ttotime"); for(i=0;i<n;i++) { printf("\n\t%3d\t%3d\t%3d\t%3d\n",s[i].id,s[i].btime,s[i].wtime,s[i].totime); wsum=wsum+s[i].wtime; 8

fsum=fsum+s[i].totime; s[i+1].wtime=s[i].wtime+s[i].btime; s[i+1].totime=s[i+1].wtime+s[i+1].btime; } for(i=0;i<75;i++) printf("-"); aw=wsum*1.0/n; at=fsum*1.0/n; printf("\n\n average waiting time\n%6.2f\n",aw); printf("\n\n average turn around time\n%6.2f\n",at); } OUTPUT: enter the number of job 3 enter the btime & priority for each process pid btime ********************************************************* 1 6 2 5 3 2 ************* shortest job first **************** pid btime wtime totime 3 2 0 2 2 5 2 7

1 6 7 13 --------------------------------------------------------------------------average waiting time 3.00 average turn around time 7.33

DINING PHILOSOPHER PROBLEM


#include<stdio.h> char state[10],self[10]; void pickup(int); void putdown(int); int i,n,a,ch,k; main() { printf("DINING PHILOSOPHER PROBLEM\n"); printf("\n___________________\n"); for(i=0;i<5;i++) { state[i]='t'; self[i]='s'; } printf("initialise state of each philosopher\n"); for(i=0;i<5;i++) { printf("\n%d\t%c\t%c\n",i,state[i],self[i]); } printf("MAIN MENU"); printf("\n 1.HUNGRY \n2.THINKING \n3.EXIT\n"); printf("\n enter your choice"); scanf("%d",&ch); while(ch!=3) { switch(ch) { case 1: printf("enter which philosopher is hungry\n"); scanf("%d",&n); pickup(n); break; case 2: printf("enter which philosopher want to think\n"); scanf("%d",&n); putdown(n); break; } printf("\n initialize state of each philosopher\n"); for(i=0;i<5;i++) { printf("\n%d\t%c\t%c\n",i,state[i],self[i]); } printf("\n MAIN MENU \n 1.HUNGRY \n 2.THINKING \n 3.EXIT \n"); 10

printf("\n enter your choice"); scanf("%d",&ch); } } void test(int k) { if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k+1)%5]!='e')) { state[k]='e'; self[k]='s'; } } void pickup(int i) { state[i]='h'; test(i); if(state[i]!='e') { self[i]='w'; } } void putdown(int i) { state[i]='t'; test((i+4)%5); test((i+1)%5); } OUTPUT: DINING PHILOSOPHER PROBLEM ___________________ initialise state of each philosopher 0 1 2 3 t t t t s s s s

4 t s MAIN MENU 11

1.HUNGRY 2.THINKING 3.EXIT enter your choice1 enter which philosopher is hungry 1 initialize state of each philosopher 0 1 2 3 4 t e t t t s s s s s

MAIN MENU 1.HUNGRY 2.THINKING 3.EXIT enter your choice1 enter which philosopher is hungry 2 initialize state of each philosopher 0 1 2 3 4 t e h t t s s w s s

MAIN MENU 1.HUNGRY 2.THINKING 3.EXIT

12

enter your choice1 enter which philosopher is hungry 3 initialize state of each philosopher 0 1 2 3 4 t e h e t s s w s s

MAIN MENU 1.HUNGRY 2.THINKING 3.EXIT enter your choice2 enter which philosopher want to think 1 initialize state of each philosopher 0 1 2 3 4 t t h e t s s w s s

MAIN MENU 1.HUNGRY 2.THINKING 3.EXIT enter your choice3

13

PRODUCER CONSUMER

#include<stdio.h> #define SIZE 5 int stack[SIZE],top=-1,in=0; void produce(); void consume(); main() { char ch; printf("\n MENU\n"); printf("\np.PRODUCE\n"); printf("\nc.CONSUME\n"); printf("\nq.QUIT\n"); while(1) { ch=getchar(); switch(tolower(ch)) { case 'p': produce(); break; case 'c': consume(); break; case 'q': exit(0); } } } void produce() { int value; if(top<SIZE-1) { value=top+2; stack[++top]=value; printf("produce: %d",value); } else { printf("cannot produce"); } } 14

void consume() { if(top==-1) printf("cannot consume"); else printf("consume: %d",stack[top--]); } OUTPUT: MENU p.PRODUCE c.CONSUME q.QUIT p produce: 1 p produce: 2 p produce: 3 p produce: 4 p produce: 5 p cannot producec consume: 5 c consume: 4 c consume: 3 c consume: 2 c consume: 1 c cannot consume q

15

READ SYSTEM CALL


#include<stdio.h> #include<fcntl.h> main(int argc,char * argv[]) { int fd,i; char ch[1]; if(argc<2) { printf("usage:my cat filename\n"); exit(0); } fd=open(argv[1],O_RDONLY); if(fd==-1) printf("%s is not exit",argv[1]); else { printf("CONTENT of the file %s:\n",argv[1]); while(read(fd,ch,1)>0) printf("%c",ch[0]); close(fd); } } OUTPUT: CONTENT of the file red.c: #include<stdio.h> main() { if(fork()==0) { system("clear"); printf("child:i am child number is %d\n",getpid()); printf("child:my parent number is %d\n",getpid()); } else { printf("parent:i am parent %d\n",getpid()); sleep(2); printf("parent:my parent is %d\n",getpid()); } } 16

WRITE SYSTEM CALL


#include<stdio.h> #include<fcntl.h> #include<stdlib.h> main(int argc,char *argv[]) { int fd,i; char ch[1]; if(argc<2) { printf("usage:my cat filename\n"); exit(0); } fd=open(argv[1],O_WRONLY); if(fd==-1) printf("%s is not exit",argv[1]); else { while(write(fd,ch,3)>0) scanf("%c",&ch[0]); close(fd); printf("THE CONTENT IN THE FILE is"); } } OUTPUT: ^C^@h^C^@a^C^@i^C^@ ^C^@h^C^@o^C^@w^C^@ ^C^@r^C^@ ^C^@u^C^@ C^@/^C^@ C^@e^C^@n^C^@d^C^@

17

LS SIMULATION CALL
#include<stdio.h> #include<dirent.h> int main() { struct dirent **namelist; int n,i; char pathname[100]; getcwd(pathname); n=scandir(pathname,&namelist,0,alphasort); if(n<0) printf("error"); else { for(i=0;i<n;i++) printf("%s\n",namelist[i]->d_name); } } OUTPUT: . .. .osthe.c.swp .roundeee.c.swp \ ] a.out aegan.c bbroy.c billa.c billa2.c citizen.c cool.c datafile.dat dazzlingluck.c fool.c indian2.c lucknenercomes.c lucknevercomes.c mangatha.c marina.c omegaranger.c osthe.c osthee.c red.c 18

STAT SYSTEM CALL


#include<sys/stat.h> int main() { struct stat fp; if(stat("/etc/passwd",&fp)==-1) { printf("error"); exit(0); } printf("inode:%d\n",fp.st_ino); printf("type&permission:%d\n",fp.st_nlink); printf("userid:%d\n",fp.st_uid); printf("groupid:%d\n",fp.st_gid); printf("deviceid:%d\n",fp.st_rdev); printf("previous access time:%d\n",fp.st_atime); printf("modification time:%d\n",fp.st_mtime); exit(0); } OUTPUT: inode:1653738 type&permission:1 userid:0 groupid:0 deviceid:0 previous access time:1329466058 modification time:1328762930

19

CREATE SYSTEM CALL


#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> int main() { int fd; fd=creat("datafile.dat",S_IREAD|S_IWRITE); if(fd==-1) printf("error in opening datafile.dat\n"); else { printf("datafile.dat opened for read/write access\n"); printf("datafile.dat is currently empty"); } close(fd); exit(0); } OUTPUT: datafile.dat opened for read/write access datafile.dat is currently empty

20

OPENDIR CLOSEDIR AND READDIR SYSTEM CALL


#include<stdio.h> #include<dirent.h> int main(int argc,char *argv[]) { DIR *dirEntry; struct dirent *de; dirEntry=opendir("/home/student"); while((de=readdir(dirEntry))!=NULL) printf("%d\t%s\n",de->d_ino,de->d_name); closedir(dirEntry); exit(0); } OUTPUT: 1368142 sunday.c 1366182 .difcfs.c.swp 1365887 vcv.c 1365921 aparnaa.c 1366546 dinshortestjob.c 1367533 prishe.sh 1366083 .559.swp 1365994 bumeena.c 1368063 harisshh.c 1366317 dinfcfs.cclear 1367816 ss.c 1365625 mos.c 1365832 .fes.c.swp 1365819 fcfs460.c 1365671 .kkkkk.c.swp 1368193 fcfs1.c 1365933 fifs.c 1368166 .firstcome1.c.swo 1366351 .ICEauthority 1365987 dinning.c 1366247 alclr 1367239 .fz.c.swo 1366649 Hudson 1365831 .kebin.sh.swp 1366922 .muthu.sh.swp 1365799 .har.c.swp 1366204 .mari.c.swp 1367185 vrevu.c 1366184 .wait.swp 21

PAGING
#include<stdio.h> int i,j,k,ps,np1,op,np,np2,fn,f=0; char p1[50][50],p2[50][50]; int pgtb(int r,int fn,int np) { for(i=0;i<np;i++) printf("\n\t%d\t\t%d",i,r++); return(r); } void frame(int np1,int np2,int fn,int ps,char p1[50][50],char p2[50][50]) { for(i=0;i<np1;i++) { if(i<fn) { printf("\n.......................\n"); printf("\n frame no %d\n",i); for(j=0;j<ps;j++) printf("\t%c",p1[i][j]); } } k=np1; for(i=0;i<np2;i++) { if(k<fn) { k++; for(j=0;j<ps;j++) printf("\t%c",p2[i][j]); } } } int main() { printf("\n enter the page size:"); scanf("%d",&ps); printf("\n enter the no of frames:"); scanf("%d",&fn); printf("enter the no of pages of process 1:"); scanf("%d",&np1); printf("\n enter the no of pages of process 2:"); scanf("%d",&np2); if(np1+np2>fn) printf("\n page fault will occur\n"); 22

printf("\n PROCESS 1"); printf("\n........\n"); p1[np1][ps]; p2[np2][ps]; for(i=0;i<np1;i++) { printf("\n enter CHAR for page %d:",i); scanf("%s",&p1[i]); } printf("\n PROCESS 2"); printf("\n............\n"); for(i=0;i<np2;i++) { printf("\n enter CHAR for page %d:",i); scanf("%s",&p2[i]); } while(1) { printf("\n 1.page table for PROCESS 1\n 2.page table for PROCESS 2\n 3.frame all otment\n 4.free frame list\n 5.exit\n"); printf("\n enter ur choice"); scanf("%d",&op); switch(op) { case 1: printf("\n page table for PROCESS 1:"); printf("\n PAGE INDEX \t FRAME INDEX\n"); f=pgtb(f,fn,np1); break; case 2: printf("\n page table for PROCESS 2:"); printf("\n PAGE INDEX \tFRAME INDEX\n"); f=pgtb(f,fn,np2); break; case 3: frame(np1,np2,fn,ps,p1,p2); break; case 4: if(np1+np2>fn) printf("page fault has occured"); else if(np1+np2==fn) printf("\n no free frames"); else printf("free frame list"); printf("\n..........\n"); for(i=np1+np2;i<fn;i++) 23

printf("%d th frame",i); break; case 5: return(0); break; } } } OUTPUT: enter the page size:10 enter the no of frames:3 enter the no of pages of process 1:2 enter the no of pages of process 2:3 page fault will occur PROCESS 1 ........ enter CHAR for page 0:a enter CHAR for page 1:b PROCESS 2 ............ enter CHAR for page 0:c enter CHAR for page 1:d enter CHAR for page 2:e 1.page table for PROCESS 1 2.page table for PROCESS 2 3.frame allotment 4.free frame list 5.exit enter ur choice1 page table for PROCESS 1: PAGE INDEX FRAME INDEX 24

0 0 1 1 1.page table for PROCESS 1 2.page table for PROCESS 2 3.frame allotment 4.free frame list 5.exit enter ur choice2 page table for PROCESS 2: PAGE INDEX FRAME INDEX 0 2 1 3 2 4 1.page table for PROCESS 1 2.page table for PROCESS 2 3.frame allotment 4.free frame list 5.exit enter ur choice3 ....................... frame no 0 a ....................... frame no 1 b c 1.page table for PROCESS 1 2.page table for PROCESS 2 3.frame allotment 4.free frame list 5.exit enter ur choice4 page fault has occured ..........

25

1.page table for PROCESS 1 2.page table for PROCESS 2 3.frame allotment 4.free frame list 5.exit enter ur choice5

26

PARENT CHILD RELATIONSHIP


#include<stdio.h> main() { if(fork()==0) { system("clear"); printf("child:i am child number is %d\n",getpid()); printf("child:my parent number is %d\n",getpid()); } else { printf("parent:i am parent %d\n",getpid()); sleep(2); printf("parent:my parent is %d\n",getpid()); } } OUTPUT: child:i am child number is 10836 parent:i am parent 10835 child:my parent number is 10836 parent:my parent is 10835

27

FIRST FIT
#include<stdio.h> struct memblock { char name[20]; int size; int status; }m[20]; struct process { char pro[20]; int pro_size; }p[20]; main() { int n,j,i; struct memblock m1[20]; system("clear"); printf("\n enter the no of processes"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("enter name size of the process"); scanf("%s%d",p[i].pro,&p[i].pro_size); m[i].size=rand()%100; m1[i].size=m[i].size; } printf("\n******************************\nBLOCK\tSIZE\tSTATUS\tPROCESS NAME\t P OCESS SIZE\n**************************\n"); for(i=1;i<=n;i++) printf("\n\nBLOCK%d\t%d\tFREE\t\t%s\t%d",i,m[i].size,p[i].pro,p[i].pro_size); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(p[i].pro_size<=m1[j].size) { m[j].status=1; m1[j].size=0; break; } } } printf("\n FIRST FIT\n_________\n"); 28

for(i=1;i<=n;i++) { printf("\n\n"); if(m[i].status==0) printf("BLOCK%d\t%d\tFREE",i,m[i].size); else printf("BLOCK%d\t%d\tALLOCATED",i,m[i].size); } } OUTPUT: enter the no of processes4 enter name size of the process1 60 enter name size of the process2 63 enter name size of the process3 23 enter name size of the process4 52 ****************************** BLOCK SIZE STATUS PROCESS NAME ************************** BLOCK1 83 BLOCK2 86 BLOCK3 77 BLOCK4 15 FIRST FIT _________ BLOCK1 83 BLOCK2 86 BLOCK3 77 BLOCK4 15 FREE FREE FREE FREE 1 2 3 4 60 63 23 52 PROCESS SIZE

ALLOCATED ALLOCATED ALLOCATED FREE

29

WORST FIT
#include<stdio.h> struct memblock int size; int status; }m[20]; struct process { char pro[20]; int pro_size; }p[20]; main() { int n,j,i,temp; struct memblock m1[20],w[20]; system("clear"); printf("enter the no of process"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("enter name size of the process"); scanf("%s%d",p[i].pro,&p[i].pro_size); m[i].size=rand()%100; m1[i].size=m[i].size; } for(i=1;i<=n;i++) printf("\n\nBLOCK%d\t%d\tFREE\t\t%s\t%d",i,m[i].size,p[i].pro,p[i].pro_size); for(i=1;i<=n-1;i++) { for(j=i+1;j<=n;j++) { if(m1[i].size<m1[j].size) { temp=m1[i].size; m1[i].size=m1[j].size; m1[j].size=temp; } } } for(i=1;i<=n;i++) w[i].size=m1[i].size; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { 30

if(p[i].pro_size<=m1[j].size) { m[j].status=1; m1[j].size=0; break; } } } printf("\n\nWORSTFIT\n__________\n"); for(i=1;i<=n;i++) { printf("\n\n"); if(m[i].status==0) printf("BLOCK%d\t%d\tFREE",i,w[i].size); else printf("BLOCK%d\t%d\tALLOCATED",i,w[i].size); } } OUTPUT: enter the no of process4 enter name size of the process1 90 enter name size of the process2 89 enter name size of the process3 60 enter name size of the process4 80 ***************************** BLOCK SIZE STATUS PROCESS NAME PROC ************************* BLOCK1 83 BLOCK2 86 BLOCK3 77 BLOCK4 15 WORSTFIT __________ 31 FREE FREE FREE FREE 1 2 3 4 90 89 60 80

BLOCK1 86 BLOCK2 83 BLOCK3 77 BLOCK4 15

ALLOCATED ALLOCATED FREE FREE

32

IPC MESSAGE QUEUE


#include<stdio.h> #include<string.h> #include<sys/ipc.h> #include<sys/msg.h> int main() { int pid,len,qid; struct { long mtype; char mtext[10]; }message; system("clear"); printf("\n inter process communication using message queue\n"); qid=msgget((key_t)13,IPC_CREAT|0666); printf("the message queue is created\n"); if(qid==-1) { printf("message queue is not created\n"); exit(1); } printf("the value of qid is %d",qid); strcpy(message.mtext,"priya"); message.mtype=1; len=strlen(message.mtext); pid=fork(); printf("\n the value of pid is:%d\n",pid); if(pid==0) { msgsnd(qid,&message,len,IPC_NOWAIT); printf("\n message send\n"); } if(pid>0) { msgrcv(qid,&message,strlen(message.mtext),0,IPC_NOWAIT|MSG_NOERROR); printf("\n message is %s\n",message.mtext); printf("\n message is received\n"); } if(pid==-1) { printf("error in creating a child\n"); exit(1); } 33

} OUTPUT: inter process communication using message queue the message queue is created the value of qid is 0 the value of pid is:0 message send the value of qid is 0 the value of pid is:12096 message is priya message is received

34

INTERPROCESS COMMUNICATION USING PIPES


#include<stdio.h> #include<sys/ipc.h> #define MAXSIZE 40 main() { char*m1="hai"; char inbuf[MAXSIZE]; int p[2],pid; system("clear"); printf("ipc using pipes\n"); pipe(p); pid=fork(); printf("the value of pid is %d\n",pid); if(pid==-1) { printf("error\n"); exit(1); } if(pid==0) { printf("\n child process is writing\n"); write(p[7],m1,MAXSIZE); printf("\n parent is reading the msg from pipe sent by child using fd[0]\n"); read(p[0],inbuf,MAXSIZE); printf("\n the msg read is %s\n",inbuf); exit(0); } } OUTPUT: ipc using pipes the value of pid is 0 child process is writing parent is reading the msg from pipe sent by child using fd[0] the value of pid is 12875

35

BEST FIT
#include<stdio.h> #include<string.h> #include<math.h> struct segment { char jobid[10]; int size; char status[10]; }s[10]; char progid[20][20];\ int n,v,t,pos,i,j,progsize[10],val,ch; int totmem=0,maxmem=0,sn=0; main() { system("clear"); menu(); for(i=0;i<10;i++) { for(j=i+1;j<10;j++) { if(s[i].size>s[j].size) { t=s[i].size; s[i].size=s[j].size; s[j].size=t; } } } for(j=0;j<n;j++) { for(i=0;i<10;i++) { if((s[i].size>=progsize[j])&&!(strcmp(s[i].status,"FREE"))) { strcpy(s[i].jobid,progid[j]); strcpy(s[i].status,"OCCUPIED"); sn=1; break; } } if(sn==0) printf("%s has no space",progid[j]); } printf("\n BLOCK \t SIZE \t STATUS \t\t JOB \n"); 36

for(i=0;i<10;i++) printf("\n BLOCK%d\t%d\t%s\t\t%s",s[i].size,s[i].status,s[i].jobid); } menu() { printf("\nBLOCK\tSIZE\tSTATUS\n"); for(i=0;i<10;i++) { val=rand()%100; if(val==0) s[i].size=rand()%80; else s[i].size=val; totmem+=s[i].size; if(s[i].size>maxmem) maxmem=s[i].size; strcpy(s[i].jobid,"NULL"); strcpy(s[i].status,"FREE"); printf("\nBLOCK%d\t%d\t%s\n",i,s[i].size,s[i].status); } printf("enter the no of job you want to store"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the id and size of the program"); scanf("%s%d",progid[i],&progsize[i]); if(maxmem<progsize[i]) { printf("insufficient memory %d enter again",maxmem); scanf("%d",&progsize[i]); } } } OUTPUT: BLOCK SIZE STATUS BLOCK0 83 BLOCK1 86 BLOCK2 77 BLOCK3 15 FREE FREE FREE FREE 37

BLOCK4 93 BLOCK5 35 BLOCK6 86 BLOCK7 92 BLOCK8 49

FREE FREE FREE FREE FREE

BLOCK9 21 FREE enter the no of job you want to store3 enter the id and size of the program1 80 enter the id and size of the program2 10 enter the id and size of the program3 19 BLOCK SIZE STATUS BLOCK15 BLOCK21 BLOCK35 BLOCK49 BLOCK77 BLOCK83 BLOCK86 BLOCK86 BLOCK92 BLOCK93 134517072 134517100 134517128 134517156 134517184 134517212 134517240 134517268 134517296 134517324 2 3 NULL NULL NULL 1 NULL NULL NULL NULL JOB

38

ROUND ROBIN ALGORITHM


#include<stdio.h> main() { int i,j=0,m,n,b[20],b1[20],f[20],w[20],tat[20],start,finish,total=0,t; float awt=0.0,atat=0.0; system("clear"); printf("\n enter no of process"); scanf("%d",&n); printf("enter the time quantum"); scanf("%d",&t); for(i=1;i<=n;i++) { printf("\n enter the burst time for process P%d",i); scanf("%d",&b[i]); b1[i]=b[i]; total+=b[i]; } start=0; while(j<total) { for(i=1;i<=n;i++) { if(b[i]==0) continue; if(b[i]>t) { printf("\tP%d",i); finish=start+t; j+=t; start=finish; b[i]-=t; } else { printf("\tP%d",i); finish=start+b[i]; j+=b[i]; start=finish; f[i]=finish; b[i]=0; w[i]=finish-b1[i]; tat[i]=w[i]+b1[i]; } 39

} } printf("\n\nJOB\tSERVICE TIME\t TURNAROUND TIME\tWAITING TIME"); for(i=1;i<=n;i++) { printf("\nP%d\t%d\t\t%d\t\t%d",i,b1[i],tat[i],w[i]); awt+=w[i]; atat+=tat[i]; } awt/=n; atat/=n; printf("\n average waiting time is %f",awt); printf("\n average turnaroundtime is %f\n",atat); } OUTPUT: enter no of process3 enter the time quantum2 enter the burst time for process P1 24 enter the burst time for process P2 3 enter the burst time for process P3 3 P1 P2 P3 P1 P2 P3 P1 P1 P1 P1 P1 P1 P1 P1 P1 P1 WAITING TIME

JOB SERVICE TIME TURNAROUND TIME P1 24 30 6 P2 3 9 6 P3 3 10 7 average waiting time is 6.333333 average turnaroundtime is 16.333334

40

GREP SIMULATION PROGRAM

#include<stdio.h> int main(int argc,char *argv[]) { FILE *f1; char c,s[50]; int i=0,k=0,cmp=0; f1=fopen(argv[2],"r"); if(f1==NULL) printf("file doesnot exit"); else { while(!feof(f1)) { fscanf(f1,"%c",&c); s[i]=c; if(c!='\n') { if(cmp==strlen(argv[1])) i++; else if(c==argv[1][k]) { cmp++; k++; i++; } else { k=0; cmp=0; i++; } } else { s[i]='\0'; if(cmp==strlen(argv[1])) fprintf(stdout,"%s\n",s); strcpy(s," "); i=0; k=0; cmp=0; } 41

} } } OUTPUT: Hai

42

FILE ALLOCATION USING LINKED LIST


#include<stdio.h> #include<stdlib.h> #include<string.h> void print(int); struct { char data[10]; int link; }fat[20]; int table[5]; main() { int fid; char ch; strcpy(fat[0].data,"hello"); fat[0].link=2; strcpy(fat[1].data,"wat"); fat[1].link=3; strcpy(fat[2].data,"hello"); fat[2].link=4; strcpy(fat[3].data,"my"); fat[3].link=5; strcpy(fat[4].data,"is"); fat[4].link=9; strcpy(fat[5].data,"karthik"); fat[5].link=6; strcpy(fat[6].data,"EOF"); fat[6].link=0; strcpy(fat[7].data,"SELVA"); fat[7].link=8; strcpy(fat[8].data,"EOF"); fat[8].link=0; strcpy(fat[9].data,"is"); fat[9].link=7; strcpy(fat[10].data,"hello"); fat[10].link=11; strcpy(fat[11].data,"do you"); fat[11].link=12; strcpy(fat[12].data,"know me"); fat[12].link=8; table[0]=0; table[1]=1; table[2]=10; while(1) 43

{ printf("enter the file id"); scanf("%d",&fid); print(fid); printf("continue......(y/n)"); scanf("%s",&ch); if((ch=='n')||(ch=='N')) break; } } void print(int fid) { int start; start=table[fid]; while(1) { if(strcmp(fat[start].data,"EOF")==0) break; printf("%s",fat[start].data); start=fat[start].link; } printf("\n"); } OUTPUT: enter the file id0 hellohelloisisSELVA continue......(y/n)y enter the file id3 hellohelloisisSELVA continue......(y/n)y enter the file id2 hellodo youknow me continue......(y/n)n

44

IPC USING SHARED MEMORY


#include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/types.h> #include<sys/msg.h> #include<sys/ipc.h> #define MAX_TEXT 512 #define BUF 50 struct my_msg_st { long int my_msg_type; char some_text[BUF]; }; int main() { int running=1,msgid; struct my_msg_st some_data; char buffer[BUF]; msgid=msgget((key_t)123,0666|IPC_CREAT); if(msgid==-1) { printf(stderr,"msgget failed\n"); exit(0); } while(running) { printf("enter some text:"); fgets(buffer,BUF,stdin); some_data.my_msg_type=1; strcpy(some_data.some_text,buffer); if(msgsnd(msgid,(void *)&some_data,MAX_TEXT,0)==-1) { printf(stderr,"msgsnd failed with error\n"); exit(0); } if(strncmp(buffer,"end",3)==0) { running=0; } } exit(1); } 45

OUTPUT: enter some text:my brothers enter some text:naveen enter some text:prabhu enter some text:kevin enter some text:end

46

CLIENT SERVER
#include<stdio.h> #include<stdlib.h> #include<sys/ipc.h> #include<sys/msg.h> struct my_msg_st { long int my_msg_type; char some_text[BUFSIZ]; }; int main() { int running=1,msgid; struct my_msg_st some_data; long int msg_to_receive=0; msgid=msgget((key_t)123,0666|IPC_CREAT); if(msgid==-1) { printf(stderr,"msgget failed\n"); exit(0); } while(running) { if(msgrcv(msgid,(void *)&some_data,BUFSIZ,msg_to_receive,0)==-1) { fprintf(stderr,"msgnd failed with error\n"); exit(0); } printf("you wrote:%s",some_data.some_text); if(strncmp(some_data.some_text,"end",3)==0) { running=0; } } exit(1); } OUTPUT: you wrote:my brothers you wrote:naveen you wrote:prabhu you wrote:kevin you wrote:end

47

Вам также может понравиться