Top Banner
QUES.1 WRITE A PROGRAM (USING FORK() AND EXEC()) WHERE PARENT AND CHILD EXECUTE: A) SAME PROGRAM, SAME CODE CODE: #include<stdio.h> int main() { int pid; pid=fork(); if(pid==-1) { printf("fork failed\n"); exit(-1); } else { printf("my process identifier =%d \n",pid); exit(0); } } Output: [user3@localhost user3]$ cc p1a.c [user3@localhost user3]$ ./a.out my process identifier =0 my process identifier =8217
29

OS Practical

Nov 08, 2014

Download

Documents

operating system practicals 4th sem du bsc
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OS Practical

QUES.1 WRITE A PROGRAM (USING FORK() AND EXEC()) WHERE PARENT AND CHILD EXECUTE:

A) SAME PROGRAM, SAME CODE

CODE:#include<stdio.h>int main(){int pid;pid=fork();if(pid==-1){printf("fork failed\n");exit(-1);}else{printf("my process identifier =%d \n",pid);exit(0);}}

Output:[user3@localhost user3]$ cc p1a.c[user3@localhost user3]$ ./a.outmy process identifier =0my process identifier =8217

Page 2: OS Practical

B) SAME PROGRAM, DIFFERENT CODE

CODE:#include<stdio.h>int main(){ int pid; pid=fork(); if(pid==-1) { printf("fork failed\n"); exit(-1); } else if(pid==0) { printf("i am a child process\n"); exit(0); } else if(pid>0) { printf("i am a parent process\n"); exit(0); }}

OUTPUT:[user3@localhost user3]$ cc p1b.c[user3@localhost user3]$ ./a.outi am a child processi am a parent process

C) DIFFERENT PROGRAMS

Page 3: OS Practical

CODE:#include<stdio.h>int main(){ int pid; pid=fork(); if(pid==-1) { printf("fork failed\n"); exit(-1); } else if(pid==0) { execlp("/bin/ls","ls",NULL); exit(0); } else if(pid>0) { printf("i am a parent process\n"); exit(0); }}

OUTPUT: [user3@localhost user3]$ cc p1c.c

[user3@localhost user3]$ ./a.out03 cient.c client2.out new.c richsan serv.c tanvi26 clent.c client.c p1a.c ser1.c server1.c5.c client1.c clnt1.out p1b.c ser1.out server1.outa.out client1.out clnt.c p1c.c ser.c server2.outi am a parent process

D) PARENT WILL FAIL FOR CHILD CHILD TO TERMINATE

Page 4: OS Practical

CODE:#include<stdio.h>int main(){ int pid; pid=fork(); if(pid==-1) { printf("fork failed\n"); exit(-1); } else if(pid==0) { execlp("/bin/ls","ls",NULL); exit(0); } else if(pid>0) { wait(NULL); printf("i am a parent process\n"); printf("child terminated"); exit(0); }}

OUTPUT:[user3@localhost user3]$ cc p1d.c[user3@localhost user3]$ ./a.out03 cient.c client2.out new.c p1d.c ser.c server2.out26 clent.c client.c p1a.c richsan serv.c tanvi5.c client1.c clnt1.out p1b.c ser1.c server1.ca.out client1.out clnt.c p1c.c ser1.out server1.outi am a parent process

Page 5: OS Practical

E) WHERE PARENT AND CHILD CAN EXECUTE DIFFERENT PROGRAM AND A CHILD PROCESS IS EXECUTING A USER CREATED FILE.

CODE:#include<stdio.h>int main(){ int pid; pid=fork(); if(pid==-1) { printf("fork failed\n"); exit(-1); } else if(pid==0) { execlp("/home/user3/square","square",NULL); exit(0); } else if(pid>0) { printf("i am a parent process\n"); wait(NULL); printf("child terminated"); exit(0); }}

Vi square.c#include<stdio.h>int main(){int x;printf("enter a no.\n");

Page 6: OS Practical

scanf("%d",&x);printf("square=%d",x*x);exit(0);}

OUTPUT:[user3@localhost user3]$ cc p1e.c[user3@localhost user3]$ ./a.outenter a no.6square=36i am a parent processchild terminated

F) HIERARCHY OF PROCESS

Page 7: OS Practical

CODE:#include<stdio.h>int main(){ int pid; pid=fork(); if(pid==-1) { printf("fork failed\n"); exit(-1); } else if(pid==0) { int cid; cid=fork(); if(cid==0) { printf("i am the grandchild\n"); exit(0); } else { printf("i am a child\n"); exit(0); } } else if(pid>0) { printf("i am a parent process\n"); exit(0); }}

OUTPUT:[user3@localhost user3]$ ./a.outi am the grandchildi am a childi am a parent process

Page 8: OS Practical

QUES.2(a) WRITE A PROGRAM TO DEMONSTRATE INTER PROCESS COMMUNICATION BETWEEN PARENT AND CHILD.CODE:#include<stdio.h>int main(){ int fd[2],pid; int n; char line[20]; if(pipe(fd) < 0) { printf("Error in creating pipe"); } else{ pid=fork(); if(pid < 0) printf("Fork failed"); else if (pid > 0) { close(fd[0]); write(fd[1],"Hello World",12); } else if(pid==0) { close(fd[1]); n=read(fd[0],line,20); if(n<0) printf("Error in reading");write(1,line,n);}}return 0;}OUTPUT:[user3@localhost user3]$ cc prog2.c

Page 9: OS Practical

[user3@localhost user3]$ ./a.out[user3@localhost user3]$ Hello World

QUES 2(b) : WRITE A PROGRAM TO DEMONSTRATE INTER PROCESS COMMUNICATION BETWEEN PARENT AND CHILD(FULL DUPLEX).CODE :#include<stdio.h>int main(){ int fd1[2],fd2[2],pid,n; char line[20]; if(pipe(fd1)<0) { printf("Error creating file"); } else { pid=fork(); if(pid<0) printf("Fork failed"); else if(pid>0) { close(fd1[0]); write(fd1[1],"Hello World",12); } else if(pid==0) { close(fd1[1]); n=read(fd1[0],line,20); if(n<0) printf("Error in reading"); write(1,line,n); } } if(pipe(fd2)<0) { printf("Error creating file"); } else { pid=fork(); if(pid<0) printf("Fork failed"); else if(pid==0) { close(fd2[0]);

Page 10: OS Practical

write(fd2[1],"Good Bye",9); } else if(pid>0) { close(fd2[1]); n=read(fd2[0],line,20); if(n<0) printf("Error in reading"); write(1,line,n); } }return 0;}

OUTPUT :[user3@localhost user3]$ cc q2b.c[user3@localhost user3]$ ./a.outGood Bye[user3@localhost user3]$ Hello World Good Bye

QUES 3 : WRITE A PROGRAM TO REPORT A LINUX KERNEL BEHAVIOR INCLUDING CPU TYPE AND MODEL,KERNEL VERSION,INFORMATION ON CONFIGURED MEMORY,AMOUNT OF FREE AND USED MEMORY.CODE :#include<stdio.h>int main(){ system("echo Amount of time CPU has spend in User Mode : "); system("cat /proc/stat|grep -m1 'cpu'|cut -c 6-9"); system("echo"); system("echo Amount of time CPU has spend in System Mode : " ); system("cat /proc/stat|grep -m1 'cpu'|cut -c 11"); system("echo"); system("echo Amount of time CPU has spend in Idle Mode : "); system("cat /proc/stat|grep -m1 'cpu'|cut -c 13-16"); system("echo"); system("echo Number of context switches : "); system("cat /proc/stat|grep -m1 'ctxt'|cut -c 6-11"); system("echo"); system("echo Number of processes created sinc system was last booted : "); system("cat /proc/stat|grep -m1 'processes'|cut -c 11-14");

Page 11: OS Practical

system("echo"); system("echo CPU type : "); system("cat /proc/cpuinfo|grep -m1 'cpu'|cut -c 13-15"); system("echo"); system("echo CPU model : "); system("cat /proc/cpuinfo|grep -m1 'model name'"); system("echo"); system("echo Kernel Version : "); system("cat /proc/sys/kernel/osrelease"); system("echo"); system("echo Total memory in KB : "); system("free -o |grep 'Mem'|cut -c 10-20"); system("echo"); system("echo Used memory in KB : "); system("free -o |grep 'Mem'|cut -c 20-30"); system("echo"); system("echo Free memory in KB : "); system("free -o |grep 'Mem'|cut -c 30-40"); system("echo");return 0;}

OUTPUT :

Amount of time CPU has spend in User Mode :8150

Amount of time CPU has spend in System Mode :1

