Department of IT & CS
Course Instructor: ____________________ Dated: ________________
Semester: __________________________ Section: ________________
COMP-262L: Operating Systems
Lab 11: Critical Section, Inter Process Communication
Utilities: #Pragma omp parallel for, atomic, reduction
CLO1 CLO2 CLO3
Name Reg. No. Lab Tasks Report Viva Total
Marks Marks Marks Marks
20 5 5 30
Compile and Run ”Hello World” program of c/c++ language using gcc/g++ compiler.
Objectives:
Use OpenMP to run the first programme in parallel.
Parallel For Loop, Inter Process Communication
Set
Critical Section,
number Reduction.
of threads = 10 and print thread id while executing program.
COMP-262L: Operating Systems Page 1
Lab Tasks:
1. Write a program that calculates the sum of an array using a parallel for loop in
OpenMP.
Create an array of numbers and initialize it with random values.
Divide the array among multiple threads using the parallel for loop.
Each thread should calculate the sum of its assigned portion of the array.
Finally, combine the partial sums computed by each thread to get the final sum.
2. Implement a parallel program using OpenMP to calculate the sum of an array of
integers using atomic operations.
Initialize an array of integers with some values.
Create a shared variable sum and set it to 0.
Divide the work among multiple threads using an OpenMP parallel region.
Within the parallel region, use an OpenMP for loop to iterate over the array
elements.
Inside the loop, use the atomic construct to add the current array element to the
shared sum variable.
After the loop, exit the parallel region.
Print the final value of the sum variable.
3. Perform parallel sum calculation of an array using the OpenMP parallel for construct
with a reduction clause (reduction(+:sum))
Inter Process Communication Using Shared Memory
shmget() Syntax
#include <sys/ipc.h> #include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg);
shmat() Syntax
#include <sys/types.h> #include <sys/shm.h>
void *shmat(int shmid, const void *shmaddr, int shmflg);
COMP-262L: Operating Systems Page 2
4. Write a program to create a shared memory segment of 2048 bytes and write some
content into it. Then create a child process which then reads the content written by
the parent process in the shared memory segment.
COMP-262L: Operating Systems Page 3