You are on page 1of 14

12/21/2021

OS-3
Lab Assingment

Mujtaba Ahmed-079
Tooba Noor-095
COMSATS University Islamabad Campus
Department of Computer Science
FALL 2021

Operating Systems (CSC322)


BSSE-4 (A,B)
Lab Assignment No. 3
Deadline: 21-12-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 a, b;//number of elements in array 1 and 2


int c=0;//total number of same elements
int a1[30], a2[30];
cout<<"How many values you wish to enter in First array: "<<endl;
cin>>a;
cout<<"Enter Elements for First Array: "<<endl;
for(int i=0; i<a; i++)
cin>>a1[i];
cout<<"How many values you wish to enter in Second array: "<<endl;
cin>>b;
cout<<"Enter Elements for Second Array: "<<endl;
for(int i=0; i<b; i++)
cin>>a2[i];

// Finding and storing common elements


//cout<<"common elements"<<endl;
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++){
if (a1[i] == a2[j])
{
//cout<<a1[i]<<","<<a2[j]<<endl;
c++;
i++;
}

}
}
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>

using namespace std;

struct child{
int r;
};

int main(){
int a;
pid_t p1 = fork();
if(p1==0){

char* args[] = {(char*)"./child",NULL};


int s = execv (args[0], args);
exit(0);

}else{

wait(NULL);
}
int id = shmget(111,1024,0666|IPC_CREAT);
child *c = (child *) shmat (id,0,0);
a = c->r;

cout<<"\nNumber of common elements are:"<<a<<endl;


shmdt(&id);
shmctl(id,IPC_RMID,NULL);

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>

using namespace std;


int counter=0;

void *a(void *args)


{

cout<<"/t/t THIS IS THREAD 1"<<endl;


int a, b;//number of elements in array 1 and 2
int a1[30], a2[30];
cout<<"How many values you wish to enter in First array: "<<endl;
cin>>a;
cout<<"Enter Elements for First Array: "<<endl;
for(int i=0; i<a; i++)
cin>>a1[i];
cout<<"How many values you wish to enter in Second array: "<<endl;
cin>>b;
cout<<"Enter Elements for Second Array: "<<endl;
for(int i=0; i<b; i++)
cin>>a2[i];
// Finding common elements
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++){
if (a1[i] == a2[j])
{
counter++;
i++;

}
}

return 0;
}

int main(){

pthread_t t1;
pthread_create(&t1,NULL,&a,NULL);

pthread_join(t1,NULL);

cout<<"The Number of same values are: "<<counter<<endl;


return 0;
}
Question 2:
Part 1:
Write a C++ program that creates an array of size 30 of type integer. During execution, the program
should ask the user to provide input. When the input is completed, the program should count how
many prime numbers are entered by the user. Calculate the total time required by the system to
execute the program.
Q2pta.cpp
#include<iostream>
#include<ctime>
using namespace std;
int main(){
time_t start, finish;
time(&start);
int arr[30],a, j, p;
int count=0;
cout <<"Enter Elements to enter in array:"<<endl;
for (int i = 0; i < 30; i++) {
cin >> arr[i];
}

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


j = 2;
p = 1;
if(arr[i]==1){
continue;
}

while (j < arr[i]) {

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 checkPrime(int num){


int j,p;
for (int i = 0; i < num; i++) {
j = 2;
p = 1;
if(num==1){
return 0;
}
while (j < num) {
if (num % j == 0) {
p = 0;
return 0;
}
j++;
}
if (p == 1) {
return 1;
}
}
return 0;
}

void childa(int arr[]){


if(fork()==0){
int id = shmget(1234,1024,0666|IPC_CREAT);
nofp *a = (nofp *) shmat (id,0,0);
int isprime=0;
cout<<"|";
for(int i=0;i<15;i++){
isprime=checkPrime(arr[i]);
if(isprime==1){
cout<<arr[i]<<"|";
a->count1++;
}
}
exit(0);
}
}

void childb(int arr[]){


if(fork()==0){
int id = shmget(1234,1024,0666|IPC_CREAT);
nofp *b = (nofp *) shmat (id,0,0);
int isprime=0;
for(int i=15;i<30;i++){
isprime=checkPrime(arr[i]);
if(isprime==1){
cout<<arr[i]<<"|";
b->count2++;}
}
exit(0);
}
}

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;

int checkPrime(int num){


int j,p;
for (int i = 0; i < num; i++) {
j = 2;
p = 1;
if(num==1){
return 0;
}
while (j < num) {
if (num % j == 0) {
p = 0;
return 0;
}
j++;
}
if (p == 1) {
return 1;
}
}
return 0;
}

void *thread1(void *args){


int isprime=0;
for(int i=0;i<15;i++){
isprime=checkPrime(arr[i]);
if(isprime==1){
p1++;
}
}
return 0;
}

void *thread2(void *args){


int isprime=0;
for(int i=15;i<30;i++){
isprime=checkPrime(arr[i]);
if(isprime==1){
p2++;}
}
return 0;
}
int main(){
time_t s, f;
time(&s);
int i;
cout <<"Enter Elements to enter in array:"<<endl;
for (i = 0; i < 30; i++) {
cin >> arr[i];
}

pthread_t t1,t2;
pthread_create(&t1,NULL,&thread1,NULL);
pthread_create(&t2,NULL,&thread2,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);

cout<<"\nTotal Number of Prime values is: "<<p1+p2<<endl;


time(&f);
cout<<"Time taken: "<<difftime(f, s)<<" seconds"<<endl;
return 0;
}

You might also like