Beruflich Dokumente
Kultur Dokumente
x=x+et[i];
printf("\n %s -> %d",pn[i],et[i]);
et[i]=0;}
}
}
printf("\n Total Estimated Time:%d",x);
}
Output:
Enter the no of processes: 2
Enter the time quantum: 3
Enter the process name & estimated time: p1 12
Enter the process name & estimated time: p2 15
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p2 -> 3
Total Estimated Time: 27
b) SJF
#include <stdio.h>
#include <string.h>
struct process
{
int at,bt,st,wt,tt,ft,f;
char name;
}
main()
{
int n,n1,np,ftime,i,j,sum=0,sum1=0,temp2;
char temp1;
struct process p[100],temp;
float awt,atat;
clrscr();
printf("enter size");
scanf("%d",&n);
printf("enter process,at,bt");
for(i=0;i<n;i++)
{
scanf(" %c%d%d",&p[i].pr,&p[i].at,&p[i].bt);
p[i].f=0;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i].at>p[j].at)
{
temp2=p[i].at;
p[i].at=p[j].at;
p[j].at=temp2;
temp2=p[i].bt;
p[i].bt=p[j].bt;
p[j].bt=temp2;
temp1=p[i].pr;
p[i].pr=p[j].pr;
p[j].pr=temp1;
}
}
}
p[0].wt=0;
p[0].ft=p[0].at+p[0].bt;
p[0].tt=p[0].ft;
p[0].f=1;
ftime=p[0].ft;
n1=1;
while(n1<n)
{
3
for(i=1;i<n;i++)
{
if((p[i].at<=ftime)&&(p[i].f!=1))
np=i;
if((p[i].at<=ftime)&&(p[i].f!=1)&&(p[i].bt<p[np].bt))
np=i;
}
p[np].ft=ftime+p[np].bt;
p[np].tt=p[np].ft-p[np].at;
p[np].wt=ftime-p[np].at;
p[np].f=1;
ftime=p[np].ft;
n1++;
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(p[i].wt>p[j].wt)
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
printf("PROCESS\tATIME \tBTIME \tWTIME \tTATIME \n ");
for(i=0;i<n;i++)
{
printf(" %c\t%d\t%d\t%d\t%d\n",p[i].name,p[i].at,p[i].bt,p[i].wt,p[i].tt);
}
for(i=0;i<n;i++)
{
twt=twt+p[i].wt;
ttt=ttt+p[i].tt;
}
awt= (float)twt/n;
att=(float)ttt/n;
printf(avg. wt= %f \n,awt);
printf(avg. tt= %f\n,att);
}
Output:
Enter the no.of process:
4
Enter process, arrival time, burst time:
A
1
6
B
2
7
C
3
2
D
4
4
PROCESS
ATIME
BTIME
A
1
6
D
4
4
C
3
2
B
2
7
avg wt= 5.5
WTIME
0
3
8
11
TATIME
7
7
10
18
c) FCFS
#include <stdio.h>
#include <string.h>
struct process
{
char na;
int at,bt,st,wt,tat;
}p[10];
void main()
{
int i,j,n,t=0,sum=0,sum1=0;
float awt=0,atat=0;
clrscr();
printf("enter the no.of processess \n");
scanf("%d",&n);
printf("enter nameof the process,bt: \n");
for(i=0;i<n;i++)
{
scanf(" %c%d",&p[i].na,&p[i].bt);
}
p[0].st=0,p[0].wt=0,p[0].tat=p[0].bt;
for(i=1;i<n;i++)
{
p[i].st=p[i-1].tat+1;
p[i].wt=p[i-1].tat;
p[i].tat=p[i].st-1+p[i].bt;
}
printf("\nname \tst\twt\ttat\n");
for(i=0;i<n;i++)
{
printf(" %c\t%d\t%d\t%d\n",p[i].na,p[i].st,p[i].wt,p[i].tat);
}
for(i=0;i<n;i++)
{
sum=sum+p[i].wt;
sum1=sum1+p[i].tat;
}
awt=(float)sum/n;
atat=(float)sum1/n;
printf(" \n average waiting time=%f \n average turn around time=
%f\n",awt,atat);
getch();
}
Output:
Enter the size:
4
Enter the process, burst time
A
3
B
5
C
7
D
9
PROCESS
A
B
C
D
avg.wt=6.5
STIME
0
3
8
15
WTIME
0
3
8
15
avg.tt=12.5
TATIME
3
8
15
24
for(i=1;i<n;i++)
{
p[i].wt=p[i-1].et-p[i].at;
}
p[0].tat=p[0].et;
for(i=1;i<n;i++)
{
p[i].tat=p[i].et-p[i].at;
}
printf("\nname \tst\twt\ttat\n");
for(i=0;i<n;i++)
{
printf(" %c\t%d\t%d\t%d\n",p[i].na,p[i].st,p[i].wt,p[i].tat);
}
for(i=0;i<n;i++)
{
sum=sum+p[i].wt;
sum1=sum1+p[i].tat;
}
awt=(float)sum/n;
atat=(float)sum1/n;
printf(" \n average waiting time=%f \n average turn around time=
%f\n",awt,atat);
getch();
}
Output:
Enter the size:
4
Enter process arrival time, burst time:
A
1
6
B
2
7
C
3
2
D
4
4
PROCESS ATIMEBTIME
WTIME
A
1
6
B
2
7
C
3
2
D
4
4
Avg.wt=6.25
avg.tt=11.00
TATIME
0
6
4
11
10
12
11
15
d) Priority
#include<stdio.h>
#include<string.h>
void main()
{
int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
float awt,ata;
char pn[10][10],t[10];
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter process name,arrivaltime,execution time & priority:");
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
10
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage waiting time is:%f",awt);
printf("\nAverage turnaroundtime is:%f",ata);
}
Output:
Enter the number of process:3
Enter process name,arrivaltime,execution time & priority:1
2
3
1
Enter process name,arrivaltime,execution time & priority:2
4
5
2
Enter process name,arrivaltime,execution time & priority:3
5
6
3
Pname
arrivaltime executiontime
priority
1
2
3
1
0
2
4
5
2
1
3
5
6
3
5
Average waiting time is:2.000000
Average turnaroundtime is:6.666667
waitingtime tatime
3
6
11
11
12
Output:
Enter starting block and length of file
3 7
3 ->1
4 ->1
5 ->1
6 ->1
7 ->1
8 ->1
9 ->1
The file is allocated to disk.
If u want to enter more files? (y-1/n-0) 1
Enter starting block and length of file
4 8
Block already allocated
If u want to enter more files? (y-1/n-0) 1
13
b) Indexed
#include<stdio.h>
void main()
{
int f[50],i,k,j,index[50],n,c,count=0;
for(i=0;i<50;i++)
f[i]=0;
x:
printf("enter index block");
scanf("%d",&i);
if(f[i]!=1)
{
f[i]=1;
printf("enter no of files on index");
scanf("%d",&n);
}
y:
printf("enter files\n");
for(i=0;i<n;i++)
scanf("%d",&index[i]);
if(f[index[i]]==0)
count++;
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("\n allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d -> %d : %d",i,index[k],f[index[k]]);
}
else
{
printf("\n file in the index already allocated");
printf("\n enter another file indexed");
goto y;
}
printf("\n index is already allocated");
count=0;
printf("\nif you enter one more block(1/0)");
scanf("%d",&c);
if(c==1)
goto x;
}
14
Output:
Enter index block 5
Enter no. of files on index: 3
Enter files 667
668
669
Allocated
File indexed
3 ->667:1
3 ->668:1
3 ->669:1
Index is already allocated
If you enter one more block (1/0) 1
Enter index block 4
Enter no. of files on index: 2
Enter files 10
20
File in the index is already allocated
Enter another file index enter files
15
c) Linked
#include<stdio.h>
#include<stdlib.h>
void main()
{
int f[50],p,i,j,k,a,st,len,n,c;
for(i=0;i<50;i++)
f[i]=0;
printf("enter how many blocks that are already allocated");
scanf("%d",&p);
printf("enter block no's that are already allocated");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x:
printf("enter starting index block and length");
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
{
printf("\n%d->file is already allocated",j);
k++;
}
}
printf("\n if you want to enter one more file ?(yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto x;
else
exit(1);
}
16
Output:
Enter how many blocks that are already allocated 4
Enter block no.s that are already allocated 2 4 6 8
Enter starting index block and length 0 10
0 ->1
1 ->1
2 -> file is already allocated
3 ->1
4 -> file is already allocated
5 ->1
6 -> file is already allocated
7 ->1
8 -> file is already allocated
9 ->1
10 ->1
11 ->1
12 ->1
13 ->1
If you want to enter one more files? (yes-1/no-0) 0
17
18
Output:
Enter the memory capacity: 50
Enter the no of processes: 3
Enter memory req for process 1: 12
The memory allocated for process 1: 50
Remaining memory is: 38
External fragmentation for process is : 38
Enter memory req for process 2: 27
The memory allocated for process 2: 38
Remaining memory is : 11
External fragmentation for this process is : 11
Enter memory req for process 3: 30
Memory is not allowed for process 3.
19
MFT:
#include<stdio.h>
int main()
{
int m,p,s,p1;
int m1[4],i,f,f1=0,f2=0,fra1,fra2,s1,pos;
printf("Enter the memory size:");
scanf("%d",&m);
printf("Enter the no of partitions:");
scanf("%d",&p);
s=m/p;
printf("Each partn size is:%d",s);
printf("\nEnter the no of processes:");
scanf("%d",&p1);
pos=m;
for(i=0;i<p1;i++)
{
if(pos<s)
{
printf("\nThere is no further memory for process%d",i+1);
m1[i]=0;
break;
}
else
{
printf("\nEnter the memory req for process%d:",i+1);
scanf("%d",&m1[i]);
if(m1[i]<=s)
{
printf("\nProcess is allocated in partition%d",i+1);
fra1=s-m1[i];
printf("\nInternal fragmentation for process is:%d",fra1);
f1=f1+fra1;
pos=pos-s;
}
else
{
printf("\nProcess not allocated in partition%d",i+1);
s1=m1[i];
while(s1>s)
{
s1=s1-s;
20
pos=pos-s;
}
pos=pos-s;
fra2=s-s1;
f2=f2+fra2;
printf("\nExternal Fragmentation for this process is:%d",fra2);
}
}
}
printf("\nProcess\tallocatedmemory");
for(i=0;i<p1;i++)
printf("\n%5d\t%5d",i+1,m1[i]);
f=f1+f2;
printf("\nThe tot no of fragmentation is:%d",f);
return 0;
}
Output:
Enter the memory size: 100
Enter the no of partitions: 2
Each partn size: 50
Enter the no of processes: 2
Enter the memory req for process1: 20
Process is allocated in partition1
Internal fragmentation for process is: 30
Enter the memory req for process2: 30
Process is allocated in partition2
Internal fragmentation for process is : 20
Process
1
2
allocatedmemory
20
30
}
getch();
}
int present(char x[10],int k)
{
int j=0,res=0;
for(j=0;j<k;j++)
{
if(strcmp(myfiles[j].name,x)==0)
{
res=1;
}
}
return res;
}
Output:
Enter no.of files: 2
Enter file name:
A
Enter owner name,date,size: 514 16-8-13
20
Enter file name:
B
Enter owner name,date,size: 258 25-8-13
24
ONAME
FILE
DATE
SIZE
514
A
16-8-13
20
258
B
25-8-13
24
b) Two Level
#include<stdio.h>
23
#include<string.h>
struct dir
{
int n;
char name[20];
char f[20][50];
};
void main()
{
int m,i,j,k;
char dname[30];
struct dir d[5];
printf("enter name of the main directory:");
scanf("%s",dname);
printf("enter no of subdirectories:");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\nenter name of subdirectory %d:",(i+1));
scanf("%s",d[i].name);
printf("\nenter no of files in subdirectory %d:",(i+1));
scanf("%d",&d[i].n);
for(j=0;j<d[i].n;j++)
{
printf("\nenter name of %d file:",(j+1));
scanf("%s",d[i].f[j]);
}
}
printf("\n main directory is: %s",dname);
for(i=0;i<m;i++)
{
printf("\n%d subdirectory is: %s",(i+1),d[i].name);
printf("\nfiles in this subdirectory are:");
for(j=0;j<d[i].n;j++)
printf("\n\t%s",d[i].f[j]);
}
}
Output:
Enter name of the main directory JNTUH
24
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10],
safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
26
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
Output:
Enter the no of processes : 3
Enter the no of resources : 3
Enter the Max Matrix for each process :
For process 1 : 5 4 2
For process 2 : 6 2 1
For process 3 : 8 0 3
Enter the allocation for each process :
For process 1 : 5 7 1
For process 2 : 2 0 7
For process 3 : 4 0 2
Enter the Available Resources : 4 5 1
Max matrix:
542
621
803
Allocation matrix:
571
207
402
28
printf("\t%d",maxres[i]);
}
printf("\nThe allocated resource table:\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
printf("\t%d",curr[i][j]);
}
printf("\n");
}
printf("\nThe maximum resource table:\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
printf("\t%d",maxclaim[i][j]);
}
printf("\n");
}
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
{
alloc[j]+=curr[i][j];
}
}
printf("\nAllocated resources:");
for(i=0;i<r;i++)
{
printf("\t%d",alloc[i]);
}
for(i=0;i<r;i++)
{
avl[i]=maxres[i]-alloc[i];
}
31
printf("\nAvailable resources:");
for(i=0;i<r;i++)
{
printf("\t%d",avl[i]);
}
printf("\n");
//Main procedure goes below to check for unsafe state.
while(count!=0)
{
safe=0;
for(i=0;i<p;i++)
{
if(running[i])
{
exec=1;
for(j=0;j<r;j++)
{
if(maxclaim[i][j] - curr[i][j] > avl[j]){
exec=0;
break;
}
}
if(exec)
{
printf("\nProcess%d is executing\n",i+1);
running[i]=0;
count--;
safe=1;
for(j=0;j<r;j++) {
avl[j]+=curr[i][j];
}
break;
}
}}
if(!safe)
{
printf("\nThe processes are in unsafe state.\n");
break;
}
else
{
32
Output:
Enter the number of processes: 3
Enter the number of resources: 3
Enter the resource for instance :1: 2
Enter the resource for instance :2: 3
Enter the resource for instance :3: 1
Enter maximum resource table: 5 6 7 2 1 0 3 6 1
Enter allocated resource table: 1 3 2 8 9 0 1 2 3
The resource of instances:
0
2
3
The allocated resource table:
1
3
2
8
9
0
1
2
3
The maximum resource table:
5
6
7
2
1
0
3
6
1
Allocated resources:
Available resources:-10
10
-12
14
-2
#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
Output:
ENTER THE NUMBER OF PAGES:
20
ENTER THE PAGE NUMBER :
70120304230321201701
34
b)LRU
#include<stdio.h>
main()
{
int q[20],p[50],c=0,c1,d,f,i,j,k=0,n,r,t,b[20],c2[20];
printf("Enter no of pages:");
35
scanf("%d",&n);
printf("Enter the reference string:");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("Enter no of frames:");
scanf("%d",&f);
q[k]=p[k];
printf("\n\t%d\n",q[k]);
c++;
k++;
for(i=1;i<n;i++)
{
c1=0;
for(j=0;j<f;j++)
{
if(p[i]!=q[j])
c1++;
}
if(c1==f)
{
c++;
if(k<f)
{
q[k]=p[i];
k++;
for(j=0;j<k;j++)
printf("\t%d",q[j]);
printf("\n");
}
else
{
for(r=0;r<f;r++)
{
c2[r]=0;
for(j=i-1;j<n;j--)
{
if(q[r]!=p[j])
c2[r]++;
else
break;
}
}
for(r=0;r<f;r++)
b[r]=c2[r];
36
for(r=0;r<f;r++)
{
for(j=r;j<f;j++)
{
if(b[r]<b[j])
{
t=b[r];
b[r]=b[j];
b[j]=t;
}
}
}
for(r=0;r<f;r++)
{
if(c2[r]==b[0])
q[r]=p[i];
printf("\t%d",q[r]);
}
printf("\n");
}
}
}
printf("\nThe no of page faults is %d",c);
}
Output:
Enter no of pages:10
Enter the reference string:7
5
9
4
3
37
7
9
6
2
1
Enter no of frames:3
7
7
7
4
4
4
9
9
9
1
5
5
5
3
3
3
6
6
6
9
9
9
7
7
7
2
2
scanf("%d",&np);
printf("enter page size\n");
scanf("%d",&ps);
sa=(int *) malloc (2 *np);
for(i=0;i<np;i++)
{
sa[i]=(int) malloc(ps);
printf("page %d\tadress%u\n",i+1,sa[i]);
}
}
Output:
Enter how many pages 5
Enter page size 4
Page 1 address 1900
Page 2 address 1908
Page 3 address 1916
Page 4 address 1924
Page 5 address 1932
LINUX PROGRAMS
Output:
Multiplication Table
Enter a number
3
1*1=1
1*2=2
40
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1 * 10 = 10
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
3*1=3
3*2=6
3*3=9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
Output:
enter a new directory name
newd
enter number of files
2
enter filename
prime.sh
$ls newd
prime.sh fact.sh
enter file name
fact.sh
do
no=`expr $nol+1`
set $line
now= `expr $now+ $#`
done
echo "no. of lines: $nol"
echo"no. of words:$now"
exec>$terminal
Output:
enter a file name
palindrome.sh
no. of lines:23
no. of words:71
Output:
List of Files which have Read, Write and Execute Permissions in Current Directory
page
priority
echo $y;;
3)echo "multiplication of two numbers is"
z=`expr $a \* $b`
echo $z;;
4)echo "division of two numbers is"
s=`expr $a / $b`
echo $s;;
esac
Output:
enter the numbers
2
3
enter ur choice
3
multiplication of two numbers is
6
FILE *fp;
int count=0;
char c;
char buf[65000];
int l=0;
int i=0;
printf("enter the fle name");
gets(fn);
fp=fopen(fn,"r");
if(fp==NULL)
{
printf("no such file exists\n");
exit(0);
}
while(!feof(fp))
{
fread(buf,1,65000,fp);
}
l=strlen(buf);
for(i=0;i<l;i++)
{
if(buf[i]==' ')
count++;
}
printf("\nthe number of blanks in the given file is %d\n",count);
}
Output:
enter the fle namemv.c
the number of blanks in the given file is 67
{
int n=0;
int count=0;
char buf[65000];
char fn[50];
int i=0;
int fd=0;
printf("enter the file name");
gets(fn);
fd=open(fn,O_RDONLY);
n=read(fd,buf,65000);
if(n<0)
{
printf("read error\n");
exit(0);
}
for(i=0;i<n;i++)
if(buf[i]==' ')
count++;
printf("the number of blanks in the given file is %d\n",count);
}
Output:
enter the file namehome
the number of blanks in the given file is 17
char f1[50],f2[50];
int fd1,fd2;
int t,n=0;
char buf[65000];
printf("enter the files to be concatenated");
gets(f1);
gets(f2);
fd1=open(f1,O_RDONLY);
fd2=open(f2,O_RDONLY);
while((n=read(fd1,buf,65000))>0)
{
if((t=write(1,buf,n))!=n)
{
printf("write error\n");
exit(0);
}
}
if(n<0)
{
printf("read error\n");
exit(0);
}
while((n=read(fd2,buf,65000))>0)
{
if(write(1,buf,n)!=n)
{
printf("write error\n");
exit(0);
}
}
if(n<0)
{
printf("read error\n");
exit(0);
}
}
Output:
cse@cse-Not-Specified:~/11011M2109$ vi stask8a.c
cse@cse-Not-Specified:~/11011M2109 gcc stask8a.c
cse@cse-Not-Specified:~/11011M2109$ ./stask8a
bash: ./stask8a: No such file or directory
48
cse@cse-Not-Specified:~/11011M2109$ ./a.out
enter the files to be concatenated msgqrec.c
msgqsend.c
read error
b) ls
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<dirent.h>
#include<sys/types.h>
int main()
{
DIR *dp;
struct dirent *dirp;
char dn[30];
printf("enter the name of the directory whose contents are to be displayed");
gets(dn);
dp=opendir(dn);
if(dp==NULL)
49
{
printf("no such directory exists\n");
exit(0);
}
while((dirp=readdir(dp))!=NULL)
{
printf("%s\n",dirp->d_name);
}
closedir(dp);
}
Output:
enter the name of the directory whose contents are to be displayed11011M2109
stask8b.c
msgqrec.c
stask8b.c~
msgqrec
..
a.out
msgqsend.c~
.
producer_consumer2.c
stask8a.c
producer_consumer2.c~
stask8a.c~
c) mv
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/stat.h>
int main()
{
char source[50],dest[50];
struct stat *fd;
int i=0;
fd=(struct stat*) malloc(sizeof(struct stat));
//rename("/home/kumar/delete.txt","/home/kumar/nsl/");
printf("enter the name of the source file");
gets(source);
printf("enter the name of the destination");
50
gets(dest);
stat(dest,fd);
/*if(S_ISDIR(fd->st_mode))
{
printf("destination is a directory hence copying to the directory\n");
i=rename(source,dest);
printf("return value of rename is %d\n",i);
if(!i)
printf("copied\n");
else
printf("error in copying\n");
}*/
//else
//{
//
printf("destination is a not a directory\n");
i=rename(source,dest);
//printf("return value of rename is %d\n",i);
if(!i)
printf("copied\n");
else
printf("error in copying\n");
//}
printf("end of operation\n");
}
Output:
enter the name of the source filefork.c
enter the name of the destinationhome
copied
end of operation
51
int fd;
//file descriptor
if(argc != 2)
{
//this means no file is given at command line
printf("Correct usage is %s <File name> ",argv[0]);
exit(0);
}
fd = open(argv[1],O_RDONLY);//opening a file with read permissions
//to check if file exists
if(fd == -1)
{
printf("File not found\n");
exit(0);
}
fstat(fd,&buff);
printf("Type of file is ");
switch(buff.st_mode & S_IFMT)
{
case S_IFBLK:printf("Block file\n");
break;
case S_IFDIR:printf("Directory File\n");
break;
case S_IFREG:printf("Regular file \n");
break;
case S_IFSOCK:printf("Socket file \n");
break;
default: printf("Unknown file type\n");
}
printf("No of links : %d\n",buff.st_nlink);
printf("Time of last access : %s",ctime(&buff.st_atime));
printf("File permissions : %lo\n",(long)(0777&buff.st_mode));
close(fd);
return 0;
}
Output:
server:Lu Admin$ .19 9.c
Type of file is Regular file
No of links : 1
Time of last access : Thu Oct 31 03:09:00 2013
53
perror("execl");
}
else
{
wait(2);
close(fd[1]);
dup2(fd[0],0);
close(fd[0]);
execlp(argv[2],argv[2],NULL);
perror("execl");
}
}
Output:
server:Lu Admin$ gcc 10.c
server:Lu Admin$ ./a.out ls sort
a.out
ls-l_sort.
ls_sort.c
named.c
Output:
In parent process
Pid=2831
Ppid=2230
In child process
Pid=2832
Ppid=1
for (; ;)
{
times_up(1);
sleep(60);
}
56
}
int times_up(sig)
int sig;
{
long now;
long time(struct tms *ptr);
char *ctime();
time (&now);
printf("It is now %s\n", ctime (&now));
return (sig);
}
Output:
It is now Thu Oct 3 16:09:15 2013
14: WRITE A C PROGRAM THAT IMPLEMENTS A PRODUCERCONSUMER SYSTEM WITH TWO PROCESSES. (USING
SEMAPHORES)
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>
#define N 5
struct buffer
{
int b[N];
sem_t empty,full;
int in,out;
}buf;
void *P()
{
int i=1;
while(1)
57
{
sem_wait(&(buf.empty));
buf.b[buf.in]=i;
buf.in=(buf.in+1)%N;
printf("\nProduced :%d",i++);
sleep(2);
sem_post(&(buf.full));
}
}
void *C()
{
int item;
while(1)
{
sem_wait(&(buf.full));
item=buf.b[buf.out];
buf.out=(buf.out+1)%N;
printf("Consumed :%d",item);
sem_post(&(buf.empty));
}
}
int main()
{
pthread_t t1,t2;
sem_init(&(buf.empty),0,N);
sem_init(&(buf.full),0,0);
buf.in=0;
buf.out=0;
pthread_create(&t1,NULL,P,NULL);
pthread_create(&t2,NULL,C,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
}
Output:
cse@cse-Not-Specified:~/11011P0519$ gcc producer_consumer2.c -lpthread
cse@cse-Not-Specified:~/11011P0519$ ./a.out
USAGE:./main.out <INT> <INT> <INT>
Segmentation fault
cse@cse-Not-Specified:~/11011P0519$ a.out 10 10 10
58
{
perror("shmget");
exit(1);
}
}
else
{
printf("Creating a new shared memory seg \n");
printf("SHMID:%d", shmid);
}
system("ipcs_m");
if((segptr=shmat(shmid,0,0))==(char*)-1)
{
perror("shmat");
exit(1);
}
printf("Writing data to shared memory...\n");
strcpy(segptr,buff);
printf("DONE\n");
printf("Reading data from shared memory...\n");
printf("DATA:-%s\n",segptr);
printf("DONE\n");
printf("Removing shared memory Segment...\n");
if(shmctl(shmid,IPC_RMID,0)== -1)
printf("Cant Remove Shared memory Segment...\n");
else
printf("Removed Successfully");
}
Output:
ramyakala@linux-ng56:~/Desktop/oslab/fwdlinux141516> gcc shmem1.c
ramyakala@linux-ng56:~/Desktop/oslab/fwdlinux141516> ./a.out
Creating a new shared memory seg
------ Shared Memory Segments -------key
shmid
owner
perms
bytes
nattch status
0x00000000 1507328 ramyakala 600
393216 2
dest
0x00000000 262145 ramyakala 700
7680
2
dest
0x00000000 294914 ramyakala 700
33120
2
dest
0x00000000 327683 ramyakala 700
11508
2
dest
0x00000000 360452 ramyakala 700
11508
2
dest
60
nsems
used-bytes messages
a) Receiver side:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 128
void die(char *s)
{
perror(s);
exit(1);
}
61
main()
{
int msqid;
key_t key;
struct msgbuf rcvbuffer;
key = 1234;
if ((msqid = msgget(key, 0666)) < 0)
die("msgget()");
Output:
cse@cse-Not-Specified:~/11011P0519$ vi msgqrec.c
cse@cse-Not-Specified:~/11011P0519$ cc msgqrec.c -o msgqrec
msgqrec.c:18:1: warning: useless storage class specifier in empty declaration
[enabled by default]
cse@cse-Not-Specified:~/11011P0519$ ./msgqrec
40
62
b) Sender side:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 128
void die(char *s)
{
perror(s);
exit(1);
}
struct msgbuf
{
long mtype;
char mtext[MAXSIZE];
};
main()
{
63
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
struct msgbuf sbuf;
size_t buflen;
key = 1234;
if ((msqid = msgget(key, msgflg )) < 0) //Get the message queue ID for the given
key
die("msgget");
//Message Type
sbuf.mtype = 1;
printf("Enter a message to add to message queue : ");
scanf("%[^\n]",sbuf.mtext);
getchar();
buflen = strlen(sbuf.mtext) + 1 ;
if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
{
printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buflen);
die("msgsnd");
}
else
printf("Message Sent\n");
exit(0);
}
Output:
cse@cse-Not-Specified:~/11011P0519$ vi msgqsend.c
cse@cse-Not-Specified:~/11011P0519$ cc msgqsend.c -o msgqsend
msgqsend.c: In function main:
msgqsend.c:45:9: warning: format %d expects argument of type int, but argument
3 has type long int [-Wformat]
cse@cse-Not-Specified:~/11011P0519$ ./msgqsend
Enter a message to add to message queue : 40
Message Sent
64
----------------------------------------------------------------------------------------------------------------
65