0% found this document useful (0 votes)
6 views7 pages

Linux Module Programs

The document contains multiple C programs demonstrating inter-process communication using pipes and FIFOs, as well as various utility functions for string manipulation and file handling. It includes implementations for prime number checking, character counting, and finding the largest file in a directory. Additionally, it features a watchdog timer that monitors child process termination.

Uploaded by

nithish.ee20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Linux Module Programs

The document contains multiple C programs demonstrating inter-process communication using pipes and FIFOs, as well as various utility functions for string manipulation and file handling. It includes implementations for prime number checking, character counting, and finding the largest file in a directory. Additionally, it features a watchdog timer that monitors child process termination.

Uploaded by

nithish.ee20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

// P I P E

#include<stdio.h>
#include<unistd.h>
int prime_check(int n){
int i;
for(i=2;i<n;i++){
if(n%i==0)
break;
}
if(n==i)
return 1;
else
return 0;

void main(){
//int ptr;
int p[2];
int n;
pipe(p);
perror("pipe");
printf("p[0]readend=%d\n p[1]writeend=%d\n",p[0],p[1]);
if(fork()==0){
printf("waiting for the data\n");
read(p[0],&n,sizeof(n));

printf("received in child =%d\n",n);

int res=prime_check(n);
if(res==1)
write(p[1],"it is prime",12);
else
write(p[1],"it is not prime",16);
}
else{
char s[100];
printf("enter the string\n");
scanf("%d",&n);
write(p[1],&n,sizeof(int));
sleep(1);
read(p[0],s,sizeof(s));
printf("%s\n",s);
}

// F I F O

-----> TRANSMITTER
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <string.h>

void main(int argc,char **argv){


//create
mkfifo(argv[1],0664);
perror("mkfifio");
mkfifo(argv[2],0664);
perror("mkfifo");
//opening
int fd1,fd2;
fd1=open(argv[1],O_WRONLY);
perror("open");
fd2=open(argv[2],O_RDONLY);
perror("open");
//IO OPERATION
char s[100];
while(1){
printf("enter the string\n");
scanf(" %[^\n]",s);
write(fd1,s,strlen(s)+1);
read(fd2,s,sizeof(s));
printf("after updating=%s\n",s);

// RECEIVER

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <string.h>

void count char (char *s){

for(i=0;s[i];i++)
{
for(k=i-1,flag=0;k>=0;k--)
{
if(s[k]==s[i])
{
flag=1;
break;
}
}
if(flag==0)
{
for(c=1,j=i+1;s[j];j++)
{
if(s[i]==s[j])
c++;
}
printf("%c is present %d times\n",s[i],c);
}
}
}

void count_dup_char(char *s){

for(i=0;s[i];i++)
{
for(j=i+1,count=1;s[j];j++)
{
if(s[i]==s[j])
count++;
}

for(k=i-1,flag=0;k>=0;k--)
if(s[i]==s[k])
{
flag=1;
break;
}

if(flag==0)
{
if(count>1)
printf("%c is %d times present\n",s[i],count);
}
}

int my_atoi(char *p){

int i,j,res;
if(p[0]==’+’||p[1]==’-’)
i=1;
else
i=0;
for(res=0;p[i];i++){
if(p[i]>=’0’&&p[i]<=’9’)
res=res*10+(p[i]-48);

else
break;

}
if(p[0]==’-’)
res=-res;

return res;
}

void delete_dup(char *p){

int i,j,k;
for(i=0;p[i];i++)
{
for(j=i+1;p[j];j++)
{
if(p[i]==p[j])
{
for(k=j;p[k];k++)
p[k]=p[k+1];
j--;
// p[k+1]=’\0’;
}

}
}
void strrev(char *s){
int i,j;
char ch;
for(i=0,j=strlen(s)-1;i<j;i++,j--){
ch=s[i];
s[i]=s[j];
s[j]=ch;
}

float my_atof(char *s)


{
int num,n1,c,i;
if(s[0]==’-’||s[0]==’+’)
i=1;
else
i=0;
for(num=0;s[i];i++)
{
if(s[i]>=’0’&&s[i]<=’9’)
num=num*10+s[i]-48;
else if(s[i]==’.’)
{
for(n1=0,c=1,i++;s[i];i++)
{
if(s[i]>=’0’&&s[i]<=’9’)
{
n1=n1*10+s[i]-48;
c=c*10;
}
else
break;
}
break;
}
else
break;
}
n1=n1/c;
num=num+n1;
if(s[0]==’-’)
num=-num;
return num;
}

void main(int argc,char **argv){


int fd1,fd2;
fd1=open(argv[1],O_RDONLY);
perror("open");
fd2=open(argv[2],O_WRONLY);
perror("open");
char s[100];

while(1){
read(fd1,s,sizeof(s));
printf("received string=%s\n",s);
// strrev(s);
//delete_dup(s);
//printf("after deleting dup=%s\n",s);
int num=my_atoi(s);
printf("after converting int num=%d\n",num);
write(fd2,s,strlen(s)+1);

// FINDING THE HIGHEST SIZE FILE IN A DIRECTORY


#include <sys/types.h>
#include <sys/stat.h>
#include<string.h>
#include <unistd.h>
#include<stdio.h>
#include<dirent.h>
void main(int argc,char **argv){
if(argc!=2){
printf("uauage: ./a.out dirname\n");
return;
}
int n=0;
char arr[200];
DIR *dp=opendir(argv[1]);
struct dirent *p;
struct stat v;
while(p=readdir(dp))
{
stat(p->d_name,&v);

//printf("length of file =%d ",v.st_size);


//printf("%s \n",p->d_name);
if(v.st_size>n){
n=v.st_size;
strcpy(arr,p->d_name);
}
}
printf("highest file size name=%s\n",arr);
printf("length of file=%d\n",n);

// WATCH DOG TIMER

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

int r;

void my_isr(int n){


if(n==SIGCHLD)
{
printf("child terminate normally \n");

}
else if(n==SIGALRM)
{
if(kill(r,0)!=-1){
printf("child terminate forcefully\n");
}
signal(SIGCHLD,SIG_DFL);
kill(r,9);
}
}
void main(){

r=fork();

if(r==0){
int n;

srand(getpid());
n=rand()%10+1;
printf("child before sleep random num=%d\n",n);
sleep(n);
printf("after sleep\n");}

else{
printf("main function id=%d\n",getpid());
struct sigaction v;
//////////////////////
v.sa_handler=my_isr;
sigemptyset(&v.sa_mask);
//v.sa_flags=SA_NOCLDWAIT|SA_NOCLDSTOP;
v.sa_flags=0;
sigaction(SIGCHLD,&v,0);
////////////////////////
v.sa_flags=0;
sigaction(SIGALRM,&v,0);
alarm(5);
printf("hello...\n");
// sleep(10);
while(1);
}
}

You might also like