Bash Codes

These are the programs that make you get started on OS.All of them need to be run on unix/Linux shells.The first three programmes are shell scripts and the rest all are in plain-C


Q1.Write a programme to take the input time in seconds and convert it into HH:MM:SS format ?
#!/bin/bash
echo "$(($1/3600))hr:$((($1%3600)/60))min:$((($1%3600)%60))sec"

Q2.Write a Programme to take two numbers as input and display all the numbers in between them ?

#!/bin/bash
for ((i=$1;i<=$2;i++))
do
echo $i
done

Q3.Write a Programme to display date,time,username,hostname and the number of users currently logged in ?

#!/bin/bash
clear
date +%D
date +%T
hostname
pwd
logname
echo "$((`who|wc -l`-1))"

Q4.Write a Programme to create child process using fork system call and return it’s PID ?

#include<stdio.h>
#include<sys/types.h>
main()
{int pid;
printf("The process id of the parent process is %d\n",getpid());
pid=fork();
if(pid==0)
printf("The process id of the child process is %d\n",getpid());
else if(pid<0)
printf("Sorry, couldn't create the child process");
else
printf("The process id of parent process is %d\n",getpid());
}


Q5.Write a C-programme that uses “pipe” system call to communicate using a string between a parent and a child process ?

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{int n,l;
char buf[100];
int fd[2];
printf("Starting the piping process\n");
pipe(fd);
l=fork();
if(l==0)
{close(fd[1]);
printf("Running the child process\n");
n=read(fd[0],buf,100);
printf("The value of n is:%d\n",n);
printf("The value received is\n%s\n",buf);
}
else if(l<0)
printf("Child cannot be created\n");
else
{close(fd[0]);
printf("Running the parent process\n");

write(fd[1],"I am a good boy\n",16);
printf("Wrote to the pipe\n");
}
getche();
return 0;
}

Q6.Write a C-programme to create n-processes, the first process accepts an integer increments it by “1” and writes it to the pipe and the second process receives, increments it by “1” and so on. The last process reads, increments by “1” and finally displays it’s value ?

#include<stdio.h>
int main()
{int fd[2];
int n,i,l,a[1];
if(fork()==0)
{read(fd[0],a,1);
a[0]++;printf("%d\n",a[0]);
write(fd[1],a,1);
exit(0);
}
else
{
printf("Enter the number of processes you want:");
scanf("%d",&n);
printf("Enter the number to be incremented:");
scanf("%d",&a[0]);
read(fd[0],a,1);
write(fd[1],a,1);
exit(0);
}
for(i=0;i<n-1;i++)
{l=fork();
if(l==0)
{read(fd[0],a,1);
a[0]++;
write(fd[1],a,1);
exit(0);
}
}
if(i==n)
{read(fd[0],a,1);
printf("%d",a);
exit(0);
}
return 0;
}

Q7.Write a C-programme to create a child process, the parent process will run, “ls –l” command and sends the input to child process, which runs “wc –l” command ?

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
int main()
{int n,l;
FILE *fp;
char command[400];//For storing the command
char buf[300];
int fd[2];
printf("Starting the piping process\n");
pipe(fd);
l=fork();
if(l==0)
{close(fd[1]);
printf("Running the child process\n");
n=read(fd[0],buf,300);
sprintf(command,"wc -l %s",buf);
system("command");
}
else if(l<0)
printf("Child cannot be created\n");
else
{close(fd[0]);
printf("Running the parent process\n");
fp=popen("ls -l","r");//For system calls reading
write(fd[1],fp,200);
printf("Wrote to the pipe\n");
}
getche();
return 0;
}

2 comments: