OS LAB MIDs Assignment
OS LAB MIDs Assignment
Total Marks: 15
Marks Obtained:
OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
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>
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);
operations[a] = input[i];
a++;
}
n = (a);
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);
int x, y, z;
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
#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
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();
OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
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 *reader(void *rno)
{
pthread_mutex_lock(&mutex);
numreader++;
if(numreader == 1) {
sem_wait(&wrt);
}
pthread_mutex_unlock(&mutex);
pthread_mutex_lock(&mutex);
numreader--;
if(numreader == 0) {
OS BSSE 4 SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology
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);
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
OS BSSE 4 SZABIST-ISB