Amount of time CPU has spend in Idle Mode :62 5

Number of context switches :113578

Number of processes created sinc system was last booted :8656

Page 12: OS Practical

CPU type : 15

CPU model :model name : Intel(R) Pentium(R) 4 CPU 3.00GHz

Kernel Version :2.4.20-8smp

Total memory in KB : 504368

Used memory in KB : 481576

Free memory in KB : 22792

Ques 4.a) Write a program to print file details includeing owner access permissions, file access time, where filename is given an argument.

#include<stdio.h>#include<stdlib.h>#include<sys/stat.h>int main(int argc, char *argv[]){struct stat statbuf;int fd;fd = open(argv[1],0,0744);if(fd==-1){printf("file not opened");exit(0);}fstat(fd,&statbuf);close(fd);printf("\n owner id is %d", statbuf.st_uid);printf("\n group id is %d", statbuf.st_gid);printf("\n size is %d", statbuf.st_size);printf("\n no. of links is %d", statbuf.st_nlink);printf("\n last access time is %s", ctime(&statbuf.st_atime));

Page 13: OS Practical

printf("\n last modification time is %s", ctime(&statbuf.st_mtime));return 0;}

OUTPUT:

[user3@localhost richsan]$ cc prog4a.c[user3@localhost richsan]$ ./a.out file.c owner id is 502

group id is 502

size is 11

no. of links is 1

last access time is Mon Feb 18 10:46:38 2013

last modification time is Mon Feb 18 10:46:38 2013

Ques 4.b) Write a print information about a file if it exists else create the file and then print information.

#include<stdio.h>#include<stdlib.h>#include<sys/stat.h>#include<fcntl.h>int main(int argc, char *argv[]){struct stat statbuf;int fd;fd = open(argv[1],O_RDONLY|O_CREAT,0744);if(fd==-1){printf("file not opened");exit(0);}fstat(fd,&statbuf);close(fd);printf("\n owner id is %d", statbuf.st_uid);printf("\n group id is %d", statbuf.st_gid);printf("\n size is %d", statbuf.st_size);printf("\n no. of links is %d", statbuf.st_nlink);

Page 14: OS Practical

printf("\n last access time is %s", ctime(&statbuf.st_atime));printf("\n last modification time is %s", ctime(&statbuf.st_mtime));return 0;}

OUTPUT:[user3@localhost richsan]$ cc prog4b.c[user3@localhost richsan]$ ./a.out file1.cowner id is 502

group id is 502

size is 0

no. of links is 1

last access time is Mon Feb 18 10:58:58 2013

last modification time is Mon Feb 18 10:58:58 2013

QUES 5 : WRITE A PROGRAM TO COPY FILES USING SYSTEM CALLS.CODE :#include<stdio.h>int main(int argc,char *argv[]){ char buf[500]; int fd1,fd2,count; fd1=open(argv[1],0); if(fd1== -1) { printf("File cannot be opened"); exit(-1); } fd2=creat(argv[2],0744); if(fd2==-1) { printf("File cannot be created"); exit(-1); } count=read(fd1,buf,sizeof(buf));

Page 15: OS Practical

write(fd2,buf,count); return 0;}

OUTPUT:user3@localhost user3]$ cc prog3.c [user3@localhost user3]$ ./a.out f1 f2[user3@localhost user3]$ cat f2hello....this is the lab of operating system..you are welcomed ...

QUES 6(a) : IMPLEMENT FCFS WITH ARRIVAL TIME 0 FOR ALL PROCESSES.CODE :#include<stdio.h>int main(){int P[50],B[50],C[50],W[50],T[50];int sum1=0,sum2=0;int i,n;printf("Enter the number of processes : ");scanf("%d",&n);for(i=0;i<n;i++){ P[i]=i+1; printf("Enter the burst time for process %d",i+1); scanf(" %d",&B[i]);}for(i=0;i<n;i++){ C[0]=B[0]; C[i]=C[i-1]+B[i];}for(i=1;i<n;i++){ W[i]=C[i-1];}for(i=0;i<n;i++){ T[i]=C[i];}printf("PROCESSES WAITING TIME TURNAROUND TIME");for(i=0;i<n;i++)

Page 16: OS Practical

