0% found this document useful (0 votes)
3 views37 pages

OS FINALfileprojects

The document provides a series of C code examples demonstrating various system calls and process management techniques in UNIX-like operating systems. Key topics include the FORK() system call, parent-child process interactions for computing sums, process states (orphan and zombie), inter-process communication methods like PIPE, FIFO, message queues, and shared memory, as well as scheduling algorithms such as FCFS, SJF, and priority scheduling. Each section includes code snippets and expected outputs to illustrate the concepts.

Uploaded by

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

OS FINALfileprojects

The document provides a series of C code examples demonstrating various system calls and process management techniques in UNIX-like operating systems. Key topics include the FORK() system call, parent-child process interactions for computing sums, process states (orphan and zombie), inter-process communication methods like PIPE, FIFO, message queues, and shared memory, as well as scheduling algorithms such as FCFS, SJF, and priority scheduling. Each section includes code snippets and expected outputs to illustrate the concepts.

Uploaded by

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

Q1. Demonstration of FORK() System Call.

Code:
#include <stdio.h>
#include <unistd.h>
int main()
{
fork();
printf("LINUX\n");
fork();
printf("WINDOWS\n");
fork();
printf("IOS\n");
return 0;
}
*****OUTPUT*****
Q2. Parent Process Computes the sum of EVEN And Child Processes Computes
the sum Of ODD numbers using FORK().

Code:
#include <stdio.h>
#include <unistd.h>
int main()
{
int s;
printf("Enter array size: ");
scanf("%d",&s);
int a[s];
printf("Enter array elements: ");
for(int x=0;x<s;x++)
{
scanf("%d",&a[x]);
}
int n=fork();
int odd_s=0, even_s=0, i;
if(n>0)
{
for(int i=0;i<s;i++)
{
if(a[i]%2==0)
{
even_s+=a[i];
}
}
printf("Parent Process\n");
printf("Sum of even numbers: %d\n",even_s);
}
else
{
for(int i=0;i<s;i++)
{
if(a[i]%2!=0)
{
odd_s+=a[i];
}
}
printf("Child Process\n");
printf("Sum of odd numbers: %d\n",odd_s);
}
return 0;
}
*****OUTPUT*****
Q3. Demonstration of WAIT() System Call.

Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("I am Child\n");
exit(0);
}
else
{
wait(NULL);
printf("I am Parent\n");
printf("The Child PID = %d\n",pid);
}
return 0;
}
*****OUTPUT*****
Q4. Implementation of ORPHAN PROCESS & ZOMBIE PROCESS.

Code:
ORPHAN
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
int main()
{
pid_t id;
id=fork();
if(id>0)
{
printf("parent process\n");
printf("%d\t%d\n",getpid(),getppid());
exit(0);
printf("Error");
}
else if(id==0)
{
printf("child process\n");
sleep(50);
printf("%d\t%d\n",getpid(),getppid());
}
}
ZOMBIE
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main()
{
pid_t id;
id=fork();
if(id>0)
{
sleep(50);
printf("parent process\n");
printf("%d\t%d\n",getpid(),getppid());
}
else if(id==0)
{
printf("child process\n");
printf("%d\t%d\n",getpid(),getppid());
}
}
*****OUTPUT*****

ORPHAN:

ZOMBIE:
Q5. Implementation of PIPE.

Code:
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int fd[2],n;
char buffer[100];
pid_t p;
pipe(fd);
p=fork();
if(p>0)
{
printf("passing values to child\n");
write(fd[1],"hello\n",6);
}
else
{
printf("child recieved data\n");
n=read(fd[0],buffer,100);
write(1,buffer,n);
}
}
*****OUTPUT*****
Q6. Implementation of FIFO.

Code:
WRITER PROCESS
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int file,n;
char s[100];
mknod("myfifo",S_IFIFO|0666,0);
printf("Write for Reader process:\n");
file=open("myfifo",O_WRONLY);
while(gets(s))
{
n=write(file,s,strlen(s));
printf("Writing %d bytes: %s\n",n,s);
}
return 0;
}
READER PROCESS
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int file,n;
char a[100];
mknod("myfifo",S_IFIFO|0666,0);
file=open("myfifo",O_RDONLY);
printf("If you have a write process then type data\n");
do
{
n=read(file,a,sizeof(a));
a[n]='\0';
printf("Reader process read %d bytes: %s\n",n,a);
}while(n>0);
return 0;
}
*****OUTPUT*****

WRITER PROCESS:

READER PROCESS:
Q7. Implementation of MESSAGE QUEUE.

Code:
WRITER PROCESS
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>

struct msgbuff
{
long mtype;
char mtext[100];
}mb;

int main()
{
key_t key;
int msgid,c;
key=ftok("progfile",'A');
msgid=msgget(key,0666|IPC_CREAT);
mb.mtype=1;
printf("\nEnter a string: ");
gets(mb.mtext);
c=msgsnd(msgid,&mb,strlen(mb.mtext),0);
printf("Sender wrote the text:\t%s\n",mb.mtext);
return 0;
}
READER PROCESS
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>

struct msgbuff
{
long mtype;
char mtext[100];
}mb;

int main()
{
key_t key;
int msgid,c;
key=ftok("progfile",'A');
msgid=msgget(key,0666|IPC_CREAT);
msgrcv(msgid,&mb,sizeof(mb),1,0);
printf("Data Received is:\t%s\n",mb.mtext);
msgctl(msgid,IPC_RMID,NULL);
return 0;
}
*****OUTPUT*****

WRITER PROCESS:

READER PROCESS:
Q8. Implementation of SHARED MEMORY.

Code:
WRITER PROCESS
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>

int main()
{
key_t key;
int shmid;
void *ptr;
key=ftok("shmfile",'A');
shmid=shmget(key,1024,0666|IPC_CREAT);
ptr=shmat(shmid,(void *)0,0);
printf("\nInput Data: ");
gets(ptr);
shmdt(ptr);
return 0;
}
READER PROCESS
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>

int main()
{
key_t key;
int shmid;
void *ptr;
key=ftok("srfile",'A');
shmid=shmget(key,1024,0666|IPC_CREAT);
ptr=shmat(shmid,(void *)0,0);
printf("\nThe data stored is: %s\n",ptr);
shmdt(ptr);
shmctl(shmid,IPC_RMID,NULL);
return 0;
}
*****OUTPUT*****

WRITER PROCESS:

READER PROCESS:
Q9. Implementation of the First Come First Served (FCFS) Scheduling Algorithm.

