Unix and OS Lab Programs
Unix and OS Lab Programs
1. Design a program using fork () system call that creates a child process. The child process prints
its own pid, id of its parent, does directory listing (using exec () system call) and exits. The parent
process has to invoke wait () system call to wait for child process to complete and prints its own
pid and id of its child process and then exists.
#include <stdlib.h>
#include<sys/types.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
int main( ) {
pid_t pid ;
pid = fork( );
if ( pid < 0 )
{
exit(-1);
}
else if ( pid==0)
{
fflush ( stdout ) ;
else {
wait(NULL);
exit(0);
18IS060 2020-21
Operating system laboratory
int sum ;
void *runner ( void *param ) ;
18IS060 2020-21
Operating system laboratory
3. Write a program that accepts a directory name as argument and checks whether it exists as a
directory. If it doesn’t exist or exists as an ordinary file, then remove the file and create the
directory. Also change to that directory and display the current directory.
#include<sys/stat.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc,char **argv)
{
struct stat statbuf;
int exists=1;
char ptr[100];
if((access(argv[1],F_OK)==0))
{
lstat(argv[1],&statbuf);
if(S_ISREG(statbuf.st_mode))
{
unlink(argv[1]);
exists=0;
}
}
else
exists=0;
if(exists==0)
mkdir(argv[1],S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
if(chdir(argv[1])<0)
{
printf("chdir failed");
exit;
}
if(getcwd(ptr,100)==NULL)
{
printf("getcwd error");
exit;
}
printf("\n cwd=%s\n",ptr);
exit;
}
18IS060 2020-21
Operating system laboratory
04. Write a program that shares a pipe between two processes. Demonstrate how the data flows from the
parent to the child processes.
#include<stdio.h>
#include<unistd.h>
int main() {
int pipefds[2];
int returnstatus;
int pid;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1) {
printf("Unable to create pipe\n");
return 1;
}
pid = fork();
// Child process
if (pid == 0) {
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 1 is %s\n", readmessage);
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 2 is %s\n", readmessage);
} else { //Parent process
printf("Parent Process - Writing to pipe - Message 1 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
printf("Parent Process - Writing to pipe - Message 2 is %s\n", writemessages[1]);
write(pipefds[1], writemessages[1], sizeof(writemessages[1]));
}
return 0;
}
18IS060 2020-21
Operating system laboratory
5. Write a program to illustrate the race condition between parent and child processes.
#include<stdio.h>
#include<stdlib.h>
#include<error.h>
static void charatatime(char *);
int main(void)
{
pid_t pid;
if ((pid = fork()) < 0)
{
printf("fork error");
}
else if (pid == 0)
{
charatatime("output from child\n");
}
else
{
charatatime("output from parent\n");
}
exit(0);
}
static void charatatime(char *str)
{
char *ptr;
int c;
setbuf(stdout, NULL); /* set unbuffered */
for (ptr = str; (c = *ptr++) != 0; )
putc(c, stdout);
}
18IS060 2020-21
Operating system laboratory
6. Write a program that creates a zombie and then calls system to execute the ps command to verify that
the process is zombie.
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main ()
{
pid_t child_pid; /* Create a child process. */
child_pid = fork ();
if (child_pid == 0)
{
exit (0); /* This is the child process.Exit immediately. */
}
else
{
sleep(3); /* This is the parent process. Sleep for a minute. */
system("ps -e -o pid,ppid,stat,cmd");
}
return 0;
}
18IS060 2020-21
Operating system laboratory
07. Develop two programs (server and client) that illustrate the passing of a string via shared memory
between the processes running simultaneously. The server program creates the shared memory portion
and string. The client program attaches itself to the created shared memory portion and uses the string.
18IS060 2020-21
Operating system laboratory
shmdt(shm);
shmctl(shmid,IPC_RMID,NULL);
exit(0);
}
Process 2:
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
/* Get the segment named "5678", created by the server */
key = 5678;
/* Locate the segment */
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
exit(1);
}
/*Attach the segment to process2’s data space*/
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
18IS060 2020-21
Operating system laboratory
}
/* Read what the process1 put in the memory */
for (s = shm; *s != '\0' ; s++)
putchar(*s);
putchar('\n');
/* Finally, change the first character of the segment to '*', indicating process2
has read the segment */
*shm = '*';
exit(0);
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define SHMSZ 27
main()
{
char c;
int shmid;
key_t key;
char *shm, *s;
/*Name shared memory segment "5678" */
key = 5678;
18IS060 2020-21
Operating system laboratory
/*Create the segment*/
Process 2:
#include <sys/types.h>
18IS060 2020-21
Operating system laboratory
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
/* Get the segment named "5678", created by the server */
key = 5678;
/* Locate the segment */
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) {
perror("shmget");
exit(1);
}
/*Attach the segment to process2’s data space*/
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
perror("shmat");
exit(1);
}
/* Read what the process1 put in the memory */
for (s = shm; *s != '\0' ; s++)
putchar(*s);
putchar('\n');
/* Finally, change the first character of the segment to '*', indicating process2
has read the segment */
*shm = '*';
exit(0);
}
18IS060 2020-21
Operating system laboratory
08. Design, develop and execute a program to simulate the working of Shortest Job First scheduling
algorithm. Display the Gantt chart, compute and print the average waiting time and average
turnaround time.
#include <stdio.h>
struct process
{
int pid;
int btime;
int wtime;
int ttime;
} p[10], temp;
main()
{
int i,j,k,n,ttur,twat;
float awat,atur;
printf("Enter no. of process : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}
for(i=0; i<n-1; i++)
{
for(j=i+1; j<n; j++)
{
if((p[i].btime > p[j].btime) ||
(p[i].btime == p[j].btime && p[i].pid > p[j].pid))
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
p[0].wtime = 0;
for(i=0; i<n; i++)
{
p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}
ttur = twat = 0;
for(i=0; i<n; i++)
{
ttur += p[i].ttime;
twat += p[i].wtime;
}
18IS060 2020-21
Operating system laboratory
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n SJF Scheduling\n\n");
for(i=0; i<28; i++)
printf("-");
printf("\nProcess B-Time T-Time W-Time\n");
for(i=0; i<28; i++)
printf("-");
for(i=0; i<n; i++)
printf("\n P%-4d\t%4d\t%3d\t%2d",
p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n");
for(i=0; i<28; i++)
printf("-");
printf("\n\nGANTT Chart\n");
printf("-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n|");
for(i=0; i<n; i++)
{
k = p[i].btime/2;
for(j=0; j<k; j++)
printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++)
printf(" ");
printf("|");
}
printf("\n-");
for(i=0; i<(p[n-1].ttime + 2*n); i++)
printf("-");
printf("\n0");
for(i=0; i<n; i++)
{
for(j=0; j<p[i].btime; j++)
printf(" ");
printf("%2d",p[i].ttime);
}
printf("\n\nAverage waiting time : %5.2fms", awat);
printf("\nAverage turn around time : %5.2fms\n", atur);
}
18IS060 2020-21
Operating system laboratory
09. Design, develop and execute a program to simulate the working of Round Robin Scheduling
algorithm with different Quantum sizes. Display the Gantt chart, compute and print the average
waiting time and average turnaround time.
#include <stdio.h>
main()
{
int i,x=-1,k[10],m=0,n,t,s=0;
int a[50],temp,b[50],p[10],bur[10],bur1[10];
int wat[10],tur[10],ttur=0,twat=0,j=0;
float awat,atur;
printf("Enter no. of process : ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Burst time for process P%d : ", (i+1));
scanf("%d", &bur[i]);
bur1[i] = bur[i];
}
printf("Enter the time slice (in ms) : ");
scanf("%d", &t);
for(i=0; i<n; i++)
{
b[i] = bur[i] / t;
if((bur[i]%t) != 0)
b[i] += 1;
m += b[i];
}
printf("\n\t\tRound Robin Scheduling\n");
printf("\nGANTT Chart\n");
for(i=0; i<m; i++)
printf("--------");
printf("\n");
a[0] = 0;
while(j < m)
{
if(x == n-1)
x = 0;
else
x++;
if(bur[x] >= t)
{
bur[x] -= t;
a[j+1] = a[j] + t;
if(b[x] == 1)
{
p[s] = x;
k[s] = a[j+1];
18IS060 2020-21
Operating system laboratory
s++;
}
j++;
b[x] -= 1;
printf(" P%d | ", x+1);
}
else if(bur[x] != 0)
{
a[j+1] = a[j] + bur[x];
bur[x] = 0;
if(b[x] == 1)
{
p[s] = x;
k[s] = a[j+1];
s++;
}
j++;
b[x] -= 1;
printf(" P%d |",x+1);
}
}
printf("\n");
for(i=0;i<n;i++)
printf("--------");
printf("\n");
for(j=0; j<=m; j++)
printf("%d\t", a[j]);
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(p[i] > p[j])
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
temp = k[i];
k[i] = k[j];
k[j] = temp;
}
}
}
for(i=0; i<n; i++)
{
wat[i] = k[i] - bur1[i];
tur[i] = k[i];
}
for(i=0; i<n; i++)
{
ttur += tur[i];
twat += wat[i];
18IS060 2020-21
Operating system laboratory
}
printf("\n\n");
for(i=0; i<30; i++)
printf("-");
printf("\nProcess\tBurst\tTrnd\tWait\n");
for(i=0; i<30; i++)
printf("-");
for (i=0; i<n; i++)
printf("\nP%-4d\t%4d\t%4d\t%4d", p[i]+1, bur1[i], tur[i],wat[i]);
printf("\n");
for(i=0; i<30; i++)
printf("-");
awat = (float)twat / n;
atur = (float)ttur / n;
printf("\n\nAverage waiting time : %.2f ms", awat);
printf("\nAverage turn around time : %.2f ms\n", atur);
}
18IS060 2020-21
Operating system laboratory
10. Implement the Producer-Consumer problem with bounded buffer using semaphores.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#define N 5
#define BUFSIZE 1
#define PERMS 0666
int *buffer;
int nextp = 0, nextc = 0;
int mutex, full, empty; /* semaphore variables */
void producer()
{
int data;
if(nextp == N)
nextp = 0;
printf("Enter data for producer to produce : ");
scanf("%d",(buffer + nextp));
nextp++;
}
void consumer()
{
int g;
if(nextc == N)
nextc = 0;
g = *(buffer + nextc++);
printf("\nConsumer consumes data %d", g);
}
18IS060 2020-21
Operating system laboratory
pid_t pid;
if((shmid = shmget(1000, BUFSIZE, IPC_CREAT|PERMS)) < 0)
{
printf("\nUnable to create shared memory");
return;
}
if((buffer = (int*)shmat(shmid, (char*)0, 0)) == (int*)-1)
{
printf("\nShared memory allocation error\n");
exit(1);
}
if((mutex = semget(IPC_PRIVATE, 1, PERMS|IPC_CREAT)) == -1)
{
printf("\nCan't create mutex semaphore");
exit(1);
}
if((empty = semget(IPC_PRIVATE, 1, PERMS|IPC_CREAT)) == -1)
{
printf("\nCan't create empty semaphore");
exit(1);
}
if((full = semget(IPC_PRIVATE, 1, PERMS|IPC_CREAT)) == -1)
{
printf("\nCan't create full semaphore");
exit(1);
}
sem_create(mutex, 1);
sem_create(empty, N);
sem_create(full, 0);
if((pid = fork()) < 0)
{
printf("\nError in process creation");
exit(1);
}
else if(pid > 0)
{
for(i=0; i<N; i++)
18IS060 2020-21
Operating system laboratory
{
sem_wait(empty);
sem_wait(mutex);
producer();
sem_signal(mutex);
sem_signal(full);
}
}
else if(pid == 0)
{
for(i=0; i<N; i++)
{
sem_wait(full);
sem_wait(mutex);
consumer();
sem_signal(mutex);
sem_signal(empty);
}
printf("\n");
}
}
18IS060 2020-21
Operating system laboratory
11. Design, develop and run a program to implement the Banker’s Algorithm. Demonstrate its working
with different data values.
#include <stdio.h>
int curr[5][5], maxclaim[5][5],need[5][5], avl[5];
int alloc[5] = {0, 0, 0, 0, 0};
int maxres[5], running[5], safeseq[5],safe=0;
int count = 0, i, j, count2=0,exec, r, p, k = 1;
int main(void)
{
printf("\nEnter the number of processes: ");
scanf("%d", &p);
for (i = 0; i < p; i++)
{
running[i] = 1;
count++;
}
printf("\nEnter the number of resources: ");
scanf("%d", &r);
printf("\nEnter Claim Vector:");
for (i = 0; i < r; i++)
{
scanf("%d", &maxres[i]);
}
printf("\nEnter Allocated Resource Table:\n");
for (i = 0; i < p; i++)
{
for(j = 0; j < r; j++)
{
scanf("%d", &curr[i][j]);
}
}
printf("\nEnter Maximum Claim Table:\n");
for (i = 0; i < p; i++)
{
18IS060 2020-21
Operating system laboratory
for (j = 0; j < r; j++)
{
printf("\t%d", need[i][j]);
}
printf("\n");
}
//Main procedure goes below to check for unsafe state.
while (count != 0)
{
safe = 0;
for (i = 0; i < p; i++)
{
if (running[i])
{
exec = 1;
for (j = 0; j < r; j++)
{
if (maxclaim[i][j] - curr[i][j] > avl[j])
{
exec = 0;
break;
}
}
}
if (exec)
{
printf("\nProcess%d is executing\n", i + 1);
safeseq[count2]=i+1;
count2++;
running[i] = 0;
count--;
safe = 1;
for (j = 0; j < r; j++)
{
avl[j] += curr[i][j];
}
break;
18IS060 2020-21
Operating system laboratory
}
}
if (!safe)
{
printf("\nThe processes are in unsafe state.\n");
break;
}
else
{
printf("\nThe process is in safe state");
for (i = 0; i < r; i++)
{
printf("\t%d", avl[i]);
}
printf("\n");
}
}
printf("safe sequence is\n");
for(i=0;i<p;i++)
printf("%d \t",safeseq[i]);
return 0;
}
18IS060 2020-21