Lab Assignment 3 OS
Lab Assignment 3 OS
OS-3
Lab Assingment
Mujtaba Ahmed-079
Tooba Noor-095
COMSATS University Islamabad Campus
Department of Computer Science
FALL 2021
Question 1:
Part 1:
Write a program in C++ (child.cpp) that declares two integer type arrays of size 30. At runtime, the
program asks the user to enter values in theses arrays (it is up-to the user how many values he/she
enters). Once the arrays are populated, child.cpp finds how many values are same in both arrays. Write
another program in C++ (main.cpp) that creates a child process and replaces its code with child.cpp
(executable) and waits until child completes its execution. The child process shares the number of
same values with parent process and parent process displays it.
CHILD.CPP
#include<iostream>
#include<unistd.h>
#include<sys/wait.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<semaphore.h>
#include<sys/ipc.h>
#include<sys/shm.h>
using namespace std;
int main()
{
struct child{
int count;
};
}
}
int id = shmget(111,1024,0666|IPC_CREAT);
child *x = (child *) shmat (id,0,0);
x->count= c;
return 0;
}
MAIN.CPP
#include<iostream>
#include<unistd.h>
#include<sys/wait.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<semaphore.h>
#include<sys/ipc.h>
#include<sys/shm.h>
struct child{
int r;
};
int main(){
int a;
pid_t p1 = fork();
if(p1==0){
}else{
wait(NULL);
}
int id = shmget(111,1024,0666|IPC_CREAT);
child *c = (child *) shmat (id,0,0);
a = c->r;
return 0;
}
Part 2:
Write a multi-threaded program that implements the same functionality as stated in Part-1 i.e. a thread
creates arrays and find the number of same values in these arrays. Then the number of same values is
shared with the main thread. The main thread then displays it.
Q1pt2.cpp
#include<iostream>
#include<pthread.h>
}
}
return 0;
}
int main(){
pthread_t t1;
pthread_create(&t1,NULL,&a,NULL);
pthread_join(t1,NULL);
if (arr[i] % j == 0 || arr[i]==1) {
p = 0;
break;
}
j++;
}
if (p == 1) {
count++;
}
}
cout<<"Total Number of prime values are: "<<count<<endl;
time(&finish);
cout<<"Time taken: "<<difftime(finish, start)<<" seconds"<<endl;
return 0;
}
Part 2:
Convert the program given in Part 1 into multi-process application. The main parent program should
take the input while the two children processes should calculate the number of prime numbers (from
15 array elements each) and share the number of prime numbers with parent process. The parent
process should display the total number of prime numbers. Calculate the total time required by the
system to execute the program.
Q2ptb.cpp
#include<iostream>
#include<unistd.h>
#include<sys/wait.h>
#include<ctime>
#include<sys/shm.h>
using namespace std;
struct nofp{
int count1=0,count2;
};
int main(){
time_t s, f;
time(&s);
int i;
int arr[30];
cout <<"Enter Elements to enter in array:"<<endl;
for (i = 0; i < 30; i++) {
cin >> arr[i];
}
int id = shmget(1234,1024,0666|IPC_CREAT);
nofp *a = (nofp *) shmat (id,0,0);
childa(arr);
wait(NULL);
childb(arr);
wait(NULL);
int p1=0;
p1=a->count1;
int p2=0;
p2=a->count2;
cout<<"\nTotal Number of Prime values is: "<<p1+p2<<endl;
shmdt(&id);
shmctl(id,IPC_RMID,NULL);
time(&f);
cout<<"Time taken: "<<difftime(f, s)<<" seconds"<<endl;
return 0;
}
Part 3:
Convert the program given in Part 1 into multi-threaded application. The main thread should take the
input from user. Create two more threads that calculate the number of prime numbers (from 15 array
elements each) and share the number of prime numbers with main thread. The main thread should
display the total number of prime numbers. Calculate the total time required by the system to execute
the program.
Q2ptc.cpp
#include<iostream>
#include<pthread.h>
#include<ctime>
using namespace std;
int arr[30],p1,p2;
pthread_t t1,t2;
pthread_create(&t1,NULL,&thread1,NULL);
pthread_create(&t2,NULL,&thread2,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);