Assignment Name: Write a program in Python/C++ to read display
the i-node information
for a given text file, image file
1A.cpp(without fstat)
#include<iostream>
#include<fstream>
#include<string>
#include<sys/types.h>//
required
#include<sys/stat.h>//
required
#include<fcntl.h>
#include<cstring>
using namespace std;
int main()
{
struct stat buf; // stat
structure object creation
//Getting file name from
user
char filename[90];
cout<<"Enter
filename: ";
cin>> filename;
stat(filename,&buf);
// stat function which stores all req info in buffer;
// Printing Inode info
cout<<filename<<endl;
cout<<"File
type: ";
switch(buf.st_mode &
S_IFMT) // checking file type // Bit Mask for file type bit fields
{
case S_IFBLK:
cout<<"Its a Block device"<<endl;
break;
case S_IFREG:
cout<<"Its a Regular File"<<endl;
break;
case S_IFCHR:
cout<<"Its a Character device"<<endl;
break;
case S_IFDIR:
cout<<"Its a Directory"<<endl;
break;
case S_IFIFO:
cout<<"Its a Pipe/FIFO"<<endl;
break;
case S_IFLNK:
cout<<"Its a Symlink"<<endl;
break;
case S_IFSOCK:
cout<<"Its a Socket file"<<endl;
break;
default:
cout<<"File type unknown"<<endl;
break;
}
cout<<"Device
no: "<<buf.st_dev<<endl;
cout<<"File
Permission: ";
if(S_ISDIR(buf.st_mode))
{
cout<<"d";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IRUSR)
{
cout<<"r";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IWUSR)
{
cout<<"w";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IXUSR)
{
cout<<"x";
}
else
{
cout<<"-";
}
if(buf.st_mode & S_IRGRP)
{
cout<<"r";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IWGRP)
{
cout<<"w";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IXGRP)
{
cout<<"x";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IROTH)
{
cout<<"r";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IWOTH)
{
cout<<"w";
else
{
cout<<"-";
}
if(buf.st_mode &
S_IXOTH)
{
cout<<"x";
}
else
{
cout<<"-";
}
cout<<endl;
cout<<"Inode
no: "<<buf.st_ino<<endl;
cout<<"No. of
Hard Links: "<<buf.st_nlink<<endl;
cout<<"User
ID of owner: "<<buf.st_uid<<endl;
cout<<"Group
ID of owner: "<<buf.st_gid<<endl;
cout<<"Total
Size (in bytes): "<<buf.st_size<<"
Bytes"<<endl;
cout<<"Blocksize
for filesystem I/O: "<<buf.st_blksize<<endl;
cout<<"Number
of blocks allocated: "<<buf.st_blocks<<endl;
cout<<"Time
(Last Access): "<<ctime(&buf.st_atime)<<endl;
cout<<"Time
(Last Modification): "<< ctime(&buf.st_mtime)<<endl;
cout<<"Time
(Last Changed): "<<ctime(&buf.st_ctime)<<endl;
return 0;
}
OUTPUT:
B.cpp(with fstat)
#include<iostream>
#include<fstream>
#include<string>
#include<sys/types.h>//
required
#include<sys/stat.h>//
required
#include<fcntl.h>
#include<cstring>
using namespace std;
int main()
{
struct stat buf; // stat
structure object creation
//Getting file name from
user
char pathname[90];
cout<<"Enter
filename: ";
cin>> pathname;
int fd;
fd=open(pathname,O_RDONLY);
if(fd==-1)
{
cout<<"ERROR";
}
else
{
fstat(fd,&buf); //
fstat function which stores all req info in buffer;
}
// Printing Inode info
cout<<pathname<<endl;
cout<<"File
type: ";
switch(buf.st_mode &
S_IFMT) // checking file type // Bit Mask for file type bit fields
{
case S_IFBLK:
cout<<"Its a Block device"<<endl;
break;
case S_IFREG:
cout<<"Its a Regular File"<<endl;
break;
case S_IFCHR:
cout<<"Its a Character device"<<endl;
break;
case S_IFDIR:
cout<<"Its a Directory"<<endl;
break;
case S_IFIFO:
cout<<"Its a Pipe/FIFO"<<endl;
break;
case S_IFLNK:
cout<<"Its a Symlink"<<endl;
break;
case S_IFSOCK:
cout<<"Its a Socket file"<<endl;
break;
default:
cout<<"File type unknown"<<endl;
break;
}
cout<<"Device
no: "<<buf.st_dev<<endl;
cout<<"File
Permission: ";
if(S_ISDIR(buf.st_mode))
{
cout<<"d";
}
else
{
cout<<"-";
}
if(buf.st_mode & S_IRUSR)
{
cout<<"r";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IWUSR)
{
cout<<"w";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IXUSR)
{
cout<<"x";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IRGRP)
{
cout<<"r";
}
else
{
cout<<"-";
}
if(buf.st_mode & S_IWGRP)
{
cout<<"w";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IXGRP)
{
cout<<"x";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IROTH)
{
cout<<"r";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IWOTH)
{
cout<<"w";
}
else
{
cout<<"-";
}
if(buf.st_mode &
S_IXOTH)
{
cout<<"x";
}
else
{
cout<<"-";
}
cout<<endl;
cout<<"Inode
no: "<<buf.st_ino<<endl;
cout<<"No. of
Hard Links: "<<buf.st_nlink<<endl;
cout<<"User
ID of owner: "<<buf.st_uid<<endl;
cout<<"Group
ID of owner: "<<buf.st_gid<<endl;
cout<<"Total
Size (in bytes): "<<buf.st_size<<"
Bytes"<<endl;
cout<<"Blocksize
for filesystem I/O: "<<buf.st_blksize<<endl;
cout<<"Number
of blocks allocated: "<<buf.st_blocks<<endl;
cout<<"Time
(Last Access): "<<ctime(&buf.st_atime)<<endl;
cout<<"Time
(Last Modification): "<< ctime(&buf.st_mtime)<<endl;
cout<<"Time
(Last Changed): "<<ctime(&buf.st_ctime)<<endl;
return 0;
}
Assignment no:2a
Title:Write an IPC program using pipe. Process A
accepts a character string and Process B inverses the string. Pipe is used to
establish communication between A and B processes using C++(named pipe).
--------------------------------------------------------------------------------------------------------------------
#include
<iostream>
#include
<unistd.h>
#include <stdio.h>
#include
<sys/types.h>
#include
<string.h>
#include
<sys/stat.h>
#include <fcntl.h>
using namespace std;
int main()
{
int c_pid;
char
str[20],buf[20],rev[20];
mknod("mypipe.
txt",S_IFIFO|S_IRUSR|S_IWUSR,0);
c_pid = fork();
if(c_pid == -1)
{
cout<<"\nError
in Fork system call. . .\n";
return 1;
}
if(c_pid == 0)
{
cout<<"\nProcess
A-> ";
cout<<"Enter
the string: ";
cin>>str;
int mypipe =
open("mypipe.txt",O_WRONLY);
write(mypipe,str,strlen(str)+1);
close(mypipe);
return 0;
}
else
{
cout<<"\n\nProcess
B->";
int mypipe =
open("mypipe.txt",O_RDONLY);
read(mypipe,buf,sizeof(buf));
close(mypipe);
int l=strlen(buf);
int j=0;
for(int
i=l-1;i>=0;i--)
{
rev[j]=buf[i];
j++;}
cout<<"Reverse
of the string through Pipe: ";
for(int i=0;i<l;i++){
cout<<rev[i];
}
cout<<"\n\n";
return 0;
}
return 0;
}
OUTPUT:
student@pm17:~$ g++
npipe. cpp
student@pm17:~$ . /a.
out
Process A-> Enter the
string: sunil
Process B-> Reverse
of the string through Pipe:linus
student@pm17:~$
Assignment no :2b
Title :Write an IPC program using pipe. Process A
accepts a character string
and Process B inverses
the string. Pipe is used to establish communication between A and B
processes using
C++(unname pipe).
--------------------------------------------------------------------------------------------------------------------
include <iostream>
#include
<unistd.h>
#include <stdio.h>
#include
<sys/types.h>
#include<string.h>
using namespace std;
int main()
{
int pfd[2];
int c_pid;
char
str[20],buf[20],rev[20];
pipe(pfd);
c_pid = fork();
if(c_pid == -1)
{
cout<<"\nError
in Fork system call...\n";
return 1;
}
if(c_pid == 0)
{
cout<<"\nProcess
A->
";
cout<<"Enter
the string: ";
cin>>str;
close(pfd[0]);
write(pfd[1],str,strlen(str)+1);
close(pfd[1]);
return 0;
}
else
{
cout<<"\n\nProcess
B->
";
close(pfd[1]);
read(pfd[0],buf,sizeof(buf));
close(pfd[0]);
int l=strlen(buf);
int j=0;
for(int
i=l-1;i>=0;i--)
{
rev[j]=buf[i];
j++;
}
cout<<"Reverse
of the string through Pipe: ";for(int i=0;i<l;i++)
{
cout<<rev[i];
}
cout<<"\n\n";
return 0;
}
return 0;
}
OUTPUT:
student@pm17:~$ g++
pipe.cpp
student@pm17:~$ ./a.out
Process A-> Enter the
string: sunil
Process B-> Reverse
of the string through Pipe:linus
student@pm17:~$
Assignment Name :Use python for socket programming to
connect two or more pc to
share textfile.
________________________________________________________________________
SERVER.py
import sys
import socket
import os
host=''
port=6000
sc_server =
socket.socket()
sc_server.bind((host,port))
sc_server.listen(10)
print "Server in
listening mode"
bFileFound = 0
while True:
con,addr =
sc_server.accept()
print addr
filename =
con.recv(1024)
for file in
os.listdir("."):
if file == filename:
bFileFound = 1
break
if bFileFound == 0:
print filename+"
not present on Server."
else:
print filename+"
File Found"
ClientFile =
open(filename,"rb")
sRead =
ClientFile.read(1024)
while sRead:
con.send(sRead)
sRead =
ClientFile.read(1024)
print "File sent to
client."
break
con.close()
sc_server.close()
*************************** SERVER OUTPUT
****************************
administrator@pm10:~$ cd
TECOA145/
administrator@pm10:~/TECOA145$
python server.py
Server in listening mode
(‘192.168.20.100’,56508)
myfile.txt File Found
File send to Client
administrator@pm10:~/TECOA145$
*************************************************************************
CLIENT.py
import sys
import socket
sc_client =
socket.socket(socket.AF_INET, socket.SOCK_STREAM,0)
sc_client.connect(('192.168.20.100',6000))
filename =
raw_input("Enter the name of file to download : ")
while True:
sc_client.send(filename)
sData =
sc_client.recv(1024)
download =
open("myfile.txt","wb")
while sData:
download.write(sData)
print sData
sData =
sc_client.recv(1024)
break
sc_client.close()
************************** CLIENT OUTPUT *****************************
administrator@pm10:~$
cd TECOA145/
administrator@pm10:~/TECOA145$
python client.py
enter the name of file
to download : myfile.txt
Address: Pune
administrator@pm10:~/TECOA145$
***********************MYFILE.txt_OUTPUT:*****************************
Name:Sunil jadhav
Address: Pune
********************************************************************************
Title : Program in Python/C++ to test that computer is
booted with Legacy Boot ROM-BIOS or UEFI..
********************************************************************************
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"Your
computer is booted with..";
FILE *file;
if(file=fopen("/sys/firmware/efi","r"))
{
cout<<"UEFI\n";
return 1;
}
else
{
cout<<"Legacy
BIOS\n";
}
return 0;
}
*********************************
OUTPUT **************************************
administrator@pm10:~$ cd
TECOA145/
administrator@pm10:~/TECOA145$
g++ uefi.cpp
administrator@pm10:~/TECOA145$
./a.out
Your computer is booted
with..Legacy BIOS
administrator@pm10:~/TECOA145$
********************************************************************************
ASSIGNMENT NAME: Create
a iso boot image using open source tool
********************************************************************************
#include<iostream>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
using namespace std;
int main()
{
char name[20],cmd[200];
system("clear");
cout<<"\n\n\tProgram
to Create a iso boot image using open source tool\n";
cout<<"\nPlease
enter .iso filename (image) for boot image: ";
cin.getline(name,50);
cout<<"\n\n";
system("mkdir -p
isofiles/boot/grub");
system("cp
/usr/lib/grub/x86_64-pc/stage2_eltorito isofiles/boot/grub/");
sprintf(cmd,"genisoimage
-quiet -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4
-boot-info-table -o %s.iso isofiles",name);
system(cmd);
cout<<"ISO
file created successfully"<<endl;
}
-------------------------------------------------------------
OUTPUT:
student@pm18:~$ g++ iso
iso.cpp iso.cpp~
isofiles/
student@pm18:~$ g++
iso.cpp
student@pm18:~$ ./a.out
Program to Create a iso
boot image using open source tool
Please enter .iso
filename (image) for boot image: boot.iso
ISO file created
successfully
ASSIGNMENT NAME : Write a program in C++ to make USB Device
Bootable by installing required system files
------------------------------------------------------------------
#include<iostream>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
char path[50],cmd[100];
system("clear");
cout<<"\n\n\tProgram
for making USB device Bootable.\n";
cout<<"\nPlease
enter .iso file (image) path for making USB flash drive Bootable:
"<<"\n\n";
cin.getline(path,50);
sprintf(cmd,"sudo
dd if=%s of=/dev/sdb",path);
system(cmd);
return 0;
}
OUTPUT
pccoe@ubuntu:~$ cd
Desktop
pccoe@ubuntu:~/Desktop$
g++ usbBootable.cpp
pccoe@ubuntu:~/Desktop$
./a.out
--------------------------------------------
Program for making USB
device Bootable.
Please enter .iso file
(image) path for making USB flash drive Bootable:
/home/pccoe
[sudo] password for
pccoe:
dd: reading : Is a
directory
2009088+0 records in
2009088+0 records out
1028653056 bytes (1.0
GB) copied, 679.642 s, 1.5 MB/s
pccoe@ubuntu:~/Desktop$
ASSIGNMENT NAME : Write a program in C++ to create a
RAMDRIVE and associate an acyclic directory structure to it. Use this RAMDRIVE
to store input, out files to run a
calculator program.
---------------------------------------------------------------------------------------------------------------
7.1 : C++ code to create
RAM Disk
note : to unmout ramdisk
use command sudo umount /mnt/ramdisk
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
system("free
-m");
cout<<"\n\n";
system("sudo mkdir
/mnt/ramdisk");
system("sudo mount
-t tmpfs -o size=512m tmpfs /mnt/ramdisk");
cout<<"\n\nRAMdisk
Created.\n\n";
system("cp
input.txt /mnt/ramdisk");
system("cp cal.cpp
/mnt/ramdisk");
cout<<"\nFiles
copied into RAMdisk. Now execute ur program inside RAMdisk.\n\n";
return 0;
}
7.2: CALCULATOR PROGRAM:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a,b;
FILE *fp;
fp=fopen("/mnt/ramdisk/input.txt","r");
fscanf(fp,"%d",&a);
fscanf(fp,"%d",&b);
fclose(fp);
fp=fopen("/mnt/ramdisk/output.txt","w");
fprintf(fp,"\nAddition:
%d",(a+b));
fprintf(fp,"\nSub:
%d",(a-b));
fprintf(fp,"\nMul:
%d",(a*b));
fprintf(fp,"\nDiv:
%d",(a/b));
return 0;
}
OUTPUT
administrator@ubuntu:~$
g++ RAM.cpp
administrator@ubuntu:~$
./a.out
total used free shared
buffers cached
Mem: 1874 1811 62 0 567
834
-/+ buffers/cache: 410
1463
Swap: 255 0 255
[sudo] password for
administrator:
RAMdisk Created.
Files copied into
RAMdisk. Now execute ur program inside RAMdisk.
administrator@ubuntu:~$
cd /mnt/ramdisk
administrator@ubuntu:/mnt/ramdisk$
ls
cal.cpp input.txt
administrator@ubuntu:/mnt/ramdisk$
g++ cal.cpp
administrator@ubuntu:/mnt/ramdisk$
./a.out
administrator@ubuntu:/mnt/ramdisk$
ls
a.out cal.cpp input.txt
output.txt
administrator@ubuntu:/mnt/ramdisk$
cat input.txt
70
60
administrator@ubuntu:/mnt/ramdisk$
ls
a.out cal.cpp input.txt
output.txt
output.txt:
Addition: 130
Sub: 10
Mul: 4200
Div: 1
ASSIGNMENT NO. :8
----------------------------------------------------------
MainActivity.java
package com.example.test;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1 =
(TextView)findViewById(R.id.TextView1);
t1.setText("OS Name\t: " + System.getProperty("os.name"));
TextView t2 =
(TextView)findViewById(R.id.TextView2);
t2.setText("OS Release\t: "+ Build.VERSION.RELEASE + ", SDK
\t:"+Build.VERSION.SDK_INT);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu;
this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ASSIGNMENT NO.9: Write a python program for creating
virtual file system(VFS) on Linux environment.
-------------------------------------------------------------
PROGRAM
import shelve
fs =
shelve.open('filesystem.fs', writeback=True)
current_dir = []
def install(fs):
# create root and others
username =
raw_input('What do you want your username to be? ')
fs[""] =
{"System": {}, "Users": {username: {}}}
def
current_dictionary():
"""Return
a dictionary representing the files in the current directory"""
d = fs[""]
for key in current_dir:
d = d[key]
return d
def ls(args):
print 'Contents of
directory', "/" + "/".join(current_dir) + ':'
for i in
current_dictionary():
print i
def cd(args):
if len(args) != 1:
print "Usage: cd
<directory>"
return
if args[0] ==
"..":
if len(current_dir) ==
0:
print "Cannot go
above root"
else:
current_dir.pop()
elif args[0] not in
current_dictionary():
print "Directory
" + args[0] + " not found"
else:
current_dir.append(args[0])
def mkdir(args):
if len(args) != 1:
print "Usage: mkdir
<directory>"
return
# create an empty
directory there and sync back to shelve dictionary!
d =
current_dictionary()[args[0]] = {}
fs.sync()
COMMANDS = {'ls' : ls,
'cd': cd, 'mkdir': mkdir}
install(fs)
while True:
raw =
raw_input('>> ')
cmd = raw.split()[0]
if cmd in COMMANDS:
COMMANDS[cmd](raw.split()[1:])
#Use break instead of
exit, so you will get to this point.
raw_input('Press the
Enter key to shutdown...')
OUTPUT
student@cc-ThinkCentre-M72e:~$
python VFS.py
What do you want your
username to be? aa
>> ls
Contents of directory /:
System
Users
>> cd Users
>> ls
Contents of directory
/Users:
aa
>> cd ..
>> ls
Contents of directory /:
System
Users
>> cd ..
Cannot go above root
>> cd System
>> ls
Contents of directory
/System:
>> mkdir bb
>> ls
Contents of directory
/System:
bb
>> cd bb
>> ls
Contents of directory
/System/bb:
>> ^Z
[1]+ Stopped python
VFS.py
student@cc-ThinkCentre-M72e:~$
-------------------------------------------------------------------------------------