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

LAB REPORT ON

OPERATING SYSTEM DESIGN


UNIX SYSTEM CALLS
SYSTEM CALL:
A system call is just what its name implies… a
request for the operating system to do something on behalf of
the user’s to do something on behalf of the user’s program.
The system calls are functions used in the kernel itself. TO the
programmer,the system call appears as a normal C function
call.
However since a system call excutes code in the
kernel,”there must be a mechanism to change the mode of a
process from user mode to kernel mode”.
The c compiler uses a predefined library of functions
that have the names of the system calls.
UNIX system calls are used to manage the file
system,control processes,and to provide interprocess
communication.
The UNIX system interface consists of about 80 system calls.
● Mainly there are three types of classes are
there.
1. Process related calls.
2. Interprocess communication
3. File structure related calls.
EXAMPLES OF SYSTEM CALLS
● “Create()”: or “Open()”:
All input and out put operations start by opening a file using
either the “Create()” or “Open()” system calls.
These calls return a file descriptor that identifies the I/O
channel.
Let us see how the program look like by using creat() system
call….
EX: /* create */
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
int main()
{
int fd;
fd=creat("datafile.data",S_IREAD|S_IWRITE);
if(fd==-1)
printf("error in opening datafile.data\n");
else
{
printf("datafile.data opend for read/write access\n");
printf("datafile.data is currently empty\n");
}
close (fd);
exit (0);
}
/* output */
/* datafile.data opend for read/write access".
datafile.data is curently empty". */
SYSTEM CALL : “Fork()”
The only way for a user to create a process in Unix operating
system.
● The process that invokes fork is called parent process and
the newly created process is called child process.
● Syntax:
Newpid=fork();
● In parent process,newpid=child processid.
● In child process,new pid=0.
EX:
/* fork() */
#include<stdio.h>
main()
{
Int fpid;
Printf (“before forking..\n”);
fpid=fork();
if(fpid==0)
{
Printf(“child process fid=%d\n”,fpid);
}
else
{
Printf(“parent process fpid=%d\n”,fpid);
}
Printf(“after forking fpid=%d\n”,fpid);
}
/* output*/
Before forking…
Child process fpid=0, after forking fpid=0,
Parent process fpid=14707.
After forking fpid=14707.

System call: “exec()”

○ exec() system call invokes another program by


replacing the current process.
○ No new process table entry is created for exec()
program.thus ,the total number of processes in the
system is not changed.
○ Six different exec functions:
Execlp,execvp,execl,execv,execle,execve.
● Exe system call allows a process to choose its successor.

EX: “Exec()”
# include<stdio.h>
#include<unistd.h>
main()
{
Printf(*”before execution..\n”);
Execl(“/bin/date”,”date”,0);
Printf(“after exec\n”);
}
/* output */
Before execution..
Mon April 1:30:15 CST 2010
/* this program displays the date on which we excute.,
Along with the time….*/
Q1: write a program to implement banker’s algorithem to
avoid deadlocks between n concurrent processes with m
resourses.
#include<stdio.h>

int f(int a[][10],int b[][10],int c[],int m,int n)


