IPC and Synchronisation

128 views 10:26 am 0 Comments March 28, 2023

 

5004CEM Lab 15

IPC and Synchronisation

In this lab, you will create a program that uses two processes and achieves synchronisation using semaphores to send a song, sung by two different people, to the screen and to files.

You can either use a programming language of your choice or adapt the code provided in Session15 on the server or on AULA.

See the ‘Getting Started with Lab 15’ video on AULA to give you additional information on completing this lab.

If you have any additional questions on this lab or any other labs please do not hesitate to ask your tutor and/or email me on [email protected].

Background – Song Hole in My Bucket

Henry 1: There’s a hole in the bucket, dear Liza, dear Liza, there’s a hole in the bucket, dear Liza, a hole.

Liza 1: Then fix it, dear Henry, dear Henry, dear Henry, then fix it, dear Henry, dear Henry, fix it.

Henry 2: With what shall I fix it, dear Liza, dear Liza? With what shall I fix it, dear Liza, with what?

Liza 2: With straw, dear Henry, dear Henry, dear Henry, with straw, dear Henry, dear Henry, with straw.

Henry 3: The straw is too long, dear Liza, dear Liza, the straw is too long, dear Liza, too long.

Liza 3: Then cut it, dear Henry, dear Henry, dear Henry, then cut it, dear Henry, dear Henry, cut it.

Henry 4: With what shall I cut it, dear Liza, dear Liza? With what shall I cut it, dear Liza, with what?

Liza 4: With an axe, dear Henry, dear Henry, dear Henry, with an axe, dear Henry, dear Henry, an axe.

Basic Task – Semaphore

By using a programming language of your choice or modifying the semaphore code provided in Session15 of the server or AULA, output the song “There’s a hole in my bucket” (See Wikipedia) so that one process prints to screen Liza’s lines and the other prints to screen Henry’s (or Georgie’s depending on the version you know).

Basic Task Solution

// basicTask.c

#include <sys/ipc.h>

#include <sys/sem.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include “ops_sems.h”

int main(int argc, char argv[]){

//Use our source file as the “key”

int id=ops_semget(“basicTask.c”,1);

// 2D array containing lyrics of the song

char lines_arr[8][110] = {

“There’s a hole in the bucket, dear Liza, dear Liza, there’s a hole in the bucket, dear Liza, a hole.n”, “With what shall I fix it, dear Liza, dear Liza? With what shall I fix it, dear Liza, with what?n”, “The straw is too long, dear Liza, dear Liza, the straw is too long, dear Liza, too long.n”, “With what shall I cut it, dear Liza, dear Liza? With what shall I cut it, dear Liza, with what?n”, “Then fix it, dear Henry, dear Henry, dear Henry, then fix it, dear Henry, dear Henry, fix it.n”, “With straw, dear Henry, dear Henry, dear Henry, with straw, dear Henry, dear Henry, with straw.n”, “Then cut it, dear Henry, dear Henry, dear Henry, then cut it, dear Henry, dear Henry, cut it.n”, “With an axe, dear Henry, dear Henry, dear Henry, with an axe, dear Henry, dear Henry, an axe.n”

};

// initiate fork

int pid=fork();

if(pid){

// Process 1 (Henry)

for (int i = 0; i < 4; i++) {

// Wait on semaphore

ops_wait(id);

// Print Henry’s line to screen

printf(“Henry: %s”,lines_arr[i]);

// Signal semaphore

ops_signal(id);

}

}

else {

// Process 2 (Liza)

for (int i = 4; i < 8; i++) {

ops_wait(id);

// Print Liza’s line to screen

printf(“Liza: %s”,lines_arr[i]);

ops_signal(id);

}

}

return 0;

}

Advanced Task – stderr or stdout

Write Henry’s part to the standard error channel (stderr) instead of the standard out channel (stdout). As well as writing the lines to the screen you should redirect Henry’s part to a file (Henry.txt), Liza part to another file (Liza.txt), and both parts to a third file (Both.txt) in the form below.

Henry Part 1: There’s a hole in the bucket, dear Liza, dear Liza, there’s a hole in the bucket, dear Liza, a hole.

Liza Part 1: Then fix it, dear Henry, dear Henry, dear Henry, then fix it, dear Henry, dear Henry, fix it.

Henry Part 2: With what shall I fix it, dear Liza, dear Liza? With what shall I fix it, dear Liza, with what?

Liza Part 2: With straw, dear Henry, dear Henry, dear Henry, with straw, dear Henry, dear Henry, with straw.

Advanced Task Solution

// advancedTask.c

#include <sys/ipc.h>

#include <sys/sem.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include “ops_sems.h”