printf("\n%d\t\t%d\t\t%d",P[i],W[i],T[i]);\for(i=1;i<n;i++){ sum1=sum1+W[i];}for(i=0;i<n;i++){ sum2=sum2+T[i];}printf("The Average waiting time : %d",(sum1/n));printf("The Average turnaround time : %d",(sum2/n));return 0;}

OUTPUT :[user3@localhost user3]$ cc ques6a.c[user3@localhost user3]$ ./a.outEnter the number of processes : 4Enter the burst time for process 1 10Enter the burst time for process 2 12Enter the burst time for process 3 20Enter the burst time for process 4 05PROCESSES WAITING TIME TURNAROUND TIME1 0 102 10 223 22 424 42 47The Average waiting time : 18The Average turnaround time : 30

Page 17: OS Practical

QUES 6(b) : IMPLEMENT FCFS WITH GIVEN ARRIVAL TIME FOR ALL PROCESSES.CODE : #include<stdio.h>int main()printf("Enter the number of processes : ");scanf("%d",&n);for(i=0;i<n;i++){ P[i]=i+1; printf("Enter the burst time for process %d",i+1); scanf(" %d",&B[i]);}for(i=0;i<n;i++){ A[i]=i+1; printf("Enter the arrival time for process %d",i+1); scanf(" %d",&A[i]);}for(i=0;i<n;i++){ C[0]=B[0]; C[i]=C[i-1]+B[i];}for(i=1;i<n;i++){ W[i]=C[i-1]-A[i];}for(i=0;i<n;i++){ T[i]=C[i]-A[i];}

Page 18: OS Practical

printf("PROCESSES WAITING TIME TURNAROUND TIME");for(i=0;i<n;i++)printf("\n%d\t\t%d\t\t%d",P[i],W[i],T[i]);for(i=1;i<n;i++){ sum1=sum1+W[i];}for(i=0;i<n;i++){ sum2=sum2+T[i];}printf("The Average waiting time : %d",(sum1/n));printf("The Average turnaround time : %d",(sum2/n));return 0; }OUTPUT :[user3@localhost user3]$ cc ques6b.c[user3@localhost user3]$ ./a.outEnter the number of processes : 4Enter the burst time for process 1 10Enter the burst time for process 2 12Enter the burst time for process 3 20Enter the burst time for process 4 05Enter the arrival time for process 1 0Enter the arrival time for process 2 1Enter the arrival time for process 3 2Enter the arrival time for process 4 3PROCESSES WAITING TIME TURNAROUND TIME1 0 102 9 213 20 404 39 44The Average waiting time : 17The Average turnaround time : 28

Page 19: OS Practical

QUES 6(c) : IMPLEMENT ROUND ROBIN SCHEDULING.CODE :#include<stdio.h>int main(){ int P[50],B[50],j=0,ttime=0,tslice,n,i; printf("Enter the number of processes : "); scanf("%d",&n); for(i=0;i<n;i++) { P[i]=i+1; printf("Enter the burst time for process %d",i+1); scanf(" %d",&B[i]); } printf("Enter the time slice : "); scanf(" %d",&tslice); printf("PROCESSES REMAINING TIME TOTAL TIME"); while(j<=n) { for(i=0;i<n;i++) { if(B[i]>0) { if(B[i]>=tslice) { ttime=ttime+tslice; B[i]=B[i]-tslice; } else { ttime=ttime+B[i]; B[i]=0; } printf("\n%d\t\t%d\t\t%d",P[i],B[i],ttime); } } j++; } return 0;}

OUTPUT:

Page 20: OS Practical

[user3@localhost user3]$ cc ques6c.c[user3@localhost user3]$ ./a.outEnter the number of processes : 3Enter the burst time for process 1 : 4Enter the burst time for process 2 : 2Enter the burst time for process 3 : 3Enter the time slice : 2PROCESSES REMAINING TIME TOTAL TIME1 2 22 0 43 1 61 0 83 0 9

QUES 7(a) : USE PTHREADS TO CALCULATE THE SUM OF ‘N’ NON NEGATIVE NUMBERS.CODE :#include<pthread.h>#include<stdio.h>int sum;void *runner(void *param);int main(int argc,char *argv[]){ pthread_t tid; pthread_attr_t attr; if(argc!=2) { printf("Error : 2 arguments are needed"); exit(-1); } if(atoi(argv[1]) < 0) { printf("%d must be >= 0 \n",atoi(argv[1])); exit(-1); } pthread_attr_init(&attr); pthread_create(&tid,&attr,runner,argv[1]); pthread_join(tid,NULL); printf("Sum=%d",sum); } void *runner(void *param) { int i,upper=atoi(param);

