OS ASSignment1

Document Sample
OS ASSignment1 Powered By Docstoc
					Operating Systems
   Assignment


               R. R. A. Fernando
            DIT / 07 / C1 / 0019
               Exercise 01
Source Code

 #include <sys/types.h>
 #include <stdio.h>
 #include <unistd.h>

 int main(int argc, char *argv[])
 {
      int j, val1 = 0, val2 = 1, tempval = 0;
      pid_t pid;
      int num = atoi(argv[1]);

      if(num < 0) {
            printf("Invalid number.. Should be grater than 0");
            exit(0);
      }

      pid = fork();
      if(pid == 0) {
            for(j=1; j<=num; j++) {
                  if(j<=2) {
                        printf("%d", j-1);

                     }
                     else {
                              tempval = val1 + val2;
                              printf("%d", tempval);
                              val1 = val2;
                              val2 = tempval;
                     }

               }

      }

      else {
               wait(NULL);
               printf("\n");
      }

      return 0;

 }
Sample output
      Exercise 02

Source Code

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<unistd.h>
#include<sys/stat.h>
#define MAX_SEQUENCE 10

typedef struct
{
      long fib_sequence[MAX_SEQUENCE];
      int sequence_size;
}shared_data;

int main(int argc,char *argv[])
{
      if(argc == 1 )
      {
            printf("Please Enter Fibonacci number \n");
            exit(1);
      }
      long fib = atol(argv[1]); // Integer convert to long

      if((argc != 2) | (fib < 0)| (fib > MAX_SEQUENCE))//error checking
      {
            printf("USAGE : %s filename name with fibonacci value between 0 - 10
\n",argv[0]);
            exit(1);
      }

      if(fib > 0 & fib <=10 )
      {
      key_t key;    // define a unique key
      int shmid,status,x,rm,o;

      shared_data *data;
      pid_t pid;

      key=ftok(".",'Y'); // create a unique key


      if((shmid=shmget(key,sizeof(shared_data),IPC_CREAT|IPC_EXCL|0666))==-1)
      {
            printf("Shared Memory segment exists\n");

              if((shmid=shmget(key,sizeof(shared_data),0))==-1)
              {
                    perror("shmget\n");
                    exit(1);
             }
      }
      else
      {
             printf("Creating new Shared Memory segment\n");
      }

      if((data=(shared_data *) shmat(shmid,NULL,0)) == -1)
      {
            perror("shmat");
            exit(1);
      }

      data->sequence_size =(int) fib;

      if((pid=fork())==-1)
      {
                  perror("fork()");
                  exit(1);
      }

      if(pid == 0)
      {
            long a,b,n;
            long i;
            a = 0;
            b = 1;

             printf("Child Process is making the fibonacci numbers sequence.....\n");

             data->fib_sequence[0] = a;
             data->fib_sequence[1] = b;

             for(i=2 ; i <data->sequence_size ; i++)
      {
             n=a+b;
             data->fib_sequence[i]=n;
             a=b;
             b=n;
      }
      printf("Child Process Ends \n");
      }
      else
      {
            printf("Parent Process is waiting for child process complete...");
            wait();
            printf("\nParent Process is going to present Fibonacci numbers sequence
\n\n");

             for(x=0; x<data->sequence_size ;x++)
                   printf(" %d ",data->fib_sequence[x]);

             printf("\n\n");

             if(o=shmdt(NULL));
                   printf("Shared Memory segmant detached\n");
             if((rm=shmctl(shmid,IPC_RMID,0))==0)
                  printf("Shared Memory segment marked for deletion\n");
      }
      }
      else
      {
             printf("Please Enter Non-negative Number ...\n");
      }
return 0;
}




      Sample output
      Exercise 03

Source Code

#include<stdio.h>
#include<stdlib.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include<sys/types.h>

#define SHM_SIZE 30
int main()
{
      key_t key;
      int shmid;
      char *segptr;
      int rm;

      key=ftok(".",'F');

      if((shmid =shmget(key,SHM_SIZE,IPC_CREAT|0666)) == -1)
      {
            printf("Cannot create a Shared Memory segment \n");
            exit(1);
      }


      if((segptr=shmat(shmid,0,0)) == -1)
      {
            perror("shmat error");
            exit(1);
      }

      printf("\nShared Memory segment is created\n");

      struct shmid_ds myshm;

      shmctl(shmid,IPC_STAT,&myshm);

      printf("\na)    Segment ID: %d",shmid);
      printf("\nb)    Key: %d",key);
      printf("\nc)    Mode: %o",myshm.shm_perm.mode);
      printf("\nd)    Owner UID: %d",myshm.shm_perm.uid);
      printf("\ne)    Size: %d",myshm.shm_segsz);
      printf("\nf)    No_of_attaches: %d\n\n",myshm.shm_nattch);

      shmctl(shmid,IPC_SET,&myshm);
      if((rm=shmdt(segptr)) == 0)
            printf("Shared Memory segment is Detached\n");
}
Sample output
       Exercise 04

Source Code
#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>

int   array[100];

int Fibonacci(int no)
{
      int a, b, n, i;

       a=0;
       b=1;
       array[0] = a;
       array[1] = b;
       for(i=2 ; i<no ; i++)
       {
                   n=a+b;
                   array[i]=n;
                   a=b;
                   b=n;
       }
       pthread_exit(0);
}

int main(int argc,char *argv[])
{
      if(argc == 1 )
        {
                printf("Please Enter Fibonacci number \n");
                exit(1);
        }

       int num=atoi(argv[1]);
       if(argc != 2)
       {

                     perror("Please Enter one fibonacci Value \n");
                     exit(1);
       }

       if(num <=0)
       {
             perror("Please Enter Number more than 0 \n");
                   exit(1);
       }
       pthread_attr_t thread_attr;
       pthread_t tid;
       pthread_attr_init(&thread_attr);
       pthread_create(&tid, &thread_attr, Fibonacci,num);
       printf("\nWaiting for thead finishing \n");
       pthread_join(tid,NULL);
       printf("Thread finished \n");
    int x;
    for(x=0 ; x<num ;x++)
    {
           printf("%d ",array[x]);
    }

    printf("\n\n");

}



    Sample output

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:54
posted:7/17/2011
language:Dutch
pages:10