Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
: 180701280
Exp. No.: 6
Date:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
int main()
{
// We use two pipes
// First pipe to send input string from parent
// Second pipe to send concatenated string from child
if (pipe(fd1) == -1)
{
Register No.: 180701280
scanf("%s", input_str);
p = fork();
if (p < 0)
{
fprintf(stderr, "fork Failed" );
return 1;
}
// Parent process
else if (p > 0)
{
char concat_str[100];
// child process
else
{
close(fd1[1]);
Register No.: 180701280
exit(0);
}
}
OUTPUT:
/a.out
rec
Concatenated string recforgeeks.org
[student@localhost 180701280]$
Register No.: 180701280
Exp. No.: 7
Date:
Aim:
Algorithm:
Program Code:
SENDER.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Register No.: 180701280
#define SharedMemSize 50
void main()
{
char c;
int shmid;
key_t key;
char *shared_memory;
key = 5677;
//Create segment with the key specified
if ((shmid = shmget(key, SharedMemSize, IPC_CREAT | 0666)) < 0)
{
//perror explains error code
perror("shmget");
exit(1);
}
RECEIVER.C
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#define SharedMemSize 50
void main()
{
int shmid;
key_t key;
char *shared_memory;
Register No.: 180701280
key = 5677;
if ((shmid = shmget(key, SharedMemSize, 0666)) < 0)
{
perror("shmget");
exit(1);
}
Output:
Terminal 1
Terminal 2
[root@localhost 180701280]# gcc receiver.c -o receiver
[root@localhost 180701280]# ./receiver
Message Received: Welcome to Shared Memory
[root@localhost 180701280]#
Register No.: 180701280
Exp. No.: 8
Date:
Aim:
Algorithm:
Program Code:
FIFO
#include<stdio.h>
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
wt[0] = 0;
// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];
findavgTime(processes, n, burst_time);
return 0;
}
OUTPUT
./a.out
Processes Burst time Waiting time Turn around time
Register No.: 180701280
1 10 0 10
2 5 10 15
3 8 15 23
Average waiting time = 8
Average turn around time = 16
SJF
Program
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
Register No.: 180701280
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");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%fn",avg_tat);
}
output
./a.out
Enter number of process:5
Enter Burst Time:np1:12
p2:23
p3:21
p4:20
p5:11
Processt Burst Time Waiting TimetTurnaround Time
p5 11 0 11
p1 12 11 23
p4 20 23 43
p3 21 43 64
p2 23 64 87
Exp. No.: 9
Date:
SEMAPHORES
PRODUCER CONSUMER PROBLEM
Aim:
Algorithm:
Program Code:
#include<stdio.h>
#include<stdlib.h>
int mutex=2,full=0,empty=3,x=0;
int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
Register No.: 180701280
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}
return 0;
}
int wait(int s)
{
return (--s);
}
int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
Register No.: 180701280
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
Output :
./a.out
1.producer
2.Consumer
3.Exit
Enter your choice:1
Exp. No.: 10
Date:
Aim:
Algorithm:
Program Code:
#include <stdio.h>
int main()
{
// P0, P1, P2, P3, P4 are the Process names here
int n, m, i, j, k;
n = 5;
m = 3;
int alloc[5][3] = { { 0, 1, 0 }, // P0
{ 2, 0, 0 }, // P1
{ 3, 0, 2 }, // P2
{ 2, 1, 1 }, // P3
{ 0, 0, 2 } }; // P4
int max[5][3] = { { 7, 5, 3 }, // P0
Register No.: 180701280
{ 3, 2, 2 }, // P1
{ 9, 0, 2 }, // P2
{ 2, 2, 2 }, // P3
{ 4, 3, 3 } }; // P4
int avail[3] = { 3, 3, 2 };
int f[n], ans[n], ind = 0;
for (k = 0; k < n; k++) {
f[k] = 0;
}
int need[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
need[i][j] = max[i][j] - alloc[i][j];
}
int y = 0;
for (k = 0; k < 5; k++) {
for (i = 0; i < n; i++) {
if (f[i] == 0) {
int flag = 0;
for (j = 0; j < m; j++) {
if (need[i][j] > avail[j]){
flag = 1;
break;
}
}
if (flag == 0) {
ans[ind++] = i;
for (y = 0; y < m; y++)
avail[y] += alloc[i][y];
f[i] = 1;
}
}
}
}
return (0);
}
Register No.: 180701280
OUTPUT :
RESULT :