Showing posts with label C Practical Programs. Show all posts
Showing posts with label C Practical Programs. Show all posts

10.Write a program to calculate factorial of a number using thread library

Write a program to calculate factorial of a number using thread library.

//cskecode
//cskecode.blogspot.com
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>
int fact;
void *run(void *param);
int main(int argc, char *argv[])
{
pthread_t tid;
pthread_attr_t attr;
if(argc !=2)
{
printf("Error !");
return 1;
}
if(atoi(argv[1])<O)
{
printf("no. Should be +ive");
return 1;
}
pthread_attr_init(&attr);
pthread_create(&tid, &attr,run, argv[1]);
pthread_join(tid,NULL);
printf("fact= %d in", fact);
}
void *run(void *param)
{
int i, upper;
fact=1;
upperratoi(param);
if(upper>0)
for(i=1;i<=upper; ++i)
fact*=i;
pthread_exit(0);
}
//cskecode.blogspot.com

Output:


9.A program to calculate sum of n numbers using thread library

A program to calculate sum of n numbers using thread library. 

cskecode

//cskecode.blogspot.com
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<pthread.h>
int sum;
void *run(void *param);
int main(int argc, char *argv[]).
{
pthread_t tid;
pthread_attr_t attr;
if(argc !=2)
{
printf("Error !");
return 1;
}
if(atoi(argv[1])<0)
{
printf("no. Should be +ive");
return 1;
}
pthread_attr_init(&attri
pthread_create(&tid,&attr.run.arguri):
pthread_join(tid, NULL);
printf("Sum = %d in", sum);
}
void *run(void *param)
{
int i,upper;
sum=0 ;
upper=atoi(param);
if(upper>0)
{
for(i=1;i<=upper; ++i)
SUM+=i:
pthread_exit(0);
}
}
//cskecode.blogspot.com

Output:


8.Write program to implement SJF scheduling algorithm (Preemptive)

Write program to implement SJF scheduling algorithm (Preemptive)

//cskecode

//cskecode.blogspot.com
#include<iostream>
using namespace std;
class sched{
public:
int n,bt[10],at[10],tat[10],wt[10],rt[10],finish[10],twt,ttat,total;
void readData();
void computeSRT();
void Init();
void dispTime();
int getNextProcess(int);
};

void sched::readData()
{
cout<<"Enter no. of processes\n";
cin>>n;
cout<<"Enter the burst times in order :\n";
for(int i=0;i<n;i++)
cin>>bt[i];
cout<<"Enter the arrival times in order:\n";
for(int i=0;i<n;i++)
cin>>at[i];
}

void sched::Init()
{
total=0;
twt=0;
ttat=0;
for(int i=0; i<n; i++)
{
 rt[i]=bt[i];
finish[i]=0;
wt[i]=0;
tat[i]=0;
total+=bt[i];
}
}

void sched::computeSRT()
{
 readData();
Init();
int time,next=0,old,i;
cout<<"Gantt Chart\n ";
for(time=0;time<total;time++)
{
old=next;
next=getNextProcess(time);
if(old!=next || time==0) 
{
cout<<"("<<time<<")|==P"<<next+1<<"==|";
}
rt[next]=rt[next]-1;
if(rt[next]==0) 
{
finish[next]=1;
}
for(i=0;i<n;i++)
if(i!=next && finish[i]==0 && at[i]<=time)
wt[i]++;
}
cout<<"("<<total<<")"<<endl;
for(i=0;i<n;i++)
if(!finish[i]) {cout<<"Scheduling failed, cannot continue\n"; return;}
dispTime();
}

int sched::getNextProcess(int time)

int i,low;
for(i=0;i<n;i++)
if(finish[i]==0){low=i; break; }
for(i=0;i<n;i++)
if(finish[i]!=1)
if(rt[i]<rt[low] && at[i]<=time)
low=i;
return low;
}

void sched::dispTime()
{
for(int i=0;i<n;i++)
{
twt+=wt[i];
tat[i]=wt[i]+bt[i];
ttat+=tat[i];
cout<<"Waiting time for P"<<(i+1)<<" = "<<wt[i]<<", Turnaround time = "<<tat[i]<<endl;
}
cout<<"Avg Waiting time = "<<(double)twt/n<<" and Avg Turnaround time = "<<(double)ttat/n<<endl;
cout<<"Scheduling complete\n";
}

