Digital Assignment - 2
Digital Assignment - 2
• A context switches
• A trap to a specific location in the interrupt vector
• Control passes to a service routine, which runs in 'monitor' mode
• The monitor determines what system call has occurred
• Monitor verifies that the parameters passed are correct and legal
For example, here's a sample code for measuring the time taken for
a simple system call and a simple function call:
#include <unistd.h>
#include <assert.h>
int foo(){
return(10);
}
ALGORITHM:
PROGRAM:
#include <cstdio>
#include <unistd.h>
#include <cstdlib>
#include <sys/types.h> //needed for open
#include <sys/stat.h> //needed for open
#include <fcntl.h> //needed for open
inFile=open(argv[1],O_RDONLY);
if (inFile==-1)
{
exit(1);
}
close (inFile);
return 0;
}
➢Process Management
x=fork();
/* fork()
create a child process.
If fork returns value 0, then it is the child process
If it returns some positive number then it is parent process. The
positive number is the pid of a child.
*/
if(x==0) // This part of a if is executed by Child Process
{
printf(“\nchild process = %d”,getpid()); // Pid returns the Pid of the
process executing getpid() function
for(i=0;i<n;i++)
printf(” %d”,a[i]);
}
else // This part of a if is executed by Parent Process
{
printf(“\nparent process=%d \n”,getpid());
partition(a,0,n-1); // Parent process calls Merge Sort function
printf(“\nAfter merge sorting elements are: “);
for(i=0;i<n;i++)
printf(“%d “,a[i]);
}
}
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int i,j,n;
int *status=NULL;
int arr[30];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
pid_t pid;
pid=fork();
if(pid==0)
{
printf("\n\t This is child process. ");
printf("\n\t My process id is : %d", getpid());
printf("\n\t My Parent process id is : %d", getppid());
quickSort(arr,0,n-1);
printf("\nQuicksort");
for(i=0;i<n;i++)
printf(" %d",arr[i]);
printf("\n\n");
}
else
{
do
{
do
i++;
while(arr[i]<pivot && i<=high);
do
j--;
while(arr[j]>pivot);
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
while(i<j);
arr[low]=arr[j];
arr[j]=pivot;
return(j);
}
}
void merge(int arr[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1;
j=i2;
k=0;
for(i=i1,j=0;i<=j2;i++,j++)
arr[i]=temp[j];
}
//Program 2 :
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
int i,j,c,ele;
int arr[argc];
for(i=0;i<argc-1;i++)
{
int n=atoi(argv[i]);
arr[i]=n;
}
ele=atoi(argv[i]);
i=0;
j=argc-1;
c=(i+j)/2;
if(i<=j)
printf("Elemets found");
else
printf("Not found");
}
III. Run an experiment to determine the context switch time
from one process to another and one kernel thread to
another. Compare the findings
To summaries –
T = 2 * (Td + Tc + Tr)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //Header file for sleep(). man 3 sleep for
details.
#include <pthread.h>
int main()
{
pthread_t thread_id;
printf("Before Thread\n");
pthread_create(&thread_id, NULL, myThreadFun, NULL);
pthread_join(thread_id, NULL);
printf("After Thread\n");
exit(0);
}