0% found this document useful (0 votes)
18 views22 pages

Linux Lab Ex 2a-6

The document contains various C and shell programming exercises focusing on system calls, process management, file operations, and basic arithmetic operations. It includes implementations of directory reading, process forking, file opening and reading, as well as simulating commands like grep, cp, and cat. Additionally, it covers CPU scheduling algorithms such as First Come First Served and Shortest Job First, along with examples of arithmetic operations and command substitution in shell scripting.

Uploaded by

ideathoncsb
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)
18 views22 pages

Linux Lab Ex 2a-6

The document contains various C and shell programming exercises focusing on system calls, process management, file operations, and basic arithmetic operations. It includes implementations of directory reading, process forking, file opening and reading, as well as simulating commands like grep, cp, and cat. Additionally, it covers CPU scheduling algorithms such as First Come First Served and Shortest Job First, along with examples of arithmetic operations and command substitution in shell scripting.

Uploaded by

ideathoncsb
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/ 22

EX.NO: 2a IMPLEMENTING opendir, readdir, closedir, etc..

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;
}

// Read and print directory entries


struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}

// Close the directory


closedir(dir);

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

EX.NO: 2b IMPLEMENTING Fork, getpid, Exit, Etc…. SYSTEM CALLS


PROGRAM:

#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:

[2CB101@localhost ~]$ vi a1.c


[2CB101@localhost ~]$ gcc a1.c
[2CB101@localhost ~]$ ./a.out
Starting the program. Parent process ID: 2818
This is the parent process. PID: 2818, Child PID: 2819
Exiting process with PID: 2818
This is the child process. PID: 2819, Parent PID: 1
EX.NO: 2c IMPLEMENTING open,read SYSTEM CALLS

1.OPEN SYSTEM CALL

PROGRAM:

#include <fcntl.h>
#include <stdio.h>

int main() {
int fd;

// Open the file


fd = open("example.txt", O_RDONLY);
if (fd < 0) {
perror("Error opening file");
return 1;
}

printf("File opened successfully with file descriptor: %d\n", fd);

// Close the file


close(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

2.READ SYSTEM CALL


#include<stdio.h>
#include<stdlib.h>
main()
{
char b[20];
int fd,xr;
if((fd=open("flowers.sh",0))==-1)
{
printf("cannot open file");
exit(1);
}
do
{
xr=read(fd,b,20);
b[xr]='\0';
printf("%s",b);
}
while(xr==20);
close(fd);}

OUTPUT:
[2CB101@localhost ~]$ vi a2.c
[2CB101@localhost ~]$ gcc a2.c
[2CB101@localhost ~]$ ./a.out
a
b
e
f

EX.NO:3a SIMULATE grep COMMAND USING C

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

EX.NO:3b SIMULATE cp COMMAND USING C

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>

#define BUFF_SIZE 1024

int main(int argc, char* argv[])


{
int srcFD,destFD,nbread,nbwrite;
char *buff[BUFF_SIZE];
/*Check if both src & dest files are received or --help is received to get usage*/
if(argc != 3 || argv[1] == "--help")
{
printf("\nUsage: cpcmd source_file destination_file\n");
exit(EXIT_FAILURE);
}
/*Open source file*/
srcFD = open(argv[1],O_RDONLY);
if(srcFD == -1)
{
printf("\nError opening file %s errno = %d\n",argv[1],errno);
exit(EXIT_FAILURE);
}
/*Open destination file with respective flags & modes O_CREAT & O_TRUNC is
to truncate existing file or create a new file S_IXXXX are file permissions for the
user,groups & others*/
destFD = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR |
S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(destFD == -1)
{
printf("\nError opening file %s errno = %d\n",argv[2],errno);
exit(EXIT_FAILURE);
}

/*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

EX.NO:3c SIMULATE cat COMMAND USING C

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

EX.NO: 4a AREA OF A CIRCLE

PROGRAM:
echo "Enter the radius: "
read r
p=3.14
a=`expr $p\*$r\*$r|bc`
echo "Area is $a"

[2CB101@localhost ~]$ sh ex4a.sh


Enter the radius:
4
Area is 50.24
[2CB101@localhost ~]$

EX.NO: 4b GREATEST OF THREE NUMBERS

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 ~]$

EX.NO: 4c ARITHMETIC OPERATION USING SWITCH STATEMENT

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 ~]$

EX.NO: 4d SUM OF DIGITS OF GIVEN NUMBER

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

3)Display the date


Mon Jan 1 11:19:10 IST 2001

4)Display the file content


cat: area1.sh: No such file or directory

[2CB101@localhost ~]$

EX.NO:5a IMPLEMENTING FIRST COME FIRST SERVED CPU SCHEDULING ALGORITHM

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

First In First Out


No. Of Process : 2

Process Burst Time


P1 1
P2 2

Process Burst-Time Wait-Time Turnaround-Time

P1 1 0 1

P2 2 1 3

Average Turn Around Time : 2.000000

Average Waiting Time : 0.500000


[2CB101@localhost ~]$

EX.NO:5b IMPLEMENTING SHORTEST JOB FIRST CPU SCHEDULING ALGORITHM


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\t Shortest Job First\n\t");

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]);
p[i]=i;
}

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;
}
}
}

printf("\n\t Executed In Following Order\n");


printf("\n\t Process Burst Time \n");
for(i=1;i<=n;i++)
printf("\n\t P%d \t %d",p[i],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",p[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);
return 0;
}

OUTPUT:

[2CB101@localhost ~]$ gcc ex5b.c


[2CB101@localhost ~]$ ./a.out

Shortest Job First

No. Of Process : 2

Process Burst Time


P1 1
P2 3
Executed In Following Order

Process Burst Time

P1 1
P2 3
Process Burst-Time Wait-Time Turnaround-Time

P1 1 0 1

P2 3 1 4

Average Turn Around Time : 2.500000

Average Waiting Time : 0.500000


[2CB101@localhost ~]$

EX.NO:5c IMPLEMENTING PRIORITY CPU SCHEDULING ALGORITHM

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;

printf("\n\t\t Priority Scheduling \n\t");

printf("\n\t No. Of Process : ");


scanf(" %d",&n);
for(i=1;i<=n;i++)
{
printf("\n\t For Process P%d : \n\tBurst Time : ",i);
scanf(" %d",&bt[i]);
printf("\t Priority : ");
scanf(" %d",&pr[i]);
p[i]=i;
}

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;
}
}
}

printf("\n\t Executed In Following Order\n");

printf("\n\t Process Burst Time Priority\n");


for(i=1;i<=n;i++)
printf("\n\t P%d \t %d \t %d",p[i],bt[i],pr[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",p[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);
return 0;
}

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

Executed In Following Order

Process Burst Time Priority

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

Average Waiting Time : 1.000000


[2CB101@localhost ~]$

EX.NO:5d IMPLEMENTING ROUND ROBIN CPU SCHEDULING ALGORITHM

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;

printf("\n\t\t Round Robin Scheduling \n\t");

printf("\n\t No. Of Process : ");


scanf(" %d",&n);

printf("\n\t Process Burst Time ");

for(i=1;i<=n;i++)
{
printf("\n\t P%d \t ",i);
scanf(" %d",&bt[i]);
cbt[i]=bt[i];
p[i]=i;
}

printf("\n\t Time Quantum : ");


scanf(" %d",&q);

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];
}

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",p[i],cbt[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);
return 0;
}

OUTPUT:
[2CB101@localhost ~]$ gcc ex5d.c
[2CB101@localhost ~]$ ./a.out

Round Robin Scheduling

No. Of Process : 2

Process Burst Time


P1 1

P2 2

Time Quantum : 2

Process Burst-Time Wait-Time Turnaround-Time

P1 1 0 1

P2 2 1 3

Average Turn Around Time : 2.000000

Average Waiting Time : 0.500000


[2CB101@localhost ~]$

EX.NO:6 IMPLEMENTING PRODUCER – CONSUMER ALGORITHM USING


SEMAPHORES

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:

You might also like