Beruflich Dokumente
Kultur Dokumente
#include<stdio.h>
main()
{
int id;
printf("Before fork()\n");
id=fork();
if(id==0)
{
printf("Child has started: %d\n ",getpid());
printf("Parent of this child : %d\n",getppid());
printf("child prints 1 item :\n ");
sleep(10);
printf("child prints 2 item :\n");
}
else
{
printf("Parent has started: %d\n",getpid());
printf("Parent of the parent proc : %d\n",getppid());
}
printf("After fork()");
}
#include <stdio.h>
void main()
{
int i, fact = 1, num;
PROCESS CREATION
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(void)
pid_t childPID;
int var_lcl = 0;
childPID = fork();
var_lcl++;
var_glb++;
var_lcl = 10;
var_glb = 20;
{
printf("\n Fork failed, quitting!!!!!!\n");
return 1;
return 0;
ZOMBIE PROCESSS
#include<stdio.h>
int main()
int pid;
pid=fork();
if(pid>0)
sleep(30);
else if(pid==0)
return 1;
ORPHAN PROCESS
#include<stdio.h>
int main()
int pid;
pid=fork();
if(pid>0)
else if(pid==0)
sleep(30);
return 1;
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t p;
if(p==0) {
/* fork() returns Zero to child */
sleep(10);
}
printf("The child process pid is %d parent pid %d\n", getpid(), getppid());
/*parent/child waits for 20 secs and exits*/
sleep(20);
printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());
return 0;
}
Server Program
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
char c;
int shmid;
key_t key;
* "5678".
*/
key = 5678;
/*
*/
perror("shmget");
exit(1);
/*
*/
perror("shmat");
exit(1);
/*
*/
s = shm;
*s++ = c;
*s = NULL;
/*
* we put there.
*/
sleep(1);
exit(0);
Client Program
/*
*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
int shmid;
key_t key;
/*
*/
key = 5678;
/*
*/
perror("shmget");
exit(1);
/*
*/
perror("shmat");
exit(1);
/*
*/
putchar(*s);
putchar('\n');
/*
* the segment.
*/
*shm = '*';
exit(0);
void initialize()
{
pthread_mutex_init(&mutex,NULL);
sem_init(&full,1,0);
sem_init(&empty,1,buffersize);
counter=0;
}
int main()
{
int n1,n2,i;
initialize();
printf("\nEnter the no of producers: ");
scanf("%d",&n1);
printf("\nEnter the no of consumers: ");
scanf("%d",&n2);
for(i=0;i<n1;i++)
pthread_create(&tidP[i],NULL,producer,NULL);
for(i=0;i<n2;i++)
pthread_create(&tidC[i],NULL,consumer,NULL);
for(i=0;i<n1;i++)
pthread_join(tidP[i],NULL);
for(i=0;i<n2;i++)
pthread_join(tidC[i],NULL);
//sleep(5);
exit(0);
}
DINING PHILOSPHER
Problem Description
problem using threads. The input to the program is the number of philosophers to be seated
around the table. Output shows the various stages that each philosopher passes through
within a certain time. A philosopher can be in anyone of the three stages at a time: thinking,
The arrays represent the philosophers and corresponding chopsticks for them.Each element
in the philosopher’s array corresponds to a thread and each element in the chopstick’s array
5. pthread_join (ptr to thread, &msg)-This function will make the main program
7. pthread_exit(NULL)
Algorithm
6. Stop.
1. Start.
2. Philosopher i is thinking.
5. Philosopher i is eating.
6. sleep
10. Stop.
1. #include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
pthread_t philosopher[5];
pthread_mutex_t chopstick[5];
int main()
{
int i,k;
void *msg;
for(i=1;i<=5;i++)
k=pthread_mutex_init(&chopstick[i],NULL);
if(k==-1)
exit(1);
for(i=1;i<=5;i++)
if(k!=0)
exit(1);
for(i=1;i<=5;i++)
k=pthread_join(philosopher[i],&msg);
if(k!=0)
exit(1);
for(i=1;i<=5;i++)
k=pthread_mutex_destroy(&chopstick[i]);
if(k!=0)
{
printf(“\n Mutex Destroyed \n”);
exit(1);
return 0;
}void *func(int n)
pthread_mutex_lock(&chopstick[(n+1)%5]);
sleep(3);
pthread_mutex_unlock(&chopstick[n]);
pthread_mutex_unlock(&chopstick[(n+1)%5]);
BANKERS ALGORITHM
#include<iostream>
int pt[10];
int work[5],finish[5],i,j,k=0;
for(i=0;i<r;i++)
work[i]=avlb[i];
for(i=0;i<p;i++)
finish[i]=0;
i=0;
while(k<p)
if(finish[i]==0)
j=0;
while(j<r)
if(need[i][j]>work[j]) break;
j++;
if(j==r)
j=0;
while(j<r)
work[j]+=alloc[i][j];
j++;
pt[k]=i;
k++;
finish[i]=1;
i=(i+1)%p;
return pt;
}
int main()
int avlb[5],alloc[5][10],need[5][10],max[5][10],rq[5],i,j,r,t,p,rp,flag;
cin>>p;
cin>>r;
for(i=0;i<r;i++)
cin>>avlb[i];
for(i=0;i<p;i++)
for(j=0;j<r;j++)
cin>>alloc[i][j];
for(i=0;i<p;i++)
for(j=0;j<r;j++)
cin>>max[i][j];
for(i=0;i<r;i++)
t=0;
for(j=0;j<p;j++)
{
t+=alloc[j][i];
need[j][i]=max[j][i]-alloc[j][i];
avlb[i]-=t;
cout<<"\t "<<avlb[i];
for(i=0;i<p;i++)
for(j=0;j<r;j++)
cout<<"\t"<<need[i][j];
cout<<endl;
safety(need,avlb,alloc,p,r);
for(i=0;i<p;i++)
cout<<"P"<<pt[i]<<"\t";
cout<<endl;
cin>>rp;
for(i=0;i<r;i++)
flag=0;
cin>>rq[i];
if(rq[i]>need[rp][i])
flag=1;
break;
else
for(i=0;i<r;i++)
if(rq[i]>avlb[i])
flag=2;
break;
if(flag==2)
else{
for(j=0;j<r;j++)
avlb[j]-=rq[j];
alloc[rp][j]+=rq[j];
need[rp][j]-=rq[j];
safety(need,avlb,alloc,p,r);
for(i=0;i<p;i++)
cout<<"P"<<pt[i]<<"\t";
}
return 0;
Best Fit
#include<stdio.h>
#include<conio.h>
int main()
{
int ns,np,i,j,count,temp1,temp2;
int segments[10],request[10],flag[10],position[10];
clrscr();
for(i=0;i<ns;i++)
{
printf("\nEnter the Size of page segment%d:",(i+1));
scanf("%d",&segments[i]);
flag[i]=0;
position[i]=(i+1);
}
for(i=0;i<np;i++)
{
printf("\nEnter the Size of required by segment%d:",(i+1));
scanf("%d",&request[i]);
}
clrscr();
if(np<=ns)
{
for(i=0;i<np;i++)
{
count=0;
while(count<ns)
{
count++;
}
if(count==ns)
printf("\nThere is no page available of size process p%d to
allocate!!!!",(i+1));
}
}
else
{
printf("\nThe number of requried pages is more than number of pages
available");
}
for(i=0;i<ns;i++)
{
flag[i]=0;
for(j=0;j<ns;j++)
{
if(segments[i]<segments[j])
{
temp1=segments[i];
segments[i]=segments[j];
segments[j]=temp1;
temp2=position[i];
position[i]=position[j];
position[j]=temp2;
if(np<=ns)
{
for(i=0;i<np;i++)
{
count=0;
while(count<ns)
{
count++;
}
if(count==ns)
printf("\nThere is no page available of size process p%d to
allocate!!!!",(i+1));
}
}
else
{
printf("\nThe number of requried pages is more than number of pages
available");
}
getch();
return 0;
}
First Fit
#include<stdio.h>
#include<conio.h>
int main()
{
int ns,np,i,j,count;
int segments[10],request[10],flag[10];
clrscr();
for(i=0;i<ns;i++)
{
printf("\nEnter the Size of page segment%d:",(i+1));
scanf("%d",&segments[i]);
flag[i]=0;
}
for(i=0;i<np;i++)
{
printf("\nEnter the Size of required by segment%d:",(i+1));
scanf("%d",&request[i]);
}
clrscr();
printf("\n\nFIRST FIT MEMORY MANAGEMENT ALOGRITH:");
if(np<=ns)
{
for(i=0;i<np;i++)
{
count=0;
while(count<ns)
{
count++;
}
if(count==ns)
printf("\nThere is no page available of size process p%d to
allocate!!!!",(i+1));
}
}
else
{
printf("\nThe number of requried pages is more than number of pages
available");
}
getch();
return 0;
#include<stdio.h>
#include<conio.h>
int main()
{
int n,m,i,j,k,a,flag=0,pos,hits=0,flag1=0,pos1=0;
int fr[30][10],p[30];
for(i=0;i<n;i++)
{
flag=0;flag1=0;
if(i>0)
{
a=i;
for(k=0;k<m;k++)
{
fr[i][k]=fr[a-1][k];
}
}
for(j=0;j<m;j++)
{
if(p[i]==fr[i][j])
{
flag=1;
hits++; }
}
if(flag==0)
{
for(k=0;k<m;k++)
{
if(fr[i][k]==0)
{ pos=k;
flag1=1;
break;
}
}
if(flag1==1)
{
fr[i][pos]=p[i];
pos1=pos+1;
if(pos1>=m) pos1=0;
}
else
{
fr[i][pos1]=p[i];
pos1=pos1+1;
if(pos1>=m) pos1=0;
}
}
else continue;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d",fr[i][j]);
}
printf("\n");
}
printf("\n\nThe no of page faults is:%d",n-hits);
getch();
return 0;
}
#include<conio.h>
#include<stdio.h>
int pa[30];
int fr[30][10];
int i=0;
int nf,np;
int getpos()
{
int j,k;
int min,ret;
int temp[10][2];
for(j=0;j<nf;j++)
temp[j][0]=fr[i-1][j];
for(k=0;k<nf;k++)
for(j=0;j<i;j++)
if(temp[k][0]==pa[j])
{
temp[k][1]=j;
}
min=temp[0][1];
ret=0;
for(j=0;j<nf;j++)
{
if(temp[j][1]<min)
{
min=temp[j][1];
ret=j;
}
}
return ret;
}
void main()
{
int j;
int pos=0,temp,hits=0,flag=0,flag1=0;
printf("Enter number of pages");
scanf("%d",&np);
printf("Enter frame size");
scanf("%d",&nf);
for(i=0;i<np;i++)
for(j=0;j<nf;j++)
fr[i][j]=0;
for(i=0;i<np;i++)
{
flag=0;
flag1=0;
if(i>0)
{
temp=i;
for(j=0;j<nf;j++)
fr[i][j]=fr[temp-1][j];
}
for(j=0;j<nf;j++)
{
if(fr[i][j]==pa[i])
{
hits++;
flag=1;
}
}
if(flag==0)
{
for(j=0;j<nf;j++)
{
if(fr[i][j]==0)
{
flag1=1;
}
}
if(flag1==1)
{
fr[i][pos]=pa[i];
pos+=1;
}
else if(flag1==0)
{
pos=getpos();
fr[i][pos]=pa[i];
}
}
}
clrscr();
printf("\nLEAST RECENTLY USED ALGORITH");
printf("\nFrame1 frame2 frame3\n");
for(i=0;i<np;i++)
{
for(j=0;j<nf;j++)
printf("%d\t",fr[i][j]);
printf("\n");
}
printf("\nNO of page faults:%d",np-hits);
}
Aim:
Theory
a)Sequential file allocation strategy: In this type of strategy, the files are allocated in a sequential
manner such that there is a continuity among the various parts or fragments of the file.
b)Indexed file allocation strategy: In this type of strategy, the files are allocated based on the indexes
that are created for each fragment of the file such that each and every similar indexed file is
maintained by the primary index thereby providing flow to the file fragments.
c)Linked file allocation strategy: In this type of strategy, the files are allocated in a linked list format
where each and every fragment is linked to the other file through either addresses or pointers. Thus,
the starting location of the file serves for the purpose of extraction of the entire file because every
fragment is linked to each other.
#include
#include
main()
int f[50],i,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
X:
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
f[j]=1;
printf("\n%d->%d",j,f[j]);
else
break;
if(j==(st+len))
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
#include
#include
main()
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
scanf("%d",&p);
for(i=0;i{
scanf("%d",&a);
f[a]=1;
X:
scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
f[j]=1;
printf("\n%d->%d",j,f[j]);
else
k++;
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );
Output:
#include
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:
scanf("%d",&p);
if(f[p]==0)
f[p]=1;
scanf("%d",&n);
else
goto x;
for(i=0;iscanf("%d",&inde[i]);
for(i=0;iif(f[inde[i]]==1)
goto x;
for(j=0;jf[inde[j]]=1;
printf("\n allocated");
for(k=0;kprintf("\n %d->%d:%d",p,inde[k],f[inde[k]]);
scanf("%d",&c);
if(c==1)
goto x;
else
exit();
getch();
Output: