0% found this document useful (0 votes)
73 views

OS LAB MIDs Assignment

The document contains the solutions to three questions related to operating systems concepts. Question 1 involves designing a calculator program that performs arithmetic operations in parallel using processes and pipes. Question 2 involves generating Fibonacci numbers using multithreading where one thread generates the numbers and another prints them. Question 3 involves solving the readers-writers problem for shared database access using semaphores and mutex locks to allow concurrent reading and exclusive writing.

Uploaded by

Saad Afridi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

OS LAB MIDs Assignment

The document contains the solutions to three questions related to operating systems concepts. Question 1 involves designing a calculator program that performs arithmetic operations in parallel using processes and pipes. Question 2 involves generating Fibonacci numbers using multithreading where one thread generates the numbers and another prints them. Question 3 involves solving the readers-writers problem for shared database access using semaphores and mutex locks to allow concurrent reading and exclusive writing.

Uploaded by

Saad Afridi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: 15

Marks Obtained:

Operating System LAB


(Mids Assignment)

Submitted To: Mr. Muhammad Naveed____

Student Name: Saad Waheed Khan___________________

Student Reg. No._1880105_________________

Date of Submission: 12-04--2020________________

OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Q1:

i) Using process management concepts you are required to design simple calculator
where four (add, multiple, subtract and divide) main operations are performed. All
these operations execute in parallel form while the parent process get user input and
print result and child processes perform their arithmetic operations.

#include <iostream>
#include <fstream>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <sstream>

using namespace std;

const int MAX =21;


int pipes[MAX][2];
char operations[MAX];

int main()
{
int a=0;
int n;
fstream datafile;
pid_t pid;
string line;
datafile.open("data.txt");

if(datafile)
{
string input;
getline(datafile, input);

for(int i=0; i< input.size(); i++)


{

if (input[i] == '*'|| input[i] == '/'||input[i] == '+'||input[i] == '-')


{
OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

operations[a] = input[i];
a++;

}
n = (a);

for(int i =0; i<(2*n+1); i++)


pipe(pipes[i]);

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


{
pid = fork();
if(pid == 0)
{

close(0);
dup(pipes[2*i][0]);
close(3);
dup(pipes[2*i+1][0]);
close(1);
dup(pipes[2*i+2][1]);

switch(operations[i])
{
case '+':
execl("add","add", NULL);
case '-':
execl("subtract","multiply", NULL);
case '*':
execl("multiply","multiply", NULL);
case '/':
execl("divide","divide", NULL);

cout<< "No match for operation!";


}
else if(pid <0)
cerr<<"Fork has failed";
OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

int x, y, z;

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


{
getline(datafile,line);
istringstream ins(line);
ins >> x >> y >> z;

write(0,(char *)&x, sizeof(x));


write(3,(char *)&z, sizeof(z));
write(1,(char *)&y, sizeof(y));
}

close(0);
close(3);
close(1);
}

else
cerr<<"Error reading file";

datafile.close();

int result;
while(read(pipes[2*n][1],(char *)&result,sizeof(result)))
cout<<result<<endl;
}

OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

ii) Compute the Factorial of a number using IPC (PIPE implementation).

• Parent creates pipe


• Forks a child
• Child writes into pipe (Take the number from the user for factorial)
• Parent reads from pipe and computes the Factorial of a number sent by child

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
int pfds[2];
int n,f=1,i,n1;
pipe(pfds);
if(!fork())
{
printf("Child: Writing to the PIpe \n");
printf("Child: Enter the Number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
f=f*i;
write(pfds[1],(char *)&f,sizeof(f));
close(pfds[1]);
printf("Child: Exiting\n");
}
else
{
wait(0);
printf("Parent: reaading from pipe\n");
read(pfds[0],&n1,sizeof(int));
printf("Parent : Read %d\n",n1);
sleep(1);
close(pfds[0]);
}
return 0;
}

OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Q2:
The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed
as:
=0
=1
=+
Write a multithreaded program that generates the Fibonacci sequence. This program should work as
follows: On the command line, the user will enter the number of Fibonacci numbers that the program
is to generate. The program will then create a separate thread that will generate the Fibonacci
numbers, placing the sequence in data that can be shared by the threads (an array is probably the most
convenient data structure). When the thread finishes execution, the parent thread will output the
sequence generated by the child thread. Because the parent thread cannot begin outputting the
Fibonacci sequence until the child thread finishes, the parent thread will have to wait for the child
thread to finish.
CODE:

#include <pthread.h>
#include <stdio.h>
int value = 0;
void *runner(void *param); /* the thread */
int main(int argc, char * argv[])
{
int pid;
pthread_t tid;
pthread_attr_t attr;
pid = fork();

if (pid == 0) {/* child process */


pthread_attr_init(&attr);
pthread_create(&tid,&attr,runner,NULL);
pthread_join(tid,NULL);
printf(“CHILD: value = %d”, value); /* LINE C */
}
else if (pid > 0) { /* parent provess */
wait(NULL);
printf(“PARENT: value = %d”, value); /* LINE P */
}
}
void *runner(void *param) {
value = 5;
pthread_exit(0);

OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Q3) Suppose that a database is to be shared among several concurrent processes. Some of these
processes may want only to read the database, whereas others may want to update (that is, to read
and write) the database. We distinguish between these two types of processes by referring to the
former as readers and to the latter as writers. Obviously, if two readers access the shared data
simultaneously, no adverse effects will result. However, if a writer and some other process (either
a reader or a writer) access the database simultaneously, chaos may ensue. To ensure that these
difficulties do not arise, we require that the writers have exclusive access to the shared database
while writing to the database. This synchronization problem is referred to as the readers– writers
problem. Please write program which solve such synchronization problem and provide and
smooth solution.

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
sem_t wrt;
pthread_mutex_t mutex;
int cnt = 1;
int numreader = 0;

void *writer(void *wno)


{
sem_wait(&wrt);
cnt = cnt*2;
printf("Writer %d modified cnt to %d\n",(*((int *)wno)),cnt);
sem_post(&wrt);

}
void *reader(void *rno)
{

pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt);
}
pthread_mutex_unlock(&mutex);

printf("Reader %d: read cnt as %d\n",*((int *)rno),cnt);

pthread_mutex_lock(&mutex);
numreader--;
if(numreader == 0) {
OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

sem_post(&wrt);
}
pthread_mutex_unlock(&mutex);
}

int main()
{

pthread_t read[10],write[5];
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt,0,1);

int a[10] = {1,2,3,4,5,6,7,8,9,10};

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


pthread_create(&read[i], NULL, (void *)reader, (void *)&a[i]);
}
for(int i = 0; i < 5; i++) {
pthread_create(&write[i], NULL, (void *)writer, (void *)&a[i]);
}

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


pthread_join(read[i], NULL);
}
for(int i = 0; i < 5; i++) {
pthread_join(write[i], NULL);
}

pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);

return 0;

OS BSSE 4 SZABIST-ISB

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