Unix Programs
Unix Programs
int main(void)
{
DIR *d;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s\n", dir->d_name);
}
closedir(d);
}
return(0);
}
//Write a program to implement your own signal handler.
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
void sig_handler(int signum){
int main(){
signal(SIGINT,sig_handler); // Register signal handler
for(int i=1;;i++){ //Infinite loop
printf("%d : Inside main function\n",i);
sleep(1); // Delay for 1 second
}
return 0;
}
//namei
#include <dirent.h>
#include <iostream>
cout<<"Directory\t\t\tInode"<<endl;
//pipes
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <dirent.h>
int pipefd[2];
pipe(pipefd);
if (fork() == 0)
{
close(1);
dup(pipefd[1]);
close(pipefd[0]);
close(pipefd[1]);
execvp(cmd1[0], cmd1);
}
else if (fork() == -1)
{
close(0);
dup(pipefd[0]);
close(pipefd[0]);
close(pipefd[1]);
execvp(cmd2[0], cmd2);
}
char *cmd1[50];
char *cmd2[50];
cmd1[0] = "cat";
cmd1[1] = "/etc/passwd";
cmd1[2] = NULL;
cmd2[0] = "grep";
cmd2[1] = "root";
cmd2[2] = NULL;
PipeImple(cmd1, cmd2);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
int a=fork();
int c,b,x;
pid_t pid= getpid();
pid_t ppid= getppid();
if(a==0){
printf("child process is created \n");
printf("child process id : %d \n",pid);
printf("parent process id : %d \n",ppid);
signal(SIGFPE, Exception);
printf("Enter First Number: \n");
scanf("%d",&c);
printf("Enter second Number: \n");
scanf("%d",&b);
x=c/b;
printf("Result:%d \n",x);
}
else{
signal(SIGCHLD,parent);
printf("\n parent waiting for the child to finish");
pause();
}
return 0;
}
int main(void) {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
printf("%s", asctime(tm));
return 0;
}
//addition of two numbers shell script(not accurate)
#!/bin/bash
# Take input from user and calculate sum.
//Write a script to compare larger integer values from a ‘n’ number of arguments
using
command line arguments
//Write a shell script to display the longest and shortest user-names on the
system.
#!/bin/bash
MAX_LEN=0
MIN_LEN=100
// Write a shell script to shutdown the system on receiving a ‘bye bye’ string.
Else display
greeting messages according to time. E.g.- Good morning (Till 12.00 noon).(else
part implemented)
h=$(date +"%H")
if [ $h -gt 6 -a $h -le 12 ]
then
echo good morning
elif [ $h -gt 12 -a $h -le 16 ]
then
echo good afternoon
elif [ $h -gt 16 -a $h -le 20 ]
then
echo good evening
else
echo good night
fi
#!/bin/bash
read -p "Enter file name : " filename
while read -n1 character
do
echo $character
done < $filename
#include <stdio.h>
#include <sys/types.h>
return 0;
}