int main(int argc, char argv[]){

//Use our source file as the “key”

int id=ops_semget(“critical_example2.c”,1);

int c;

FILE *henry, *liza, *both; // files for the text

henry=freopen(“Henry.txt”,”w+”,stderr);//Henry’s file

liza=fopen(“Liza.txt”,”w+”); // Liza file

both=fopen(“Both.txt”,”w+”); //Open ‘both’ in w+ to clear it

fclose(both); //Close both file right after

int pid=fork();

int a=1;

int b=1;

if(pid){

//P1

while(1){

if(a<5) //File declared only on the available cases

both=fopen(“Both.txt”,”a”); // create to put both person’s lines in

ops_wait(id);

switch(a){

// Write the lines to files

case 1:

fprintf(stderr, “Henry 1: There’s a hole in the bucket, dear Liza, dear Liza, there’s a hole in the bucket, dear Liza, a hole. n”);

fprintf(both, “Henry 1: There’s a hole in the bucket, dear Liza, dear Liza, there’s a hole in the bucket, dear Liza, a hole. n”);

// Write line to file

printf(“Henry 1: There’s a hole in the bucket, dear Liza, dear Liza, there’s a hole in the bucket, dear Liza, a hole. n”);

fclose(both);

break;

case 2:

fprintf(stderr,”Henry 2: With what shall I fix it, dear Liza, dear Liza? With what shall I fix it, dear Liza, with what? n”);

fprintf(both,”Henry 2: With what shall I fix it, dear Liza, dear Liza? With what shall I fix it, dear Liza, with what? n”);

printf(“Henry 2: With what shall I fix it, dear Liza, dear Liza? With what shall I fix it, dear Liza, with what? n”);

fclose(both);

break;

case 3:

fprintf(stderr,”Henry 3: The straw is too long, dear Liza, dear Liza, the straw is too long, dear Liza, too long. n”);

fprintf(both,”Henry 3: The straw is too long, dear Liza, dear Liza, the straw is too long, dear Liza, too long. n”);

printf(“Henry 3: The straw is too long, dear Liza, dear Liza, the straw is too long, dear Liza, too long. n”);

fclose(both);

break;

case 4:

fprintf(stderr,”Henry 4: With what shall I cut it, dear Liza, dear Liza? With what shall I cut it, dear Liza, with what? n”);

fprintf(both,”Henry 4: With what shall I cut it, dear Liza, dear Liza? With what shall I cut it, dear Liza, with what? n”);

printf(“Henry 4: With what shall I cut it, dear Liza, dear Liza? With what shall I cut it, dear Liza, with what? n”);

fclose(henry);

fclose(both);

}

rsleep();

a++;

ops_signal(id);

}

}else{

//P2

while(1){

if(b<5) //File declared only on the available cases

both=fopen(“Both.txt”,”a”);

ops_wait(id);

switch(b){

case 1:

fprintf(liza, “Liza 1: Then fix it, dear Henry, dear Henry, dear Henry, then fix it, dear Henry, dear Henry, fix it. n”);

fprintf(both, “Liza 1: Then fix it, dear Henry, dear Henry, dear Henry, then fix it, dear Henry, dear Henry, fix it. n”);

printf(“Liza 1: Then fix it, dear Henry, dear Henry, dear Henry, then fix it, dear Henry, dear Henry, fix it. n”);

fclose(both);

break;

case 2:

fprintf(liza, “Liza 2: With straw, dear Henry, dear Henry, dear Henry, with straw, dear Henry, dear Henry, with straw. n”);

fprintf(both, “Liza 2: With straw, dear Henry, dear Henry, dear Henry, with straw, dear Henry, dear Henry, with straw. n”);

printf(“Liza 2: With straw, dear Henry, dear Henry, dear Henry, with straw, dear Henry, dear Henry, with straw. n”);

fclose(both);

break;

case 3:

fprintf(liza, “Liza 3: Then cut it, dear Henry, dear Henry, dear Henry, then cut it, dear Henry, dear Henry, cut it. n”);

fprintf(both, “Liza 3: Then cut it, dear Henry, dear Henry, dear Henry, then cut it, dear Henry, dear Henry, cut it. n”);

printf(“Liza 3: Then cut it, dear Henry, dear Henry, dear Henry, then cut it, dear Henry, dear Henry, cut it. n”);

fclose(both);

break;

case 4:

fprintf(liza, “Liza 4: With an axe, dear Henry, dear Henry, dear Henry, with an axe, dear Henry, dear Henry, an axe. n”);

fprintf(both, “Liza 4: With an axe, dear Henry, dear Henry, dear Henry, with an axe, dear Henry, dear Henry, an axe. n”);

printf(“Liza 4: With an axe, dear Henry, dear Henry, dear Henry, with an axe, dear Henry, dear Henry, an axe. n”);

fclose(liza);

fclose(both);

}

rsleep();

b++;

ops_signal(id);

}

}

}

Lab Evidence

Commented code and examples of the code running to display the song on the screen, send Henry’s part down the stderr channel and put the song into different files.