Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1

Mini-Project
The Sandwich Problem
Course Title: Operating Systems
Course Code :CSE325
Section:02
Group No.: 10

Submitted to:-
Dr. Md. Nawab Yousuf Ali
Professor
Department of Computer Science Engineering

Submitted by:-

Hossain Sheikh
St. ID:2020-1-60-201
Md Hasibul Hasan Mehedi
St. ID: 2018-3-60-002
Sajib Laskar
St. ID:2019-3-60-069
Department of Computer Science Engineering
2

Being Ted Mosby's

Project Description:
Ted's Stories is an event planning company in New York City that creates romantic and
memorable events by bringing people's love stories to life. A limited number of event
planners work together, and Ted encourages them to embrace spontaneous ideas while
maintaining organization and execution. A C program can be developed using
semaphores, processes, and thread mutex locks to address the challenges of managing
event planner availability, shared resource management, and collaboration. This program
simulates the environment of "Ted's Stories" while effectively addressing concurrency
issues.

Project Analysis:
 Four event is already booked.
 There are four planners who worked to complete an event.
 One worker is worked for spot selected, one is for audiovisual equipment, one for decoration and
another planner work for ensuring this project.
 Each of this four work combination the event is create.

Abstruct:
In our following project, we will implement and test a solution for the IPC (Inter Process
Communication) problem of T\the sandwich problem in which we will use semaphore and multi
threads to execute our code in order to have a synchronization fort this problem. In this
program, we will mainly focus on four functions Input_function(),planner_work(),
Mutext_function() and Event_info(). These functions are related to each other with semaphore
and created by threads. The following code will define some variables at the beginning of the
program. But we can change it according to one’s choice.

Introduction:
An operating system (OS) is a type of software that manages both computer hardware and
software resources, while also providing common functions to computer programs. Time-
sharing operating systems effectively plan tasks to maximize system resources, and may also
include accounting software for cost allocation of processor time, storage, printing, and other
resources. The operating system acts as an intermediary between programs and the computer
hardware, even though application code is usually executed directly by the hardware and
may frequently make system calls to an OS function or be interrupted by it. Operating
systems can be found on various devices that incorporate a computer, ranging from cellular
phones and video game consoles to web servers and super computers.
In our problem we will focus on the three main topics. Threads, process andsemaphore.

Threads: Within a process, a thread is a path of execution. Multiple threads can exist in a
process. The lightweight process is also known as a thread. By dividing a process into numerous
threads, parallelism can be achieved. Multiple tabs in a browser, for example, can represent
different threads. MS Word makes use of numerous threads: one to format the text, another to
receive inputs, and so on. Below are some more advantages of multithreading.

Process: A process is essential for running software. The execution of a process must be done in
a specific order. To put it another way, we write our computer programs in a text file, and when
we run them, they turn into a process that completes all of the duties specified in the program. A
program can be separated into four components when it is put into memory and becomes a
process: stack, heap, text, and data. The diagram below depicts a simplified structure of a process
in main memory.

Semaphore: Dijkstra proposed the semaphore in 1965, which is a very important technique for
managing concurrent activities using a basic integer value called a semaphore. A semaphore is
just an integer variable shared by many threads. In a multiprocessing context, this variable is
utilized to solve the critical section problem and establish process synchronization.
There are two types of semaphores:

1. Binary Semaphore –
This is also known as mutex lock. It can have only two values – 0 and 1. Its value is
initialized to 1. It is used to implement the solution of critical section problems with
multiple processes.
2. Counting Semaphore –
Its value can range over an unrestricted domain. It is used to control access to a resource
that has multiple instances
Main Code:

1. There are some important header files in this code for semaphore, threads Here I define some
integers value and semaphore and threads .Declare some global variable and two semaphore

#include<stdio.h>
#include<stdlib.h>
#include<semaphore.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>

int events,vent_planners;
sem_t sem1;
sem_t sem2;
pthread_mutex_t mutex;
int j = 1;
int x = 0;
int Count = 0;
int temp;
6

2. Here I use 1 functions for taking user input.If we add a new event then we take a input.Also here we
print the total event number..

//input finction for taking event planers and events

void Input_function(int *events){

printf(" \n There are 4 events are already


booked.\n");
printf("\n Add new number of event: ");
scanf("%d",events);

printf(" Total events will be: %d", *events+4);


printf("\n Old events are: \n");

}
3. Here I use planner_work functions for print the work of the four planners and when they are
free.

void planner_work(int planner_id){

Count++;
sem_wait(&sem1);

if(planner_id == 1){
printf(" Planner %d proposal sopt for %d number
event.\n",planner_id, Count);
}
else if(planner_id == 2){
printf(" Planner %d decoration at %d number
event.\n",planner_id, Count);
}
else if(planner_id == 3){
printf(" Planner %d audiovisual eqipment at %d
number event.\n",planner_id, Count);
}
else if(planner_id ==4){
printf(" Planner %d ensuring %d number
event.\n",planner_id, Count);
}

sleep(2);

printf(" Planner %d is now free.\n", planner_id);


sem_post(&sem1);

}
4. In this part, events info function and mutext function()

void* Events_info(){

sem_wait(&sem2);
sleep(2);
temp = 1;
sem_post(&sem2);
j++;
}
void* mutext_func(){

pthread_mutex_lock(&mutex);
sleep(2);
pthread_mutex_unlock(&mutex);

}
5. In this part, events info function and mutext function()

int main(){

Input_function(&events);
events+=3;

pthread_t Event[events];
pthread_t thread2[events];

int i = 1;

sem_init(&sem2, 1, 1);
pthread_mutex_init(&mutex, NULL);

while(i <= events){

if(i<=4)
printf(" Event number %d is booked.\n",i);
if(i==5)
printf("\n New events are:\n");

if(i >= 4)

while(i >= 4 && temp == 0){


}

sem_wait(&sem2);

pthread_create(&Event[i], NULL, &Events_info, NULL);


pthread_create(&thread2[i], NULL, &mutext_func, NULL);

temp = 0;
if(i>4)
printf(" Event number %d is booked.\n",i);

sem_post(&sem2);

if(x == 0){

x = 1;
int j,k;

pid_t child[4];

sem_init(&sem1, 1, 2);

for( k = 0; k<4; k++){

child[k] = fork();

if(child[k] == -1){
9

Output :
Conclution:
Our project is about "Being Ted Mosby's". We are three members in our group solve it using C
language and ran our project in ubuntu terminal. Our honorable course instructor Md. Nawab
Yousuf Ali sir has taught us the implementation of every topic theoretically and practically. So
we use the knowledge and implemented a synchronisation method to solve the project. During
the project solving, we found some issues multiple times and fixed them together. After this we
got a perfectly done project. Overall the project is very efficient and usefull for users.

You might also like