Page 21: OS Practical

sum=0; for(i=1;i<=upper;i++) sum=sum+i; pthread_exit(0); }

OUTPUT :[user3@localhost user3]$ cc -lpthread prog7a.c -o prog7a[user3@localhost user3]$ ./prog7a 5Sum=15

QUES 7(b) : USE PTHREADS TO CALCULATE THE FACTORIAL OF ‘N’ NUMBERS.CODE :#include<pthread.h>#include<stdio.h>int fact;void *factorial(void *param);int main(int argc,char *argv[]){ pthread_t tid; pthread_attr_t attr; if(argc!=2) { printf("Error : 2 arguments are needed"); exit(-1); } if(atoi(argv[1]) < 0) { printf("%d must be >= 0 \n",atoi(argv[1])); exit(-1); } pthread_attr_init(&attr); pthread_create(&tid,&attr,factorial,argv[1]); pthread_join(tid,NULL); printf("Factorial = %d",fact); } void *factorial(void *param)

Page 22: OS Practical

{ int i,upper=atoi(param); fact=1; for(i=upper;i>=1;i--) fact=fact*i; pthread_exit(0); }

OUTPUT :[user3@localhost user3]$ cc -lpthread prog7b.c -o prog7b[user3@localhost user3]$ ./prog7b 5Factorial = 120

QUES 7(c) : USE PTHREADS TO PRINT THE FIBONACCI SERIES OF ‘N’ NUMBERS.CODE :#include<pthread.h>#include<stdio.h>int fib,x,y;int fib,x,y;void *fibonacci(void *param);int main(int argc,char *argv[]){ pthread_t tid; pthread_attr_t attr; if(argc!=2) { printf("Error : 2 arguments are needed"); exit(-1); } if(atoi(argv[1]) < 0) { printf("%d must be >= 0 \n",atoi(argv[1])); exit(-1); } pthread_attr_init(&attr); pthread_create(&tid,&attr,fibonacci,argv[1]); pthread_join(tid,NULL); } void *fibonacci(void *param)

Page 23: OS Practical

{ int i,upper=atoi(param); x=1; y=1; printf("Fibonacci Series = "); printf(" %d",x); printf(" %d",y); for(i=3;i<=upper;i++) { fib=y+x; printf(" %d",fib); x=y; y=fib; } pthread_exit(0);}

OUTPUT : [user3@localhost user3]$ cc -lpthread prog7c.c -o prog7c[user3@localhost user3]$ ./prog7c 5Fibonacci Series = 1 1 2 3 5

Page 24: OS Practical

QUES 7(d) : USE PTHREADS,CREATE 2 THREADS WHICH COMPUTE SUM OF ALL EVEN AND ODD NUMBERS UPTO ‘N’.CODE :#include<pthread.h>#include<stdio.h>int sum1,sum2;void *runner1(void *param);void *runner2(void *param);int main(int argc,char *argv[]){ pthread_t tid1,tid2; pthread_attr_t attr1,attr2; if(argc!=2) { printf("Error : 2 arguments are needed"); exit(-1); } if(atoi(argv[1]) < 0) { printf("The number must be >= 0 \n",atoi(argv[1])); exit(-1); } pthread_attr_init(&attr1); pthread_create(&tid1,&attr1,runner1,argv[1]); pthread_join(tid1,NULL); pthread_attr_init(&attr2); pthread_create(&tid2,&attr2,runner2,argv[1]);

Page 25: OS Practical

pthread_join(tid2,NULL); printf("Sum of odd numbers = %d ",sum1); printf("Sum of even numbers = %d",sum2);} void *runner1(void *param) { int i,upper=atoi(param); sum1=0; for(i=1;i<=upper;i=i+2) sum1=sum1+i; pthread_exit(0); } void *runner2(void *param) { int i,upper=atoi(param); sum2=0; for(i=0;i<=upper;i=i+2) sum2=sum2+i; pthread_exit(0); }

OUTPUT :[user3@localhost user3]$ cc -lpthread prog7d.c -o prog7d[user3@localhost user3]$ ./prog7d 10Sum of odd numbers = 25 Sum of even numbers = 30[user3@localhost user3]$ ./prog7d -2The number must be >= 0