[go: up one dir, main page]

0% found this document useful (0 votes)
18 views4 pages

Slip 2

solved slip

Uploaded by

Vivek Dalvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Slip 2

solved slip

Uploaded by

Vivek Dalvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

slip2 Q1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200
typedef struct dir
{
char fname[20];
int start;
struct dir *next;
}NODE;
NODE *first,*last;
int n,fb,bit[MAX];
void init()
{
int i;
printf("Enter total no.of disk blocks:");
scanf("%d",&n);
fb = n;
for(i=0;i<fb;i++)
bit[i] = 0;
printf("\n");
}
void show_bitvector()
{
int i;
for(i=0;i<n;i++)
printf("%d ",bit[i]);
printf("\n");
}
void show_dir()
{
NODE *p;
int i;
printf("File\t\t\tNumber of blocks\n");
p = first;
while(p!=NULL)
{
printf("%s\t",p->fname);
i = p->start; while(i!=-1)
{
printf("%d->",i); i=bit[i];
}
printf("NULL\n");
p=p->next;
}
}
void create()
{
NODE *p;
char fname[20];
int i,j,k,nob;
printf("Enter file name:");
scanf("%s",fname);
printf("Enter no.of blocks:");
scanf("%d",&nob);
if(nob>fb)
{
printf("Failed to create file %s\n",fname);
return;
}
for(i=0;i<n;i++)
{
if(bit[i]==0) break;
}
p = (NODE*)malloc(sizeof(NODE));
strcpy(p->fname,fname);
p->start=i;
p->next=NULL;
if(first==NULL)
first=p;
else
last->next=p;
last=p;
fb-=nob;
j=i+1;
nob--;
while(nob>0)
{
if(bit[j]==0)
{
bit[i]=j;
i=j;
nob--;
}
j++;
}
bit[i]=-1;
printf("File %s created successully.\n",fname);
}
void delete()
{
char fname[20];
NODE *p,*q;
int nob=0,i,j;
printf("Enter file name to be deleted:"); scanf("%s",fname);
p = q = first;
while(p!=NULL)
{
if(strcmp(p->fname,fname)==0)
break;
q=p;
p=p->next;
}
if(p==NULL)
{
printf("File %s not found.\n",fname);
return;
}
i = p->start;
while(i!=-1)
{
nob++; j = i;
i = bit[i]; bit[j] = 0;
}
fb+=nob;
if(p==first)
first=first->next;
else if(p==last)
{
last=q;
last->next=NULL;
}
else
q->next = p->next;
free(p);
printf("File %s deleted successfully.\n",fname);
}
int main()
{
int ch;
init();
while(1)
{
printf("1.Show bit vector\n");
printf("2.Create new file\n");
printf("3.Show directory\n");
printf("4.Delete file\n");
printf("5.Exit\n");
printf("Enter your choice (1-5):");
scanf("%d",&ch);
switch(ch)
{
case 1:
show_bitvector();
break;
case 2:
create();
break;
case 3:
show_dir();
break;
case 4:
delete();
break;
case 5:
exit(0);
}
}
return 0;
}

Q2

#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>
int main (int argc, char* argv[]){
int i,my_id, num_procs,N=50;
int array[N],array_final_sum[N],array_final_mult[N];
int r_num,max_value;
unsigned seed;
double t0, t1, time;
MPI_Init(&argc, &argv);
MPI_Comm_rank (MPI_COMM_WORLD, &my_id);
MPI_Comm_size (MPI_COMM_WORLD, &num_procs);
t0 = MPI_Wtime();
for(i=0;i<N;i++){
array[i]=my_id +1 ;
}
MPI_Reduce(array,array_final_sum,N,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
MPI_Reduce(array,array_final_mult,N,MPI_INT,MPI_PROD,0,MPI_COMM_WORLD);
if(my_id == 0) {
for(i=0;i<N;i++) printf("Final array after sum: %d\n", array_final_sum[i]);
}
if(my_id == 0) {
for(i=0;i<N;i++) printf("Final array after product: %d\n", array_final_mult[i]) ;
}
seed=my_id+1;
srand(seed);
r_num=rand();
printf("my id is %d and r_num is %d\n", my_id,r_num);
MPI_Reduce(&r_num,&max_value,1,MPI_INT,MPI_MAX,0,MPI_COMM_WORLD);
t1 = MPI_Wtime();
time = t1 - t0 ;
if(my_id == 0) {
printf("Max_value is (AND THE WINNER IS ....): %d\n", max_value) ;
printf("Total elapsed time [sec] : %f\n", time); }
MPI_Finalize();
return 0;
}

You might also like