{
int i,j,k,l;
for(i=1;i<=m;i++)
{
l=0;for(j=1;j<=n;j++) if(b[i][j]<=c[j]) l++;
if(l==n)
{
for(j=1;j<=n;j++) c[j]=c[j]+a[i][j];
for(k=i;k<m;k++) { for(j=1;j<=n;j++) {
a[k][j]=a[k+1][j];b[k][j]=b[k+1][j];}}
m--;break;
}}
return m;
}
int g(int a[][10],int b[][10],int c[],int m,int n)
{
int i,t;
t=m;
for(i=1;i<=t;i++) m=f(a,b,c,m,n);
return m;
}
main()
{
int
i,j,m,n,av[10],max[10][10],need[10][10],allocate[10][10],a[10],
b[10][10],c[10][10],d[10][10];
printf("give no.of processes and no.of resourcses\n");
scanf("%d%d",&m,&n);
printf("give the available cash with bank\n");
for(i=1;i<=n;i++) scanf("%d",&av[i]);
printf("give the max need processwise\n");
for(i=1;i<=m;i++) for(j=1;j<=n;j++)
scanf("%d",&max[i][j]);
printf("give the allocations processwise\n");
for(i=1;i<=m;i++) for(j=1;j<=n;j++)
scanf("%d",&allocate[i][j]);
for(i=1;i<=m;i++) for(j=1;j<=n;j++) need[i][j]=max[i][j]-
allocate[i][j];
n=g(allocate,need,av,m,n);
if(n==0) printf("given state is safe");
else printf("given state is not safe because %d persons need
is not met",n);
}
// * output *//
give no.of processes and no.of resourcses
34
give the available cash with bank
3112
give the max need processwise
3322
1234
1150
give the allocations processwise
1221
1033
1110
given state is safe .

/*dining Philospher Problem */


#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
int take()
{ int t,r=0;
t=rand();
r=t%5;
return(r);
}
main()
{ int p,q,i,j,k,m,n,t;
int *p1=0,*p2=0,*p3=0,*p4=0,*p5,*ptr,shmid,*f,*g,*h;
shmid=shmget((key_t)1,20,IPC_CREAT|0666);
ptr=shmat(shmid,0,0);
p1=ptr; p2=ptr+1; p3=ptr+2; p4=ptr+3; p5=ptr+4; f=ptr+5;
g=ptr+6;h=ptr+7;
(*f)=0;
(*g)=0;
(*h)=0;
(*p1)=getpid();
fork();
if(getpid()!=(*p1))
{ (*p2)=getpid();
sleep(4);
}
else
{ fork();
if(( getpid()!=(*p1))&&(getpid()!=(*p2)))
{ (*p3)=getpid();
sleep(3);
}
}
if(getpid()==(*p1))
{ fork();

if((getpid()!=(*p3))&&(getpid()!=(*p2))&&(getpid()!=(*p1)))
{ (*p4)=getpid();
sleep(2);
}
}
if(getpid()==(*p1))
{ fork();

if((getpid()!=*p1)&&(getpid()!=*p2)&&(getpid()!=*p3)&&(ge
tpid()!=*p4))
(*p5)=getpid();
}
sleep(1);
do{
if(getpid()==(*p1))
{ k=*f;
while(k!=0)
k=*f;
if(*g==0)
{ printf("\n1st eating now and \n");
*h=1;
i=take();
if(i%2==0) *f=2;
else *f=3;
}
else
{ printf("1st also eating now \n");
(*h)=0;
i=take();
(*f)=i;
printf("\n");printf("\n");
}
*g=*h;
sleep(1);
}
if(getpid()==(*p2))
{ k=*f;
while(k!=1)
k=*f;
if(*g==0)
{ printf("\n2nd eating and \n");
*h=1;
i=take();
if(i%2==0) *f=3;
else *f=4;
}
else
{ printf("2nd also eating now \n");
(*h)=0;
i=take();
(*f)=i;
printf("\n");printf("\n");
}
*g=*h;
sleep(1);
}
if(getpid()==(*p3))
{ k=*f;
while(k!=2)
k=*f;
if(*g==0)
{ printf("\n3rd eating now \n");
*h=1;
i=take();
if(i%2==0) *f=4;
else *f=0;
}
else
{ printf("3rd also eating now \n");
(*h)=0;
i=take();
(*f)=i;
printf("\n");printf("\n");
}
*g=*h;
sleep(1);
}
if(getpid()==(*p4))
{ k=*f;
while(k!=3)
k=(*f);
if(*g==0)
{ printf("\n4th eating now \n");
*h=1;
i=take();
if(i%2==0) *f=0;
else *f=1;
}
else
{ printf("4th also eating now \n");
(*h)=0;
i=take();
(*f)=i;
printf("\n"); printf("\n");
}
*g=*h;
sleep(1);
}
if(getpid()==(*p5))
{ k=*f;
while(k!=4)
k=*f;
if(*g==0)
{ printf("\n5th eating and \n ");
*h=1;
i=take();
if(i%2==0) *f=1;
else *f=2;
}
else
{ printf("5th also eating now \n");
(*h)=0;
i=take();
(*f)=i;
printf("\n");printf("\n");
}
*g=*h;
sleep(1);
}
}while(*g!=2);
}
OUTPUT:
1st eating now and
4th also eating now
4th eating now
1st also eating now
1st eating now and
3rd also eating now
5th eating and
2nd also eating now
1st eating now and
3rd also eating now
4th eating now
1st also eating now
3rd eating now
5th also eating now
3rd eating now
5th also eating now
2nd eating and
5th also eating now
1st eating now and
4th also eating now
2nd eating and
5th also eating now
2nd eating and
4th also eating now
2nd eating and
4th also eating now
1st eating now and
3rd also eating now
1st eating now and
4th also eating now
1st eating now and
3rd also eating now
3rd eating now
5th also eating now
/* Sleeping Barber Problem */
#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/sem.h>
int take(int x)
{ int t,r=0;
t=rand();
r=t%x;
return(r);
}
main()
{ int p,q,i,j,k,m,n,t;
int
*b=0,*w=0,*ch[10],*ch2=0,*ch3,*ptr,shmid,*ch4,*ch5,*f,*co
unt,*e,*g,*h;
shmid=shmget((key_t)1,20,IPC_CREAT|0666);
ptr=shmat(shmid,0,0);
b=ptr; w=ptr+1; f=ptr+2;count=ptr+3;e=ptr+4;
g=ptr+10;h=ptr+11;
*f=0;
*g=0;
*h=10;
ch[1]=ptr+5;
ch[2]=ptr+6;
ch[3]=ptr+7;
ch[4]=ptr+8;
ch[5]=ptr+9;
*count=0;
*e=9;
*b=getpid();
fork();
if(getpid()!=(*b))
*w=getpid();
do{
if(getpid()==*b)
{ if((*f==0)&&(*ch[1]==0))
{ printf("\nBarber is sleeping\n");
k=*f;
while((k==0)&&(*ch[1]==0))
k=*f;
}
if((*f>0)&&(ch[1]!=0))
{ printf("\nCutting Customer id is %d waiting customers
is %d\n",*ch[1],*f-1);
j=take(10);
for(i=0;i<j;i++);
k=*f;
*e=*ch[1];
*ch[1]=*ch[k];
*f=*f-1;
*ch[k]=0;
*g=*g+1;
sleep(1);
}
}
if(getpid()==*e)
{ printf("\nCustomer who paid Rs-10 is %d \n ",getpid());
exit(0);
}
if((getpid()==*w)&&(*count<30))
{ k=*f;
while(k>4)
k=*f;
j=take(81000);
i=take(6);
if(i==3) *h=999999999;
else *h=10;
for(i=0;i<j+(*h);i++);
k=*f;
k=k+1;
fork();
if(getpid()!=*w)
{ *ch[k]=getpid();
printf("\n new customer id is %d\n",*ch[k]);
*count=*count+1;
}
if(getpid()==*w)
{ *f=k;
}
}
}while(*g<30);
printf("\n");printf("\n");
if((*count==30)&&(getpid()==*b))
{printf("\nTotal no of customers in that day is %d and total
is Rs==%d\n",(*count),10*(*g));
exit(0);
}
exit(0);
}
OUTPUT:
new customer id is 3060
Customer who paid Rs-25 is 3060
new customer id is 3063
Customer who paid Rs-25 is 3063
new customer id is 3064
Customer who paid Rs-25 is 3064
new customer id is 3065
Customer who paid Rs-25 is 3065
new customer id is 3066
Customer who paid Rs-25 is 3066
new customer id is 3067
Customer who paid Rs-25 is 3067
new customer id is 3068
Customer who paid Rs-25 is 3068
new customer id is 3069
Customer who paid Rs-25 is 3069
new customer id is 3070
Customer who paid Rs-25 is 3070
new customer id is 3062
Customer who paid Rs-25 is 3062
new customer id is 3061
Customer who paid Rs-25 is 3061
new customer id is 3071
Customer who paid Rs-25 is 3071
new customer id is 3075
Customer who paid Rs-25 is 3075
new customer id is 3076
Customer who paid Rs-25 is 3076
new customer id is 3074
Customer who paid Rs-25 is 3074
new customer id is 3073
Customer who paid Rs-25 is 3073
new customer id is 3072
Customer who paid Rs-25 is 3072
new customer id is 3077
Customer who paid Rs-25 is 3077
new customer id is 3078
Customer who paid Rs-25 is 3078
new customer id is 3079
Customer who paid Rs-25 is 3079
new customer id is 3080
Customer who paid Rs-25 is 3080
new customer id is 3084
Customer who paid Rs-25 is 3084
new customer id is 3086
Customer who paid Rs-25 is 3086
new customer id is 3087
Customer who paid Rs-25 is 3087
new customer id is 3083
Customer who paid Rs-25 is 3083
new customer id is 3082
Customer who paid Rs-25 is 3082
new customer id is 3081
Customer who paid Rs-25 is 3081
new customer id is 3089
new customer id is 3090
new customer id is 3088
Customer who paid Rs-25 is 3088
Cutting Customer id is 3060 waiting customers is 3
Cutting Customer id is 3063 waiting customers is 4
Cutting Customer id is 3064 waiting customers is 4
Cutting Customer id is 3065 waiting customers is 4
Cutting Customer id is 3066 waiting customers is 4
Cutting Customer id is 3067 waiting customers is 4
Cutting Customer id is 3068 waiting customers is 4
Cutting Customer id is 3069 waiting customers is 4
Cutting Customer id is 3070 waiting customers is 3
Cutting Customer id is 0 waiting customers is 2
Cutting Customer id is 3062 waiting customers is 1
Cutting Customer id is 3061 waiting customers is 0
Cutting Customer id is 3071 waiting customers is 4
Cutting Customer id is 3075 waiting customers is 4
Cutting Customer id is 3076 waiting customers is 3
Cutting Customer id is 3074 waiting customers is 2
Cutting Customer id is 3073 waiting customers is 1
Cutting Customer id is 3072 waiting customers is 0
Cutting Customer id is 3077 waiting customers is 1
Cutting Customer id is 3078 waiting customers is 2
Cutting Customer id is 3079 waiting customers is 1
Cutting Customer id is 0 waiting customers is 0
Cutting Customer id is 3080 waiting customers is 4
Cutting Customer id is 3084 waiting customers is 4
Cutting Customer id is 3086 waiting customers is 4
Cutting Customer id is 3087 waiting customers is 3
Cutting Customer id is 3083 waiting customers is 2
Cutting Customer id is 3082 waiting customers is 1
Cutting Customer id is 3081 waiting customers is 0
Cutting Customer id is 3088 waiting customers is 1
Total no of customers in that day is 30 and total is Rs==750
/*interupt handeler */

#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
FILE *outfile;
struct sigaction mysig;
//signal handling function
void leave(int sig) {
char inp;
printf("Are you sure to quit press Y for YES otherwise
NO: \n");
scanf("%c",&inp);
if(inp=='Y'||inp=='y') {
fprintf(outfile,"Interrupted...\n");
fclose(outfile);
exit(sig);
}
}
int main() {
int i;
struct sigaction oldsig;
sigemptyset(&mysig.sa_mask);
//old signal value
sigaction(SIGINT,NULL,&oldsig);
mysig.sa_handler=leave;
sigaction(SIGINT,&mysig,NULL);
outfile=fopen("signal.txt","w");
for(;;) {
for(i=0;;i++) {
if(i%10!=0) continue;
else{
fprintf(outfile,"%d-->satir\n",i);
printf("Ready...\n");
}
}
}
exit(0);
}

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