int main()
{
sched s;
s.computeSRT();
}
//cskecode.blogspot.com


Output:

8.Write program to implement SJF scheduling algorithm (Non-Preemptive)

Write program to implement SJF scheduling algorithm (Non-Preemptive)


//cskecode

#include<iostream>
#include<stdio.h>
using namespace std;
class sjf
{      
public:
int burst_tm,arv_tm,wait_tm,temp;
char pid;
sjf()
{
temp=0;
//status=0;
}
};
int main()
{
int n,sum=0,carry=0;
float avg_wait_tm=0,avg_around_tm=0;
cout<<"Enter the no. of processes:\n";
cin>>n;
sjf temp;
sjf *f=new sjf[n];
for( int i=0;i<n;i++)
{
cout<<"Enter the process id:\n";
cin>>f[i].pid;
cout<<"Enter the burst timing:\n";
cin>>f[i].burst_tm;
cout<<"Enter the arrival timing:\n";
cin>>f[i].arv_tm;
}
   
int k1=0,flag=1;
while(k1<n)
{
if(f[k1].arv_tm==0)
{
flag=0;
break;
}
k1++;
}
if(flag==1)
{
cout<<"enter all values of arival time as no arrival time is 0\n";
return 1;
}
   
for(int k=0;k<n;k++)
{
temp=f[k];
for(int i=k+1;i<n;i++)
{
if(temp.arv_tm>f[i].arv_tm)
{
temp=f[i];
f[i]=f[k];
f[k]=temp;
}
//cout<<"f[0]: "<<f[0].pid<<endl;
}
}
   
for(int k=0;k<n;k++)
{
cout<<"pid: "<<f[k].pid<<endl<<"Burst time: "<<f[k].burst_tm<<endl<<"arrival time: "<<f[k].arv_tm<<endl<<"Waiting time: "<<f[k].wait_tm<<endl;
                   
}
   
cout<<"Entered info is as follows:\n";
carry=f[0].arv_tm;
int i=0;
for(int k=1;k<n;k++)
{
if(f[k].arv_tm==carry)
{
if(f[i].burst_tm>f[k].burst_tm)
i=k;
}
else
{
cout<<"carry break\n";
break;
}
}
   
int j,tem=0,k,b;
do
{
j=0;
if((f[i].arv_tm<=sum)&&(f[i].temp!=1))
{
f[i].wait_tm=sum-f[i].arv_tm;
f[i].temp=1;
sum+=f[i].burst_tm;
}
else
{
if(tem==0)
{
sum=f[i].arv_tm;
tem=1;
}
f[i].wait_tm=sum-f[i].arv_tm;
f[i].temp=1;
sum+=f[i].burst_tm;
}
flag=1;
for(int k=0;k<n;k++)
{
if(f[k].arv_tm<=sum)
{
 //flag=0;
if(f[k].temp==1)
j++;
else if((f[k].burst_tm<f[i].burst_tm)||(f[i].temp==1))
{
flag=0;
i=k;
}
}
}
                                   
if((flag==1)&&(j!=n-1))
{
for(b=i;b<n;b++)
if(!f[b].temp)
{
carry=f[b].arv_tm;
i=b;
break;
}
for(int t=b+1;t<n;t++)
{
if(f[t].arv_tm==carry)
{
if(f[t].burst_tm<f[i].burst_tm)
i=t;
}
else
break;
}
}
}while(j!=n);
                                                                                                   
                                             
for(k=0;k<n;k++)
{
cout<<"pid: "<<f[k].pid<<endl<<"Burst time: "<<f[k].burst_tm<<endl<<"arrival time: "<<f[k].arv_tm<<endl<<"Waiting time: "<<f[k].wait_tm<<endl;
avg_wait_tm+=f[k].wait_tm;
avg_around_tm+=f[k].wait_tm+f[k].burst_tm;
                   
                   
}
avg_around_tm/=n;
avg_wait_tm/=n;
cout<<"average turn around time:\n"<<avg_around_tm<<endl<<"average waiting time:\n"<<avg_wait_tm<<endl;
   
//cout<<"average turn around time:\n"<<avg_around_tm<<endl<<"average waiting time:\n"<<avg_wait_tm<<endl;
getchar();
getchar();
getchar();
   
return 0;
}


