TYBcs OS-Syspro Slip 9-2 | IProgramX

#include<stdio.h>
#define MAX 20

int frames[MAX],ref[MAX],mem[MAX][MAX],faults,sp,m,n;

void accept()
{
int i;

printf("Enter no.of frames:");
scanf("%d", &n);

printf("Enter no.of references:");
scanf("%d", &m);

printf("Enter reference string:\n");
for(i=0;i<m;i++)
{
printf("[%d]=",i);
scanf("%d",&ref[i]);
}
}

void disp()
{
int i,j;

for(i=0;i<m;i++)
printf("%3d",ref[i]);

printf("\n\n");

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(mem[i][j])
printf("%3d",mem[i][j]);
else
printf("   ");
}
printf("\n");
}

printf("Total Page Faults: %d\n",faults);
}

int search(int pno)
{
int i;

for(i=0;i<n;i++)
{
if(frames[i]==pno)
return i;
}

return -1;
}

void fifo()
{
int i,j;

for(i=0;i<m;i++)
{
if(search(ref[i])==-1)
{
frames[sp] = ref[i];
sp = (sp+1)%n;
faults++;
for(j=0;j<n;j++)
mem[j][i] = frames[j];

}
}
}

int main()
{
accept();
fifo();
disp();

return 0;
}

Post a Comment

2 Comments

  1. function creates child process using fork system call. Parent process sorts the integers using bubble sort and waits for child process using wait system call. Child process sorts the integers using insertion sort. | Write a C program to illustrate the concept of orphan process. Parent process creates a child and terminates before child has finished its task. So child process becomes orphan process. (Use fork(), sleep(), getpid(), getppid())





    #include
    #include
    #include
    #include

    void bubblesort(int arr[30],int n)
    {
    int i,j,temp;
    for(i=0;iarr[j+1])
    {
    temp=arr[j];
    arr[j]=arr[j+1];
    arr[j+1]=temp;
    }
    }
    }
    }

    void insertionsort(int arr[30], int n)
    {
    int i, j, temp;
    for (i = 1; i < n; i++) {
    temp = arr[i];
    j = i - 1;

    while(j>=0 && temp <= arr[j])
    {
    arr[j+1] = arr[j];
    j = j-1;
    }
    arr[j+1] = temp;
    }
    }
    void fork1()
    {
    int arr[25],arr1[25],n,i,status;
    printf("\nEnter the no of values in array :");
    scanf("%d",&n);
    printf("\nEnter the array elements :");
    for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
    int pid=fork();
    if(pid==0)
    {
    sleep(10);
    printf("\nchild process\n");
    printf("child process id=%d\n",getpid());
    insertionsort(arr,n);
    printf("\nElements Sorted Using insertionsort:");
    printf("\n");
    for(i=0;i<n;i++)
    printf("%d,",arr[i]);
    printf("\b");
    printf("\nparent process id=%d\n",getppid());
    system("ps -x");
    }
    else
    {
    printf("\nparent process\n");
    printf("\nparent process id=%d\n",getppid());
    bubblesort(arr,n);
    printf("Elements Sorted Using bubblesort:");
    printf("\n");
    for(i=0;i<n;i++)
    printf("%d,",arr[i]);
    printf("\n\n\n");
    }
    }
    int main()
    {
    fork1();
    return 0;
    }


    Output :

    @kali-linux:~/Desktop/Ty$ cc qb1.c
    @kali-linux:~/Desktop/Ty$ ./a.out

    Enter the no of values in array :5

    Enter the array elements :2 3 4 1 6

    parent process

    parent process id=3610
    Elements Sorted Using bubblesort:
    1,2,3,4,6,

    ReplyDelete
  2. Write a C program to illustrate the concept of orphan process. Parent process creates a child and terminates before child has finished its task. So child process becomes orphan process. (Use fork(), sleep(), getpid(), getppid())
    #include
    #include
    #include

    int main()
    {
    // fork() Create a child process

    int pid = fork();
    if (pid > 0) {
    //getpid() returns process id
    // while getppid() will return parent process id
    printf("Parent process\n");
    printf("ID : %d\n\n", getpid());
    }
    else if (pid == 0) {
    printf("Child process\n");
    // getpid() will return process id of child process
    printf("ID: %d\n", getpid());
    // getppid() will return parent process id of child process
    printf("Parent -ID: %d\n\n", getppid());

    sleep(10);

    // At this time parent process has finished.
    // So if u will check parent process id
    // it will show different process id
    printf("\nChild process \n");
    printf("ID: %d\n", getpid());
    printf("Parent -ID: %d\n", getppid());
    }
    else {
    printf("Failed to create child process");
    }

    return 0;
    }
    Output
    Parent process
    ID : 2274

    Child process
    ID: 2275
    Parent -ID: 2274

    Child process
    ID: 2275
    Parent -ID: 1103

    ReplyDelete