Sei sulla pagina 1di 16

Register No.

: 180701280

Exp. No.: 6
Date:

IPC USING NAMED PIPE

AIM: To demonstrate inter-process communication(IPC) using named pipe.

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

int fd1[2]; // Used to store two ends of first pipe


int fd2[2]; // Used to store two ends of second pipe

char fixed_str[] = "forgeeks.org";


char input_str[100];
pid_t p;

if (pipe(fd1) == -1)
{
Register No.: 180701280

fprintf(stderr, "Pipe Failed" );


return 1;
}
if (pipe(fd2)==-1)
{
fprintf(stderr, "Pipe Failed" );
return 1;
}

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

close(fd1[0]); // Close reading end of first pipe

// Write input string and close writing end of first


// pipe.
write(fd1[1], input_str, strlen(input_str)+1);
close(fd1[1]);

// Wait for child to send a string


wait(NULL);

close(fd2[1]); // Close writing end of second pipe

// Read string from child, print it and close


// reading end.
read(fd2[0], concat_str, 100);
printf("Concatenated string %s\n", concat_str);
close(fd2[0]);
}

// child process
else
{
close(fd1[1]);
Register No.: 180701280

// Read a string using first pipe


char concat_str[100];
read(fd1[0], concat_str, 100);

// Concatenate a fixed string with it


int k = strlen(concat_str);
int i;
for (i=0; i<strlen(fixed_str); i++)
concat_str[k++] = fixed_str[i];

concat_str[k] = '\0'; // string ends with '\0'

// Close both reading ends


close(fd1[0]);
close(fd2[0]);

// Write concatenated string and close writing end


write(fd2[1], concat_str, strlen(concat_str)+1);
close(fd2[1]);

exit(0);
}
}

OUTPUT:

/a.out
rec
Concatenated string recforgeeks.org
[student@localhost 180701280]$
Register No.: 180701280

Exp. No.: 7
Date:

IPC USING SHARED MEMORY

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

//Attach the segment


if ((shared_memory= shmat(shmid, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1);
}

sprintf(shared_memory," Welcome to Shared Memory");


sleep(2);
exit(0);
}

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

//Attach the segment to our data space


if ((shared_memory = shmat(shmid, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1);
}

//Read the message sender sent to the shared memory


printf("Message Received: %s \n",shared_memory);
exit(0);
}

Output:

Terminal 1

[root@localhost 180701280]# gcc sender.c -o sender


[root@localhost 180701280]# ./sender

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:

CPU SHEDULING ALGORITHM

Aim:

Algorithm:

Program Code:

FIFO
#include<stdio.h>
void findWaitingTime(int processes[], int n,
int bt[], int wt[])
{
wt[0] = 0;

for (int i = 1; i < n ; i++ )


wt[i] = bt[i-1] + wt[i-1] ;
}

void findTurnAroundTime( int processes[], int n,


int bt[], int wt[], int tat[])
{
Register No.: 180701280

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


tat[i] = bt[i] + wt[i];
}

void findavgTime( int processes[], int n, int bt[])


{
int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(processes, n, bt, wt);

findTurnAroundTime(processes, n, bt, wt, tat);

printf("Processes Burst time Waiting time Turn around time\n");

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


{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ",(i+1));
printf(" %d ", bt[i] );
printf(" %d",wt[i] );
printf(" %d\n",tat[i] );
}
int s=(float)total_wt / (float)n;
int t=(float)total_tat / (float)n;
printf("Average waiting time = %d",s);
printf("\n");
printf("Average turn around time = %d ",t);
}

// Driver code
int main()
{
//process id's
int processes[] = { 1, 2, 3};
int n = sizeof processes / sizeof processes[0];

//Burst time of all processes


int burst_time[] = {10, 5, 8};

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

printf("\nEnter Burst Time:n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}

//sorting of burst times


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

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

Average Waiting Time=28.200001


Average Turnaround Time=45.59999
Register No.: 180701280

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

Producer produces the item 1


Enter your choice:1

Producer produces the item 2


Enter your choice:1

Producer produces the item 3


Enter your choice:2

Consumer consumes item 3


Enter your choice:2

Consumer consumes item 2


Enter your choice:2

Consumer consumes item 1


Enter your choice:3
Register No.: 180701280

Exp. No.: 10
Date:

DEAD LOCK AVOIDANCE


BANKER’S ALGORITHM

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

printf("Following is the SAFE Sequence\n");


for (i = 0; i < n - 1; i++)
printf(" P%d ->", ans[i]);
printf(" P%d", ans[n - 1]);

return (0);

}
Register No.: 180701280

OUTPUT :

Following is the safe sequence


P1 -> P3 -> P4 -> P0 -> P2

RESULT :

Potrebbero piacerti anche