Digital Assignment 1: Name: Mehta Karan Hemendra Registration Number: 15BCE0877 Slot: C2 Faculty Name: Vijayasherly V
Digital Assignment 1: Name: Mehta Karan Hemendra Registration Number: 15BCE0877 Slot: C2 Faculty Name: Vijayasherly V
DIGITAL ASSIGNMENT 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<signal.h>
main()
/* this part of the code is to get the list of processes owned by root
precisely the pid,commencement time and elapsed time of process
and writing it into respective files*/
FILE *p,*t,*na;
char a[100];
int pid[500],time[500];
char name[200][200];
int n=0,i=0,j=0,k=0,l;
p=fopen("/root/Downloads/PID.txt","r");
t=fopen("/root/Downloads/Seconds.txt","r");
na=fopen("/root/Downloads/Name.txt","r");
/*while not end of file the data is being scanned and stored
the respective array*/
int pq=0;
while(!feof(na))
{
fscanf(na,"\n%s",a);
strcpy(name[pq],a);
pq=pq+1;
}
while(!feof(p))
{fscanf(p,"\n%s",a);
pid[i]=atoi(a);
i=i+1;
}
while(!feof(t))
{fscanf(t,"\n%s",a);
time[j]=atoi(a);
j=j+1;
}
printf("\n-----------User Processes running for 5 seconds or more-----------\n\n");
printf("\nPID\t\tSeconds\t\tName\n");
for (l=0;l<j;l++)
{
if (time[l]>=5)
{
printf("%d\t\t%d\t\t%s\n",pid[l],time[l],name[l]);
int killreturn=kill(pid[l],SIGKILL);
}
}
fclose(p);
fclose(t);
fclose(na);
na1=fopen("/root/Downloads/comm_root.txt","r");
while(!feof(na1))
{fscanf(na1,"\n%s",b);
}
while(!feof(p1))
{fscanf(p1,"\n%s",b);
pid1[w]=atoi(b);
w=w+1;
}
while(!feof(t1))
{fscanf(t1,"\n%s",b);
etime[x]=atoi(b);
x=x+1;
}
}
}
if(flag==0)
{
printf("No Process\tNo Process\tNo Process\n");
}
fclose(p1);
fclose(t1);
fclose(na1);
printf("\n\n------------------------------------DONE-------------------------------------------------\n\n");
system("rm /root/Downloads/Name.txt /root/Downloads/time.txt /root/Downloads/Seconds.txt
/root/Downloads/PID.txt /root/Downloads/comm_root.txt /root/Downloads/etimes_root.txt
/root/Downloads/pid_root.txt /root/Downloads/file.txt" );
}
OUTPUT: