0% found this document useful (0 votes)
8 views

Oss Edited

The document contains multiple C programming examples demonstrating various concepts such as process creation using fork(), executing commands with execlp(), reading directory entries, and calculating process scheduling metrics like waiting time and turnaround time. Each program is followed by its respective output, showcasing the results of the executed code. The examples cover different scheduling algorithms including First-Come-First-Serve, Shortest Job First, Priority Scheduling, and Round Robin.

Uploaded by

mdvignesh11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Oss Edited

The document contains multiple C programming examples demonstrating various concepts such as process creation using fork(), executing commands with execlp(), reading directory entries, and calculating process scheduling metrics like waiting time and turnaround time. Each program is followed by its respective output, showcasing the results of the executed code. The examples cover different scheduling algorithms including First-Come-First-Serve, Shortest Job First, Priority Scheduling, and Round Robin.

Uploaded by

mdvignesh11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

PROGRAM

#include<stdio.h>

#include<unistd.h>

main() {

fork();

printf("Hello World\n");

OUTPUT :

[it1@localhost ~]$ vi ex1a.c

[it1@localhost ~]$ cc ex1a.c

[it1@localhost ~]$ ./a.out Hello World

Hello World

PROGRAM

#include<stdio.h>

#include<unistd.h>

main()

fork();

fork();

fork();

printf("Hello World\n");

}
OUTPUT:

[it1@localhost ~]$ cc

ex1aa.c[it1@localhost ~]$./a.out

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World

Hello World
PROGRAM :
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<conio.h> main()
{
int a=-1,b=1,i,c=a+b,n,pid,cpid;
printf("\nenter no. of terms "); scanf("%d",&n);
pid=getpid();
printf("\nparent process id is %d",pid); pid=fork();
cpid=getpid();
printf("\nchild process:%d",cpid); if(pid==0)
{
printf("\nchild is producing fibonacci series ");

for(i=0;i<n;i++)
{
c=a+b; printf("\n%d",c); a=b;
b=c;
}
printf("\nchild ends");
}
else
{
printf("\nparent is waiting for child to complete"); wait(NULL);
printf("\nparent ends");
}
}

OUTPUT
[it27@mm4 ~]$ cc fork.c[it27@mm4
~]$a.out enter no. of terms 5 parent
process id is 8723
child process:8723
parent process id is 8723
child process:8732
child is producing fibonacci series0
1
1
2
3
child ends parent is waiting for child to complete
PROGRAM:
#include<stdio.h>

#include<unistd.h>
#include<sys/wait.h>main()
{
int pid; pid=fork();

if(pid<0){

fprintf(stderr,"fork failed\n");

exit(-1);
}
else if(pid==0){
execlp("/bin/ls","ls",NULL);
}
else{

wait(NULL);

printf("Child Complete");exit(0);
}

OUTPUT:
[it1@localhost ~]$ cc
ex1c.c[it1@localhost ~]$
./a.out
a1 ara.carunv ex1aa.c facts itbgalskpmatrix.vpre.c rad2.sh rad.cskwooow a2
arain.cbaabaaa ex1a.c fib.c k1 lee obupriya rad3.sh rad.sh ss xxx a.carav.c cat
ex1b.c fio k2 lyffactzos rad1.c rad4.c sat sum.c

PROGRAM:
#include<stdio.h>

#include<unistd.h>
#include<sys/types.h>
#include<dirent.h>

main(int argc,char *argv[])


{
DIR *dirname;

struct dirent*dir;
dirname=opendir(argv[1]);
dir=readdir(dirname); while(dir!
=NULL)
{
printf("Entry found :%s\n",dir->d_name);

printf("Inode number of entry:%d\n",dir->d_ino);


printf("Length of this record:%d\n",dir->d_reclen); getchar();
}
}

OUTPUT :
[it1@localhost ~]$ cc ex1f.c
[it1@localhost ~]$mkdir anu
[it1@localhost ~]$ cd
[it1@localhost ~]$ cd anu
[it1@localhost anu]$ cat>>new os is an
interface
[it1@localhost anu]$ cat>>new1 os is
resident monitor [it1@localhost anu]$ cd
[it1@localhost ~]$ ./a.out
anu Entry found :..
Inode number of entry:26542353
Length of this record:16
PROGRAM:
#include<stdio.h>

#include<stdlib.h>
main()
{
int n,a[10],b[10],t[10],w[10],g[10],i;
float att=0,awt=0; for(i=0;i<10;i++)
{
a[i]=0;
b[i]=0;
w[i]=0;
g[i]=0;
}
printf("\n Enter the number of process"); scanf("%d",&n);
printf("\n Enter the burst time");
for(i=0;i<n;i++) scanf("%d",&b[i]);
printf("\n Enter the arrival time");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
g[0]=0;
for(i=0;i<n;i++)
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{
w[i]=g[i]-a[i];
t[i]=g[i+1]- a[i];
awt=awt+w[i];

att=att+t[i];

}
awt=awt/n;
att=att/n;
printf("\n\t Process\tWaiting time\tTurn around time");
for(i=0;i<n;i++)
{
printf("\n\t\tp%d\t\t%d\t\t%d",i+1,w[i],t[i]);
}
printf("\nAverage waiting time %f",awt); printf("\nAverageturn
around time %f",att);
printf("\n \n");
for(i=0;i<n;i++)
printf("\tp%d\t",i+1); printf("\t\n");
printf("\n \n");
printf("\n"); for(i=0;i<=n;i++) printf("\
n"); for(i=0;i<=n;i++) printf("\t%d\
t",g[i]);
printf("\n \n");
printf("\n");
}

OUTPUT:
[oslab@localhost~]$ cc fcfs.c
[oslab@localhost~]$
./a.out Enter the number of process4
Enter the burst time
3
2
4
1
Enter the arrival time 0
0
0
0
Process Waiting time Turn around time
p1 0 3
p2 3 5
p3 5 9
p4 9 10

Average waiting time 4.250000

Average turn around time 6.750000

GANTT CHART:

p1 | p2 | p3 | p4 |

0 3 5 9 10
PROGRAM:
#include<stdio.h>

main()
{
int n,i,j,temp,p[10],b[10],bu[10],wt[10],tt[10];

float sum=0.0,twt=0.0;
for(i=0;i<10;i++)
{
b[i]=p[i]=bu[i]=wt[i]=tt[i]=0;
}
printf("\nEnter the number of process"); scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\nEnter the burst time for process P%d",i); scanf("\n
%d",&bu[i]);
}
for(i=1;i<=n;i++)
{
b[i]=bu[i];
p[i]=i;
}
for(i=n;i>=1;i--)
{
for(j=2;j<=n;j++)
{
if(b[j-1]>b[j])
{
temp=b[j-1];

b[j-1]=b[j]; b[j]=temp;
temp=p[j-1];

p[j-1]=p[j];
p[j]=temp;
}
}
} wt[1]=0;
for(i=2;i<=n;i++)
{
wt[i]=b[i-1]+wt[i-1];
}
for(i=1;i<=n;i++)
{
twt=twt+wt[i]; tt[i]=b[i]+wt[i];

sum=sum+tt[i];
}
twt=twt/n; sum=sum/n;
printf("\nProcess\tBtime\tWT\tTT\n");
for(i=1;i<=n;i++)
{
printf("\nP%d\t%d\t%d\t%d\n",p[i],b[i],wt[i],tt[i]);
}
printf("\nAVG waiting time:%f\n",twt);

printf("\nAVGturn around time:%f\n",sum);


printf("\n \n");
for(i=1;i<=n;i++)
printf("\tP%d\t|",p[i]);
printf("\n");
printf(" \n");
printf("\n");

for(i=1;i<=n;i++) printf("%d\t\
t",wt[i]);
printf("%d",tt[n]); printf("\n");
}
OUTPUT:
[oslab@localhost~]$ cc sjfs.c

[oslab@localhost~]$./a.out

Enter the number of process4


Enter the burst time for process P1 4
Enter the burst time for process P2 6
Enter the burst time for process P3 1
Enter the burst time for process P4 5

Process Btime WT TT
P3 1 0 1
P1 4 1 5
P4 5 5 10
P2 6 10 16

AVG waiting time: 4.000000 AVG turn


around time: 8.000000
GANTT CHART:

P3 | P1 | P4 | P2

0 1 5 10 16
PROGRAM:
#include<stdio.h>

int main()
{
int n,i,j,temp,temp1,temp2,b[10],t[10],w[10],p[10],pr[10];

float att=0.0,awt=0.0;
for(i=0;i<10;i++)
{ b[i]=0;
w[i]=0;
}
printf("\nEnter the no. of process");

scanf("%d",&n);
printf("\nEnter the burst time");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]); p[i]=i+1;
}
printf("\nEnter the priority "); for(i=0;i<n;i+
+)

scanf("%d",&pr[i]);

for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(pr[i]>pr[j])
{
temp=b[i]; temp1=p[i];
temp2=pr[i]; b[i]=b[j];
p[i]=p[j];
pr[i]=pr[j]; b[j]=temp;
p[j]=temp1; pr[j]=temp2;
}}}
w[0]=0;
for(i=0;i<n;i++) w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
awt=awt+w[i];
att=att+t[i];
}
awt=awt/n;

att=att/n;
printf("\n\tProcess\tWaitingTIme\tTurn around time\t\n");
for(i=0;i<n;i++)
printf("\n\tP%d\t\t%d\t\t%d\n",p[i],w[i],t[i]);
printf("AVG WT is %f",awt);
printf("\nAVGTTis %f",att);
printf("\n\nGANTT Chart");
printf("\n \n");
for(i=0;i<n;i++)
printf("\tP%d\t",p[i]);
printf("\t\n");
printf("\n \n");
for(i=0;i<n;i++)
printf("%d\t\t",w[i]);
printf("%d\t\t",t[n-1]);
printf("\n \n");
printf("\n");
}
OUTPUT:
[cse023@localhost ~]$ cc priority.c

[cse023@localhost ~]$./a.out
Enter the no. of process 4 Enter the burst time
3
4
1
2
Enter the priority 3
2
4
1
Process WaitingTime Turn around time
P4 0 2

P2 2 6

P1 6 9

P3 9 10

AVG WT is 4.250000AVG TTis


6.750000

GANTT Chart:

P4 | P2 | P1 | P3

0 2 6 9 10
PROGRAM:
#include<stdio.h>

main()
{
struct
{
int p,bt,rm,flag;
}

rr[10];
int slice,i,k,n,g[100],cnt=0,pn[100],to,from,sum=0;
int wait[100],turn[100 ],occ[100],tturn=0,twait=0; float
await=0.0,aturn=0.0;
printf("\nEnter the no. of process"); scanf("%d",&n);
printf("\nEnter the time slice");
scanf("%d",&slice); for(i=0;i<n;i++)
{
printf("\nEnter the burst time %d:\t",i+1);
scanf("%d",&rr[i].bt); wait[i]=turn[i]=occ[i]=0;
rr[i].flag=0; rr[i].rm=rr[i].bt;
sum=sum+rr[i].bt;
}
k=i=0;

printf("\nFrom\tTO\tProcess\n");
while(k<sum)
{
from=k;

if(rr[i].rm>0){

if(rr[i].rm<slice)
{
to=from+rr[i].rm;
}
else to=from+slice;
rr[i].rm=rr[i].rm-(to- from);k=to;
printf("\n%d\t%d\t%d\n",from,to,i+1);
}
if((rr[i].rm==0)&&(rr[i].flag==0))
{
rr[i].flag=1; wait[i]=from-
occ[i]; turn[i]=to;
}
else
occ[i]=occ[i]+(to-from); i++;
if(i==n) i=0;
}
printf("\n\tProcess\tBurst_time\tWaiting_time\tTurn_around_time"); for(i=0;i<n;i++)
{
printf("\n\tP%d\t\t%d\t\t%d\t\t%d",i+1,rr[i].bt,wait[i],turn[i]);
twait=twait+wait[i];
tturn=tturn+turn[i];
}
printf("\n");
printf("\n \n");
await=(float)twait/n;
aturn=(float)tturn/n;
printf("\nTotal waiting time=%d\nTotalturn around time=%d",twait,tturn);

printf("\nAverage waiting time=%5.2f",await);


printf("\nAverageturn around time=%5.2f",aturn);
}

