Академический Документы
Профессиональный Документы
Культура Документы
#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
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
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
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
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
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
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
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
19
20
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
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
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
32
} 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
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
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
} } 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
#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
{ 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
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