Code:
#include<stdio.h>
#include<string.h>
int main()
{
char pn[10][10],t[10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;
int totwt=0,tottat=0;
printf("Enter the number of processes: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter the Process ID, Arrival Time & Burst Time: ");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}

}
}
for(i=0; i<n; i++)
{
if(i==0)
star[i]=arr[i];
else
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
printf("\nPID \tArrival time\tBurst time\tWait time\tStart \tTAT \tFinish");
for(i=0; i<n; i++)
{

printf("\n%s\t\t\t%d\t\t\t%d\t\t\t%d\t\t\t%d\t\t\t%d\t\t\t%d",pn[i],arr[i],bur[i],wt[i],star[i],tat[i]
,finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f",(float)totwt/n);
printf("\nAverage Turn Around Time:%f",(float)tottat/n);
return 0;
}
*****OUTPUT*****
Q10. Implementation of Shortest Job First (SJF) Scheduling Algorithm.

Code:
#include<string.h>
int main()
{
int bt[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
int totwt=0,totta=0;
double awt,ata;
char pn[10][10],t[10];
printf("Enter the number of process: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter process id, arrival time & burst time: ");
scanf("%s%d%d",pn[i],&at[i],&bt[i]);
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
if(bt[i]<bt[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0; i<n; i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+bt[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(double)totwt/n;
ata=(double)totta/n;
printf("\nProcessID\tArrivaltime\tBursttime\tWaitingtime\tTurnaroundtime");
for(i=0; i<n; i++)
{
printf("\n%s\t\t\t%d\t\t\t%d\t\t\t%d\t\t\t%d",pn[i],at[i],bt[i],wt[i],ta[i]);
}
printf("\nAverage waiting time: %f",awt);
printf("\nAverage turnaroundtime: %f",ata);
return 0;
}
*****OUTPUT*****
Q11. Implementation of Priority Scheduling Algorithm.

Code:
#include <stdio.h>
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int n;
printf("Enter number of processes: ");
scanf("%d",&n);
int burst[n],priority[n],index[n];
for(int i=0;i<n;i++)
{
printf("Enter Burst Time and Priority Value for Process %d: ",i+1);
scanf("%d %d",&burst[i],&priority[i]);
index[i]=i+1;
}
for(int i=0;i<n;i++)
{
int temp=priority[i],m=i;

for(int j=i;j<n;j++)
{
if(priority[j] > temp)
{
temp=priority[j];
m=j;
}
}

swap(&priority[i], &priority[m]);
swap(&burst[i], &burst[m]);
swap(&index[i],&index[m]);
}
int t=0;
printf("Order of process Execution is:\n");
for(int i=0;i<n;i++)
{
printf("P%d is executed from %d to %d\n",index[i],t,t+burst[i]);
t+=burst[i];
}
printf("\n");
printf("ProcessId\tBurst Time\tWait Time\n");
int wait_time=0;
int total_wait_time = 0;
for(int i=0;i<n;i++)
{
printf("P%d\t\t\t%d\t\t\t%d\n",index[i],burst[i],wait_time);
total_wait_time += wait_time;
wait_time += burst[i];
}

float avg_wait_time = (float) total_wait_time / n;


printf("Average Waiting time is %f\n", avg_wait_time);
int total_Turn_Around = 0;
for(int i=0; i < n; i++){
total_Turn_Around += burst[i];
}
float avg_Turn_Around = (float) total_Turn_Around / n;
printf("Average TurnAround Time is %f",avg_Turn_Around);
return 0;
}

*****OUTPUT*****
Q12. Implementation of First In First Out (FIFO) Page Replacement Policy.

Code:
#include <stdio.h>
int main()
{
int num;
printf("Enter Limit: ");
scanf("%d",&num);
printf("Enter page string: ");
int incomingStream[num];
for(int i=0;i<num;i++)
{
scanf("%d",&incomingStream[i]);
}
int pageFaults = 0;
int pageHits = 0;
int frames;
printf("Enter number of frames: ");
scanf("%d",&frames);
int m, n, s, pages;
pages = sizeof(incomingStream) / sizeof(incomingStream[0]);
printf("Pages\tFrame 1\t\tFrame 2\t\tFrame 3\t\tPage Hits\n");
int temp[frames];
for (m = 0; m < frames; m++)
{
temp[m] = -1;
}
for (m = 0; m < pages; m++)
{
s = 0;
for (n = 0; n < frames; n++)
{
if (incomingStream[m] == temp[n])
{
s++;
pageHits++;
}
}
if (s == 0)
{
pageFaults++;
if ((pageFaults <= frames))
{
temp[m] = incomingStream[m];
}
else
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
}
printf("%d\t\t\t", incomingStream[m]);
for (n = 0; n < frames; n++)
{
if (temp[n] != -1)
printf(" %d\t\t\t", temp[n]);
else
printf(" - \t\t\t");
}
printf("%d\n", s);
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
printf("Total Page Hits:\t%d\n", pageHits);
return 0;
}

*****OUTPUT*****
Q13. Implementation of LRU Page Replacement Policy.

Code:
#include<stdio.h>
#include<limits.h>

int checkHit(int incomingPage, int queue[], int occupied) {


for (int i = 0; i < occupied; i++) {
if (incomingPage == queue[i])
return 1;
}
return 0;
}

void printFrame(int queue[], int occupied) {


for (int i = 0; i < occupied; i++)
printf("%d\t\t\t", queue[i]);
}

int main() {
int n;
printf("Enter the number of pages in the stream: ");
scanf("%d", &n);

int incomingStream[n];
printf("Enter the page stream: ");
for (int i = 0; i < n; i++)
scanf("%d", &incomingStream[i]);

int frames = 3;
int queue[n];
int distance[n];
int occupied = 0;
int pagefault = 0;
int pagehit = 0;

printf("Page\t Frame1 \t Frame2 \t Frame3\n");

for (int i = 0; i < n; i++) {


printf("%d: \t\t", incomingStream[i]);

if (checkHit(incomingStream[i], queue, occupied)) {


printFrame(queue, occupied);
printf("Hit");
pagehit++;
} else if (occupied < frames) {
queue[occupied] = incomingStream[i];
pagefault++;
occupied++;
printFrame(queue, occupied);
printf("Page Fault");
} else {
int max = INT_MIN;
int index;
for (int j = 0; j < frames; j++) {
distance[j] = 0;
for (int k = i - 1; k >= 0; k--) {
++distance[j];
if (queue[j] == incomingStream[k])
break;
}
if (distance[j] > max) {
max = distance[j];
index = j;
}
}
queue[index] = incomingStream[i];
printFrame(queue, occupied);
printf("Page Fault");
pagefault++;
}

printf("\n");
}

printf("Page Hit: %d\n", pagehit);


printf("Page Fault: %d\n", pagefault);

return 0;
}
*****OUTPUT*****

You might also like