Output:

8.Write program to implement SJF scheduling algorithm

Write program to implement SJF scheduling algorithm (IF Arrival time is 0)

//cskecode

#include<iostream>
#include<stdio.h>
using namespace std;
class sjf
{      
public:
int burst_tm,arv_tm,wait_tm,temp;
char pid;
};

int main()
{
int n,sum=0,carry;
cout<<"Enter the no. of processes:\n";
cin>>n;
sjf temp;
sjf *f=new sjf[n];
for( int i=0;i<n;i++)
{
cout<<"Enter the process id:\n";
cin>>f[i].pid;
cout<<"Enter the burst timing:\n";
cin>>f[i].burst_tm;
cout<<"Enter the arrival timing:\n";
cin>>f[i].arv_tm;
}

int k=0,flag=1;
while(k!=0)
{
if(f[k].arv_tm==0)
{
flag=0;
break;
}
k++;
}
if(!flag)
{
cout<<"enter all values of arival time as no arrival time is 0\n";
return 1;
}
for(k=0;k<n;k++)
{
temp=f[k];
for(int i=k+1;i<n;i++)
{
if(temp.burst_tm>f[i].burst_tm)
{
temp=f[i];
f[i]=f[k];
f[k]=temp;
}
}
}
cout<<"Entered info is as follows:\n";
float avg_wait_tm=0,avg_around_tm=0;
for(k=0;k<n;k++)
{                                            
f[k].wait_tm=sum-f[k].arv_tm;
if(f[k].wait_tm<0)
f[k].wait_tm=0;
if(f[k].arv_tm>(f[k-1].burst_tm+f[k-1].arv_tm))
{
carry=f[k].arv_tm-sum;
sum+=(f[k].burst_tm+carry);
}
else
sum+=f[k].burst_tm;
avg_wait_tm+=f[k].wait_tm;
avg_around_tm+=f[k].wait_tm+f[k].burst_tm;
}
avg_around_tm/=n;
avg_wait_tm/=n;

for(k=0;k<n;k++)
{
cout<<"pid: "<<f[k].pid<<endl<<"Burst time: "<<f[k].burst_tm<<endl<<"arrival time: "<<f[k].arv_tm<<endl<<"Waiting time: "<<f[k].wait_tm<<endl;

cout<<"This is my Blog: ducskecode.blogspot.com" \n";

}
cout<<"average turn around time:\n"<<avg_around_tm<<endl<<"average waiting time:\n"<<avg_wait_tm<<endl;

return 0;
}

Output:



7.Write a program to implement round robin scheduling algorithm.

Write a program to implement round robin scheduling algorithm.


#include<iostream>
using namespace std;
class sched
{
public:
int n,bt[10],at[10],tat[10],wt[10],rt[10],finish[10],twt,ttat,total;
void readData();
void Init();
void dispTime();
void computeRR();
};

void sched::readData()
{
cout<<"Enter no. of processes\n";
cin>>n;
cout<<"Enter the burst times in order :\n";
for(int i=0;i<n;i++)
cin>>bt[i];
cout<<"Enter the arrival times in order:\n";
for(int i=0;i<n;i++)
cin>>at[i];
}

void sched::Init()
{
total=0;
twt=0;
ttat=0;
for(int i=0; i<n; i++)
{
rt[i]=bt[i];
finish[i]=0;
wt[i]=0;
tat[i]=0;
total+=bt[i];
}
}

void sched::dispTime()
{
for(int i=0;i<n;i++)
{
twt+=wt[i];
tat[i]=wt[i]+bt[i];
ttat+=tat[i];
cout<<"Waiting time for P"<<(i+1)<<" = "<<wt[i]<<", Turnaround time = "<<tat[i]<<endl;
}

cout<<"Avg Waiting time = "<<(double)twt/n<<" and Avg Turnaround time = "<<(double)ttat/n<<endl;
cout<<"Scheduling complete\n";
}

void sched::computeRR(){
readData();
Init();
int time,j,q,i,dec=0;
cout<<"Enter the time quantum:\n";
cin>>q;
cout<<"Gantt Chart\n ";
for(time=0;time<total;)
{
for(i=0;i<n;i++)
{
if(at[i]<=time && finish[i]==0)
{
cout<<"("<<time<<")|==P"<<(i+1)<<"==|";
if(rt[i]<q)  {
dec=rt[i];
}
else {dec=q;}
rt[i]=rt[i]-dec;
if(rt[i]==0)
finish[i]=1;
for(j=0;j<n;j++)
if(j!=i && finish[j]==0 && at[j]<=time)
wt[j]+=dec;
time=time+dec;
}
}
}
cout<<"("<<total<<")"<<endl;
dispTime();
}
int main()
{
sched t;
int ch;
t.computeRR();
}

Output:



6.Write a program to implement fcfs scheduling algorithm.

Write a program to implement fcfs scheduling algorithm.

//ducskecode

//ducskecode.blogspot.com
#include<iostream>
#include<stdio.h>
using namespace std;
class fcfs
{  
public:
int burst_tm,arv_tm,wait_tm;
char pid;
};
int  main()
{
int n,sum=0,carry;
cout<<"Enter the no. of processes:\n";
cin>>n;
fcfs temp;
fcfs *f=new fcfs[n];
for( int i=0;i<n;i++)
{
cout<<"Enter the process id:\n";
cin>>f[i].pid;
cout<<"Enter the burst timing:\n";
cin>>f[i].burst_tm;
cout<<"Enter the arrival timing:\n";
cin>>f[i].arv_tm;
//system("cls");
}
int k=0,flag=1;
while(k!=0)
{
if(f[k].arv_tm==0)
{
flag=0;
break;
}
k++;
}
if(!flag)
{
cout<<"enter all values of arival time as no arrival time is 0\n";
return 1;
}
for(k=0;k<n;k++)
{
temp=f[k];
for(int i=k+1;i<n;i++)
{
if(temp.arv_tm>f[i].arv_tm)
{
temp=f[i];
f[i]=f[k];
f[k]=temp;
}
}
}
cout<<"Entered info is as follows:\n";
int avg_wait_tm=0,avg_around_tm=0;
for(k=0;k<n;k++)
{                                            
f[k].wait_tm=sum-f[k].arv_tm;
if(f[k].wait_tm<0)
f[k].wait_tm=0;
                                   
sum+=f[k].burst_tm;
avg_wait_tm+=f[k].wait_tm;
avg_around_tm+=f[k].wait_tm+f[k].burst_tm;
}
avg_around_tm/=n;
avg_wait_tm/=n;
for(k=0;k<n;k++)
{
cout<<"pid: "<<f[k].pid<<endl<<"Burst time: "<<f[k].burst_tm<<endl<<"arrival time:"<<f[k].arv_tm<<endl<<"Waiting time: "<<f[k].wait_tm<<endl;
 }
cout<<"average turn around time:\n"<<avg_around_tm<<endl<<"average waiting time:\n"<<avg_wait_tm<<endl;
return 0;
}

Output:


5.A program to copy file using system call

A program to copy file using system call.

//ducskecode

#include<iostream>
using namespace std;
#include<fontl.h>
#include<stdlib.h>
#include<unistd.h>
void filecopy(int fi, int f2);
int main(int argc, char *argv[])
{
if(argc!=3)
{
cout<<"File not found ";
exit(1);
}
int fd1=open(argv[1], 0);
if(fd1==-1)
{
cout<<"\nError in file opening.";
exit(1);
}
int fd2=creat(argv[2], 0666);
if(fd2==-1)
{
cout<<"\n Error while creating file ";
exit(1);
}
filecopy (fdi, fd2);
close(fd1);
close(fd2);
return 0;
}
void filecopy (int fi, int f2)
{
char buf[512];
int cnt;
while(cnt=read(f1, buf, sizeof(buf)))
{
write(f2, buf, cnt) ;
}
}

Output:



4.A PROGRAM to print file details including owner access permissions, file access time, where file name is given as argument.

A PROGRAM to print file details including owner access permissions, file access time, where file name is given as argument.

//ducskecode

#include<iostream>
using namespace std;
#include<stdlib.h>
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main(int argc, char *argv[])
{
int i;
struct stat s;
if (argc < 2)
{
cout<<"\n enter filename in";
//exit();
}
for(i=1;i<argc; i++)
{
cout<<"File : "<<argv[i]<<"\n";
if(stat(argv[i],&s)<O)
cout<<"error in obtaining stats In":
else
{
cout<<"owner UID : "; cout<<s.st_uid; cout<<"\n";
cout<<"group ID :"; cout<<s.st_gid; cout<<"\n";
cout<<"Acess permissions : "; cout<<s.st_mode; COut<<"\n";
cout<<"Acess Time : :cout<<s.st_atime; cout<<"\n":
cout<<"File Size : "; cout<<s.st_size; cout<<"\n";
cout<<"File Size(in blocks) : "; cout<<s.st_blksize; cout<<"\n";
}
}
return 0;
}


Output:



2.A PROGRAM to report behaviour of Linux kernel including kernel version, CPU type and model. (CPU information)

2.A PROGRAM to report behaviour of Linux kernel including kernel version, CPU type and model. (CPU information)

OR

3.A PROGRAM to report behaviour of Linux kernel including information on configured memory, amount of free and used memory. (memory information)


//ducskecode

//ducskecode.blogspot.com
#include<iostream>
using namespace std
#include<stdlib.h>
#include<stdio.h>
int main() 
{
cout<<"\n The kernel version is, \n";
system("car/proc/sys/kernel/osrelease") ;
cout<<"\n The cpu space: \n";
system("cat /proc/cputnfo | awk 'NR==3, NR==4{print}' \n ");
cout<<"\n Amount of cpu time since system was last booted is: ";
system("cat /proc/uptime \n");
system("cat /proc/meminfo | awk 'NR==1, NR==4{print $2}' \n ");
cout<<"\n Amount of free memory: \n";
system("cat /proc/merminfo  |awk 'NR = 2{Print $2}' \n ")
cout<<"\n Amount of used memory is: \n";
system("cat /proc/meninfo | awk '{ if (NR==1) a=$2; if(NR==2) b=$2 }END {print a-b} ' \n");
cout<<endl;
return(0) ;
}

Output:




1. write a program (using fork() and/or exec() commands) where parent and child execute

/*WRITE A PROGRAM (using fork() and/or exec() commands) where parent and child execute:*/

a) same program, same code.

//ducskecode.blogspot.com
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main ()
{
int pid_t,pid, p;
p=fork();.
pid=getpid() ;
if(p<0)
{
printf(" Fork Failed ") ;
return 1;
}
printf("output of fork id %d \n",P);
printf("process id is : %d \n",pid);
return 0;
}

Output:




b) same program, different code

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int p_id;
pid fork();
if(p_id<0)
{
Print("\n Error "):
exit(1);
}
else if(p_id==0)
{
printf("\n Hello I an child process ");
printf("\n my pid is %d ",getpid ());
exit(o);
}
else
{
printf("\n Hello i am parent process ");
printf("\n My actual pid is %d \n",getpid ());
exit(1);
}
}

Output:


C) parents waits for child to terminate first then parent process terminates after child. 

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int pid;
pid=fork()
if(pid<0)
{
printf("\n Error ")
exit(1)
}
else if(pid==0)
{
printf("\n Hello I arn child process ");
printf("\n my pid is xd ",getpid ( ));
exit(0);
}
else if(pid>0)
{
Printf("\n Hello 1 an parent Process ").
printf("\n My actual pid is %d \n",getpid ());
wait(null);
exit(1);
}
}
Output:


Featured post

Amazon Interview Process

On July 5, 1994, Jeff Bezos started the world's most "customer-centric" firm out of his garage in Bellevue, Washington. The A...