OUTPUT:
[oslab@localhost~]$ cc roundrobin.c

[oslab@localhost~]$ ./a.out
Enter the no. of process 4 Enter the
time slice 4
Enter the burst time for P1: 6
Enter the burst time for P2: 2
Enter the burst time for P3: 5
Enter the burst time for P4: 8
From TO Process
0 4 1
4 6 2
6 10 3
10 14 4
14 16 1
16 17 3
17 21 4

Process Burst_time Waiting_time Turn_around_time


P1 6 10 16
P2 2 4 6
P3 5 12 17
P4 8 13 21

Total waiting time=39


Total turn around time=60 Average
waiting time= 9.75
Average turn around time=15.00
PROGRAM:

#include<stdio.h>
#defineBsize 10
void produce();
void consumer();
void producer();
int a,b,c,d;
int i,j,k;
intbuf[Bsize],consumed[10];
struct pro

{
int item_no;
}
item[10];

int main()
{
do
{
if(j==Bsize-i)
{ i=0; j=0;
produce(); consumer();
}
else{ producer();
consumer();
}
printf("\nDo you want to continue(YES-1/NO-0)");
scanf("%d",&a);
}
while(a==1); void
producer()
{
for(i=0;i<Bsize;i++)
{
if(buf[i]!=’\0’) continue;
else

{
b=Bsize-i;
printf("\nTotal free space is %d",b);
printf("\nDo you want to produce items(YES-1/NO-0)"); scanf("%d",&c);
if(c==1)
{
produce();
}
else break;
}
}
}
void produce()
{
printf("\nEnter the no. of items you want to produce");
scanf("%d",&d);
while(j<i+d)
{
printf(“\nItem no. %d”,item_no);
scanf("%d",&item[j].item_no); j++;
}
for(k=0;k<Bsize;k++)
{
buf[k]=item[k].item_no; printf("\nBuffer content
is %d",buf[k]);
}

}
void consumer()
{
for(j=0;j<i;j++)
{
consumed[j]=buf[j];
buf[j]=buf[j-1];
printf("\nConsumed item is %d\n",consumed[j]);
item[j].item_no=0;
} j=0;
}
OUTPUT:

[oslab@localhost~]$ cc semaphore.c [oslab@localhost~]$ ./a.out

Total free space is 10


Do you want to produce items(YES-1/NO-0) 1 Enter the
no. of items you want to produce 3
Item.no 1 7
Item.no 2 8
Item.no 3 17
Buffer content is 7
Buffer content is 8
Buffer content is 17
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Total free space is 7
Do you want to produce items(YES-1/NO-0) 1

Enter the no. of items you want to produce 2

Item.no 4 12
Item.no 5 3
Buffer content is 7
Buffer content is 8
Buffer content is 17
Buffer content is 12
Buffer content is 3
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Total free space is 5
Do you want to produce items(YES-1/NO-0) 0
Consumed item is 7
Consumed item is 8
Consumed item is 17
Consumed item is 12
Consumed item is 3
Do you want to continue(YES-1/NO-0) 1
Total free space is 10
Do you want to produce items(YES-1/NO-0) 1
Enter the no. of items you want to produce 2

Item.no 1 7
Item.no 2 8
Buffer content is 7
Buffer content is 8
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0
Buffer content is 0

Total free space is 8


Do you want to produce items(YES-1/NO-0) 0
Consumed item is 7
Consumed item is 8
Do you want to continue(YES-1/NO-0) 0
PROGRAM:
#include<stdio.h>

int main()

{
int fd[2],child;
char buf[30];
printf("\n enter the string to store the pipe:");
scanf("%s",buf);
pipe(fd);
child=fork();
if(!child)
{
close(fd[0]);
write(fd[1],buf,5);

wait(0);

}
else{ close(fd[1]);

read(fd[0],buf,5);
printf("\n string retrived from the pipe is %s\n",buf);

} return (0);
}

OUTPUT:
[cse023@localhost ~]$ cc ipc.c
[cse023@localhost ~]$ ./a.out

enter the string to store the pipe:osstring


retrived from the pipe is os

You might also like