Os Practicals
Os Practicals
NAME:S.Jaswanth varma
AIM: Create a new process by invoking the appropriate system call. Get
the process identifier of the currently running process and its respective
parent using system calls and display the same using a C program.
ALGORITHM:
1. Include necessary headers: Include the necessary header files like
<stdio.h> and <unistd.h> for using system calls.
2. Declare variables: Declare variables to hold the process ID (pid_t pid)
and the parent process ID (pid_t ppid).
3. Get the current process ID: Use the getpid() system call to retrieve the
process ID of the current process.
4. Get the parent process ID: Use the getppid() system call to retrieve the
parent process ID.
5. Display the process IDs: Print the process IDs to the console.
6. Create a new process: Use the fork() system call to create a new
process. Check the return value of fork() to determine whether the code
is running in the parent or child process.
7. Display process IDs in child and parent processes: Depending on
whether the process is the parent or the child, display the respective
process IDs.
PROGRAM:
#include<stdio.h>
#include<unistd.h>
int main()
{
printf("Process ID: %d\n", getpid() );
printf("Parent Process ID: %d\n", getpid() );
return 0;
}
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
fclose(fptr1);
fclose(fptr2);
return 0;
}
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
for (i = 1; i < n; i++) {
A[i][2] = 0;
for (j = 0; j < i; j++)
A[i][2] += A[j][1];
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
printf("P BT WT TAT\n");
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("nProcesst Burst Time tWaiting TimetTurnaround Time\n");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}
sum_turnaround+=time-at[over];
over++;
}
avgwait=(float)sum_wait/(float)n;
avgturn=(float)sum_turnaround/(float)n;
printf("Average waiting time is %f\n",avgwait);
printf("Average turnaround time is %f\n",avgturn);
return 0;
}
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10],
temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP;
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t");
scanf("%d", &at[i]);
printf(" \nBurst time is: \t");
scanf("%d", &bt[i]);
temp[i] = bt[i];
}
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0)
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--;
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-
at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}