Download - Assignment no39
Assignment No. 1
Title:-Write a C/C++ script to display all logged in users.
#include<stdio.h>
#include<sys/utsname.h>
#include<utmp.h>
int main(void)
{
structutmp *n;
char *a;
int i;
setutent();
n=getutent();
while(n!=NULL)
{
if(n->ut_type==7)
{
printf("%9s",n->ut_user);
printf("%12s",n->ut_line);
//a=ctime(&n->ut_time);
printf(" ");
for(i=4;i<16;i++)
printf("%c",a[i]);
printf(" (");
printf("%s",n->ut_host);
printf(")");
printf("\n");
}
n=getutent();
}
}
Output:
[jay@localhost se_b_29]$gccallloggedin.c
[jay@localhost se_b_29]$./a.out
jay :0 �^H��H���PTI (:0)
jaypts/0 �^H��H���PTI (:0)
// [Ctrl+Altr+F4] login as jay in tty4
Assignment No. 2
Title:-Write a function to display the list of devices connected to your system including the phys-ical names and its instance number. Write a function using mount and unmount command to mount device and un-mount it.
# include <dirent.h># include <stdio.h># include <sys/stat.h># include<string.h># include<fcntl.h>
void quit(char *,int);
char *path="/run/media/root/";struct file1{char *filename;
} f[50];
int main(){DIR *dir;int i=0,k=0,count=1;
structdirent *direntry;
if ((dir=opendir(path))==NULL)quit("opendir",1);printf("\n List of connected usb to this computer: \n");while((direntry=readdir(dir)) !=NULL){
if(!strcmp(direntry->d_name,".")||!strcmp(direntry->d_name,"..")){}else{f[k].filename=direntry->d_name;printf(" %d. %s \n",count,f[k].filename);k++;count++;}
i++;}
closedir(dir);
return 0;}
Output:[jay@localhost se_b_29]$gcc conn.cquit.o[jay@localhost se_b_29]$./a.out
List of connected usb to this computer: 1. SONY_16GM
Assignment no 4Title: C/C++ Program to assign nice values to processes and dynamically monitor them.
#include<stdio.h>#include<stdlib.h>#include<sys/resource.h>#include<unistd.h>int main(){
int rc, nc;int ch;while(1){system("ps axl");printf("\n Current value can be checked from above table");printf("\n the current priority process is %d is
%d",getpid(),getpriority(PRIO_PROCESS,0));printf("\n Enter new nice value");scanf("%d",&nc);rc=setpriority(PRIO_PROCESS,0,nc);if(0 != rc){perror("setpriority");}sleep(2);printf("\n the current priority process is %d is %d",getpid(),getpriority(PRIO_PROCESS,0));printf("\n do you want to continue(1 continue and 0 for exit):");scanf("%d",&ch);if(ch==0)exit(0);}return 0;}
========================================================================================
OutPut:-0 0 2037 1 20 0 578004 18364 poll_sSl ? 0:00 /usr/libexe
4 0 2040 2037 20 0 8456 668 unix_s S ? 0:00 gnome-pty-h4 0 2041 2037 20 0 116008 2276 wait Sspts/0 0:00 bash4 0 2064 2041 20 0 199540 3992 wait S pts/0 0:00 su4 0 2067 2064 20 0 116012 2316 wait S pts/0 0:00 bash0 0 2179 1 20 0 665848 17396 poll_sSl ? 0:00 file-roller1 0 2334 2 20 0 0 0 worker S ? 0:00 [kworker/0:1 0 2335 2 20 0 0 0 worker S ? 0:00 [kworker/0:0 0 2355 1461 20 0 320988 11312 pipe_w S ? 0:15 /usr/bin/py0 0 2371 1461 20 0 320328 10744 pipe_w S ? 0:15 /usr/bin/py0 0 2373 2067 30 10 4164 324 wait SN+ pts/0 0:00 ./a.out0 0 2375 1461 20 0 320464 10556 - Rl ? 0:00 /usr/bin/py0 0 2377 2373 30 10 110700 828 - RN+ pts/0 0:00 psaxl
Current value can be checked from above tablethe current priority process is 2373 is 10 Enter new nice value
Title:-C/C++ program to identify the available memory in the system./*
* C program to print the system statistics like system uptime,
* total RAM space, free RAM space, process count, page size
*/
#include <sys/sysinfo.h> // sysinfo
#include <stdio.h>
#include <unistd.h> // sysconf#include "syscalls.h" // just contains a wrapper function - errorint main()
{
struct sys infomyinfo;
unsigned long total_bytes;unsigned long total_fb; unsigned long total_fc; if (sysinfo(&myinfo) != 0)
error("sysinfo: error reading system statistics");
total_bytes = myinfo.mem_unit * myinfo.totalram; total_fb = myinfo.mem_unit * myinfo.freeram;total_fc = total_bytes - total_fb; printf("total usable main memory is %lu Bytes, %lu MB\n",total_bytes, total_bytes/1024/1024); printf("Free available memory is: %lu Bytes, %lu MB\n",total_fc, (((total_fb)/1024)/1024)); printf("Uptime: %ld:%ld:%ld\n", myinfo.uptime/3600, myinfo.uptime%3600/60, myinfo.uptime%60);printf("Process count: %d\n", myinfo.procs);printf("Page size: %ld bytes\n", sysconf(_SC_PAGESIZE));return 0;
}
Output:-[jay@localhost se_b_29]$ gccmyram_c.c[jay@localhost se_b_29]$ ./a.out
total usable main memory is 1036783616 Bytes, 988 MB // Here is total usable memoryFree available memory is: 962154496 Bytes, 71 MB
// Free available memoryUptime: 1:3:57
// Time from log inProcess count: 279
//All process countPage size: 4096 bytes
// Page size
// compare memory output with system-call output
[jay@localhost se_b_47]$ free -m
total used free shared buffers cached
Mem: 988 918 70 0 18 240 // total memory is 988 anf free memory is 70-/+ buffers/cache: 660 328
Swap: 2079 6 2073
package hello;
importjava.io.BufferedReader;importjava.io.InputStreamReader;public class Main{public static void main(String[] args){int count=0;try{String line;Process p=Runtime.getRuntime().exec("who");p.waitFor();BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
while((line=in.readLine()) != null)
{System.out.println(line);count++;}System.out.print("\n Number of users are:"+count);in.close();}catch(Exception err){err.printStackTrace();}}}
/// --- OUTPUT ---///
jay :0 2013-09-23 16:22 (:0)jay tty3 2013-09-23 16:24jay tty4 2013-09-23 16:25
Number of users are:3
Title:-Java Program to display the list of devices connected to your system including the physical names and its instance number
import java.io.*;public class connected
public static void main(String[] args) {
String path = "/run/media/root/";
File file = new File(path); String[] myFiles;
int count=1; if(file.isDirectory())
{
myFiles = file.list(); System.out.println("\n List of connected usb to this computer:");
for (int i=0; i<myFiles.length; i++) {
System.out.println("\n"+count+". "+myFiles[i]);count++;}
System.out.println();}
}}
Output:-[jay@localhost se_b_29]$ java connected
List of connected usb to this computer:
1 SONY_16GM
Assignment No 13
Title:-Write python script to display all logged in users.
Import psutilprint("\nList of currently logged in users in the system are as fol-lows:\n");
count=1;
for temp in psutil.get_users():
print("\nuser:",count);
count=count+1;
print(temp);
print("\n Total number of user logged into system:",count-1);
output:[jay@localhost se_b_47]$ cd /tmp[jay@localhosttmp]$ python python1.py
List of currently logged in users in the system are as follows:
('\nuser:', 1)
user(name='root', terminal=':0', host=':0', started=1379354752.0)
('\nuser:', 2)
user(name='root', terminal='pts/0', host=':0', started=1379356032.0)
('\nuser:', 3)
user(name='root', terminal='tty4', host='', started=1379355776.0)
('\nTotal number of user logged into system:', 3)
[[jay@localhosttmp]$ w
23:56:51 up 20 min, 3 users, load average: 0.30, 0.33, 0.27
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
jay :0 23:36 ?xdm? 2:48 0.13s gdm-session-worker [pam/gdm-pas
jaypts/0 23:56 3.00s 0.02s 0.00s w
jay tty4 23:53 ? 0.01s 0.01s -bash
[jay@localhosttmp]$ w
23:57:37 up 21 min, 4 users, load average: 0.13, 0.28, 0.26
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
jay :0 23:36 ?xdm? 2:51 0.13s gdm-session-worker [pam/gdm-pas
jay/0 23:56 1.00s 0.03s 0.01s w
jay tty4 23:53 ? 0.01s 0.01s -bash
jay tty2 23:57 ? 0.02s 0.02s -bash
[jay@localhosttmp]$ w
23:57:58 up 22 min, 3 users, load average: 0.09, 0.26, 0.25
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
jay :0 23:36 ?xdm? 2:54 0.14s gdm-session-worker [pam/gdm-pas
jaypts/0 23:56 6.00s 0.03s 0.00s w
jay tty4 23:53 ? 0.01s 0.01s -bash
Assignment no 14Title:-Python Program to demonstrate debugging of script.
14 B.I) Python program to demonstrate to debugging script.
import sysimportpdbpdb.set_trace();print("\n Enter the numbers:");a=input();b=input();c=int(a)+int(b);print("\n the first number:");print(a);print("\n the second number:");print(b);print("\n The addition of two numbers:");print(c);
///--- OUTPUT ---///
[jay@localhost se_b_47]$ python debug.py> /jay/debug.py(4)<module>()->print("\n Enter the numbers:");(Pdb) n
Enter the numbers:> /jay/debug.py(5)<module>()-> a=input();(Pdb) n11> /jay/debug.py(6)<module>()-> b=input();(Pdb) n12> /jay/debug.py(7)<module>()-> c=int(a)+int(b);(Pdb) n> /jay/debug.py(8)<module>()->print("\n the first number:");(Pdb) n
the first number:
> /jay/debug.py(9)<module>()->print(a);(Pdb) n11> /jay/debug.py(10)<module>()->print("\n the second number:");(Pdb) n
the second number:> /jay/debug.py(11)<module>()->print(b);(Pdb) n12> /jay/debug.py(12)<module>()->print("\n The addition of two numbers:");(Pdb) n
The addition of two numbers:> /jay/debug.py(13)<module>()->print(c);(Pdb) n23
Assignment No. 15Title:-Write a shell program to convert all lowercase letter in a file to upper-case letter.
#!/bin/bash # get filename echo -n "Enter File Name : " readfileName
# make sure file exits for readingif [ ! -f $fileName ]; then echo "Filename $fileName does not exists" exit 1fi
# convert uppercase to lowercase using tr commandtr '[a-z]' '[A-Z]' < $fileName
Output:[jay@localhost se_b_29]$cat data.txt Larry [email protected] 111-1111 Curly [email protected] 222-2222 Moe [email protected] 333-3333 [jay@localhost se_b_29]$ perl 7.shEnter File Name : data.txt LARRY [email protected] 111-1111 CURLY [email protected] 222-2222 MOE [email protected] 333-3333
Assignment No 17Title:- Write a Perl program that reads a file and counts the number of lines, characters, and words it contains and also prints count of the number of times the word appeared
$lines=0;
$words=0;
$chars=0;
print("\n\nCountingnumbar of words, lines and characters\n");
open(FILE,"<perl.txt");
while(<FILE>)
{
$lines++;
$chars+=length($_);
$words+=scalar(split(/\W+/,$_));
}
print("\nThenumbar of lines are=$lines");
print("\nThe number of words are=$words");
print("\nThe number of characters are=$chars");
close FILE;
print("\n\nWord count of word in the input file");
open(FILE,"<perl.txt");
my %count_of;
while(my $line=<FILE>)
{
foreach my $word(split /\s+/,$line)
{
$count_of{$word}++;
}
}
print("\nWords and their count:\n\n");
for my $word(sort keys %count_of)
{
print "'$word':$count_of{$word}\n";
}close FILE
output :
[jay@localhost se_b_29]$ cd home
[jay@localhost home]$perl perl1.pl
Counting numbar of words, lines and characters
The numbar of lines are=1
The number of words are=4
The number of characters are=22
Word count of word in the input file
Words and their count:
'is':1
'program.':1
'test':1
'this':1
Group CAssignment No 1
Title:-Write program to find number of CPU cores and CPU Manufacturer
publicclassNewClass{
publicstaticvoid main(String[] args){
String nameOS = "os.name";String versionOS = "os.version";String architectureOs="os.arch";
System.out.println("\n The information about processor \n no. of CPU in the System are:-");
System.out.println("Available processors (cores): " + Runtime.getRuntime().availableProcessors());
System.out.println(System.getenv("NUMBER_OF_PROCESSORS"));System.out.println("\n Processor Manufacturer");System.out.println(System.getenv("PROCESSOR_IDENTIFIER"));System.out.println("\n \n the Information about OS");System.out.println("\n Name of the OS:-"+System.getProperty(nameOS));System.out.println("\n VERSION of the OS:-"+System.getProperty(versionOS));System.out.println("\n Architecture of the OS:-"+System.getProperty(architectureOs));}
}
///--- OUTPUT ---///
The information about processor no. of CPU in the System are:-Available processors (cores): 1 Processor Manufacturernullthe Information about OS
Name of the OS:-Linux
VERSION of the OS:-3.9.5-301.fc19.x86_64
Architecture of the OS:-amd64