Professional Documents
Culture Documents
FORK
#include<stdio.h>
#include<unistd.h>
int main()
{
fork();
printf("child");
return 0;
}
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main ()
{
pid_t pid,pid2;
pid=fork();
if (pid < 0)
{
// printf ("Fork Failed\n");
return -1;
}
else if (pid == 0)
{
}
else
{
pid2=fork();
printf("I'm the parent %d , %d\n",getpid(),getppid());
sleep(5);
}
return 0;
1|Page
Operating System Programs
THREAD
#include<pthread.h>
#include<stdio.h>
void *th()
{
printf("Thread");
}
int main()
{
pthread_t t1;
pthread_create(&t1,NULL,&th,NULL);
pthread_cancel(t1);
pthread_join(t1,NULL);
printf("thread 1");
}
#include<pthread.h>
#include<stdio.h>
void *th(int *a)
{
int *j=(int *)a;
printf("Thread %d created\n",j);
pthread_exit(a);
}
int main()
{
pthread_t t1[2];
int i,*j;
for(i=0;i<2;i++)
{
pthread_create(&t1[i],NULL,&th,(void *)i);
}
for(i=0;i<2;i++)
{
pthread_join(t1[i],&j);
printf("thread %d joined\n",j);
}
printf("Parent thread");
}
#include<pthread.h>
#include<stdio.h>
void *th(void *a)
2|Page
Operating System Programs
{
int *j=(int *)a;
printf("Thread %d created\n",(int)j);
pthread_exit(a);
}
int main()
{
pthread_t t1;
int i=20,*j;
pthread_create(&t1,NULL,&th,(void *)i);
pthread_join(t1,(void **)&j);
printf("thread %d joined\n",(int)j);
printf("Parent thread");
}
int p[20],b[20],w[20],t[20],i,v,n,at;
int wt=0;
float tw,tr;
printf("Enter the number of process : ");
scanf("%d",&n);
printf("enter CPU burst time : ");
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
t[1]=b[1];
w[1]=0;
at=t[1];
wt=w[1];
for(i=2;i<=n;i++)
{
t[i]=b[i]+t[i-1];
at=at+t[i];
w[i]=t[i-1];
wt=wt+w[i];
}
}
3|Page
Operating System Programs
SJF SCHEDULING
#include<stdio.h>
int main()
{
int time,bt[10],at[10],sum_bt=0,smallest,n,i;
int sum_turnaround=0,sum_wait=0;
printf("Enter no of processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time for process P%d : ",i+1);
scanf("%d",&at[i]);
printf("Enter burst time for process P%d : ",i+1);
scanf("%d",&bt[i]);
sum_bt+=bt[i];
}
bt[9]=9999;
printf("\n\nProcess\t|Turnaround Time| Waiting Time\n\n");
for(time=0;time<sum_bt;)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && bt[i]>0 && bt[i]<bt[smallest])
smallest=i;
}
if(smallest==9)
{
4|Page
Operating System Programs
time++;
continue;
}
printf("P[%d]\t|\t%d\t|\t%d\n",smallest+1,time+bt[smallest]-at[smallest],time-at[smallest]);
sum_turnaround+=time+bt[smallest]-at[smallest];
sum_wait+=time-at[smallest];
time+=bt[smallest];
bt[smallest]=0;
}
printf("\n\n average waiting time = %f",sum_wait*1.0/n);
printf("\n\n average turnaround time = %f",sum_turnaround*1.0/n);
return 0;
}
PRIORITY SCHEDULING
#include<stdio.h>
int main()
{
int i,j,n,time,sum_wait=0,sum_turnaround=0;
int smallest,at[10],bt[10],priority[10],remain;
printf("Enter no of Processes : ");
scanf("%d",&n);
remain=n;
for(i=0;i<n;i++)
{
printf("Enter arrival time, burst time and priority for process p%d :",i+1);
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
scanf("%d",&priority[i]);
}
priority[9]=11;
printf("\n\nProcess\t|Turnaround time|waiting time\n");
for(time=0;remain!=0;)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && priority[i]<priority[smallest] && bt[i]>0)
{
smallest=i;
}
}
5|Page
Operating System Programs
time+=bt[smallest];
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",smallest+1,time-at[smallest],time-at[smallest]-bt[smallest]);
sum_wait+=time-at[smallest]-bt[smallest];
sum_turnaround+=time-at[smallest];
bt[smallest]=0;
}
printf("\nAvg waiting time = %f\n",sum_wait*1.0/n);
printf("Avg turnaround time = %f",sum_turnaround*1.0/n);
return 0;
}
void main()
{
int ts,pid[10],need[10],wt[10],tat[10],i,j,n,n1;
int bt[10],flag[10],ttat=0,twt=0;
float awt,atat;
clrscr();
n1=n;
printf("\n Enter the Timeslice \n");
scanf("%d",&ts);
for(i=1;i<=n;i++)
{
printf("\n Enter the process ID %d",i);
scanf("%d",&pid[i]);
printf("\n Enter the Burst Time for the process");
scanf("%d",&bt[i]);
need[i]=bt[i];
}
for(i=1;i<=n;i++)
{
flag[i]=1;
wt[i]=0;
6|Page
Operating System Programs
}
while(n!=0)
{
for(i=1;i<=n;i++)
{
if(need[i]>=ts)
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]-=ts;
if(need[i]==0)
{
flag[i]=0;
n--;
}
}
else
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=need[i];
}
need[i]=0;
n--;
flag[i]=0;
}
}
}
for(i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;
7|Page
Operating System Programs
ENVIRONMENT VARIABLES
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[], char *envp[])
{
char *home, *host,*sh,*path;
int i=10;
home = getenv("HOME");
host = getenv("HOSTNAME");
sh=getenv("SHELL");
path=getenv("PATH");
printf ("Your home is %s host is %s.Shell %s and the path is %s\n", home, host, sh,path);
8|Page
Operating System Programs
for(i=0;1<10;i++)
printf("%s\n",envp[i]);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
extern char **environ;
int main()
{
char **env = environ;
while(*env)
{
printf("%s\n",*env);
env++;
}
exit(0);
}
SETJMP LONGJMP
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
int main()
{
int val;
jmp_buf buf;
return(0);
}
9|Page
Operating System Programs
SETRLIMIT GETRLIMIT
#include<sys/resource.h>
#include<stdio.h>
int main ()
{
struct rlimit r;
getrlimit (RLIMIT_FSIZE, &r);
printf("%ld", r.rlim_cur);
printf("%ld", r.rlim_max);
return 0;
}
#include<sys/resource.h>
#include<sys/time.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<sys/types.h>
void work()
{
FILE *f;
int i;
double x = 4.5;
f = tmpfile();
for(i = 0; i < 10000; i++) {
fprintf(f,"Do some output\n");
if(ferror(f)) {
fprintf(stderr,"Error writing to temporary file\n");
exit(1);
}
}
for(i = 0; i < 1000000; i++)
x = log(x*x + 3.21);
}
int main()
{
10 | P a g e
Operating System Programs
RACE CONDITION
#include<stdio.h>
#include<pthread.h>
int c = 5;
void * f1();
void * f2();
int main()
{
pthread_t t1, t2;
pthread_create(&t1, NULL, f1, NULL);
pthread_create(&t2, NULL, f2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf(“%d”,c);
}
void *f1()
{
int a;
a = c;
a = a+1;
//sleep(1);
c = a;
11 | P a g e
Operating System Programs
printf(“%d”,c);
}
void *f2()
{
int a;
a = c;
a = a-1;
//sleep(1);
c = a;
printf(“%d”,c);
}
MUTEX LOCK
#include<stdio.h>
#include<pthread.h>
int c = 5;
void * f1();
void * f2();
pthread_mutex_t l;
int main()
{
pthread_t t1, t2;
pthread_create(&t1, NULL, f1, NULL);
pthread_create(&t2, NULL, f2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf(“%d”,c);
}
void *f1()
{
pthread_mutex_lock(&l);
int a;
a = c;
a = a+1;
//sleep(1);
c = a;
printf(“%d”,c);
pthread_mutex_unlock(&l);
}
void *f2()
{
pthread_mutex_lock(&l);
int a;
12 | P a g e
Operating System Programs
a = c;
a = a-1;
//sleep(1);
c = a;
printf(“%d”,c);
pthread_mutex_unlock(&l);
}
SEMAPHORE
#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
int c = 5;
void * f1();
void * f2();
sem_t s;
int main()
{
pthread_t t1, t2;
sem_init(&s, 0, 1);
pthread_create(&t1, NULL, f1, NULL);
pthread_create(&t2, NULL, f2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf(“%d”,c);
}
void *f1()
{
sem_wait(&s);
int a;
a = c;
a = a+1;
//sleep(1);
c = a;
printf(“%d”,c);
sem_post(&s);
}
void *f2()
{
sem_wait(&s);
int a;
a = c;
a = a-1;
13 | P a g e
Operating System Programs
//sleep(1);
c = a;
printf(“%d”,c);
sem_post(&s);
}
14 | P a g e
Operating System Programs
DEADLOCK
#include<stdio.h>
#include<pthread.h>
void * f1();
void * f2();
pthread_mutex_t l1,l2;
int main()
{
pthread_t t1, t2;
pthread_create(&t1, NULL, f1, NULL);
pthread_create(&t2, NULL, f2, NULL);
15 | P a g e
Operating System Programs
pthread_join(t1, NULL);
pthread_join(t2, NULL);
}
void *f1()
{
pthread_mutex_lock(&l1);
printf(“l1 lock t1”);
sleep(1);
pthread_mutex_lock(&l2);
printf(“l2 lock t1”);
}
void *f2()
{
pthread_mutex_lock(&l2);
printf(“l2 lock t2”);
sleep(1);
pthread_mutex_lock(&l1);
printf(“l1 lock t2”);
}
BANKER’S ALGORITHM
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
16 | P a g e
Operating System Programs
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1;
17 | P a g e
Operating System Programs
count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
FCFS/SSTF/SCAN
#include<stdio.h>
#include<math.h>
18 | P a g e
Operating System Programs
while(1)
{
min=999;
for(i=0;i<noq;i++)
if (visit[i] == 0)
{
if(min > abs(st - qu[i]))
{
min = abs(st-qu[i]);
p = i;
}
}
if(min == 999)
break;
visit[p]=1;
s=s + min;
st = qu[p];
}
printf("\n Total seek time is: %d",s);
}
19 | P a g e
Operating System Programs
}
printf("\n Total seek time : %d",s);
}
int main()
{
int n,qu[20],st,i,j,t,noq,ch,visit[20];
printf("\n Enter the maximum number of cylinders : ");
scanf("%d",&n);
printf("enter number of queue elements");
scanf("%d",&noq);
printf("\n Enter the work queue");
for(i=0;i<noq;i++)
{
scanf("%d",&qu[i]);
visit[i] = 0;
}
printf("\n Enter the disk head starting posision: \n");
scanf("%d",&st);
while(1)
{
printf("\n\n\t\t MENU \n");
printf("\n\n\t\t 1. FCFS \n");
printf("\n\n\t\t 2. SSTF \n");
printf("\n\n\t\t 3. SCAN \n");
printf("\n\n\t\t 4. EXIT \n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
if(ch > 2)
{
for(i=0;i<noq;i++)
for(j=i+1;j<noq;j++)
if(qu[i]>qu[j])
{
t=qu[i];
qu[i] = qu[j];
qu[j] = t;
}
}
switch(ch)
{
case 1: printf("\n FCFS \n");
printf("\n*****\n");
fcfs(noq,qu,st);
break;
20 | P a g e
Operating System Programs
21 | P a g e
Operating System Programs
printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}
22 | P a g e
Operating System Programs
{
fr[p]=page[i];
printf(" \t%d:\t",page[i]);
p++;
}
23 | P a g e
Operating System Programs
if(t[j]<t[k])
{
t1=t[j];
t[j]=t[k];
t[k]=t1;
}
}
}
for(j=1; j<=nf; j++)
{
if(t[1]==u[j])
{
fr[j]=page[i];
u[j]=i;
}
}
printf("page fault\t");
}
else
printf(" \t");
printf("%d:\t",page[i]);
for(j=1; j<=nf; j++)
printf(" %d ",fr[j]);
printf("\n");
}
printf("\ntotal page faults: %d",p+3);
// getch();
}
int full(int a[],int n)
{
int k;
for(k=1; k<=n; k++)
{
if(a[k]==-1)
return 0;
}
return 1;
}
PIPE
// writing to a program
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
24 | P a g e
Operating System Programs
#include<string.h>
int main()
{
FILE *rd;
char buffer[500];
int n;
sprintf(buffer,"name first");
rd=popen("wc -c","w");
if(rd !=NULL)
fwrite(buffer,sizeof(char),strlen(buffer),rd);
pclose(rd); //required
}
• #include<stdio.h>
• #include<stdlib.h>
• #include<unistd.h>
• int main()
• {
• int fd[2],n;
• char buffer[100];
• pid_t p;
• pipe(fd);
• p=fork();
• if(p>0)
• {
• close(fd[0]);
• printf("Passing value to child\n");
• write(fd[1],"hello\n",6);
• wait();
•
• }
• else
• {
• close(fd[1]);
• n=read(fd[0],buffer,100);
• write(1,buffer,n);
• }
• exit(0);
• }
25 | P a g e