0% found this document useful (0 votes)
26 views9 pages

Lab 09 - Synchronization

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Lab 09 - Synchronization

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Faculty of Computing

Lab 09: Synchronization

CS-330 Operating System

BESE – 14AB
12th April 2025

Lab Engineer: Mr. Junaid Sajid


Instructor: Engr Taufeeq Ur Rehman
Submitted By:
Ushba Fatima 467212
Jarrar Haider Nemati 467291

CS330: Operating Systems Page 1


Lab 8: Synchronization
Introduction

The purpose of this lab is to introduce synchronization primitives like semaphores, mutexes, etc.
to produce the output as per requirement.

Note :- You have to submit this lab individually.

Objectives

This lab will enable you to use synchronization primitives programmatically and alter the output
as per requirements.

Tools/Software Requirement

Linux OS installed on laptops or systems.

Description
#include <pthread.h>

#include <stdio.h>

#define FIRST_ODD_NUM 1

#define FIRST_EVEN_NUM 2

#define MAX 10

void *print_even_nums(void *param) {

int even_num_to_print;

for(even_num_to_print = FIRST_EVEN_NUM; even_num_to_print < MAX;) {

printf("%d ", even_num_to_print);

even_num_to_print += 2;
CS330: Operating Systems Page 2
}

void *print_odd_nums(void *param) {

int odd_num_to_print;

for(odd_num_to_print = FIRST_ODD_NUM; odd_num_to_print < MAX;) {

printf("%d ", odd_num_to_print);

odd_num_to_print += 2;

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

pthread_t even_thread; pthread_t odd_thread;

pthread_create(&even_thread, NULL, print_even_nums, NULL);

pthread_create(&odd_thread, NULL, print_odd_nums, NULL);

pthread_join(even_thread, NULL); pthread_join(odd_thread, NULL);

Above written code presents a program that uses two threads to print odd and even numbers from
1 to MAX (a macro defined in the code). Line 25 defines the main method, which is the starting
point for this program. Line 26 initializes two handles even_thread and odd_thread to represent
the two printing threads. On line 28 and 29, the main method starts even_thread and odd_thread
(asynchronously) and calls join for both threads to finish its execution. At this point, odd_thread
and even_thread have started their execution in parallel. The odd_thread is responsible for
printing odd numbers between FIRST_ODD_NUM and MAX—this thread prints 1, 3,5,7,9.

CS330: Operating Systems Page 3


Similarly, the even_thread is responsible for printing even numbers between
FIRST_EVEN_NUM and MAX (prints 2, 4, 6, and 8).

Tasks

a. Your first task is to compile and run this code on your system. The issue is that we have
no control over the order these numbers are printed on the console. For example, one
sample run of the program produces:

The output order varies because the two threads run concurrently without synchronization.
The operating system schedules them independently, leading to unpredictable interleaving of
their outputs. Since both threads access the console simultaneously, the final order depends
on thread execution timing.

b. Your second and main task here is to modify the source code and use synchronization
primitives available to you (semaphore and mutex) to produce the output in ascending
order.

Semaphore Implementation:
#include <pthread.h>

#include <stdio.h>

#include <semaphore.h>

#define MAX 10

#define FIRST_ODD_NUM 1

#define FIRST_EVEN_NUM 2

sem_t sem_odd;

sem_t sem_even;

CS330: Operating Systems Page 4


void *print_odd_nums(void *param) {

for (int i = FIRST_ODD_NUM; i < MAX; i += 2) {

sem_wait(&sem_odd);

printf("%d ", i);

sem_post(&sem_even);

return NULL;

void *print_even_nums(void *param) {

for (int i = FIRST_EVEN_NUM; i < MAX; i += 2) {

sem_wait(&sem_even);

printf("%d ", i);

sem_post(&sem_odd);

return NULL;

int main() {

pthread_t t1, t2;

// Initialize semaphores

printf("Semaphore Implementation\n");

sem_init(&sem_odd, 0, 1); // Start with odd

sem_init(&sem_even, 0, 0); // Even waits


CS330: Operating Systems Page 5
pthread_create(&t1, NULL, print_odd_nums, NULL);

pthread_create(&t2, NULL, print_even_nums, NULL);

pthread_join(t1, NULL);

pthread_join(t2, NULL);

sem_destroy(&sem_odd);

sem_destroy(&sem_even);

printf("\n");

return 0;

Output:

Mutex Implementation
#include <pthread.h>

#include <stdio.h>

#define MAX 10

pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

CS330: Operating Systems Page 6


int current = 1;

void *print_odd_nums(void *param) {

while (1) {

pthread_mutex_lock(&lock);

while (current % 2 == 0 && current < MAX) {

pthread_cond_wait(&cond, &lock);

if (current >= MAX) {

pthread_mutex_unlock(&lock);

break;

printf("%d ", current);

current++;

pthread_cond_signal(&cond);

pthread_mutex_unlock(&lock);

return NULL;

void *print_even_nums(void *param) {

while (1) {

pthread_mutex_lock(&lock);

while (current % 2 == 1 && current < MAX) {

pthread_cond_wait(&cond, &lock);
CS330: Operating Systems Page 7
}

if (current >= MAX) {

pthread_mutex_unlock(&lock);

break;

printf("%d ", current);

current++;

pthread_cond_signal(&cond);

pthread_mutex_unlock(&lock);

return NULL;

int main() {

pthread_t odd_thread, even_thread;

printf("Mutex Implementation\n");

pthread_create(&odd_thread, NULL, print_odd_nums, NULL);

pthread_create(&even_thread, NULL, print_even_nums, NULL);

pthread_join(odd_thread, NULL);

pthread_join(even_thread, NULL);

printf("\n");

return 0;

CS330: Operating Systems Page 8


Output:

CS330: Operating Systems Page 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy