Monday, 2 December 2013

Parent creates shared memory to store N numbers and child find largest and smallest number

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

int main()
{
   long shmid;
   key_t key;
   int *shm;
   int *s,shm_size,n,i,lar,smal;
   printf("\nEnter the limit :- ");
   scanf("%d",&n);
   shm_size=n*sizeof(long);
   key = 110200;
   if ((shmid = shmget(key, shm_size, IPC_CREAT | 0666)) < 0) {
     perror("shmget");
     exit(1);
   }
   if ((shm = shmat(shmid, NULL, 0)) == (int *) -1) {
     perror("shmat");
     exit(1);
   }
   s = shm;
   for(i=0;i<n;i++,s++)
     scanf("%d",s);
   if(fork()==0){
     s=shm;
     lar=*s;
     smal=*s;
     for(i=1;i<n;i++){
       if(*(s+i)>lar)
         lar=*(s+i);
       else if(*(s+i)<smal)
         smal=*(s+i);
     }
   }
   else{
     wait(NULL);
     return 0;
   }
   printf("\nLargest= %d \nSmallest= %d\n",lar,smal);
   return 0;
}

Saturday, 23 November 2013

NOS SAMPLE QUESTIONS



S7 CSE EXTERNAL LAB EXAMINATION NOTICE

NOTE: SUBJECT CODES ARE WRONG... CONSIDER ONLY SUBJECT NAME... COME ACCORDING TO SUBJECT NAME ONLY...
if images below are not clear,,, u can see the pdf here: 
Bring Hall Ticket and Fair Record on the day of examination



Friday, 22 November 2013

To send palindrome back to parent process

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(){
  int pid,a,n,p[2],p2[2],i,len,j,k,flag,n2=0;
  char buffr1[100][100],buffr2[100][100],buf[5];
  printf("Enter the limit:- ");
  scanf("%d",&n);
  a = pipe(p);
  if(a == -1)
  {
     fprintf(stderr, "Pipe Failed.\n");
     return EXIT_FAILURE;
  }
  a = pipe(p2);
  if(a == -1)
  {
     fprintf(stderr, "Pipe Failed.\n");
     return EXIT_FAILURE;
  }
   pid = fork();
   switch(pid)
   {
     case -1:perror("main: fork");
exit(1);
     case 0: read(p[0],buffr2,sizeof(buffr2));
      printf("In child process (ID: %d)\n", pid);
      for(i=0;i<n;i++)
{
        len=strlen(buffr2[i]);
        flag=0;
        for(j=0,k=len-1;j<len;j++,k--)
{
if(buffr2[i][j]!=buffr2[i][k])
{
flag=1;
break;
}
        }
        if(flag==0)
{
n2++;
strcpy(buffr1[n2],buffr2[i]);
        }
      }
      buffr1[0][0]=n2;
      write(p2[1],buffr1,sizeof(buffr1));
      exit(1);
      break;
     default: printf("In parent process (ID: %d)\n", pid);
      for(i=0;i<n;i++)
{
        printf("Enter the string %d:- ",i+1);
        scanf("%s",buffr1[i]);
      }
      write(p[1],buffr1,sizeof(buffr1));
      waitpid(pid,NULL,0);
      read(p2[0],buffr2,sizeof(buffr2));
      printf("In parent process (ID: %d)\n", pid);
      n=(int) buffr2[0][0];
      for(i=1;i<=n;i++)
        printf("Palindrome %d:- %s\n",i,buffr2[i]);
      break;
}
    close(p[0]);
    close(p[1]);
close(p2[0]);
close(p2[1]);
    return 0;
}


To send back prime numbers to parent process

#include <stdio.h>     //for fprintf printf scanf stderr
#include <stdlib.h>    //for exit
#include <unistd.h>    //for pipe fork read write close
#include <math.h>      //for sqrt
#include <sys/types.h> //for waitpid
#include <sys/wait.h>  //for waitpid

int main(){
   int pid,a,n,p[2],p1[2],i,j,buffr1[100],buffr2[100],flag,n2=0, check;
   a = pipe(p);
   if(a == -1)
    {
       fprintf(stderr, "Pipe Failed.\n");
       return EXIT_FAILURE;
    }
   a=pipe(p1);
   if(a == -1)
    {
       fprintf(stderr, "Pipe Failed.\n");
       return EXIT_FAILURE;
    }
    pid = fork();
   switch(pid)
   {
       case -1:perror("main: fork");
          exit(1);
       case 0: n=read(p[0],buffr2,sizeof(buffr2));
          printf("In child process (ID: %d)\n", pid);
          n=n/sizeof(int);
          for(i=0;i<n;i++)
{
    flag=0;
    if(buffr2[i]<=1)
      flag=1;
check = sqrt(buffr2[i]);
    for(j=2;j<=check;j++)
{
      if((buffr2[i]%j)==0)
      {
  flag=1;
  break;
      }
    }
    if(flag==0)
{
      buffr1[n2]=buffr2[i];
      n2++;
    }
          }
          write(p1[1],buffr1,n2*sizeof(int));
          exit(1);
          break;
       default: printf("In parent process (ID: %d)\n", pid);
          printf("Enter the limit:- ");
          scanf("%d",&n);
          for(i=0;i<n;i++)
{
    printf("Enter the element %d:- ",i+1);
    scanf("%d",&buffr1[i]);
          }
          write(p[1],buffr1,n*sizeof(int));
          waitpid(pid,NULL,0);
          printf("In parent process (ID: %d)\n", pid);
          n2=read(p1[0],buffr2,sizeof(buffr2));
          n2=n2/sizeof(int);
          for(i=0;i<n2;i++)
            printf("Prime Number %d:- %d\n",i+1,buffr2[i]);
          break;
    }
    close(p[0]);
    close(p[1]);
close(p1[0]);
close(p1[1]);
    return 0;
}

To send N strings from parent to child process

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
   int pid,a,n,p[2],i;
   char buffr1[100][100],buffr2[100][100];
   printf("Enter the limit:- ");
   scanf("%d",&n);
   a = pipe(p);
   if(a == -1)
   {
       fprintf(stderr, "Pipe Failed.\n");
       return EXIT_FAILURE;
    }
    pid = fork();
switch(pid)
{
       case -1:perror("main: fork");
exit(1);
       case 0: read(p[0],buffr2,sizeof(buffr2));
          printf("In child process (ID: %d)\n", pid);
          for(i=0;i<n;i++)
            printf("String %d:- %s\n",i+1,buffr2[i]);
          exit(1);
          break;
       default: printf("In parent process (ID: %d)\n", pid);
          for(i=0;i<n;i++)
{
            printf("Enter the string %d:- ",i+1);
//gets(buffr1[i]);
//scanf ("%[^\n]%*c", buffr1[i]);
            scanf("%s",buffr1[i]);
          }
          write(p[1],buffr1,sizeof(buffr1));
          waitpid(pid,NULL,0);
          break;
    }
    close(p[0]);
    close(p[1]);
    return 0;
}