Tuesday, May 22, 2018

Program (using fork() and/or exec() commands)

/* Program (using fork() and/or exec() commands) where parent and child execute:
c. different programs
*/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int a;
a=fork();
if(a<0)
{
 printf("Child process could not be created");
 exit(-1);
}
else if(a==0)
{
 execl("/bin/ls","ls\n",NULL);
}
else
{
 printf("\nParent process=%d\n",getppid());
}
return 0;
}




OUTPUT SCREEN:


Program (using fork() and/or exec() commands)

/* Program (using fork() and/or exec() commands) where parent and child execute:
b. same program, different code
*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
a=fork();
if(a<0)
{
 printf("Child process could not be created");
 exit(-1);
}
else if(a==0)
{
 printf("in child process=%d",getpid());
}
else
{
 printf("in parent process=%d\n",getppid());
}
return 0;
}




OUTPUT SCREEN:


Program (using fork() and/or exec() commands)

/* Program (using fork() and/or exec() commands) where parent and child execute:
a. same program, same code
*/

#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
a=fork();
if(a<0)
{
 printf("Child process could not be created");
 exit(-1);
}
else
{
 printf("My process id=%d, My parent process id=%d\n",getpid(),getppid());
}
return 0;
}





OUTPUT SCREEN:


Program to copy a Source File into the Target File

/* Program to copy a source file into the target file and display the target file using system calls.
*/

#include<stdio.h>
#include<stdlib.h>
main(int argc, char *argv[3])
{
int fold,fnew;
if(argc!=3)
{
  printf("Read two arguments");
  exit(1);
}
fold=open(argv[1],0);
if(fold==1)
{
  printf("Unable to open file %s\n",argv[1]);
  exit(1);
}
fnew=creat(argv[2],0666);
if(fnew==-1)
{
  printf("Unable to create file %s\n",argv[2]);
  exit(1);
}
copy(fold,fnew);
exit(0);
}
copy(int old,int new)
{
int count;
char buffer[512];
while(read(old,&buffer,1)>0)
{
  write(new,&buffer,1);
}
}




OUTPUT SCREEN:


Program to print details including Owner Access Permissions and File Access Time

/* Program to print details including owner access permissions, file access time, where file name is given as a command line argument.
*/

#include<stdio.h>

#include<sys/stat.h>


int main(int argc, char *argv[3])

{

int i,statchmod;
struct stat buffer;

printf("Give file name:\n");

for(i=1;i<argc;i++)

{
 
printf("file=%s:-\n",argv[1]);
   
if(stat(argv[i], &buffer)<0)
   
printf("Error in file stated");
else
   
statchmod=buffer.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
   
printf("Access permission(in octal): %o\n",statchmod);
   
        printf("Owenered=%d,gid=%d\n",buffer.st_uid,buffer.st_gid);
   
printf("Access time=%d\n", (time(&(buffer.st_atime))));

}
}




OUTPUT SCREEN:


Program to display Kernel Version, CPU Type & Model

/* WAP to display the following:
a. Kernel Version
b. CPU type and model
c. Information in configured memory, amount of free and used memory
*/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int a;
printf("\nKernel Version:\n");
system("cat /proc/sys/kernel/osrelease");
printf("\nCPU TYPE AND MODEL:\n");
system("cat /proc/cpuinfo | awk 'NR==4,NR==5 {print}'");
        printf("\nInformation on Configured amount of free and Used Memory:\n");
system("cat /proc/meminfo | awk 'NR==1,NR==2 {print}'");
return 0;
}





OUTPUT SCREEN:



Sunday, May 20, 2018

Hash Function Encryption - Decryption Program

/* Program to implement Hash Function encryption and decryption technique.
*/

#include<string.h>
#include<iostream>
using namespace std;

int main()
{
  string m,k;
  cout<<"\nEnter the Message to Encrypt: ";
  getline(cin,m);
  cout<<"Enter Key: ";
  getline(cin,k);
  char enc[m.size()];
  for(int i=0;i<m.size();i++)
  {
    enc[i]=m[i]^k[i%k.size()];
    cout<<m[i]<<" XOR "<<k[i%k.size()]<<" ";
    cout<<enc[i]<<endl;
  }
  cout<<"\nEncrypted message: ";
  for(int i=0;i<m.size();i++)
  {
  cout<<enc[i]<<" ";
  }
  cout<<"\nDecrypted message: ";
  string dec;
  for(int i=0;i<m.size();i++)
  {
    dec[i]=enc[i]^k[i%k.size()];
    cout<<dec[i];
  }
cout<<endl<<endl;
  return 0;
}




OUTPUT SCREEN:



Project on Library Management System (C++)

=> The project titled Library Management system is Library management software for monitoring and controlling the transactions in a lib...