Friday 11 September 2015

OS & linux lab updated Syllabus

III Year - I SEMESTER
                                           Image description

                       Operating Systems & Linux Programming   Laboratory
  
                                                             T   P   C
                                                             0   3    2
  Operating Systems



                                              
1.  Simulate the following CPU scheduling algorithms
a) Round Robin b) SJF c) FCFS d) Priority
2.  Multiprogramming-Memory management- Implementation of fork (), wait (), exec() and exit (), System calls
3.  Simulate the following
            a)Multiprogramming with a fixed number of tasks (MFT)
            b)Multiprogramming with a variable number of tasks (MVT)
4. Simulate Bankers Algorithm for Dead Lock Avoidance 
5.Simulate Bankers Algorithm for Dead Lock Prevention.
6. Simulate the following page replacement algorithms.
a) FIFO b) LRU c) LFU
7. Simulate the following File allocation strategies
a) Sequenced
 b) Indexed 
c) Linked









Linux Programming

1.
a) Study of Unix/Linux general purpose utility command list
man,who,cat, cd, cp, ps, ls, mv, rm, mkdir, rmdir, echo, more, date, time, kill, history, chmod, chown, finger, pwd, cal, logout, shutdown.

b) Study of vi editor.
c) Study of Bash shell, Bourne shell and C shell in Unix/Linux operating system.
d) Study of Unix/Linux file system (tree structure).
e) Study of .bashrc, /etc/bashrc and Environment variables.


2. Write a C program that makes a copy of a file using standard I/O, and system calls

3. Write a C program to emulate the UNIX ls -l command.

4. Write a C program that illustrates how to execute two commands concurrently
with a command pipe.
Ex: - ls -l | sort

5. Write a C program that illustrates two processes communicating using shared
memory

6. Write a C program to simulate producer and consumer problem usingsemaphores

7. Write C program to create a thread using pthreads library and let it run its function.


8.          Write a C program to illustrate concurrent execution of threads using pthreads library. 






Wednesday 5 August 2015

linux 8 program theory


The pstree Command



pstree is a small, command line (i.e., all-text mode) program that displays the processes (i.e., executing instances of programs) on the system in the form of a tree diagram. It differs from the much more commonly used (and more complex) ps program in a number of respects, including that the latter shows the processes in a list rather than a tree diagram but provides more detailed information about them.


Syntax
The basic syntax for pstree is:
pstree [options] [pid or username]
The square brackets indicate that the items in them are optional. If pstree is used without any options or arguments, that is, by typing
pstree
and then pressing the ENTER key, the result is a tree diagram that shows all of the processes currently on the system.

The processes that are directly connected to the main stem  of the tree are listed by default in alphabetic order. This is in contrast to ps, which by default lists the processes in the sequence in which they were created.

linux 10 program theory

PipesPipes are used to allow one or more processes to have information “flow” between them. The most common example of this is with the shell.

$ ls | wc –l
As we’ve seen the std-out from the left side (ls) is connected to the std-in on the right side (wc -l).As far the each program is concerned, it is reading or writing as it normally does. Both processes are running concurrently.

There are 2 types of pipes:
unnamed pipes
named pipes

The examples we seen at the shell command line are unnamed. They are created, used and destroyed within the life a set of processes. Each end of the pipe has it’s own file descriptor. One end is for reading and one end is for writing. When you are done with a pipe, it is closed like any other file.

Creating unnamed pipes

#include <unistd.h>

int pipe(int fd[2]);

Returns 2 file descriptors in the fd array.

fd[0] is for read

fd[1] write

Returns 0 on successful creation of pipe, 1 otherwise.

Each end of the pipe is closed individually using normal close() system call. Pipes are only available the process that creates the pipe and it’s descendants.

Reading from pipes 

When reading from a pipe:
read() will return 0 (end of file) when the write end of the pipe is closed. if write end of the is still open and there is no data, read() will sleep until input become available. if a read() tries to get more data than is currently in pipe, read() will only contain the number of bytes actually read. Subsequent reads will sleep until more data is available.

Writing to pipes
When writing to a pipe:
If read end of pipe is closes, a write() will fail and process will be sent SIGPIPE signal. Default SIGPIPE handler terminates. 

Friday 31 July 2015

C D PROGRAM RECURSIVE DESCENT PARSING



#include<stdio.h>
 #include<conio.h>
 char str[20];
 int flag=1;
 int i=0;
 void main()
 {
       int S();
       clrscr();
       printf("enter input string: ");
       gets(str);
       S();
       if(flag==1&&str[i]=='\0')
             printf("\ngiven string ' %s ' is completely parsed",str);
       else
             printf("sorry! not parsed");
       getch();
 }
 int S()
 {
       int A();
       if(str[i]=='a')
       {
             i++;
             A();
             if(str[i]=='d')
             {
                   i++;
                   return 0;
             }
             else
            {
                   flag=0;
                   return 0;
             }
       }
 }
 int A()
 {
       if(str[i]=='a')
       {
             i++;
             if(str[i]=='b')
             {
                   i++;
                   return 0;
             }
             else
             {
                   A();
                   return 0;
            }
    }
   else
   {
       // flag=1;
        return 0  }
}

Sunday 26 July 2015

ABOUT PROGRAM 3 theory IN RECORD:


fork():-
to create a new process from a program we use fork().the prototype of fork() is 
    pid_t fork();

.
*)function fork() causes the unix system to create a new process known as child process with                new process id.

*)the contents of child process are identical to contents of parent process.

*)entire address space of parent process is duplicated for child process.

*)all the statements after fork() are executed in both parent and child.

*)fork() returns '0' in child process and nn zero in parent process.
exec():-
*)the unique system call that transforms an executable binary file into a process or the exec family of systems
 i.e;   from a program by calling either of these functions we can load an executable file of a program into address space of current process.

wait():
The system call wait() is easy. This function blocks the calling process until one of its child processes exits or a signal is received. 
For our purpose, we shall ignore signals. wait() takes the address of an integer variable and returns the process ID of the completed process. 
Some flags that indicate the completion status of the child process are passed back with the integer pointer. One of the main purposes of wait() is to wait for completion of child processes.
The execution of wait() could have two possible situations.
  1. If there are at least one child processes running when the call to wait() is made, the caller will be blocked until one of its child processes exits. At that moment, the caller resumes its execution.
  2. If there is no child process running when the call to wait() is made, then this wait() has no effect at all. That is, it is as if no wait() is there.

exit():

The function _exit() terminates the calling process "immediately".

 Any open file descriptors belonging to the process are closed; any children of the process are inherited by process 1, init, and the process's parent is sent a SIGCHLD signal.

The value status is returned to the parent process as the process's exit status, and can be collected using one of the wait(2) family of calls.

The function _Exit() is equivalent to _exit().





Saturday 25 July 2015

OS PROGRAM -2 WITH THEORY

THEORY:
     System calls                      Function
      open              open an existing file or create a new file
      read              Read data from a file
      write             Write data to a file
      lseek             Move the read/write pointer to the
 specified location
      close             Close an open file

Open() system call:
used to open an existing file or to create a new file if it does not   
 exist already.
 Syntax:
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

Read()  System call: 
read from a file descriptor
Syntax:
ssize_t read(int fd, void *buf, size_t nbytes);

Write()  System call:
 write to a file descriptor
syntax:
ssize_t write(int fd, const void *buf, size_t nbytes);

Close() System call: 
 closes a file
Syntax:
int close(int fd);

lseek() System call
allows random access to a file by reposition the offset for next read or write
Syntax:
off_t lseek(int fd, off_t offset, int reference);

PRE_WORK:

CREATE A FILE(eg:file)
(USING cat COMMAND)

PROGRAM:
*/
#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
main()
{
      int fd,wcount,i;
      char buf[]="linux lab";
      fd=open("file",O_RDWR|O_CREAT,0755);
     if(fd==-1)
      {
            printf("cannot open file");
            exit(1);
      }    
      wcount=write(fd,buf,strlen(buf));
      if(wcount!=strlen(buf))
      {
            printf("write failed not enough space");
            exit(2);
      }

            lseek(fd,0,SEEK_SET);
            read(fd,0,wcount);
            printf("file contents %s\n",buf);
            close(fd);
     
}

Wednesday 22 July 2015

Compiler Design program 1

Program:
/*to divide the given program into lexemes*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
struct lex
{
  char le[50],tok[20];
}l[50];
int t=0;
 void main()
{
  char data[200],ch;
  int i,j=0,k=0,n;
  clrscr();
  printf("Enter the input:\n");
  for(i=0;(ch=getchar())!=-1;i++)
  data[i]=ch;
  data[i]='\0';
  for(i=0;data[i]!='\0';i++)
  {
   if(spe(data[i]))
   {
    if(data[i-1]!='\n')
    {
     l[j++].le[k]='\0';
     k=0;
    }
    l[j].le[k++]=data[i];
    if(rel(data[i])&&rel(data[i+1]))
    {
     l[j].le[k++]=data[i+1];
     i=i+1;
    }
   }
   else
   {
    if(data[i]==' '||data[i]=='\n')
    {
     l[j++].le[k]='\0';
     k=0;
    }
    else
    {
     if(spe(data[i-1]))
     {
      l[j++].le[k]='\0';
      k=0;
     }
     l[j].le[k++]=data[i];
    }
   }
  }
  n=j;
  t=0;
  for(i=0;i<=n;i++)
  {
   token(l[i].le);
   t++;
  }
  printf("Lexeme\t\tToken\n");
  printf("---------------------\n");
  for(i=0;i<=n;i++)
  {
   printf("%4s\t\t%4s\n",l[i].le,l[i].tok);
  }
  getch();
}


int spe(char c)
{
  int i,c1=0,t,n;
  char de[15]={'(',')','{','}','[',']',';',',','/','*','+','-','=','<','>'};
  for(i=0;i<15;i++)
  {
   if(c==de[i])
   {
    c1=1;
    break;
   }
  }
  return c1;
}

int rel(char c)
 {
  int i,c1=0;
  char rel1[8]={'<','>','=','!','+','-','&','/'};
  for(i=0;i<8;i++)
  {
   if(c==rel1[i])
   {
    c1=1;
    break;
   }
  }
  return c1;
}
token(char c[20])
{
  char key[32][10]={"auto","break","case","char","continue","default",
                       "do","double","void","else","enum","extern",
                       "float","goto","if","int","long","register","return",
                       "short","signed","sizeof","static","struct","switch",
                       "typedef","unsigned","union","volatile","while"};
  char oper[6][2]={"=","+","-","*","/","%"};
  char relop[8][3]={"++","--","&&","||","<=",">=","==","!="};
  char par[2][2]={"(",")"};
  char brac[2][2]={"{","}"};
  char spe[7][2]={" ",",",";",":","?","[","]"};
  int i,flag=0;
  for(i=0;i<30;i++)
  {
   if(strcmp(c,key[i])==0)
   {
    strcpy(l[t].tok,"Keyword");
    flag=1;
    return;
   }
  }
  for(i=0;i<6;i++)
  {
   if(strcmp(c,oper[i])==0)
   {
    strcpy(l[t].tok,"Arithmetic Operator");
    flag=1;
    return;
   }
  }
  for(i=0;i<8;i++)
  {
   if(strcmp(c,relop[i])==0)
   {
    strcpy(l[t].tok,"Relational Operator");
    flag=1;
    return;
   }
  }

for(i=0;i<2;i++)
  {
   if(strcmp(c,par[i])==0)
   {
    strcpy(l[t].tok,"paranthesis");
    flag=1;
    return;
   }
  }
  for(i=0;i<2;i++)
  {
   if(strcmp(c,brac[i])==0)
   {
    strcpy(l[t].tok,"Braces");
    flag=1;
    return;
   }
  }
  for(i=0;i<7;i++)
  {
   if(strcmp(c,spe[i])==0)
   {
    strcpy(l[t].tok,"Special Symbol");
    flag=1;
    return;
   }
  }
  if((strcmp(l[t].tok,"Keyword")!=0)&&(strcmp(l[t++].le,c)==0))
  {
   strcpy(l[t].tok,"Function");
   flag=1;
   return;
  }

if(flag==0)
  {
   strcpy(l[t].tok,"Identifier");
   return;
  }
  return;
}


Output: