Sie sind auf Seite 1von 7

Name: Mehta Karan Hemendra

Registration Number: 15BCE0877


Slot: C2
Faculty Name: Vijayasherly V.

DIGITAL ASSIGNMENT
1
Question 11:
Write a C program that identifies all processes
(user-level) running for more than 5 seconds and
kill such processes. Also list all processes owned by
root running for more than 10 days.

C Code for the question:


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


at user level and writing them to file.txt, the head command is used
to have an idea of column number so as to extract it using awk command
and writing it into respective files*/

system("ps -u `whoami` -f >file.txt");


system("cat file.txt | awk -F ' ' '{print $7}'>time.txt");
system("cat file.txt | awk -F ' ' '{print $8}'>Name.txt");
system("cat file.txt | awk -F ' ' '{print $2}'>PID.txt");
system("cat time.txt | awk -F ':' '{print $3}'>Seconds.txt");
system("sed -i -e '1,1d' Seconds.txt");
system("sed -i -e '1,1d' PID.txt");
system("sed -i -e '1,1d' Name.txt");

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

system("ps -U root -o pid,comm,etimes | awk -F ' ' '{print $1}'>pid_root.txt");


system("ps -U root -o pid,comm,etimes | awk -F ' ' '{print $2}'>comm_root.txt");
system("ps -U root -o pid,comm,etimes | awk -F ' ' '{print $3}'>etimes_root.txt");
system("sed -i -e '1,1d' pid_root.txt");
system("sed -i -e '1,1d' comm_root.txt");
system("sed -i -e '1,1d' etimes_root.txt");

//basic file handling stuff


//For killing user-level processes with time>5s

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

//To kill root processes with time>10days


FILE *p1,*t1,*na1;
char b[100];
int pid1[500],etime[500];
int w=0,x=0,y=0,z,flag=0;
p1=fopen("/root/Downloads/pid_root.txt","r");

t1=fopen("/root/Downloads/etimes_root.txt","r");
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;
}

printf("\n\n--------Root Processes running for more than 10 days----------\n");


printf("\nPID\t\tSeconds\t\tName\n");
for (l=0;l<w;l++)
{
if (etime[l]>864000) /*greater than 10 days //it will give no output as there was no such process
in my system which has process time >10 days*/
{
flag=1;
printf("%d\t\t%d\t\t%s\n",pid[l],etime[l],name[l]);

int killreturn=kill(pid[l],SIGKILL);
}
}

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 on the Next Page

OUTPUT:

Das könnte Ihnen auch gefallen