Linux Lab Ex 2a-6
Linux Lab Ex 2a-6
SYSTEM CALLS
PROGRAM:
#include <stdio.h>
#include <dirent.h>
int main() {
const char *dir_path = "/home/2CB101"; // Current directory
// Open the directory
DIR *dir = opendir(dir_path);
if (dir == NULL) {
perror("opendir");
return 1;
}
return 0;
}
OUT PUT
[2CB101@localhost ~]$ vi a1.c
[2CB101@localhost ~]$ gcc a1.c
[2CB101@localhost ~]$ ./a.out
.program9.c.swp
batch3
d44
pgm4.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // For fork, getpid, _exit
int main() {
pid_t pid; // To store process ID
printf("Starting the program. Parent process ID: %d\n", getpid());
// Create a new process
pid = fork();
if (pid < 0) {
// Fork failed
perror("fork");
_exit(1);
} else if (pid == 0) {
// Child process
printf("This is the child process. PID: %d, Parent PID: %d\n", getpid(),
getppid());
_exit(0); // Exit child process
} else {
// Parent process
printf("This is the parent process. PID: %d, Child PID: %d\n", getpid(), pid);
}
printf("Exiting process with PID: %d\n", getpid());
return 0;
}
Output:
PROGRAM:
#include <fcntl.h>
#include <stdio.h>
int main() {
int fd;
return 0;
}
Output:
[2CB101@localhost ~]$ vi a2.c
[2CB101@localhost ~]$ gcc a2.c
[2CB101@localhost ~]$ ./a.out
File opened successfully with file descriptor: 3
OUTPUT:
[2CB101@localhost ~]$ vi a2.c
[2CB101@localhost ~]$ gcc a2.c
[2CB101@localhost ~]$ ./a.out
a
b
e
f
PROGRAM:
#include<stdio.h>
#include<string.h>
void main()
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("Enter file name\n");
scanf("%s",fn);
printf("Enter pattern to be searched\n");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}
Out put:
[2CB101@localhost ~]$ gcc a2.c
[2CB101@localhost ~]$ ./a.out
Enter file name
a3
Enter pattern to be searched
a
a
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
/*Start data transfer from src file to dest file till it reaches EOF*/
while((nbread = read(srcFD,buff,BUFF_SIZE)) > 0)
{
if(write(destFD,buff,nbread) != nbread)
printf("\nError in writing data to %s\n",argv[2]);
}
if(nbread == -1)
printf("\nError in reading data from %s\n",argv[1]);
if(close(srcFD) == -1)
printf("\nError in closing file %s\n",argv[1]);
if(close(destFD) == -1)
printf("\nError in closing file %s\n",argv[2]);
exit(EXIT_SUCCESS);
}
Output:
[2CB101@localhost ~]$ gcc ex3b.c
[2CB101@localhost ~]$ ./a.out ex.txt ex1.txt
[2CB101@localhost ~]$ cat ex1.txt
linux lab
PROGRAM:
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[3] )
{
int fd,i;
char buf[2];
fd=open(argv[1],O_RDONLY,0777);
if(fd==-argc)
{
printf("file open error");
}
else
{
while((i=read(fd,buf,1))>0)
{
printf("%c",buf[0]);
}
close(fd);
}
}
Output:
[2CB101@localhost ~]$ gcc ex3c.c
[2CB101@localhost ~]$ ./a.out ex.txt
linux lab
PROGRAM:
echo "Enter the radius: "
read r
p=3.14
a=`expr $p\*$r\*$r|bc`
echo "Area is $a"
PROGRAM:
echo "Enter three numbers:"
read a
read b
read c
if test $a -gt $b -a $a -gt $c
then
echo "A is largest"
elif test $b -gt $c
then
echo "B is largest"
else
echo "C is largest"
fi
OUTPUT:
[2CB101@localhost ~]$ sh ex4b.sh
Enter three numbers:
1
2
3
C is largest
[2CB101@localhost ~]$
PROGRAM:
echo "Enter two no.s"
read a
read b
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Exit"
echo "Choose an option"
read c
case $c in
1)s=`expr $a+$b|bc`;;
2)s=`expr $a-$b|bc`;;
3)s=`expr $a\*$b|bc`;;
4)s=`expr $a\$b|bc`;;
5)exit;;
esac
echo "Result is $s"
OUTPUT:
[2CB101@localhost ~]$ sh ex4c.sh
Enter two no.s
23
12
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Choose an option
1
Result is 35
[2CB101@localhost ~]$
PROGRAM:
echo "Enter a number"
read n
s=0
z=0
t=10
while test $n -gt $z
do
r=`expr $n % $t|bc`
n=`expr $n / $t|bc`
s=`expr $s + $r|bc`
done
echo "Sum of digits is: $s"
OUTPUT:
[2CB101@localhost ~]$ vi ex4d.sh
[2CB101@localhost ~]$ sh ex4d.sh
Enter a number
3
Sum of digits is: 3
[2CB101@localhost ~]$
EX.NO: 4e COMMAND SUBSTITUTION
PROGRAM:
echo "1)Display list of files"
a=`ls`
echo $a
echo
echo
echo "2)Display calendar"
b=`cal`
echo $b
echo
echo
echo "3)Display the date"
c=`date`
echo $c
echo
echo
echo "4)Display the file content"
f=`cat area1.sh`
echo $f
echo
OUTPUT:
[2CB101@localhost ~]$ sh ex4e.sh
1)Display list of files
a a2 a3 aa aaa aa.sh abi abi23 abi27 abi.c abiii abirockz a.c ad.txt ase b b1 b3 basic cc
color cse d1 d11 d12 d15 d16 d17 d18 d2 d24 d31 d32 d36 d37 d4 d44 d5 d6 d7 d8
dd.txt demo.c Desktop di Documents Downloads e1.c e2.c e3.c e4.c e4.sh e5.c e6.c
ee.c eee.c eeee.c ex1.txt ex3a.c ex3b.c ex3c.c ex4a.sh ex4b.sh ex4c.sh ex4d.sh ex4e.sh
exam exam1 ex.txt fe.txt file2 file3 gr mech pgm4.c pgm5.c pgm6.c pgm7.c pgm.c
prime.c prime.sh program1.c program2.c program3.c program4.c program5.c
program7.c program8.c p.sh p.txt q q1 q2 qa.c q.c qq qq.c qqq.c rg sd ss s.sh sss
Templates test1 test2 test3 Videos zzz
2)Display calendar
January 2001 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31
[2CB101@localhost ~]$
PROGRAM:
#include <stdio.h>
main()
{
int i,bt[10],p[10],cp[10],j,k,wt[10],tat[10];
int n,t;
float atat,awt;
printf("\n\t First In First Out");
printf("\n\t No. Of Process : ");
scanf(" %d",&n);
printf(" \n\t Process Burst Time\n");
for(i=1;i<=n;i++)
{
printf("\t P%d\t",i);
scanf(" %d",&bt[i]);
}
wt[1]=0;
tat[1]=bt[1];
atat=tat[1];
awt=0;
for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
}
printf("\n\t Process Burst-Time Wait-Time Turnaround-Time\n");
for(i=1;i<=n;i++)
printf("\n\t P%d \t %d \t\t %d \t\t%d\n",i,bt[i],wt[i],tat[i]);
printf("\n\t Average Turn Around Time : %f \n",atat/n);
printf("\n\t Average Waiting Time : %f \n",awt/n);
}
OUTPUT:
[2CB101@localhost ~]$ gcc ex5a.c
[2CB101@localhost ~]$ ./a.out
P1 1 0 1
P2 2 1 3
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(bt[i]<bt[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}
wt[1]=0;
tat[1]=bt[1];
atat=tat[1];
awt=0;
for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
}
OUTPUT:
No. Of Process : 2
P1 1
P2 3
Process Burst-Time Wait-Time Turnaround-Time
P1 1 0 1
P2 3 1 4
PROGRAM:
#include <stdio.h>
main()
{
int i,bt[10],p[10],pr[10],cp[10],j,k,wt[10],tat[10];
int n,t;
float atat,awt;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(pr[i]<pr[j])
{
t=bt[i];
bt[i]=bt[j];
bt[j]=t;
t=p[i];
p[i]=p[j];
p[j]=t;
t=pr[i];
pr[i]=pr[j];
pr[j]=t;
}
}
}
wt[1]=0;
tat[1]=bt[1];
atat=tat[1];
awt=0;
for(i=2;i<=n;i++)
{
wt[i]=wt[i-1]+bt[i-1];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
}
OUTPUT:
[2CB101@localhost ~]$ gcc ex5c.c
[2CB101@localhost ~]$ ./a.out
Priority Scheduling
No. Of Process : 2
For Process P1 :
Burst Time : 2
Priority : 2
For Process P2 :
Burst Time : 3
Priority : 3
P1 2 2
P2 3 3
Process Burst-Time Wait-Time Turnaround-Time
P1 2 0 2
P2 3 2 5
Average Turn Around Time : 3.500000
PROGRAM:
#include <stdio.h>
main()
{
int i,f[10],bt[10],p[10],cbt[10],j,k,wt[10],tat[10];
int n,t,q,flag;
float atat,awt;
for(i=1;i<=n;i++)
{
printf("\n\t P%d \t ",i);
scanf(" %d",&bt[i]);
cbt[i]=bt[i];
p[i]=i;
}
for(i=1;i<=n;i++)
{
wt[i]=0;
tat[i]=0;
f[i]=0;
}
do
{
for(i=1;i<=n;i++)
{
if(f[i]==0)
{
if(bt[i]<=q)
{
f[i]=1;
t=bt[i];
}
else
t=q;
bt[i]=bt[i]-t;
for(j=1;j<=n;j++)
{
if((j==i)||(f[j]==1))
continue;
wt[j]=wt[j]+t;
}//for j
}//if finish
}//for i
flag=0;
for(i=1;i<=n;i++)
if(f[i]==0)
flag=1;
}while(flag==1);
atat=awt=0;
for(i=1;i<=n;i++)
{
tat[i]=wt[i]+cbt[i];
atat+=tat[i];
awt+=wt[i];
}
OUTPUT:
[2CB101@localhost ~]$ gcc ex5d.c
[2CB101@localhost ~]$ ./a.out
No. Of Process : 2
P2 2
Time Quantum : 2
P1 1 0 1
P2 2 1 3
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
OUTPUT: