Showing posts with label OS. Show all posts
Showing posts with label OS. Show all posts

Saturday, May 24, 2014

For all people with power problems using Ubuntu 14.04 try downgrading your Linux kernel to Long term supported version
either 3.10 or 3.12
PS: it worked for me and will work for most of the people as 3.12 kernel is stable

 firstly download the 3 deb files which suit ur system architecture
if for 32 bit =>i386
for 64 bit =>amd64
go for files which have generic written
http://kernel.ubuntu.com/~kernel-ppa/mainline/v3.12.20-trusty/


Go to the downloaded folder

then run this

sudo dpkg -i linux-headers-3.12.20* linux-image-3.12.20* .deb
And then remove the 3.13 default kernel

sudo apt-get remove linux-headers-3.13.0* linux-image-3.13.*
And ur through

If possible change the URI for Kernel updates in Software Center.

Install power monitoring tools like Powertop.

sudo apt-get install powertop

In powertop go to turnables tab and toggle every row from bad to good

also install pm-utils

sudo apt-get install pm-utils

sudo pm-powersave true

this puts ur pc on power save mode

Wednesday, October 30, 2013

To implement SEMAPHORE in C

 /*
To implement SEMAPHORE in C

*/
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdio.h>

int main()
{
        int semid,pid,val,i,withdraw;
        struct sembuf sop;
        FILE *fp;
        int balance;
        semid=semget(0x20,1,IPC_CREAT|0666);
        semctl(semid,0,SETVAL,1);
        sop.sem_num=0;
        sop.sem_op=-1;
        sop.sem_flg=SEM_UNDO;
        pid=fork();
        if(pid==0)
        {
                printf("Child Before Sem Operation\n");
                semop(semid,&sop,1);
                printf("Child is in Critical Section\n");
                fp = fopen("data.txt","r+");
                fscanf(fp,"%d",&balance);
                printf("Enter the Amount to be Withdrawn: ");
                scanf(" %d",&withdraw);
                rewind(fp);
                balance = balance - withdraw;
                fprintf(fp,"%d",balance);
                fclose(fp);
                sleep(2);
                printf("\nBalance = %d\n",balance);
                printf("Child Outside Critical Section\n");
        }
        else
        {
                printf("Parent Before Sem Operation\n");
                semop(semid,&sop,1);
                printf("Parent is in Critical Section\n");
                sleep(2);
                fp = fopen("data.txt","r+");
                fscanf(fp,"%d",&balance);
                printf("\nBalance = %d\n",balance);
                printf("Enter the Amount to be Withdrawn: ");
                scanf(" %d",&withdraw);
                rewind(fp);
                balance = balance - withdraw;
                fprintf(fp,"%d",balance);
                fclose(fp);
                sleep(2);
                printf("\nBalance = %d\n",balance);
                printf("Parent Outside Critical Section\n");
        }
        return 0;
}

/* Output:
Child Before Sem Operation
Child is in Critical Section
Parent Before Sem Operation
Enter the Amount to be Withdrawn: 10

Balance = 850
Child Outside Critical Section
Parent is in Critical Section

Balance = 850
Enter the Amount to be Withdrawn: 50

Balance = 800
Parent Outside Critical Section
*/

To Implement Inter Process Communication using Pipes

/*
To Implement Inter Process Communication using Pipes
*/

#include<stdio.h>
#include<unistd.h>
#include<string.h>

int main()
{
        int fd[2],n,status;
        char buff[100];
        pipe(fd);
        if(pipe (fd)<0)
        {
                printf("Cannot create a pipe.....");
                exit(0);
        }

        else
        {
                switch(fork())
                {
                        case -1:
                                printf("\nFork error...");
                                exit(0);
 
                        case 0:
                                //child process read data from buffer

                                printf("\nRunning child process...");
                                printf("\nBefore read is performed...");
                                read(fd[0],buff,100);
                                printf("\nThe data read from the buffer is:\t %s",buff);
                                break;

                        default:
                                //parent process writes to buffer

                                printf("\nRunning parent process...\n");
                                printf("\nWriting...\n");
                                write(fd[1],"Welcome to HOME....",60);
                                wait(&status);
                                printf("\nWriting is over...\n");
                                break;
                }
        }
    return(0);
}



/*
Output:
Running child process...

Running parent process...

Writing...
Before read is performed...
The data read from the buffer is:        Welcome to HOME....
Writing is over...
*/

IMPLEMENTATATION OF INODE USING C.

/*
TITLE:IMPLEMENTATATION OF INODE USING C.

*/

#include<sys/types.h>
#include<ustat.h>
#include<stdio.h>
#include<sys/dir.h>

main(argc,argv)
int argc;
char *argv[];
{
  FILE *fp;
  struct direct dir;
  fp=fopen(argv[1],"r");
  printf("\n Directory listing of %s\n",argv[1]);
  while(fread(&dir,sizeof(dir),1,fp)!=EOF)
  {
     if(dir.d_ino==0)
         continue;
          printf("inode number:%d of file:%s\n",dir.d_ino,dir.d_name);
  }
   fclose(fp);
   exit(0);
}

To Study the File-Handling

/*
To Study the File-Handling
*/


#include<stdio.h>
#include<fcntl.h>
#include<error.h>
#include<string.h>
#include<sys/wait.h>

int fd;
char name1[100];
void createfile(char *name)
{
printf("\n Enter name of file \n");
scanf("%s",name);
fd=open(name,O_CREAT|O_EXCL,0760);
if(fd==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}

else
{
printf("\nfile created");
close(fd);
}
}


void copy()
{
char ch;
int n;
char name1[100], name2[100];
int fd1, fd2;
printf("\n Enter name of file to copy");
scanf("%s",name1);
fd1=open(name1,O_RDWR|O_APPEND,0760);
printf("\nEnter name of file to be copied into");
scanf("%s",name2);
fd2=open(name2,O_CREAT|O_EXCL|O_RDWR|O_APPEND,0761);
if(fd1==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}
if(fd2==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}


while(read(fd1,&ch,1)>0);
{
write(fd2,&ch,1);
}


printf("\n copied");
close(fd1);
close(fd2);
}
void writeandread()
{
char ch;
int n;
char name2[100], buff[100], buff1[100];
int fd1;
printf("\n Enter name of file to write");
scanf("%s",name1);
fd1=open(name1,O_RDWR|O_APPEND,0760);
/*printf("\nEnter name of file");
scanf("%s",name2);
fd2=open(name2,O_CREAT|O_EXCL|O_RDWR|O_APPEND,0761);
*/
if(fd1==-1)
{
perror("\n ERROR!!!\n");
exit(1);
}

else
{
printf("\nEnter to write");
scanf("%s",buff);
write(fd1,buff,strlen(buff));
printf("\nwrite succesfull");
lseek(fd1,0,SEEK_SET);
read(fd1,buff1,strlen(buff));
printf("\nfile contains: %s\n",buff1);
}
printf("\nexiting");
close(fd1);

}

int main()

{
char name[100];
createfile(name);
        writeandread();
copy();
return 0;
}

/*
OUTPUT:
bash-3.00$ cc file.c
bash-3.00$ ./a.out

Enter name of file a1

file created
 Enter name of file to write a1

Enter to write hi

write succesfull
file contains: hij

exiting
 Enter name of file to copy a1

Enter name of file to be copied into a2

 copiedbash-3.00$ ./a.out
*/

IMPLEMENTATATION OF PARENT CHILD PROCESS USING C.

/*

TITLE: IMPLEMENTATATION OF PARENT CHILD PROCESS USING C.
*/




#include<stdio.h>
#include<sys/wait.h>

int main()
{
      int pid,status,n1,n2,sum;
      printf("\n parent(user) program is in execution");
      pid=fork();
      printf("\n spanning the child process");
      if(pid==0)
      {
           printf("\n Child is executing :\n");
           printf("\n Enter two no.:\n");
           scanf("%d  %d",&n1,&n2);
           sum=n1+n2;
           printf("the sum is: %d\n",sum);
           printf(" pid of child is: %d\n",getpid());
           printf("pid of parent is: %d\n",getppid());
           printf("\n exiting child \n");

      }
    else
     {
       wait(&status);
              printf("\n parent is executing: \n");
           printf("\n pid of parent is :%d\n",getpid());
           printf("\n pid of parent's parent is: %d\n ",getppid());

         
           printf("\n exiting parent \n");
     }
   return 0;
}
/*
 parent(user) program is in execution
 spanning the child process
 Child is executing :

 Enter two no.:
 parent(user) program is in execution
20
14
the sum is: 34
 pid of child is: 4874
pid of parent is: 4873

 exiting child
 spanning the child process
 parent is executing:

 pid of parent is :4873

 pid of parent's parent is: 4857

 exiting parent
*/

TO IMPLEMENT SHORTEST JOB FIRST IN JAVA

/*
TOPIC : TO IMPLEMENT SHORTEST JOB FIRST IN JAVA
*/

import java.util.*;
public class SJF
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.println("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.println("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :");
p[i].atime=s.nextInt();
System.out.println("Burst TIME :");
p[i].btime=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].btime>p[j+1].btime)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime);
}
}
}
class Process {
  int id,btime,atime;
}
/* OUTPUT
Enter no of processes :
2
PROCESS ID :
99
ARRIVAL TIME :
20
Burst TIME :
12
PROCESS ID :
10
ARRIVAL TIME :
21
Burst TIME :
32
Process id      Arrival time    Burst time
...............................
99              20              12
10              21              32
*/

TO IMPLEMENT ROUND ROBIN TIME SLICING IN JAVA

/*
TO IMPLEMENT ROUND ROBIN TIME SLICING IN JAVA
*/

import java.util.*;
public class RR
{
public static void main(String arg[])throws Exception
{
Scanner s=new Scanner(System.in);
int n,quan,tat;
System.out.println("Enter no of processes:");
n=s.nextInt();
System.out.println("enter time quantum:");
quan=s.nextInt();
Process p[]=new Process[n];
for(int i=0;i<n;i++)
{
p[i]=new Process();
}
for(int i=0;i<n;i++)
{
System.out.println("PROCESS ID:");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME:");
p[i].atime=s.nextInt();
System.out.println("Burst time:");
p[i].btime=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1;j++)
{
if(p[j].atime>p[j+1].atime)
{
Process t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...................................");
tat=0;
boolean flag=true;
int pass=0;
while(pass!=2)
{
for(int i=0;i<n;i++)
{
if(flag==true)
{
p[i].rbt=p[i].btime;
p[i].rbt=p[i].rbt-quan;
}
else
{
p[i].rbt=p[i].rbt-quan;
}
if(p[i].rbt>-4)
{
if(p[i].rbt<0)
p[i].rbt=0;
System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime+"\t\t"+p[i].rbt);
}
}
flag=false;
pass++;
}
}
}
class Process
{
int id,btime,atime,rbt;
Process()
{
id=0;
atime=0;
btime=0;
rbt=0;
}
}

/* OUTPUT
Enter no of processes:
2
enter time quantum:
12
PROCESS ID:
32
ARRIVAL TIME:
21
Burst time:
125
PROCESS ID:
45
ARRIVAL TIME:
12
Burst time:
12
Process id      Arrival time    Burst time
...................................
45              12              12              0
32              21              125             113
32              21              125             101
*/

TO IMPLEMENT PROCESS PRIORITY IN JAVA

/*

Title   :TO IMPLEMENT PROCESS PRIORITY IN JAVA
*/

import java.util.*;
public class Priority
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.println("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.println("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :");
p[i].atime=s.nextInt();
System.out.println("Burst TIME :");
p[i].btime=s.nextInt();
System.out.println("Priority :");
p[i].prior=s.nextInt();
}
for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].prior>p[j+1].prior)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time\tPriority");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime+"\t\tp"+p[i].prior);
}
}
}
class Process {
  int id,btime,atime,prior;
}

/*OUTPUT
Enter no of processes :
2
PROCESS ID :
12
ARRIVAL TIME :
14
Burst TIME :
45
Priority :
1
PROCESS ID :
21
ARRIVAL TIME :
23
Burst TIME :
45
Priority :
2
Process id      Arrival time    Burst time      Priority
...............................
12              14              45              p1
21              23              45              p2
*/

TO IMPLEMENT MULTITHREADING AND OBSERVE TIMESLICING

/*

TO IMPLEMENT MULTITHREADING AND OBSERVE TIMESLICING
*/
import java.util.*;
class q extends Thread
{
public void run()
{
try{
for(int i=0;i<12;i++)
{
System.out.println("12"+"x"+i+"="+(i*12));
Thread.sleep(2000);
}
}catch(Exception e){}
}
}
class p extends Thread
{
public void run()
{
try{
for(int i=0;i<12;i++)
{
System.out.println("10"+"x"+i+"="+(i*10));
Thread.sleep(2000);
}
}catch(Exception e){}
}
}
class multi
{
public static void main(String args[]) throws Exception
{
p a=new p();
q b=new q();
a.start();
b.start();
//THIS PROGRAM IS CODED BY TG;
}
}
/*
OUTPUT
 javac multi.java
bash-4.1$ java multi
10x0=0
12x0=0
10x1=10
12x1=12
10x2=20
12x2=24
10x3=30
12x3=36
10x4=40
12x4=48
10x5=50
12x5=60
10x6=60
12x6=72
10x7=70
12x7=84
10x8=80
12x8=96
10x9=90
12x9=108
10x10=100
12x10=120
10x11=110
12x11=132
*/

TO IMPLEMENT LRU IN JAVA

/*
TO IMPLEMENT LRU IN JAVA

*/
import java.util.*;

class Queue
{
int q[]=new int[3];
int f=0;
int count=0;
 void insert(int d)
{

if(f!=3)
{

if(find(d) && count==2)
{System.out.print("\nHIT");f++;}
else
q[f++]=d;
}

else
{
f=0;

       if(find(d) && count==2)
{System.out.print("\nHIT");f++;}
else
q[f++]=d;
}
System.out.print("\nTop:"+f);
}

 boolean find(int d)
  {
if(count==3) count=2;
for(int i=0;i<=count;i++)
{
if(d==q[i] && count>=2)
return true;


}


return  false;
}
 void traverse()
{
        if(count==3) count=2;
for(int i=0;i<=count;i++)
{

System.out.print("\n"+q[i]);
}
if(count<3) count++;
System.out.println();
}

 }


class LRU
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);

                int a[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1};
Queue q=new Queue();
for(int i=0;i<14;i++)
{
q.insert(a[i]);
q.traverse();
}
}
}


TO IMPLEMENT FIFO-First In First Out

/*
TO IMPLEMENT FIFO-First In First Out
*/
import java.util.*;

class Queue
{
int q[]=new int[3];
int f=0;
int count=0;
 void insert(int d)
{
if(f!=3)
{

if(q[f]==d && count==2)
{System.out.print("\nHIT");}
else
q[f++]=d;
}

else
{
f=0;

       if(q[f]==d && count==2)
{System.out.print("\nHIT");}
else
q[f++]=d;
}
}


 void traverse()
{
for(int i=0;i<=count;i++)
{

System.out.print("\n"+q[i]);
}
if(count<2) count++;
System.out.println();
}

 }


class FIFO
{
public static void main(String args[])
{
Scanner src=new Scanner(System.in);

                int a[]={7,0,1,2,0,3,0,4,2,3,0,3,2,1};
Queue q=new Queue();
for(int i=0;i<14;i++)
{
q.insert(a[i]);
q.traverse();
}
}
}

/*OUTPUT

bash-3.00$ java FIFO

7

7
0

7
0
1

2
0
1

HIT
2
0
1

2
3
1

2
3
0

4
3
0

4
2
0

4
2
3

0
2
3

0
3
3

0
3
2

1
3
2

*/

TO IMPLEMENT First Come First Service

/*
TO IMPLEMENT First Come First Service

*/
import java.util.*;
public class FCFS
{
  public static void main(String args[])throws Exception{
Scanner s=new Scanner(System.in);
int n=0;
System.out.print("Enter no of processes :");
n=s.nextInt();
Process[] p=new Process[n];
for(int i=0;i<n;i++)
{
   p[i]=new Process();
}
for(int i=0;i<n;i++)
{
  System.out.print("PROCESS ID :");
p[i].id=s.nextInt();
System.out.println("ARRIVAL TIME :"+i);
p[i].atime=i;
System.out.print("Burst TIME :");
p[i].btime=s.nextInt();
}
/*for(int i=0;i<n-1;i++)
{
   for(int j=0;j<n-1;j++)
{
   if(p[j].atime>p[j+1].atime)
{
  Process t=p[j];
   p[j]=p[j+1];
   p[j+1]=t;
}
}
}
System.out.println("Process id\tArrival time\tBurst time");
System.out.println("...............................");
for(int i=0;i<n;i++)
{
   System.out.println(""+p[i].id+"\t\t"+p[i].atime+"\t\t"+p[i].btime);
}
*/
System.out.print("\nStarting to execute process");
for(int i=0;i<n;i++)
{
System.out.print("\nProcess "+p[i].id+" under execution");
Thread.sleep((p[i].btime)*1000);
System.out.println("\nProcess "+p[i].id+" executed");
}

}
}
class Process {
  int id,btime,atime;
}
/*OUTPUT  
bash-3.00$ java FCFS
bash-3.00$ java FCFS
Enter no of processes :3
PROCESS ID :1
ARRIVAL TIME :0
Burst TIME :7
PROCESS ID :2
ARRIVAL TIME :1
Burst TIME :1
PROCESS ID :3
ARRIVAL TIME :2
Burst TIME :3

Starting to execute process
Process 1 under execution
Process 1 executed

Process 2 under execution
Process 2 executed

Process 3 under execution
Process 3 executed

*/