You are on page 1of 25

Operating System Programs

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)
{

printf("I'm the child %d , %d\n",getpid(),getppid());

}
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");
}

FCFS PROCESS SCHEDULING


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{

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

printf("process \t burst_time \t wait_time \t turn_around \t \n");


for(i=1;i<=n;i++)
{
printf("%d \t\t %d \t\t %d \t\t %d",i,b[i],w[i],t[i]);
printf("\n");
}
printf("\n average wait time");
tw=wt/n;
printf("%f",tw);
printf("\n Average turn around time");
tr=at/n;
printf("%f",tr);

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;
}

ROUND ROBIN SCHEDULING


#include<stdio.h>
#include<conio.h>

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();

printf("\t\t ROUND ROBIN SCHEDULING \n");


printf("Enter the number of Processors \n");
scanf("%d",&n);

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

printf("\n\n ROUND ROBIN SCHEDULING ALGORITHM \n\n");


printf("\n\n Process \t Process ID \t BurstTime \t Waiting Time \t TurnaroundTime \n ");
for(i=1;i<=n1;i++)
{
printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
}

printf("\n The average Waiting Time=4.2f",awt);


printf("\n The average Turn around Time=4.2f",atat);
getch();
}

COMMAND LINE ARGUMENTS


#include<stdio.h>
int main( int argc, char *argv[] )
{
if( argc == 2 )
{
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");
}
}

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;

/* save calling environment for longjmp */


val = setjmp( buf );
if( val != 0 )
{
printf("Returned from a longjmp() with value = %d\n", val);
exit(0);
}
printf("Jump function call\n");
jmpfunction( buf );

return(0);
}

9|Page
Operating System Programs

void jmpfunction(jmp_buf buff)


{
longjmp(buff, 100);
}

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

struct rusage r_usage;


struct rlimit r_limit;
int priority;
work();
getrusage(RUSAGE_SELF, &r_usage);
printf("CPU usage: User = %ld.%06ld, System = %ld.%06ld\n",
r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,
r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec);
priority = getpriority(PRIO_PROCESS, getpid());
printf("Current priority = %d\n", priority);
getrlimit(RLIMIT_FSIZE, &r_limit);
printf("Current FSIZE limit: soft = %ld, hard = %ld\n",
r_limit.rlim_cur, r_limit.rlim_max);
r_limit.rlim_cur = 2048;
r_limit.rlim_max = 4096;
printf("Setting a 2K file size limit\n");
setrlimit(RLIMIT_FSIZE, &r_limit);
work();
exit(0);
}

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);
}

READER WRITER PROBLEM


#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>
sem_t readCountAccess;
sem_t databaseAccess;
int readCount=0;
void *Reader(void *arg);
void *Writer(void *arg);
int main()
{
int i=0,NumberofReaderThread=0,NumberofWriterThread;
sem_init(&readCountAccess,0,1);
sem_init(&databaseAccess,0,1);
pthread_t Readers_thr[100],Writer_thr[100];
printf(“\nEnter number of Readers thread(MAX 10)”);
scanf(“%d”,&NumberofReaderThread);
printf(“\nEnter number of Writers thread(MAX 10)”);
scanf(“%d”,&NumberofWriterThread);
for(i=0;i<numberofreaderthread;i++)
{
pthread_create(&Readers_thr[i],NULL,Reader,(void *)i);
}
for(i=0;i<numberofwriterthread;i++)
{
pthread_create(&Writer_thr[i],NULL,Writer,(void *)i);
}
for(i=0;i<NumberofWriterThread;i++)
{
pthread_join(Writer_thr[i],NULL);
}
for(i=0;i<NumberofReaderThread;i++)
{
pthread_join(Readers_thr[i],NULL);
}
sem_destroy(&databaseAccess);
sem_destroy(&readCountAccess);
return 0;
}

14 | P a g e
Operating System Programs

void * Writer(void *arg)


{
sleep(1);
int temp=(int)arg;
printf(“\nWriter %d is trying to enter into database for modifying the data”,temp);
sem_wait(&databaseAccess);
printf(“\nWriter %d is writting into the database”,temp);
printf(“\nWriter %d is leaving the database”);
sem_post(&databaseAccess);
}
void *Reader(void *arg)
{
sleep(1);
int temp=(int)arg;
printf(“\nReader %d is trying to enter into the Database for reading the data”,temp);
sem_wait(&readCountAccess);
readCount++;
if(readCount==1)
{
sem_wait(&databaseAccess);
printf(“\nReader %d is reading the database”,temp);
}
sem_post(&readCountAccess);
sem_wait(&readCountAccess);
readCount–;
if(readCount==0)
{
printf(“\nReader %d is leaving the database”,temp);
sem_post(&databaseAccess);
}
sem_post(&readCountAccess);
}

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!!");
}

DISK SCHEDULING ALGORITHMS

FCFS/SSTF/SCAN

#include<stdio.h>
#include<math.h>

void fcfs(int noq, int qu[10], int st)


{
int i,s=0;
for(i=0;i<noq;i++)
{
s=s+abs(st-qu[i]);
st=qu[i];
}
printf("\n Total seek time :%d",s);
}

void sstf(int noq, int qu[10], int st, int visit[10])


{
int min,s=0,p,i;

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);
}

void scan(int noq, int qu[10], int st, int ch)


{
int i,j,s=0;
for(i=0;i<noq;i++)
{
if(st < qu[i])
{
for(j=i-1; j>= 0;j--)
{
s=s+abs(st - qu[j]);
st = qu[j];
}
if(ch == 3)
{
s = s + abs(st - 0);
st = 0;
}
for(j = 1;j < noq;j++)
{
s= s + abs(st - qu[j]);
st = qu[j];
}
break;
}

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

case 2: printf("\n SSTF \n");


printf("\n*****\n");
sstf(noq,qu,st,visit);
break;
case 3: printf("\n SCAN \n");
printf("\n*****\n");
scan(noq,qu,st,ch);
break;
case 4: exit(0);
}
}
}

FIFO PAGE REPLACEMENT


#include<stdio.h>
int main()
{
int i,j,n,a[50],frame[10],no,k,avail,count=0;
printf("\n ENTER THE NUMBER OF PAGES:\n");
scanf("%d",&n);
printf("\n ENTER THE PAGE NUMBER :\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\n ENTER THE NUMBER OF FRAMES :");
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
printf("\tref string\t page frames\n");
for(i=1;i<=n;i++)
{
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
{
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)

21 | P a g e
Operating System Programs

printf("%d\t",frame[k]);
}
printf("\n");
}
printf("Page Fault Is %d",count);
return 0;
}

OPTIMAL PAGE REPLACEMENT


#include<stdio.h>
#include<conio.h>
main()
{
int fr[5],i,j,k,t[5],p=1,flag=0,page[25],psz,nf,t1,u[5];
clrscr();
printf("enter the number of frames:");
scanf("%d",&nf);
printf("\n enter the page size");
scanf("%d",&psz);

printf("\nenter the page sequence:");


for(i=1; i<=psz; i++)
scanf("%d",&page[i]);

for(i=1; i<=nf; i++)


fr[i]=-1;
for(i=1; i<=psz; i++)
{
if(full(fr,nf)==1)
break;
else
{
flag=0;
for(j=1; j<=nf; j++)
{
if(page[i]==fr[j])
{
flag=1;
printf(" \t%d:\t",page[i]);
break;
}
}
if(flag==0)

22 | P a g e
Operating System Programs

{
fr[p]=page[i];
printf(" \t%d:\t",page[i]);
p++;
}

for(j=1; j<=nf; j++)


printf(" %d ",fr[j]);
printf("\n");
}
}
p=0;
for(; i<=psz; i++)
{
flag=0;
for(j=1; j<=nf; j++)
{
if(page[i]==fr[j])
{
flag=1;
break;
}
}
if(flag==0)
{
p++;
for(j=1; j<=nf; j++)
{
for(k=i+1; k<=psz; k++)
{
if(fr[j]==page[k])
{
u[j]=k;
break;
}
else
u[j]=21;
}
}
for(j=1; j<=nf; j++)
t[j]=u[j];
for(j=1; j<=nf; j++)
{
for(k=j+1; k<=nf; k++)
{

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

You might also like