Articles by "Operating System"
Showing posts with label Operating System. Show all posts
no image
Write the program to implement the system calls getpid() and getppid(). 

Program Algorithm: 
Step 1 : Declare the variables pid , parent pid , child id and grand chil id. 
Step 2 : Get the child id value using system call fork(). 
Step 3 : If child id value is less than zero then print as “error at fork() child”. 
Step 4 : If child id !=0 then using getpid() system call get the process id. 
Step 5 : Print “I am parent” and print the process id. 
Step 6 : Get the grand child id value using system call fork(). 
Step 7 : If the grand child id value is less than zero then print as “error at fork() grand child”. 
Step 8 : If the grand child id !=0 then using getpid system call get the process id. 
Step 9 : Assign the value of pid to my pid. 
Step 10 : Print “I am child” and print the value of my pid. 
Step 11 : Get my parent pid value using system call getppid(). 
Step 12 : Print “My parent's process id” and its value. 
Step 13 : Else print “I am the grand child”. 
Step 14 : Get the grand child's process id using getpid() and print it as “my process id”.
Step 15 : Get the grand child's parent process id using getppid() and print it as “my parent‟s process id 

System Calls Used: 
1.getpid( ) Each process is identified by its id value. This function is used to get the id value of a particular process. 
2.getppid( ) Used to get particular process parent's id value. 
3.perror( ) Indicate the process error. 


Program Code: 
#include<stdio.h> 
#include<unistd.h> 
#include<stdlib.h> 
int main( ) 
{ 
int pid; 
pid=fork( ); 
if(pid== -1) 
{
perror(“fork failed”); 
exit(0); 
} 
if(pid==0) 
{ 
printf(“\nChild process is under execution”); 
printf(“\nProcess id of the child process is %d”, getpid()); 
printf(“\nProcess id of the parent process is %d”, getppid()); 
} 
else 
{ 
printf(“\nParent process is under execution”); 
printf(“\nProcess id of the parent process is %d”, getpid()); 
printf(“\nProcess id of the child process in parent is %d”, pid()); 
printf(“\nProcess id of the parent of parent is %d”, getppid()); 
} 
return(0); 
} 


no image
Write the program to create a Child Process using system call fork(). 

Program Algorithm: 
Step 1 : Declare the variable pid. 
Step 2 : Get the pid value using system call fork(). 
Step 3 : If pid value is less than zero then print as “Fork failed”. 
Step 4 : Else if pid value is equal to zero include the new process in the system‟s file using execlp system call. 
Step 5 : Else if pid is greater than zero then it is the parent process and it waits till the child completes using the system call wait() Step 6 : Then print “Child complete”.

System Calls Used: 

1. fork( ) Used to create new processes. The new process consists of a copy of the address space of the original process. The value of process id for the child process is zero, whereas the value of process id for the parent is an integer value greater than zero. 
Syntax : fork( ) 

2.execlp( ) Used after the fork() system call by one of the two processes to replace the process‟ memory space with a new program. It loads a binary file into memory destroying the memory image of the program containing the execlp system call and starts its execution.The child process overlays its address space with the UNIX command /bin/ls using the execlp system call. 
Syntax : execlp( ) 

3. wait( ) The parent waits for the child process to complete using the wait system call. The wait system call returns the process identifier of a terminated child so that the parent can tell which of its possibly many children has terminated. 
Syntax: wait( NULL) 

4. exit( ) A process terminates when it finishes executing its final statement and asks the operating system to delete it by using the exit system call. At that point, the process may return data (output) to its parent process (via the wait system call). 
Syntax: exit(0)

Program Code: 
#include<stdio.h> 
#include<stdlib.h> 
#include<unistd.h> 
void main(int argc,char *arg[]) 
{ 
int pid;
pid=fork(); 
if(pid<0) 
{ 
printf("fork failed"); 
exit(1); 
} 
else if(pid==0) 
{ 
execlp("whoami","ls",NULL); 
exit(0); 
} 
else 
{ 
printf("\n Process id is -%d\n",getpid()); 
wait(NULL); 
exit(0); 
} 
}
Write a C program to simulate Single Level Directory.

Description of File Organization Techniques: The directory contains information about the files, including attributes, location, and ownership. Sometimes the directories consisting of subdirectories also. The directory is itself a file, owned by the operating system and accessible by various file management routines.

a)Single Level Directory
b)Two Level
c)Hierarchical
d)General Graph Directory


Single Level Directories: It is the simplest of all directory structures, in this the directory system having only one directory, it consisting of all files. Sometimes it is said to be the root directory. The following dig. Shows single level directory that contains four files (A, B, C, D). It has the simplicity and ability to locate files quickly. It is not used in the multi-user system, it is used on the small embedded system.

Pic Source: http://www.osinfoblog.com/post/143/directories/

Algorithm for Single Level Directory Structure:
Step 1:Start
Step 2: Initialize values gd=DETECT,gm,count,i,j,mid,cir_x;
Initialize character array fname[10][20];
Step 3: Initialize graph function as
Initgraph(& gd, &gm," c:/tc/bgi");
Clear device();
Step 4:set back ground color with setbkcolor();
Step 5:read number of files in variable count.
Step 6:if check i<count
Step 7: for i=0 & i<count
i increment;
Cleardevice();
setbkcolor(GREEN);
read file name;
setfillstyle(1,MAGENTA);
Step 8: mid=640/count;
cir_x=mid/3;
bar3d(270,100,370,150,0,0);
settextstyle(2,0,4);
settextstyle(1,1);
outtextxy(320,125,"rootdirectory");
setcolor(BLUE);
i++;
Step 9:for j=0&&j<=i&&cir_x+=mid
j increment;
line(320,150,cir_x,250);
fillellipse(cir_x,250,30,30);
outtextxy(cir_x,250,fname[i]);
Step 10: End


/* Program to simulate single level directory */
Program Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int nf=0,i=0,j=0,ch;
char mdname[10],fname[10][10],name[10];
clrscr();
printf("Enter the directory name:");
scanf("%s",mdname);
printf("Enter the number of files:");
scanf("%d",&nf);
do
{
printf("Enter file name to be created:");
scanf("%s",name);
for(i=0;i<nf;i++)
{
if(!strcmp(name,fname[i]))
break;
}
if(i==nf)
{
strcpy(fname[j++],name);
nf++;
}
else
printf("There is already %s\n",name);
printf("Do you want to enter another file(yes - 1 or no - 0):");
scanf("%d",&ch);
}
while(ch==1);
printf("Directory name is:%s\n",mdname);
printf("Files names are:");
for(i=0;i<j;i++)
printf("\n%s",fname[i]);
getch();
}


Program Output:
Enter the directory name:sss
Enter the number of files:3
Enter file name to be created:aaa
Do you want to enter another file(yes - 1 or no - 0):1
Enter file name to be created:bbb
Do you want to enter another file(yes - 1 or no - 0):1
Enter file name to be created:ccc
Do you want to enter another file(yes - 1 or no - 0):0
Directory name is:sss
Files names are:
aaa
bbb
ccc
no image
Write a program to implement Dynamic allocation of memories in MVT.

Description of Multiprogramming with fixed no of tasks: MFT is the one of the memory management technique. In this technique, main memory is divided into no of static partitions at the system generated time. A process may be loaded into a partition of equal or greater size. The partition sizes are depending on o.s. in this memory management scheme the o.s occupies the low memory, and the rest of the main memory is available for user space. This scheme suffers from internal as well as external fragmentation.



Algorithm for Multiprogram Fixed Task:
Step1: start the process.
Step2: Declare variables.
Step3: Enter total memory size.
Step4: Allocate memory for operating system.
Step5: allocate total memory to the pages.
Step6: Display the wastage of memory.
Step7: Stop the process.


/* Program to simulate Multiprogram Fixed Task*/
Program Code:
#include < stdio.h>
#include <conio.h>
void main()
{
int i, p,a[10],c[15],temp=0,total=0;
float b,t;
clrscr();
printf(“Enter the total memory ”);
scanf(“%f”,&t);
printf(“Enter the processes :”);
scanf(“%d”, &p);
b=t/p;
for(i=0;i<p;i++)
a[i]=b;
for(i=0;i<p;i++)
{
lable: printf(“Enter memory for %d process: ",i);
scanf(“%d”,&temp);
if(temp<=b)
{
c[i]=a[i]-temp;
printf(“Internal fragmentation for this block”);
printf(“%d”,c[i]);
}
else
{
printf(“Required memory is not available”);
goto lable;
}
total=total+c[i];
}
printf(“Total internal fragmentation %d\n”,total);
getch();
}



Program Output:
Enter the total memory: 800
Enter the processes: 4
Enter memory for 0 process: 150
Internal fragmentation for this block: 50
Enter memory for 1 process: 100
Internal fragmentation for this block: 100
Enter memory for 2 process: 200
Internal fragmentation for this block: 0
Enter memory for 3 process: 200
Internal fragmentation for this block: 0
Total internal fragmentation 150
no image
Write a program to implement the Dynamic allocation of memories in Multiprogram Variable Task.

Algorithm for Multiprogram Variable Task:
Step1: start the process.
Step2: Declare variables.
Step3: Enter total memory size.
Step4: Allocate memory for the OS.
Step5: allocate total memory to the pages.
Step6: Display the wastage of memory.
Step7: Stop the process.

/* Program to simulate the MVT */
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int m,i,p[15];
char ch;
clrscr();
printf("Enter memory to be allocated: ");
scanf("%d",&m);
printf("Enter process size : ");
scanf("%d", &p[0]);
i=0;
do
{
m=m-p[i];
printf("\nRemaining memory is %d\n",m);
abc:printf("Do you want to continue: ");
fflush(stdin);
scanf("%c",&ch);
i++;
if(ch=='y')
{
printf("Enter the process size: ");
scanf("%d",&p[i]);
}
else
{
printf("\nExternal fragmentation is %d",m);
break;
}
if(m<p[i])
{
printf("\nRequired memory is not available\n");
goto abc;
}
}while((ch=='y')&&(m>=p[i]));
getch();
}



Program Output:
Enter memory to be allocated: 1000
Enter process size : 500
Remaining memory is 500
Do you want to continue: y
Enter the process size: 300
Remaining memory is 200
Do you want to continue: y
Enter the process size: 100
Remaining memory is 100
Do you want to continue: n
External fragmentation is 100
Write a C Program to implement Linked File Allocation method.

Description of Linked allocation:
It is easy to allocate the files because allocation is on an individual block basis. Each block contains a pointer to the next free block in the chain. Here also the file allocation table consisting of a single entry for each file. Using this strategy any free block can be added to a chain very easily. There is a link between one block to another block, that’s why it is said to be linked allocation. We can avoid the external fragmentation.


Algorithm for Linked File Allocation:
Step 1: Create a queue to hold all pages in memory
Step 2: When the page is required replace the page at the head of the queue
Step 3: Now the new page is inserted at the tail of the queue
Step 4: Create a stack
Step 5: When the page fault occurs replace page present at the bottom of the stack
Step 6: Stop the allocation.

/* Program to simulate linked file allocation strategy */
Program Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], p,i, st, len, j, c, k, a;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks already allocated: ");
scanf("%d",&p);
printf("Enter blocks already allocated: ");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
x: printf("Enter index starting block and length: ");
scanf("%d%d", &st,&len);
k=len;
if(f[st]==0)
{
for(j=st;j<(st+k);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("%d-------->%d\n",j,f[j]);
}
else
{
printf("%d Block is already allocated \n",j);
k++;
}
}
}
else
printf("%d starting block is already allocated \n",st);
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}

Program Output:
Enter how many blocks already allocated: 3
Enter blocks already allocated: 1 3 5
Enter index starting block and length: 2 2
2-------->1
3 Block is already allocated
4-------->1
Do you want to enter more file(Yes - 1/No - 0)0
Write a C Program to implement Indexed File Allocation method.

Description of Indexed allocation: Indexed allocation supports both sequential and direct access files. The file indexes are not physically stored as a part of the file allocation table. Whenever the file size increases, we can easily add some more blocks to the index. In this strategy, the file allocation table contains a single entry for each file. The entry consisting of one index block, the index blocks having the pointers to the other blocks. No external fragmentation.


Algorithm for Indexed File Allocation:
Step 1: Start.
Step 2: Let n be the size of the buffer
Step 3: check if there are any producer
Step 4: if yes check whether the buffer is full
Step 5: If no the producer item is stored in the buffer
Step 6: If the buffer is full the producer has to wait
Step 7: Check there is any consumer.If yes check whether the buffer is empty
Step 8: If no the consumer consumes them from the buffer
Step 9: If the buffer is empty, the consumer has to wait.
Step 10: Repeat checking for the producer and consumer till required
Step 11: Terminate the process.

/* Program to simulate indexed file allocation strategy */
Program Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:printf("Enter the index block: ");
scanf("%d",&ind);
if(f[ind]!=1)
{
printf("Enter no of blocks needed and no of files for the index %d on the disk : \n", ind);
scanf("%d",&n);
}
else
{
printf("%d index is already allocated \n",ind);
goto x;
}
y: count=0;
for(i=0;i<n;i++)
{
scanf("%d", &index[i]);
if(f[index[i]]==0)
count++;
}
if(count==n)
{
for(j=0;j<n;j++)
f[index[j]]=1;
printf("Allocated\n");
printf("File Indexed\n");
for(k=0;k<n;k++)
printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
}
else
{
printf("File in the index is already allocated \n");
printf("Enter another file indexed");
goto y;
}
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit(0);
getch();
}

Program Output:
Enter the index block: 5
Enter no of blocks needed and no of files for the index 5 on the disk :
4
1 2 3 4
Allocated
File Indexed
5-------->1 : 1
5-------->2 : 1
5-------->3 : 1
5-------->4 : 1
Do you want to enter more file(Yes - 1/No - 0)1
Enter the index block: 4
4 index is already allocated
Enter the index block: 6
Enter no of blocks needed and no of files for the index 6 on the disk :
2
7 8
A5llocated
File Indexed
6-------->7 : 1
6-------->8 : 1
Do you want to enter more file(Yes - 1/No - 0)0
Write a C Program to implement Sequential File Allocation method.

Description to Sequential File Allocation method: Files are normally stored on the disks. So the main problem is how to allocate space to those files. So that disk space is utilized effectively and files can be accessed quickly. Three major strategies of allocating disc space are in wide use. Sequential, indexed and linked.


Sequential allocation:
In this allocation strategy, each file occupies a set of contiguously blocks on the disk. This strategy is best suited. For sequential files, the file allocation table consists of a single entry for each file. It shows the filenames, starting block of the file and size of the file. The main problem with this strategy is, it is difficult to find the contiguous free blocks in the disk and some free blocks could happen between two files.


Algorithm for Sequential File Allocation:
Step 1: Start the program.
Step 2: Get the number of memory partition and their sizes.
Step 3: Get the number of processes and values of block size for each process.
Step 4: First fit algorithm searches all the entire memory block until a hole which is big enough is encountered. It allocates that memory block for the requesting process.
Step 5: Best-fit algorithm searches the memory blocks for the smallest hole which can be allocated to requesting process and allocates it.
Step 6: Worst fit algorithm searches the memory blocks for the largest hole and allocates it to the process.
Step 7: Analyses all the three memory management techniques and display the best algorithm which utilizes the memory resources effectively and efficiently.
Step 8: Stop the program.

/* Program to simulate sequential file allocation strategy */
Program Code:
#include < stdio.h>
#include<conio.h>
void main()
{
int f[50], i, st, len, j, c, k, count = 0;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Files Allocated are : \n");
x: count=0;
printf(“Enter starting block and length of files: ”);
scanf("%d%d", &st,&len);
for(k=st;k<(st+len);k++)
if(f[k]==0)
count++;
if(len==count)
{
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("%d\t%d\n",j,f[j]);
}
if(j!=(st+len-1))
printf(” The file is allocated to disk\n");
}
else
printf(” The file is not allocated \n");
printf("Do you want to enter more file(Yes - 1/No - 0)");
scanf("%d", &c);
if(c==1)
goto x;
else
exit();
getch();
}

Program Output:
Files Allocated are :
Enter starting block and length of files: 14 3
14 1
15 1
16 1
The file is allocated to disk
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 1
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)1
Enter starting block and length of files: 14 4
The file is not allocated
Do you want to enter more file(Yes - 1/No - 0)0